Skip to content

Commit 2c7f956

Browse files
committed
add version
1 parent 0a89241 commit 2c7f956

File tree

7 files changed

+47
-36
lines changed

7 files changed

+47
-36
lines changed

api/client.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
"net/http"
1111
"net/url"
1212
"os"
13+
"runtime"
1314
"strings"
15+
16+
"github.com/jmorganca/ollama/version"
1417
)
1518

1619
const DefaultHost = "localhost:11434"
@@ -83,21 +86,21 @@ func (c *Client) do(ctx context.Context, method, path string, reqData, respData
8386
reqBody = bytes.NewReader(data)
8487
}
8588

86-
url := c.Base.JoinPath(path).String()
87-
88-
req, err := http.NewRequestWithContext(ctx, method, url, reqBody)
89+
requestURL := c.Base.JoinPath(path)
90+
request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), reqBody)
8991
if err != nil {
9092
return err
9193
}
9294

93-
req.Header.Set("Content-Type", "application/json")
94-
req.Header.Set("Accept", "application/json")
95+
request.Header.Set("Content-Type", "application/json")
96+
request.Header.Set("Accept", "application/json")
97+
request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
9598

9699
for k, v := range c.Headers {
97-
req.Header[k] = v
100+
request.Header[k] = v
98101
}
99102

100-
respObj, err := c.HTTP.Do(req)
103+
respObj, err := c.HTTP.Do(request)
101104
if err != nil {
102105
return err
103106
}
@@ -131,13 +134,15 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
131134
buf = bytes.NewBuffer(bts)
132135
}
133136

134-
request, err := http.NewRequestWithContext(ctx, method, c.Base.JoinPath(path).String(), buf)
137+
requestURL := c.Base.JoinPath(path)
138+
request, err := http.NewRequestWithContext(ctx, method, requestURL.String(), buf)
135139
if err != nil {
136140
return err
137141
}
138142

139143
request.Header.Set("Content-Type", "application/json")
140144
request.Header.Set("Accept", "application/json")
145+
request.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
141146

142147
response, err := http.DefaultClient.Do(request)
143148
if err != nil {

cmd/cmd.go

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/jmorganca/ollama/format"
3131
"github.com/jmorganca/ollama/progressbar"
3232
"github.com/jmorganca/ollama/server"
33+
"github.com/jmorganca/ollama/version"
3334
)
3435

3536
func CreateHandler(cmd *cobra.Command, args []string) error {
@@ -727,6 +728,7 @@ func NewCLI() *cobra.Command {
727728
CompletionOptions: cobra.CompletionOptions{
728729
DisableDefaultCmd: true,
729730
},
731+
Version: version.Version,
730732
}
731733

732734
cobra.EnableCommandSorting = false

scripts/build_darwin.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
mkdir -p dist
44

5+
GO_LDFLAGS="-X github.com/jmorganca/ollama/version.Version=$VERSION"
6+
57
# build universal binary
6-
CGO_ENABLED=1 GOARCH=arm64 go build -o dist/ollama-darwin-arm64
7-
CGO_ENABLED=1 GOARCH=amd64 go build -o dist/ollama-darwin-amd64
8+
CGO_ENABLED=1 GOARCH=arm64 go build -ldflags "$GO_LDFLAGS" -o dist/ollama-darwin-arm64
9+
CGO_ENABLED=1 GOARCH=amd64 go build -ldflags "$GO_LDFLAGS" -o dist/ollama-darwin-amd64
810
lipo -create -output dist/ollama dist/ollama-darwin-arm64 dist/ollama-darwin-amd64
911
rm dist/ollama-darwin-amd64 dist/ollama-darwin-arm64
1012
codesign --deep --force --options=runtime --sign "$APPLE_IDENTITY" --timestamp dist/ollama

server/auth.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ func getAuthToken(ctx context.Context, redirData AuthRedirect, regOpts *Registry
9494
return "", err
9595
}
9696

97-
headers := map[string]string{
98-
"Authorization": sig,
99-
}
100-
97+
headers := make(http.Header)
98+
headers.Set("Authorization", sig)
10199
resp, err := makeRequest(ctx, "GET", url, headers, nil, regOpts)
102100
if err != nil {
103101
log.Printf("couldn't get token: %q", err)

server/download.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ func doDownload(ctx context.Context, opts downloadOpts, f *FileDownload) error {
156156
}
157157

158158
url := fmt.Sprintf("%s/v2/%s/blobs/%s", opts.mp.Registry, opts.mp.GetNamespaceRepository(), f.Digest)
159-
headers := map[string]string{
160-
"Range": fmt.Sprintf("bytes=%d-", size),
161-
}
159+
160+
headers := make(http.Header)
161+
headers.Set("Range", fmt.Sprintf("bytes=%d-", size))
162162

163163
resp, err := makeRequest(ctx, "GET", url, headers, nil, opts.regOpts)
164164
if err != nil {

server/images.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import (
1616
"path"
1717
"path/filepath"
1818
"reflect"
19+
"runtime"
1920
"strconv"
2021
"strings"
2122

2223
"github.com/jmorganca/ollama/api"
2324
"github.com/jmorganca/ollama/llm"
2425
"github.com/jmorganca/ollama/parser"
2526
"github.com/jmorganca/ollama/vector"
27+
"github.com/jmorganca/ollama/version"
2628
)
2729

2830
const MaxRetries = 3
@@ -1005,15 +1007,14 @@ func PushModel(ctx context.Context, name string, regOpts *RegistryOptions, fn fu
10051007

10061008
fn(api.ProgressResponse{Status: "pushing manifest"})
10071009
url := fmt.Sprintf("%s/v2/%s/manifests/%s", mp.Registry, mp.GetNamespaceRepository(), mp.Tag)
1008-
headers := map[string]string{
1009-
"Content-Type": "application/vnd.docker.distribution.manifest.v2+json",
1010-
}
10111010

10121011
manifestJSON, err := json.Marshal(manifest)
10131012
if err != nil {
10141013
return err
10151014
}
10161015

1016+
headers := make(http.Header)
1017+
headers.Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
10171018
resp, err := makeRequestWithRetry(ctx, "PUT", url, headers, bytes.NewReader(manifestJSON), regOpts)
10181019
if err != nil {
10191020
return err
@@ -1098,10 +1099,9 @@ func PullModel(ctx context.Context, name string, regOpts *RegistryOptions, fn fu
10981099

10991100
func pullModelManifest(ctx context.Context, mp ModelPath, regOpts *RegistryOptions) (*ManifestV2, error) {
11001101
url := fmt.Sprintf("%s/v2/%s/manifests/%s", mp.Registry, mp.GetNamespaceRepository(), mp.Tag)
1101-
headers := map[string]string{
1102-
"Accept": "application/vnd.docker.distribution.manifest.v2+json",
1103-
}
11041102

1103+
headers := make(http.Header)
1104+
headers.Set("Accept", "application/vnd.docker.distribution.manifest.v2+json")
11051105
resp, err := makeRequest(ctx, "GET", url, headers, nil, regOpts)
11061106
if err != nil {
11071107
log.Printf("couldn't get manifest: %v", err)
@@ -1226,11 +1226,10 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, url string, layer *Lay
12261226

12271227
sectionReader := io.NewSectionReader(f, int64(completed), chunk)
12281228

1229-
headers := make(map[string]string)
1230-
headers["Content-Type"] = "application/octet-stream"
1231-
headers["Content-Length"] = strconv.Itoa(int(chunk))
1232-
headers["Content-Range"] = fmt.Sprintf("%d-%d", completed, completed+sectionReader.Size()-1)
1233-
1229+
headers := make(http.Header)
1230+
headers.Set("Content-Type", "application/octet-stream")
1231+
headers.Set("Content-Length", strconv.Itoa(int(chunk)))
1232+
headers.Set("Content-Range", fmt.Sprintf("%d-%d", completed, completed+sectionReader.Size()-1))
12341233
resp, err := makeRequestWithRetry(ctx, "PATCH", url, headers, sectionReader, regOpts)
12351234
if err != nil && !errors.Is(err, io.EOF) {
12361235
fn(api.ProgressResponse{
@@ -1260,9 +1259,9 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, url string, layer *Lay
12601259

12611260
url = fmt.Sprintf("%s&digest=%s", url, layer.Digest)
12621261

1263-
headers := make(map[string]string)
1264-
headers["Content-Type"] = "application/octet-stream"
1265-
headers["Content-Length"] = "0"
1262+
headers := make(http.Header)
1263+
headers.Set("Content-Type", "application/octet-stream")
1264+
headers.Set("Content-Length", "0")
12661265

12671266
// finish the upload
12681267
resp, err := makeRequest(ctx, "PUT", url, headers, nil, regOpts)
@@ -1279,7 +1278,7 @@ func uploadBlobChunked(ctx context.Context, mp ModelPath, url string, layer *Lay
12791278
return nil
12801279
}
12811280

1282-
func makeRequestWithRetry(ctx context.Context, method, url string, headers map[string]string, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
1281+
func makeRequestWithRetry(ctx context.Context, method, url string, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
12831282
var status string
12841283
for try := 0; try < MaxRetries; try++ {
12851284
resp, err := makeRequest(ctx, method, url, headers, body, regOpts)
@@ -1318,7 +1317,7 @@ func makeRequestWithRetry(ctx context.Context, method, url string, headers map[s
13181317
return nil, fmt.Errorf("max retry exceeded: %v", status)
13191318
}
13201319

1321-
func makeRequest(ctx context.Context, method, url string, headers map[string]string, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {
1320+
func makeRequest(ctx context.Context, method, url string, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {
13221321
if !strings.HasPrefix(url, "http") {
13231322
if regOpts.Insecure {
13241323
url = "http://" + url
@@ -1332,15 +1331,17 @@ func makeRequest(ctx context.Context, method, url string, headers map[string]str
13321331
return nil, err
13331332
}
13341333

1334+
if headers != nil {
1335+
req.Header = headers
1336+
}
1337+
13351338
if regOpts.Token != "" {
13361339
req.Header.Set("Authorization", "Bearer "+regOpts.Token)
13371340
} else if regOpts.Username != "" && regOpts.Password != "" {
13381341
req.SetBasicAuth(regOpts.Username, regOpts.Password)
13391342
}
13401343

1341-
for k, v := range headers {
1342-
req.Header.Set(k, v)
1343-
}
1344+
req.Header.Set("User-Agent", fmt.Sprintf("ollama/%s (%s %s) Go/%s", version.Version, runtime.GOARCH, runtime.GOOS, runtime.Version()))
13441345

13451346
client := &http.Client{
13461347
CheckRedirect: func(req *http.Request, via []*http.Request) error {

version/version.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package version
2+
3+
var Version string = "0.0.0"

0 commit comments

Comments
 (0)