routes.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package server
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "net"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "text/template"
  12. "time"
  13. "dario.cat/mergo"
  14. "github.com/gin-gonic/gin"
  15. "github.com/jmorganca/ollama/api"
  16. "github.com/jmorganca/ollama/llama"
  17. )
  18. func cacheDir() string {
  19. home, err := os.UserHomeDir()
  20. if err != nil {
  21. panic(err)
  22. }
  23. return filepath.Join(home, ".ollama")
  24. }
  25. func generate(c *gin.Context) {
  26. start := time.Now()
  27. var req api.GenerateRequest
  28. if err := c.ShouldBindJSON(&req); err != nil {
  29. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  30. return
  31. }
  32. model, err := GetModel(req.Model)
  33. if err != nil {
  34. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  35. return
  36. }
  37. opts := api.DefaultOptions()
  38. if err := mergo.Merge(&opts, model.Options, mergo.WithOverride); err != nil {
  39. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  40. return
  41. }
  42. if err := mergo.Merge(&opts, req.Options, mergo.WithOverride); err != nil {
  43. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  44. return
  45. }
  46. templ, err := template.New("").Parse(model.Prompt)
  47. if err != nil {
  48. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  49. return
  50. }
  51. var sb strings.Builder
  52. if err = templ.Execute(&sb, req); err != nil {
  53. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  54. return
  55. }
  56. req.Prompt = sb.String()
  57. llm, err := llama.New(model.ModelPath, opts)
  58. if err != nil {
  59. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  60. return
  61. }
  62. defer llm.Close()
  63. ch := make(chan any)
  64. go func() {
  65. defer close(ch)
  66. llm.Predict(req.Context, req.Prompt, func(r api.GenerateResponse) {
  67. r.Model = req.Model
  68. r.CreatedAt = time.Now().UTC()
  69. if r.Done {
  70. r.TotalDuration = time.Since(start)
  71. }
  72. ch <- r
  73. })
  74. }()
  75. streamResponse(c, ch)
  76. }
  77. func pull(c *gin.Context) {
  78. var req api.PullRequest
  79. if err := c.ShouldBindJSON(&req); err != nil {
  80. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  81. return
  82. }
  83. ch := make(chan any)
  84. go func() {
  85. defer close(ch)
  86. fn := func(status, digest string, total, completed int, percent float64) {
  87. ch <- api.PullProgress{
  88. Status: status,
  89. Digest: digest,
  90. Total: total,
  91. Completed: completed,
  92. Percent: percent,
  93. }
  94. }
  95. if err := PullModel(req.Name, req.Username, req.Password, fn); err != nil {
  96. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  97. return
  98. }
  99. }()
  100. streamResponse(c, ch)
  101. }
  102. func push(c *gin.Context) {
  103. var req api.PushRequest
  104. if err := c.ShouldBindJSON(&req); err != nil {
  105. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  106. return
  107. }
  108. ch := make(chan any)
  109. go func() {
  110. defer close(ch)
  111. fn := func(status, digest string, total, completed int, percent float64) {
  112. ch <- api.PushProgress{
  113. Status: status,
  114. Digest: digest,
  115. Total: total,
  116. Completed: completed,
  117. Percent: percent,
  118. }
  119. }
  120. if err := PushModel(req.Name, req.Username, req.Password, fn); err != nil {
  121. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  122. return
  123. }
  124. }()
  125. streamResponse(c, ch)
  126. }
  127. func create(c *gin.Context) {
  128. var req api.CreateRequest
  129. if err := c.ShouldBindJSON(&req); err != nil {
  130. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  131. return
  132. }
  133. // NOTE consider passing the entire Modelfile in the json instead of the path to it
  134. file, err := os.Open(req.Path)
  135. if err != nil {
  136. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  137. return
  138. }
  139. defer file.Close()
  140. ch := make(chan any)
  141. go func() {
  142. defer close(ch)
  143. fn := func(status string) {
  144. ch <- api.CreateProgress{
  145. Status: status,
  146. }
  147. }
  148. if err := CreateModel(req.Name, file, fn); err != nil {
  149. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  150. return
  151. }
  152. }()
  153. streamResponse(c, ch)
  154. }
  155. func Serve(ln net.Listener) error {
  156. r := gin.Default()
  157. r.GET("/", func(c *gin.Context) {
  158. c.String(http.StatusOK, "Ollama is running")
  159. })
  160. r.POST("/api/pull", pull)
  161. r.POST("/api/generate", generate)
  162. r.POST("/api/create", create)
  163. r.POST("/api/push", push)
  164. log.Printf("Listening on %s", ln.Addr())
  165. s := &http.Server{
  166. Handler: r,
  167. }
  168. return s.Serve(ln)
  169. }
  170. func streamResponse(c *gin.Context, ch chan any) {
  171. c.Stream(func(w io.Writer) bool {
  172. val, ok := <-ch
  173. if !ok {
  174. return false
  175. }
  176. bts, err := json.Marshal(val)
  177. if err != nil {
  178. return false
  179. }
  180. bts = append(bts, '\n')
  181. if _, err := w.Write(bts); err != nil {
  182. return false
  183. }
  184. return true
  185. })
  186. }