routes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package server
  2. import (
  3. "embed"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "log"
  8. "math"
  9. "net"
  10. "net/http"
  11. "os"
  12. "path"
  13. "strings"
  14. "text/template"
  15. "time"
  16. "github.com/gin-gonic/gin"
  17. "github.com/lithammer/fuzzysearch/fuzzy"
  18. "github.com/jmorganca/ollama/api"
  19. "github.com/jmorganca/ollama/llama"
  20. )
  21. //go:embed templates/*
  22. var templatesFS embed.FS
  23. var templates = template.Must(template.ParseFS(templatesFS, "templates/*.prompt"))
  24. func cacheDir() string {
  25. home, err := os.UserHomeDir()
  26. if err != nil {
  27. panic(err)
  28. }
  29. return path.Join(home, ".ollama")
  30. }
  31. func generate(c *gin.Context) {
  32. start := time.Now()
  33. req := api.GenerateRequest{
  34. Options: api.DefaultOptions(),
  35. }
  36. if err := c.ShouldBindJSON(&req); err != nil {
  37. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  38. return
  39. }
  40. if remoteModel, _ := getRemote(req.Model); remoteModel != nil {
  41. req.Model = remoteModel.FullName()
  42. }
  43. if _, err := os.Stat(req.Model); err != nil {
  44. if !errors.Is(err, os.ErrNotExist) {
  45. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  46. return
  47. }
  48. req.Model = path.Join(cacheDir(), "models", req.Model+".bin")
  49. }
  50. templateNames := make([]string, 0, len(templates.Templates()))
  51. for _, template := range templates.Templates() {
  52. templateNames = append(templateNames, template.Name())
  53. }
  54. match, _ := matchRankOne(path.Base(req.Model), templateNames)
  55. if template := templates.Lookup(match); template != nil {
  56. var sb strings.Builder
  57. if err := template.Execute(&sb, req); err != nil {
  58. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  59. return
  60. }
  61. req.Prompt = sb.String()
  62. }
  63. llm, err := llama.New(req.Model, req.Options)
  64. if err != nil {
  65. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  66. return
  67. }
  68. defer llm.Close()
  69. ch := make(chan any)
  70. go func() {
  71. defer close(ch)
  72. llm.Predict(req.Context, req.Prompt, func(r api.GenerateResponse) {
  73. r.Model = req.Model
  74. r.CreatedAt = time.Now().UTC()
  75. if r.Done {
  76. r.TotalDuration = time.Since(start)
  77. }
  78. ch <- r
  79. })
  80. }()
  81. streamResponse(c, ch)
  82. }
  83. func pull(c *gin.Context) {
  84. var req api.PullRequest
  85. if err := c.ShouldBindJSON(&req); err != nil {
  86. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  87. return
  88. }
  89. remote, err := getRemote(req.Model)
  90. if err != nil {
  91. c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
  92. return
  93. }
  94. // check if completed file exists
  95. fi, err := os.Stat(remote.FullName())
  96. switch {
  97. case errors.Is(err, os.ErrNotExist):
  98. // noop, file doesn't exist so create it
  99. case err != nil:
  100. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  101. return
  102. default:
  103. c.JSON(http.StatusOK, api.PullProgress{
  104. Total: fi.Size(),
  105. Completed: fi.Size(),
  106. Percent: 100,
  107. })
  108. return
  109. }
  110. ch := make(chan any)
  111. go func() {
  112. defer close(ch)
  113. saveModel(remote, func(total, completed int64) {
  114. ch <- api.PullProgress{
  115. Total: total,
  116. Completed: completed,
  117. Percent: float64(completed) / float64(total) * 100,
  118. }
  119. })
  120. }()
  121. streamResponse(c, ch)
  122. }
  123. func Serve(ln net.Listener) error {
  124. r := gin.Default()
  125. r.GET("/", func(c *gin.Context) {
  126. c.String(http.StatusOK, "Ollama is running")
  127. })
  128. r.POST("/api/pull", pull)
  129. r.POST("/api/generate", generate)
  130. log.Printf("Listening on %s", ln.Addr())
  131. s := &http.Server{
  132. Handler: r,
  133. }
  134. return s.Serve(ln)
  135. }
  136. func matchRankOne(source string, targets []string) (bestMatch string, bestRank int) {
  137. bestRank = math.MaxInt
  138. for _, target := range targets {
  139. if rank := fuzzy.LevenshteinDistance(source, target); bestRank > rank {
  140. bestRank = rank
  141. bestMatch = target
  142. }
  143. }
  144. return
  145. }
  146. func streamResponse(c *gin.Context, ch chan any) {
  147. c.Stream(func(w io.Writer) bool {
  148. val, ok := <-ch
  149. if !ok {
  150. return false
  151. }
  152. bts, err := json.Marshal(val)
  153. if err != nil {
  154. return false
  155. }
  156. bts = append(bts, '\n')
  157. if _, err := w.Write(bts); err != nil {
  158. return false
  159. }
  160. return true
  161. })
  162. }