2025-03-14 13:18:29 +08:00
|
|
|
// Package registry implements an http.Handler for handling local Ollama API
|
|
|
|
// model management requests. See [Local] for details.
|
2025-02-28 04:04:53 +08:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cmp"
|
2025-04-18 03:43:09 +08:00
|
|
|
"context"
|
2025-02-28 04:04:53 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
"fmt"
|
2025-02-28 04:04:53 +08:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2025-04-16 14:24:44 +08:00
|
|
|
"slices"
|
2025-04-18 03:43:09 +08:00
|
|
|
"strings"
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2025-02-28 04:04:53 +08:00
|
|
|
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
"github.com/ollama/ollama/server/internal/cache/blob"
|
2025-02-28 04:04:53 +08:00
|
|
|
"github.com/ollama/ollama/server/internal/client/ollama"
|
2025-04-16 14:24:44 +08:00
|
|
|
"github.com/ollama/ollama/server/internal/internal/backoff"
|
2025-02-28 04:04:53 +08:00
|
|
|
)
|
|
|
|
|
2025-03-14 13:18:29 +08:00
|
|
|
// Local implements an http.Handler for handling local Ollama API model
|
|
|
|
// management requests, such as pushing, pulling, and deleting models.
|
2025-02-28 04:04:53 +08:00
|
|
|
//
|
2025-03-14 13:18:29 +08:00
|
|
|
// It can be arranged for all unknown requests to be passed through to a
|
|
|
|
// fallback handler, if one is provided.
|
2025-02-28 04:04:53 +08:00
|
|
|
type Local struct {
|
|
|
|
Client *ollama.Registry // required
|
|
|
|
Logger *slog.Logger // required
|
|
|
|
|
|
|
|
// Fallback, if set, is used to handle requests that are not handled by
|
|
|
|
// this handler.
|
|
|
|
Fallback http.Handler
|
2025-03-04 11:11:16 +08:00
|
|
|
|
|
|
|
// Prune, if set, is called to prune the local disk cache after a model
|
|
|
|
// is deleted.
|
|
|
|
Prune func() error // optional
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverError is like ollama.Error, but with a Status field for the HTTP
|
|
|
|
// response code. We want to avoid adding that field to ollama.Error because it
|
|
|
|
// would always be 0 to clients (we don't want to leak the status code in
|
|
|
|
// errors), and so it would be confusing to have a field that is always 0.
|
|
|
|
type serverError struct {
|
|
|
|
Status int `json:"-"`
|
|
|
|
|
|
|
|
// TODO(bmizerany): Decide if we want to keep this and maybe
|
|
|
|
// bring back later.
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
|
|
|
Message string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e serverError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common API errors
|
|
|
|
var (
|
|
|
|
errMethodNotAllowed = &serverError{405, "method_not_allowed", "method not allowed"}
|
|
|
|
errNotFound = &serverError{404, "not_found", "not found"}
|
2025-03-14 13:18:29 +08:00
|
|
|
errModelNotFound = &serverError{404, "not_found", "model not found"}
|
2025-02-28 04:04:53 +08:00
|
|
|
errInternalError = &serverError{500, "internal_error", "internal server error"}
|
|
|
|
)
|
|
|
|
|
|
|
|
type statusCodeRecorder struct {
|
|
|
|
_status int // use status() to get the status code
|
|
|
|
http.ResponseWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *statusCodeRecorder) WriteHeader(status int) {
|
|
|
|
if r._status == 0 {
|
|
|
|
r._status = status
|
2025-04-25 04:09:39 +08:00
|
|
|
r.ResponseWriter.WriteHeader(status)
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
2025-04-25 04:09:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *statusCodeRecorder) Write(b []byte) (int, error) {
|
|
|
|
r._status = r.status()
|
|
|
|
return r.ResponseWriter.Write(b)
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
|
|
|
|
2025-02-28 06:00:37 +08:00
|
|
|
var (
|
|
|
|
_ http.ResponseWriter = (*statusCodeRecorder)(nil)
|
|
|
|
_ http.CloseNotifier = (*statusCodeRecorder)(nil)
|
|
|
|
_ http.Flusher = (*statusCodeRecorder)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// CloseNotify implements the http.CloseNotifier interface, for Gin. Remove with Gin.
|
|
|
|
//
|
|
|
|
// It panics if the underlying ResponseWriter is not a CloseNotifier.
|
|
|
|
func (r *statusCodeRecorder) CloseNotify() <-chan bool {
|
|
|
|
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements the http.Flusher interface, for Gin. Remove with Gin.
|
|
|
|
//
|
|
|
|
// It panics if the underlying ResponseWriter is not a Flusher.
|
|
|
|
func (r *statusCodeRecorder) Flush() {
|
|
|
|
r.ResponseWriter.(http.Flusher).Flush()
|
|
|
|
}
|
|
|
|
|
2025-02-28 04:04:53 +08:00
|
|
|
func (r *statusCodeRecorder) status() int {
|
|
|
|
return cmp.Or(r._status, 200)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Local) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rec := &statusCodeRecorder{ResponseWriter: w}
|
|
|
|
s.serveHTTP(rec, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Local) serveHTTP(rec *statusCodeRecorder, r *http.Request) {
|
|
|
|
var errattr slog.Attr
|
|
|
|
proxied, err := func() (bool, error) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/api/delete":
|
|
|
|
return false, s.handleDelete(rec, r)
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
case "/api/pull":
|
|
|
|
return false, s.handlePull(rec, r)
|
2025-02-28 04:04:53 +08:00
|
|
|
default:
|
|
|
|
if s.Fallback != nil {
|
|
|
|
s.Fallback.ServeHTTP(rec, r)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, errNotFound
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
// We always log the error, so fill in the error log attribute
|
|
|
|
errattr = slog.String("error", err.Error())
|
|
|
|
|
|
|
|
var e *serverError
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &e):
|
|
|
|
case errors.Is(err, ollama.ErrNameInvalid):
|
|
|
|
e = &serverError{400, "bad_request", err.Error()}
|
|
|
|
default:
|
|
|
|
e = errInternalError
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
// unreachable
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
rec.Header().Set("Content-Type", "application/json")
|
|
|
|
rec.WriteHeader(e.Status)
|
|
|
|
rec.Write(data)
|
|
|
|
|
|
|
|
// fallthrough to log
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proxied {
|
|
|
|
// we're only responsible for logging if we handled the request
|
|
|
|
var level slog.Level
|
|
|
|
if rec.status() >= 500 {
|
|
|
|
level = slog.LevelError
|
|
|
|
} else if rec.status() >= 400 {
|
|
|
|
level = slog.LevelWarn
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Logger.LogAttrs(r.Context(), level, "http",
|
|
|
|
errattr, // report first in line to make it easy to find
|
|
|
|
|
|
|
|
// TODO(bmizerany): Write a test to ensure that we are logging
|
|
|
|
// all of this correctly. That also goes for the level+error
|
|
|
|
// logic above.
|
|
|
|
slog.Int("status", rec.status()),
|
|
|
|
slog.String("method", r.Method),
|
|
|
|
slog.String("path", r.URL.Path),
|
|
|
|
slog.Int64("content-length", r.ContentLength),
|
|
|
|
slog.String("remote", r.RemoteAddr),
|
|
|
|
slog.String("proto", r.Proto),
|
|
|
|
slog.String("query", r.URL.RawQuery),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type params struct {
|
2025-03-14 13:18:29 +08:00
|
|
|
// DeprecatedName is the name of the model to push, pull, or delete,
|
|
|
|
// but is deprecated. New clients should use [Model] instead.
|
|
|
|
//
|
|
|
|
// Use [model()] to get the model name for both old and new API requests.
|
|
|
|
DeprecatedName string `json:"name"`
|
|
|
|
|
|
|
|
// Model is the name of the model to push, pull, or delete.
|
|
|
|
//
|
|
|
|
// Use [model()] to get the model name for both old and new API requests.
|
|
|
|
Model string `json:"model"`
|
2025-02-28 04:04:53 +08:00
|
|
|
|
|
|
|
// AllowNonTLS is a flag that indicates a client using HTTP
|
|
|
|
// is doing so, deliberately.
|
|
|
|
//
|
|
|
|
// Deprecated: This field is ignored and only present for this
|
|
|
|
// deprecation message. It should be removed in a future release.
|
|
|
|
//
|
|
|
|
// Users can just use http or https+insecure to show intent to
|
|
|
|
// communicate they want to do insecure things, without awkward and
|
|
|
|
// confusing flags such as this.
|
|
|
|
AllowNonTLS bool `json:"insecure"`
|
|
|
|
|
2025-03-14 13:18:29 +08:00
|
|
|
// Stream, if true, will make the server send progress updates in a
|
|
|
|
// streaming of JSON objects. If false, the server will send a single
|
|
|
|
// JSON object with the final status as "success", or an error object
|
|
|
|
// if an error occurred.
|
|
|
|
//
|
|
|
|
// Unfortunately, this API was designed to be a bit awkward. Stream is
|
|
|
|
// defined to default to true if not present, so we need a way to check
|
2025-03-22 04:03:43 +08:00
|
|
|
// if the client decisively set it to false. So, we use a pointer to a
|
2025-03-14 13:18:29 +08:00
|
|
|
// bool. Gross.
|
|
|
|
//
|
|
|
|
// Use [stream()] to get the correct value for this field.
|
|
|
|
Stream *bool `json:"stream"`
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// model returns the model name for both old and new API requests.
|
|
|
|
func (p params) model() string {
|
|
|
|
return cmp.Or(p.Model, p.DeprecatedName)
|
|
|
|
}
|
|
|
|
|
2025-03-14 13:18:29 +08:00
|
|
|
func (p params) stream() bool {
|
|
|
|
if p.Stream == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return *p.Stream
|
|
|
|
}
|
|
|
|
|
2025-02-28 04:04:53 +08:00
|
|
|
func (s *Local) handleDelete(_ http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.Method != "DELETE" {
|
|
|
|
return errMethodNotAllowed
|
|
|
|
}
|
|
|
|
p, err := decodeUserJSON[*params](r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-03-03 12:55:44 +08:00
|
|
|
ok, err := s.Client.Unlink(p.model())
|
2025-02-28 04:04:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
2025-03-14 13:18:29 +08:00
|
|
|
return errModelNotFound
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
2025-03-14 13:18:29 +08:00
|
|
|
if s.Prune != nil {
|
|
|
|
return s.Prune()
|
2025-03-04 11:11:16 +08:00
|
|
|
}
|
2025-03-14 13:18:29 +08:00
|
|
|
return nil
|
2025-02-28 04:04:53 +08:00
|
|
|
}
|
|
|
|
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
type progressUpdateJSON struct {
|
2025-04-19 09:12:28 +08:00
|
|
|
Error string `json:"error,omitempty,omitzero"`
|
2025-03-14 13:18:29 +08:00
|
|
|
Status string `json:"status,omitempty,omitzero"`
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
Digest blob.Digest `json:"digest,omitempty,omitzero"`
|
|
|
|
Total int64 `json:"total,omitempty,omitzero"`
|
|
|
|
Completed int64 `json:"completed,omitempty,omitzero"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Local) handlePull(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
return errMethodNotAllowed
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := decodeUserJSON[*params](r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2025-03-14 13:18:29 +08:00
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
if !p.stream() {
|
|
|
|
if err := s.Client.Pull(r.Context(), p.model()); err != nil {
|
|
|
|
if errors.Is(err, ollama.ErrModelNotFound) {
|
|
|
|
return errModelNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2025-04-16 14:24:44 +08:00
|
|
|
enc.Encode(progressUpdateJSON{Status: "success"})
|
|
|
|
return nil
|
2025-03-14 13:18:29 +08:00
|
|
|
}
|
|
|
|
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
var mu sync.Mutex
|
2025-04-16 14:24:44 +08:00
|
|
|
var progress []progressUpdateJSON
|
2025-03-22 04:03:43 +08:00
|
|
|
flushProgress := func() {
|
2025-03-14 13:18:29 +08:00
|
|
|
mu.Lock()
|
2025-04-16 14:24:44 +08:00
|
|
|
progress := slices.Clone(progress) // make a copy and release lock before encoding to the wire
|
2025-03-14 13:18:29 +08:00
|
|
|
mu.Unlock()
|
2025-04-16 14:24:44 +08:00
|
|
|
for _, p := range progress {
|
|
|
|
enc.Encode(p)
|
|
|
|
}
|
|
|
|
fl, _ := w.(http.Flusher)
|
|
|
|
if fl != nil {
|
|
|
|
fl.Flush()
|
2025-03-14 13:18:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-16 14:24:44 +08:00
|
|
|
t := time.NewTicker(1<<63 - 1) // "unstarted" timer
|
2025-03-14 13:18:29 +08:00
|
|
|
start := sync.OnceFunc(func() {
|
2025-03-22 04:03:43 +08:00
|
|
|
flushProgress() // flush initial state
|
2025-03-14 13:18:29 +08:00
|
|
|
t.Reset(100 * time.Millisecond)
|
|
|
|
})
|
|
|
|
ctx := ollama.WithTrace(r.Context(), &ollama.Trace{
|
|
|
|
Update: func(l *ollama.Layer, n int64, err error) {
|
2025-04-16 14:24:44 +08:00
|
|
|
if err != nil && !errors.Is(err, ollama.ErrCached) {
|
|
|
|
s.Logger.Error("pulling", "model", p.model(), "error", err)
|
|
|
|
return
|
2025-03-14 13:18:29 +08:00
|
|
|
}
|
2025-04-16 14:24:44 +08:00
|
|
|
|
|
|
|
func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
for i, p := range progress {
|
|
|
|
if p.Digest == l.Digest {
|
|
|
|
progress[i].Completed = n
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
progress = append(progress, progressUpdateJSON{
|
|
|
|
Digest: l.Digest,
|
|
|
|
Total: l.Size,
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Block flushing progress updates until every
|
|
|
|
// layer is accounted for. Clients depend on a
|
|
|
|
// complete model size to calculate progress
|
|
|
|
// correctly; if they use an incomplete total,
|
|
|
|
// progress indicators would erratically jump
|
|
|
|
// as new layers are registered.
|
|
|
|
start()
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
done := make(chan error, 1)
|
2025-04-16 14:24:44 +08:00
|
|
|
go func() (err error) {
|
|
|
|
defer func() { done <- err }()
|
|
|
|
for _, err := range backoff.Loop(ctx, 3*time.Second) {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := s.Client.Pull(ctx, p.model())
|
2025-04-18 03:43:09 +08:00
|
|
|
if canRetry(err) {
|
|
|
|
continue
|
2025-04-16 14:24:44 +08:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
}()
|
|
|
|
|
2025-04-16 14:24:44 +08:00
|
|
|
enc.Encode(progressUpdateJSON{Status: "pulling manifest"})
|
2025-03-14 13:18:29 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
2025-03-22 04:03:43 +08:00
|
|
|
flushProgress()
|
2025-03-14 13:18:29 +08:00
|
|
|
case err := <-done:
|
2025-03-22 04:03:43 +08:00
|
|
|
flushProgress()
|
2025-03-14 13:18:29 +08:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ollama.ErrModelNotFound) {
|
2025-04-19 09:12:28 +08:00
|
|
|
return &serverError{
|
|
|
|
Status: 404,
|
|
|
|
Code: "not_found",
|
|
|
|
Message: fmt.Sprintf("model %q not found", p.model()),
|
|
|
|
}
|
2025-03-14 13:18:29 +08:00
|
|
|
} else {
|
2025-04-19 09:12:28 +08:00
|
|
|
return err
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
}
|
|
|
|
}
|
2025-04-16 14:24:44 +08:00
|
|
|
|
|
|
|
// Emulate old client pull progress (for now):
|
|
|
|
enc.Encode(progressUpdateJSON{Status: "verifying sha256 digest"})
|
|
|
|
enc.Encode(progressUpdateJSON{Status: "writing manifest"})
|
|
|
|
enc.Encode(progressUpdateJSON{Status: "success"})
|
2025-03-14 13:18:29 +08:00
|
|
|
return nil
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
}
|
2025-03-14 13:18:29 +08:00
|
|
|
}
|
server/internal/registry: take over pulls from server package (#9485)
This commit replaces the old pull implementation in the server package
with the new, faster, more robust pull implementation in the registry
package.
The new endpoint, and now the remove endpoint too, are behind the
feature gate "client2" enabled only by setting the OLLAMA_EXPERIMENT
environment variable include "client2".
Currently, the progress indication is wired to perform the same as the
previous implementation to avoid making changes to the CLI, and because
the status reports happen at the start of the download, and the end of
the write to disk, the progress indication is not as smooth as it could
be. This is a known issue and will be addressed in a future change.
This implementation may be ~0.5-1.0% slower in rare cases, depending on
network and disk speed, but is generally MUCH faster and more robust
than the its predecessor in all other cases.
2025-03-06 06:48:18 +08:00
|
|
|
}
|
|
|
|
|
2025-02-28 04:04:53 +08:00
|
|
|
func decodeUserJSON[T any](r io.Reader) (T, error) {
|
|
|
|
var v T
|
|
|
|
err := json.NewDecoder(r).Decode(&v)
|
|
|
|
if err == nil {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
var zero T
|
|
|
|
|
|
|
|
// Not sure why, but I can't seem to be able to use:
|
|
|
|
//
|
|
|
|
// errors.As(err, &json.UnmarshalTypeError{})
|
|
|
|
//
|
|
|
|
// This is working fine in stdlib, so I'm not sure what rules changed
|
|
|
|
// and why this no longer works here. So, we do it the verbose way.
|
|
|
|
var a *json.UnmarshalTypeError
|
|
|
|
var b *json.SyntaxError
|
|
|
|
if errors.As(err, &a) || errors.As(err, &b) {
|
|
|
|
err = &serverError{Status: 400, Message: err.Error(), Code: "bad_request"}
|
|
|
|
}
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
err = &serverError{Status: 400, Message: "empty request body", Code: "bad_request"}
|
|
|
|
}
|
|
|
|
return zero, err
|
|
|
|
}
|
2025-04-18 03:43:09 +08:00
|
|
|
|
|
|
|
func canRetry(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var oe *ollama.Error
|
|
|
|
if errors.As(err, &oe) {
|
|
|
|
return oe.Temporary()
|
|
|
|
}
|
|
|
|
s := err.Error()
|
|
|
|
return cmp.Or(
|
|
|
|
errors.Is(err, context.DeadlineExceeded),
|
|
|
|
strings.Contains(s, "unreachable"),
|
|
|
|
strings.Contains(s, "no route to host"),
|
|
|
|
strings.Contains(s, "connection reset by peer"),
|
|
|
|
)
|
|
|
|
}
|