Skip to content

Commit 8bdd0e7

Browse files
committed
feat: ollama base url
Signed-off-by: thxCode <[email protected]>
1 parent 3bf700f commit 8bdd0e7

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

cmd/gguf-parser/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ GLOBAL OPTIONS:
8484
8585
Model/Remote/Ollama
8686
87-
--ol-model value Model name of Ollama, e.g. gemma2.
88-
--ol-usage Specify respecting the extending layers introduced by Ollama, works with --ol-model, which affects the usage estimation. (default: false)
87+
--ol-base-url value Model base URL of Ollama, e.g. https://registry.ollama.ai. (default: "https://registry.ollama.ai")
88+
--ol-model value Model name of Ollama, e.g. gemma2.
89+
--ol-usage Specify respecting the extending layers introduced by Ollama, works with --ol-model, which affects the usage estimation. (default: false)
8990
9091
Output
9192

cmd/gguf-parser/main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ func main() {
223223
"works with --ms-repo/--ms-file pair or --ms-draft-repo/--ms-draft-file pair. " +
224224
"See https://modelscope.cn/my/myaccesstoken.",
225225
},
226+
&cli.StringFlag{
227+
Destination: &olBaseURL,
228+
Value: olBaseURL,
229+
Category: "Model/Remote/Ollama",
230+
Name: "ol-base-url",
231+
Usage: "Model base URL of Ollama, e.g. " +
232+
"https://registry.ollama.ai.",
233+
},
226234
&cli.StringFlag{
227235
Destination: &olModel,
228236
Value: olModel,
@@ -561,6 +569,7 @@ var (
561569
msDraftRepo string // for estimate
562570
msDraftFile string // for estimate
563571
msToken string
572+
olBaseURL = "https://registry.ollama.ai"
564573
olModel string
565574
olUsage bool
566575
// load options
@@ -730,7 +739,7 @@ func mainAction(c *cli.Context) error {
730739
}
731740
gf, err = ParseGGUFFileFromModelScope(ctx, msRepo, msFile, ropts...)
732741
case olModel != "":
733-
om := ParseOllamaModel(olModel)
742+
om := ParseOllamaModel(olModel, SetOllamaModelBaseURL(olBaseURL))
734743
gf, err = ParseGGUFFileFromOllamaModel(ctx, om, ropts...)
735744
if om != nil && olUsage {
736745
// Parameters override.

ollama_model.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,36 @@ type (
6969

7070
// ParseOllamaModel parses the given Ollama model string,
7171
// and returns the OllamaModel, or nil if the model is invalid.
72-
func ParseOllamaModel(model string) *OllamaModel {
72+
func ParseOllamaModel(model string, opts ...OllamaModelOption) *OllamaModel {
7373
if model == "" {
7474
return nil
7575
}
7676

77+
var o _OllamaModelOptions
78+
for _, opt := range opts {
79+
opt(&o)
80+
}
81+
7782
om := OllamaModel{
7883
Schema: OllamaDefaultScheme,
7984
Registry: OllamaDefaultRegistry,
8085
Namespace: OllamaDefaultNamespace,
8186
Tag: OllamaDefaultTag,
8287
}
88+
{
89+
if o.DefaultScheme != "" {
90+
om.Schema = o.DefaultScheme
91+
}
92+
if o.DefaultRegistry != "" {
93+
om.Registry = o.DefaultRegistry
94+
}
95+
if o.DefaultNamespace != "" {
96+
om.Namespace = o.DefaultNamespace
97+
}
98+
if o.DefaultTag != "" {
99+
om.Tag = o.DefaultTag
100+
}
101+
}
83102

84103
m := model
85104

ollama_model_option.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package gguf_parser
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
)
7+
8+
type (
9+
_OllamaModelOptions struct {
10+
DefaultScheme string
11+
DefaultRegistry string
12+
DefaultNamespace string
13+
DefaultTag string
14+
}
15+
OllamaModelOption func(*_OllamaModelOptions)
16+
)
17+
18+
// SetOllamaModelBaseURL parses the given base URL,
19+
// and sets default schema/registry for OllamaModel.
20+
func SetOllamaModelBaseURL(baseURL string) OllamaModelOption {
21+
baseURL = strings.TrimSpace(baseURL)
22+
return func(o *_OllamaModelOptions) {
23+
if baseURL == "" {
24+
return
25+
}
26+
27+
if !strings.Contains(baseURL, "://") {
28+
baseURL = "https://" + baseURL
29+
}
30+
31+
u, err := url.Parse(baseURL)
32+
if err != nil {
33+
return
34+
}
35+
36+
o.DefaultScheme = u.Scheme
37+
o.DefaultRegistry = u.Host
38+
}
39+
}
40+
41+
// SetOllamaModelDefaultScheme sets the default scheme for OllamaModel.
42+
func SetOllamaModelDefaultScheme(scheme string) OllamaModelOption {
43+
return func(o *_OllamaModelOptions) {
44+
if scheme == "" {
45+
return
46+
}
47+
o.DefaultScheme = scheme
48+
}
49+
}
50+
51+
// SetOllamaModelDefaultRegistry sets the default registry for OllamaModel.
52+
func SetOllamaModelDefaultRegistry(registry string) OllamaModelOption {
53+
return func(o *_OllamaModelOptions) {
54+
if registry == "" {
55+
return
56+
}
57+
o.DefaultRegistry = registry
58+
}
59+
}
60+
61+
// SetOllamaModelDefaultNamespace sets the default namespace for OllamaModel.
62+
func SetOllamaModelDefaultNamespace(namespace string) OllamaModelOption {
63+
return func(o *_OllamaModelOptions) {
64+
if namespace == "" {
65+
return
66+
}
67+
o.DefaultNamespace = namespace
68+
}
69+
}
70+
71+
// SetOllamaModelDefaultTag sets the default tag for OllamaModel.
72+
func SetOllamaModelDefaultTag(tag string) OllamaModelOption {
73+
return func(o *_OllamaModelOptions) {
74+
if tag == "" {
75+
return
76+
}
77+
o.DefaultTag = tag
78+
}
79+
}

0 commit comments

Comments
 (0)