1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234 |
- package server
- import (
- "bytes"
- "cmp"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "log/slog"
- "net"
- "net/http"
- "net/netip"
- "os"
- "os/signal"
- "path/filepath"
- "slices"
- "strings"
- "syscall"
- "time"
- "github.com/gin-contrib/cors"
- "github.com/gin-gonic/gin"
- "github.com/ollama/ollama/api"
- "github.com/ollama/ollama/envconfig"
- "github.com/ollama/ollama/gpu"
- "github.com/ollama/ollama/llm"
- "github.com/ollama/ollama/openai"
- "github.com/ollama/ollama/parser"
- "github.com/ollama/ollama/template"
- "github.com/ollama/ollama/types/errtypes"
- "github.com/ollama/ollama/types/model"
- "github.com/ollama/ollama/version"
- )
- var mode string = gin.DebugMode
- type Server struct {
- addr net.Addr
- sched *Scheduler
- }
- func init() {
- switch mode {
- case gin.DebugMode:
- case gin.ReleaseMode:
- case gin.TestMode:
- default:
- mode = gin.DebugMode
- }
- gin.SetMode(mode)
- }
- var errRequired = errors.New("is required")
- func modelOptions(model *Model, requestOpts map[string]interface{}) (api.Options, error) {
- opts := api.DefaultOptions()
- if err := opts.FromMap(model.Options); err != nil {
- return api.Options{}, err
- }
- if err := opts.FromMap(requestOpts); err != nil {
- return api.Options{}, err
- }
- return opts, nil
- }
- // scheduleRunner schedules a runner after validating inputs such as capabilities and model options.
- // It returns the allocated runner, model instance, and consolidated options if successful and error otherwise.
- func (s *Server) scheduleRunner(ctx context.Context, name string, caps []Capability, requestOpts map[string]any, keepAlive *api.Duration) (llm.LlamaServer, *Model, *api.Options, error) {
- if name == "" {
- return nil, nil, nil, fmt.Errorf("model %w", errRequired)
- }
- model, err := GetModel(name)
- if err != nil {
- return nil, nil, nil, err
- }
- if err := model.CheckCapabilities(caps...); err != nil {
- return nil, nil, nil, fmt.Errorf("%s %w", name, err)
- }
- opts, err := modelOptions(model, requestOpts)
- if err != nil {
- return nil, nil, nil, err
- }
- runnerCh, errCh := s.sched.GetRunner(ctx, model, opts, keepAlive)
- var runner *runnerRef
- select {
- case runner = <-runnerCh:
- case err = <-errCh:
- return nil, nil, nil, err
- }
- return runner.llama, model, &opts, nil
- }
- func (s *Server) GenerateHandler(c *gin.Context) {
- var req api.GenerateRequest
- if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if req.Format != "" && req.Format != "json" {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "format must be empty or \"json\""})
- return
- } else if req.Raw && (req.Template != "" || req.System != "" || len(req.Context) > 0) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "raw mode does not support template, system, or context"})
- return
- }
- caps := []Capability{CapabilityCompletion}
- r, m, opts, err := s.scheduleRunner(c.Request.Context(), req.Model, caps, req.Options, req.KeepAlive)
- if errors.Is(err, errCapabilityCompletion) {
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support generate", req.Model)})
- return
- } else if err != nil {
- handleScheduleError(c, req.Model, err)
- return
- }
- if req.Prompt == "" {
- c.JSON(http.StatusOK, api.GenerateResponse{
- Model: req.Model,
- CreatedAt: time.Now().UTC(),
- Done: true,
- DoneReason: "load",
- })
- return
- }
- images := make([]llm.ImageData, len(req.Images))
- for i := range req.Images {
- images[i] = llm.ImageData{ID: i, Data: req.Images[i]}
- }
- prompt := req.Prompt
- if !req.Raw {
- var msgs []api.Message
- if req.System != "" {
- msgs = append(msgs, api.Message{Role: "system", Content: req.System})
- } else if m.System != "" {
- msgs = append(msgs, api.Message{Role: "system", Content: m.System})
- }
- for _, i := range images {
- msgs = append(msgs, api.Message{Role: "user", Content: fmt.Sprintf("[img-%d]", i.ID)})
- }
- msgs = append(msgs, api.Message{Role: "user", Content: req.Prompt})
- tmpl := m.Template
- if req.Template != "" {
- tmpl, err = template.Parse(req.Template)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- }
- var b bytes.Buffer
- if req.Context != nil {
- s, err := r.Detokenize(c.Request.Context(), req.Context)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- b.WriteString(s)
- }
- if err := tmpl.Execute(&b, template.Values{Messages: msgs}); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- prompt = b.String()
- }
- slog.Debug("generate request", "prompt", prompt, "images", images)
- ch := make(chan any)
- go func() {
- defer close(ch)
- if err := r.Completion(c.Request.Context(), llm.CompletionRequest{
- Prompt: prompt,
- Images: images,
- Format: req.Format,
- Options: opts,
- }, func(r llm.CompletionResponse) {
- ch <- api.GenerateResponse{
- Model: req.Model,
- CreatedAt: time.Now().UTC(),
- Response: r.Content,
- Done: r.Done,
- DoneReason: r.DoneReason,
- Metrics: api.Metrics{
- PromptEvalCount: r.PromptEvalCount,
- PromptEvalDuration: r.PromptEvalDuration,
- EvalCount: r.EvalCount,
- EvalDuration: r.EvalDuration,
- },
- }
- }); err != nil {
- ch <- gin.H{"error": err.Error()}
- }
- }()
- if req.Stream != nil && !*req.Stream {
- var r api.GenerateResponse
- var sb strings.Builder
- for rr := range ch {
- switch t := rr.(type) {
- case api.GenerateResponse:
- sb.WriteString(t.Response)
- r = t
- case gin.H:
- msg, ok := t["error"].(string)
- if !ok {
- msg = "unexpected error format in response"
- }
- c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
- return
- default:
- c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected response"})
- return
- }
- }
- r.Response = sb.String()
- c.JSON(http.StatusOK, r)
- return
- }
- streamResponse(c, ch)
- }
- func (s *Server) EmbeddingsHandler(c *gin.Context) {
- var req api.EmbeddingRequest
- if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- r, _, _, err := s.scheduleRunner(c.Request.Context(), req.Model, []Capability{}, req.Options, req.KeepAlive)
- if err != nil {
- handleScheduleError(c, req.Model, err)
- return
- }
- // an empty request loads the model
- if req.Prompt == "" {
- c.JSON(http.StatusOK, api.EmbeddingResponse{Embedding: []float64{}})
- return
- }
- embedding, err := r.Embedding(c.Request.Context(), req.Prompt)
- if err != nil {
- slog.Info(fmt.Sprintf("embedding generation failed: %v", err))
- c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate embedding"})
- return
- }
- c.JSON(http.StatusOK, api.EmbeddingResponse{Embedding: embedding})
- }
- func (s *Server) PullModelHandler(c *gin.Context) {
- var req api.PullRequest
- err := c.ShouldBindJSON(&req)
- switch {
- case errors.Is(err, io.EOF):
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- case err != nil:
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- name := model.ParseName(cmp.Or(req.Model, req.Name))
- if !name.IsValid() {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid model name"})
- return
- }
- if err := checkNameExists(name); err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- ch := make(chan any)
- go func() {
- defer close(ch)
- fn := func(r api.ProgressResponse) {
- ch <- r
- }
- regOpts := ®istryOptions{
- Insecure: req.Insecure,
- }
- ctx, cancel := context.WithCancel(c.Request.Context())
- defer cancel()
- if err := PullModel(ctx, name.DisplayShortest(), regOpts, fn); err != nil {
- ch <- gin.H{"error": err.Error()}
- }
- }()
- if req.Stream != nil && !*req.Stream {
- waitForStream(c, ch)
- return
- }
- streamResponse(c, ch)
- }
- func (s *Server) PushModelHandler(c *gin.Context) {
- var req api.PushRequest
- err := c.ShouldBindJSON(&req)
- switch {
- case errors.Is(err, io.EOF):
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- case err != nil:
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- var model string
- if req.Model != "" {
- model = req.Model
- } else if req.Name != "" {
- model = req.Name
- } else {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "model is required"})
- return
- }
- ch := make(chan any)
- go func() {
- defer close(ch)
- fn := func(r api.ProgressResponse) {
- ch <- r
- }
- regOpts := ®istryOptions{
- Insecure: req.Insecure,
- }
- ctx, cancel := context.WithCancel(c.Request.Context())
- defer cancel()
- if err := PushModel(ctx, model, regOpts, fn); err != nil {
- ch <- gin.H{"error": err.Error()}
- }
- }()
- if req.Stream != nil && !*req.Stream {
- waitForStream(c, ch)
- return
- }
- streamResponse(c, ch)
- }
- func checkNameExists(name model.Name) error {
- names, err := Manifests()
- if err != nil {
- return err
- }
- for n := range names {
- if strings.EqualFold(n.Filepath(), name.Filepath()) && n != name {
- return fmt.Errorf("a model with that name already exists")
- }
- }
- return nil
- }
- func (s *Server) CreateModelHandler(c *gin.Context) {
- var r api.CreateRequest
- if err := c.ShouldBindJSON(&r); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- name := model.ParseName(cmp.Or(r.Model, r.Name))
- if !name.IsValid() {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": errtypes.InvalidModelNameErrMsg})
- return
- }
- if err := checkNameExists(name); err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if r.Path == "" && r.Modelfile == "" {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "path or modelfile are required"})
- return
- }
- var sr io.Reader = strings.NewReader(r.Modelfile)
- if r.Path != "" && r.Modelfile == "" {
- f, err := os.Open(r.Path)
- if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("error reading modelfile: %s", err)})
- return
- }
- defer f.Close()
- sr = f
- }
- f, err := parser.ParseFile(sr)
- if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- ch := make(chan any)
- go func() {
- defer close(ch)
- fn := func(resp api.ProgressResponse) {
- ch <- resp
- }
- ctx, cancel := context.WithCancel(c.Request.Context())
- defer cancel()
- quantization := cmp.Or(r.Quantize, r.Quantization)
- if err := CreateModel(ctx, name, filepath.Dir(r.Path), strings.ToUpper(quantization), f, fn); err != nil {
- ch <- gin.H{"error": err.Error()}
- }
- }()
- if r.Stream != nil && !*r.Stream {
- waitForStream(c, ch)
- return
- }
- streamResponse(c, ch)
- }
- func (s *Server) DeleteModelHandler(c *gin.Context) {
- var r api.DeleteRequest
- if err := c.ShouldBindJSON(&r); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- n := model.ParseName(cmp.Or(r.Model, r.Name))
- if !n.IsValid() {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("name %q is invalid", cmp.Or(r.Model, r.Name))})
- return
- }
- m, err := ParseNamedManifest(n)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- if err := m.Remove(); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- if err := m.RemoveLayers(); err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- }
- func (s *Server) ShowModelHandler(c *gin.Context) {
- var req api.ShowRequest
- err := c.ShouldBindJSON(&req)
- switch {
- case errors.Is(err, io.EOF):
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- case err != nil:
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if req.Model != "" {
- // noop
- } else if req.Name != "" {
- req.Model = req.Name
- } else {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "model is required"})
- return
- }
- resp, err := GetModelInfo(req)
- if err != nil {
- switch {
- case os.IsNotExist(err):
- c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)})
- case err.Error() == "invalid model name":
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- default:
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- }
- return
- }
- c.JSON(http.StatusOK, resp)
- }
- func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) {
- m, err := GetModel(req.Model)
- if err != nil {
- return nil, err
- }
- modelDetails := api.ModelDetails{
- ParentModel: m.ParentModel,
- Format: m.Config.ModelFormat,
- Family: m.Config.ModelFamily,
- Families: m.Config.ModelFamilies,
- ParameterSize: m.Config.ModelType,
- QuantizationLevel: m.Config.FileType,
- }
- if req.System != "" {
- m.System = req.System
- }
- if req.Template != "" {
- m.Template, err = template.Parse(req.Template)
- if err != nil {
- return nil, err
- }
- }
- msgs := make([]api.Message, len(m.Messages))
- for i, msg := range m.Messages {
- msgs[i] = api.Message{Role: msg.Role, Content: msg.Content}
- }
- n := model.ParseName(req.Model)
- if !n.IsValid() {
- return nil, fmt.Errorf("invalid model name")
- }
- manifest, err := ParseNamedManifest(n)
- if err != nil {
- return nil, err
- }
- resp := &api.ShowResponse{
- License: strings.Join(m.License, "\n"),
- System: m.System,
- Template: m.Template.String(),
- Details: modelDetails,
- Messages: msgs,
- ModifiedAt: manifest.fi.ModTime(),
- }
- var params []string
- cs := 30
- for k, v := range m.Options {
- switch val := v.(type) {
- case []interface{}:
- for _, nv := range val {
- params = append(params, fmt.Sprintf("%-*s %#v", cs, k, nv))
- }
- default:
- params = append(params, fmt.Sprintf("%-*s %#v", cs, k, v))
- }
- }
- resp.Parameters = strings.Join(params, "\n")
- for k, v := range req.Options {
- if _, ok := req.Options[k]; ok {
- m.Options[k] = v
- }
- }
- var sb strings.Builder
- fmt.Fprintln(&sb, "# Modelfile generated by \"ollama show\"")
- fmt.Fprintln(&sb, "# To build a new Modelfile based on this, replace FROM with:")
- fmt.Fprintf(&sb, "# FROM %s\n\n", m.ShortName)
- fmt.Fprint(&sb, m.String())
- resp.Modelfile = sb.String()
- kvData, err := getKVData(m.ModelPath, req.Verbose)
- if err != nil {
- return nil, err
- }
- delete(kvData, "general.name")
- delete(kvData, "tokenizer.chat_template")
- resp.ModelInfo = kvData
- if len(m.ProjectorPaths) > 0 {
- projectorData, err := getKVData(m.ProjectorPaths[0], req.Verbose)
- if err != nil {
- return nil, err
- }
- resp.ProjectorInfo = projectorData
- }
- return resp, nil
- }
- func getKVData(digest string, verbose bool) (llm.KV, error) {
- maxArraySize := 0
- if verbose {
- maxArraySize = -1
- }
- kvData, err := llm.LoadModel(digest, maxArraySize)
- if err != nil {
- return nil, err
- }
- kv := kvData.KV()
- if !verbose {
- for k := range kv {
- if t, ok := kv[k].([]any); len(t) > 5 && ok {
- kv[k] = []any{}
- }
- }
- }
- return kv, nil
- }
- func (s *Server) ListModelsHandler(c *gin.Context) {
- ms, err := Manifests()
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- models := []api.ListModelResponse{}
- for n, m := range ms {
- f, err := m.Config.Open()
- if err != nil {
- slog.Warn("bad manifest filepath", "name", n, "error", err)
- continue
- }
- defer f.Close()
- var cf ConfigV2
- if err := json.NewDecoder(f).Decode(&cf); err != nil {
- slog.Warn("bad manifest config", "name", n, "error", err)
- continue
- }
- // tag should never be masked
- models = append(models, api.ListModelResponse{
- Model: n.DisplayShortest(),
- Name: n.DisplayShortest(),
- Size: m.Size(),
- Digest: m.digest,
- ModifiedAt: m.fi.ModTime(),
- Details: api.ModelDetails{
- Format: cf.ModelFormat,
- Family: cf.ModelFamily,
- Families: cf.ModelFamilies,
- ParameterSize: cf.ModelType,
- QuantizationLevel: cf.FileType,
- },
- })
- }
- slices.SortStableFunc(models, func(i, j api.ListModelResponse) int {
- // most recently modified first
- return cmp.Compare(j.ModifiedAt.Unix(), i.ModifiedAt.Unix())
- })
- c.JSON(http.StatusOK, api.ListResponse{Models: models})
- }
- func (s *Server) CopyModelHandler(c *gin.Context) {
- var r api.CopyRequest
- if err := c.ShouldBindJSON(&r); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- src := model.ParseName(r.Source)
- if !src.IsValid() {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("source %q is invalid", r.Source)})
- return
- }
- dst := model.ParseName(r.Destination)
- if !dst.IsValid() {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("destination %q is invalid", r.Destination)})
- return
- }
- if err := checkNameExists(dst); err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if err := CopyModel(src, dst); errors.Is(err, os.ErrNotExist) {
- c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model %q not found", r.Source)})
- } else if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- }
- }
- func (s *Server) HeadBlobHandler(c *gin.Context) {
- path, err := GetBlobsPath(c.Param("digest"))
- if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- if _, err := os.Stat(path); err != nil {
- c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("blob %q not found", c.Param("digest"))})
- return
- }
- c.Status(http.StatusOK)
- }
- func (s *Server) CreateBlobHandler(c *gin.Context) {
- if ib, ok := intermediateBlobs[c.Param("digest")]; ok {
- p, err := GetBlobsPath(ib)
- if err != nil {
- c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- if _, err := os.Stat(p); errors.Is(err, os.ErrNotExist) {
- slog.Info("evicting intermediate blob which no longer exists", "digest", ib)
- delete(intermediateBlobs, c.Param("digest"))
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- } else {
- c.Status(http.StatusOK)
- return
- }
- }
- path, err := GetBlobsPath(c.Param("digest"))
- if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- _, err = os.Stat(path)
- switch {
- case errors.Is(err, os.ErrNotExist):
- // noop
- case err != nil:
- c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- default:
- c.Status(http.StatusOK)
- return
- }
- layer, err := NewLayer(c.Request.Body, "")
- if err != nil {
- c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- if layer.Digest != c.Param("digest") {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("digest mismatch, expected %q, got %q", c.Param("digest"), layer.Digest)})
- return
- }
- c.Status(http.StatusCreated)
- }
- func isLocalIP(ip netip.Addr) bool {
- if interfaces, err := net.Interfaces(); err == nil {
- for _, iface := range interfaces {
- addrs, err := iface.Addrs()
- if err != nil {
- continue
- }
- for _, a := range addrs {
- if parsed, _, err := net.ParseCIDR(a.String()); err == nil {
- if parsed.String() == ip.String() {
- return true
- }
- }
- }
- }
- }
- return false
- }
- func allowedHost(host string) bool {
- if host == "" || host == "localhost" {
- return true
- }
- if hostname, err := os.Hostname(); err == nil && host == hostname {
- return true
- }
- var tlds = []string{
- "localhost",
- "local",
- "internal",
- }
- // check if the host is a local TLD
- for _, tld := range tlds {
- if strings.HasSuffix(host, "."+tld) {
- return true
- }
- }
- return false
- }
- func allowedHostsMiddleware(addr net.Addr) gin.HandlerFunc {
- return func(c *gin.Context) {
- if addr == nil {
- c.Next()
- return
- }
- if addr, err := netip.ParseAddrPort(addr.String()); err == nil && !addr.Addr().IsLoopback() {
- c.Next()
- return
- }
- host, _, err := net.SplitHostPort(c.Request.Host)
- if err != nil {
- host = c.Request.Host
- }
- if addr, err := netip.ParseAddr(host); err == nil {
- if addr.IsLoopback() || addr.IsPrivate() || addr.IsUnspecified() || isLocalIP(addr) {
- c.Next()
- return
- }
- }
- if allowedHost(host) {
- if c.Request.Method == http.MethodOptions {
- c.AbortWithStatus(http.StatusNoContent)
- return
- }
- c.Next()
- return
- }
- c.AbortWithStatus(http.StatusForbidden)
- }
- }
- func (s *Server) GenerateRoutes() http.Handler {
- config := cors.DefaultConfig()
- config.AllowWildcard = true
- config.AllowBrowserExtensions = true
- config.AllowHeaders = []string{"Authorization", "Content-Type", "User-Agent", "Accept", "X-Requested-With"}
- openAIProperties := []string{"lang", "package-version", "os", "arch", "runtime", "runtime-version", "async"}
- for _, prop := range openAIProperties {
- config.AllowHeaders = append(config.AllowHeaders, "x-stainless-"+prop)
- }
- config.AllowOrigins = envconfig.AllowOrigins
- r := gin.Default()
- r.Use(
- cors.New(config),
- allowedHostsMiddleware(s.addr),
- )
- r.POST("/api/pull", s.PullModelHandler)
- r.POST("/api/generate", s.GenerateHandler)
- r.POST("/api/chat", s.ChatHandler)
- r.POST("/api/embeddings", s.EmbeddingsHandler)
- r.POST("/api/create", s.CreateModelHandler)
- r.POST("/api/push", s.PushModelHandler)
- r.POST("/api/copy", s.CopyModelHandler)
- r.DELETE("/api/delete", s.DeleteModelHandler)
- r.POST("/api/show", s.ShowModelHandler)
- r.POST("/api/blobs/:digest", s.CreateBlobHandler)
- r.HEAD("/api/blobs/:digest", s.HeadBlobHandler)
- r.GET("/api/ps", s.ProcessHandler)
- // Compatibility endpoints
- r.POST("/v1/chat/completions", openai.ChatMiddleware(), s.ChatHandler)
- r.POST("/v1/completions", openai.CompletionsMiddleware(), s.GenerateHandler)
- r.GET("/v1/models", openai.ListMiddleware(), s.ListModelsHandler)
- r.GET("/v1/models/:model", openai.RetrieveMiddleware(), s.ShowModelHandler)
- for _, method := range []string{http.MethodGet, http.MethodHead} {
- r.Handle(method, "/", func(c *gin.Context) {
- c.String(http.StatusOK, "Ollama is running")
- })
- r.Handle(method, "/api/tags", s.ListModelsHandler)
- r.Handle(method, "/api/version", func(c *gin.Context) {
- c.JSON(http.StatusOK, gin.H{"version": version.Version})
- })
- }
- return r
- }
- func Serve(ln net.Listener) error {
- level := slog.LevelInfo
- if envconfig.Debug {
- level = slog.LevelDebug
- }
- slog.Info("server config", "env", envconfig.Values())
- handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
- Level: level,
- AddSource: true,
- ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
- if attr.Key == slog.SourceKey {
- source := attr.Value.Any().(*slog.Source)
- source.File = filepath.Base(source.File)
- }
- return attr
- },
- })
- slog.SetDefault(slog.New(handler))
- blobsDir, err := GetBlobsPath("")
- if err != nil {
- return err
- }
- if err := fixBlobs(blobsDir); err != nil {
- return err
- }
- if !envconfig.NoPrune {
- // clean up unused layers and manifests
- if err := PruneLayers(); err != nil {
- return err
- }
- manifestsPath, err := GetManifestPath()
- if err != nil {
- return err
- }
- if err := PruneDirectory(manifestsPath); err != nil {
- return err
- }
- }
- ctx, done := context.WithCancel(context.Background())
- schedCtx, schedDone := context.WithCancel(ctx)
- sched := InitScheduler(schedCtx)
- s := &Server{addr: ln.Addr(), sched: sched}
- http.Handle("/", s.GenerateRoutes())
- slog.Info(fmt.Sprintf("Listening on %s (version %s)", ln.Addr(), version.Version))
- srvr := &http.Server{
- // Use http.DefaultServeMux so we get net/http/pprof for
- // free.
- //
- // TODO(bmizerany): Decide if we want to make this
- // configurable so it is not exposed by default, or allow
- // users to bind it to a different port. This was a quick
- // and easy way to get pprof, but it may not be the best
- // way.
- Handler: nil,
- }
- // listen for a ctrl+c and stop any loaded llm
- signals := make(chan os.Signal, 1)
- signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
- go func() {
- <-signals
- srvr.Close()
- schedDone()
- sched.unloadAllRunners()
- gpu.Cleanup()
- done()
- }()
- if err := llm.Init(); err != nil {
- return fmt.Errorf("unable to initialize llm library %w", err)
- }
- s.sched.Run(schedCtx)
- // At startup we retrieve GPU information so we can get log messages before loading a model
- // This will log warnings to the log in case we have problems with detected GPUs
- gpus := gpu.GetGPUInfo()
- gpus.LogDetails()
- err = srvr.Serve(ln)
- // If server is closed from the signal handler, wait for the ctx to be done
- // otherwise error out quickly
- if !errors.Is(err, http.ErrServerClosed) {
- return err
- }
- <-ctx.Done()
- return nil
- }
- func waitForStream(c *gin.Context, ch chan interface{}) {
- c.Header("Content-Type", "application/json")
- for resp := range ch {
- switch r := resp.(type) {
- case api.ProgressResponse:
- if r.Status == "success" {
- c.JSON(http.StatusOK, r)
- return
- }
- case gin.H:
- if errorMsg, ok := r["error"].(string); ok {
- c.JSON(http.StatusInternalServerError, gin.H{"error": errorMsg})
- return
- } else {
- c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected error format in progress response"})
- return
- }
- default:
- c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected progress response"})
- return
- }
- }
- c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected end of progress response"})
- }
- func streamResponse(c *gin.Context, ch chan any) {
- c.Header("Content-Type", "application/x-ndjson")
- c.Stream(func(w io.Writer) bool {
- val, ok := <-ch
- if !ok {
- return false
- }
- bts, err := json.Marshal(val)
- if err != nil {
- slog.Info(fmt.Sprintf("streamResponse: json.Marshal failed with %s", err))
- return false
- }
- // Delineate chunks with new-line delimiter
- bts = append(bts, '\n')
- if _, err := w.Write(bts); err != nil {
- slog.Info(fmt.Sprintf("streamResponse: w.Write failed with %s", err))
- return false
- }
- return true
- })
- }
- func (s *Server) ProcessHandler(c *gin.Context) {
- models := []api.ProcessModelResponse{}
- for _, v := range s.sched.loaded {
- model := v.model
- modelDetails := api.ModelDetails{
- Format: model.Config.ModelFormat,
- Family: model.Config.ModelFamily,
- Families: model.Config.ModelFamilies,
- ParameterSize: model.Config.ModelType,
- QuantizationLevel: model.Config.FileType,
- }
- mr := api.ProcessModelResponse{
- Model: model.ShortName,
- Name: model.ShortName,
- Size: int64(v.estimatedTotal),
- SizeVRAM: int64(v.estimatedVRAM),
- Digest: model.Digest,
- Details: modelDetails,
- ExpiresAt: v.expiresAt,
- }
- // The scheduler waits to set expiresAt, so if a model is loading it's
- // possible that it will be set to the unix epoch. For those cases, just
- // calculate the time w/ the sessionDuration instead.
- var epoch time.Time
- if v.expiresAt == epoch {
- mr.ExpiresAt = time.Now().Add(v.sessionDuration)
- }
- models = append(models, mr)
- }
- slices.SortStableFunc(models, func(i, j api.ProcessModelResponse) int {
- // longest duration remaining listed first
- return cmp.Compare(j.ExpiresAt.Unix(), i.ExpiresAt.Unix())
- })
- c.JSON(http.StatusOK, api.ProcessResponse{Models: models})
- }
- func (s *Server) ChatHandler(c *gin.Context) {
- var req api.ChatRequest
- if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"})
- return
- } else if err != nil {
- c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- return
- }
- caps := []Capability{CapabilityCompletion}
- r, m, opts, err := s.scheduleRunner(c.Request.Context(), req.Model, caps, req.Options, req.KeepAlive)
- if errors.Is(err, errCapabilityCompletion) {
- c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support chat", req.Model)})
- return
- } else if err != nil {
- handleScheduleError(c, req.Model, err)
- return
- }
- if len(req.Messages) == 0 {
- c.JSON(http.StatusOK, api.ChatResponse{
- Model: req.Model,
- CreatedAt: time.Now().UTC(),
- Message: api.Message{Role: "assistant"},
- Done: true,
- DoneReason: "load",
- })
- return
- }
- prompt, images, err := chatPrompt(c.Request.Context(), m, r.Tokenize, opts, req.Messages)
- if err != nil {
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- return
- }
- slog.Debug("chat request", "images", len(images), "prompt", prompt)
- ch := make(chan any)
- go func() {
- defer close(ch)
- if err := r.Completion(c.Request.Context(), llm.CompletionRequest{
- Prompt: prompt,
- Images: images,
- Format: req.Format,
- Options: opts,
- }, func(r llm.CompletionResponse) {
- ch <- api.ChatResponse{
- Model: req.Model,
- CreatedAt: time.Now().UTC(),
- Message: api.Message{Role: "assistant", Content: r.Content},
- Done: r.Done,
- DoneReason: r.DoneReason,
- Metrics: api.Metrics{
- PromptEvalCount: r.PromptEvalCount,
- PromptEvalDuration: r.PromptEvalDuration,
- EvalCount: r.EvalCount,
- EvalDuration: r.EvalDuration,
- },
- }
- }); err != nil {
- ch <- gin.H{"error": err.Error()}
- }
- }()
- if req.Stream != nil && !*req.Stream {
- var r api.ChatResponse
- var sb strings.Builder
- for rr := range ch {
- switch t := rr.(type) {
- case api.ChatResponse:
- sb.WriteString(t.Message.Content)
- r = t
- case gin.H:
- msg, ok := t["error"].(string)
- if !ok {
- msg = "unexpected error format in response"
- }
- c.JSON(http.StatusInternalServerError, gin.H{"error": msg})
- return
- default:
- c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected response"})
- return
- }
- }
- r.Message.Content = sb.String()
- c.JSON(http.StatusOK, r)
- return
- }
- streamResponse(c, ch)
- }
- func handleScheduleError(c *gin.Context, name string, err error) {
- switch {
- case errors.Is(err, errRequired):
- c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
- case errors.Is(err, context.Canceled):
- c.JSON(499, gin.H{"error": "request canceled"})
- case errors.Is(err, ErrMaxQueue):
- c.JSON(http.StatusServiceUnavailable, gin.H{"error": err.Error()})
- case errors.Is(err, os.ErrNotExist):
- c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model %q not found, try pulling it first", name)})
- default:
- c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
- }
- }
|