routes.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. log.Printf("prompt: \n%s", req.Prompt)
  58. llm, err := llama.New(model.ModelPath, opts)
  59. if err != nil {
  60. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  61. return
  62. }
  63. defer llm.Close()
  64. ch := make(chan any)
  65. go func() {
  66. defer close(ch)
  67. llm.Predict(req.Context, req.Prompt, func(r api.GenerateResponse) {
  68. r.Model = req.Model
  69. r.CreatedAt = time.Now().UTC()
  70. if r.Done {
  71. r.TotalDuration = time.Since(start)
  72. }
  73. ch <- r
  74. })
  75. }()
  76. streamResponse(c, ch)
  77. }
  78. func pull(c *gin.Context) {
  79. var req api.PullRequest
  80. if err := c.ShouldBindJSON(&req); err != nil {
  81. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  82. return
  83. }
  84. ch := make(chan any)
  85. go func() {
  86. defer close(ch)
  87. fn := func(status, digest string, total, completed int, percent float64) {
  88. ch <- api.PullProgress{
  89. Status: status,
  90. Digest: digest,
  91. Total: total,
  92. Completed: completed,
  93. Percent: percent,
  94. }
  95. }
  96. if err := PullModel(req.Name, req.Username, req.Password, fn); err != nil {
  97. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  98. return
  99. }
  100. }()
  101. streamResponse(c, ch)
  102. }
  103. func push(c *gin.Context) {
  104. var req api.PushRequest
  105. if err := c.ShouldBindJSON(&req); err != nil {
  106. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  107. return
  108. }
  109. ch := make(chan any)
  110. go func() {
  111. defer close(ch)
  112. fn := func(status, digest string, total, completed int, percent float64) {
  113. ch <- api.PushProgress{
  114. Status: status,
  115. Digest: digest,
  116. Total: total,
  117. Completed: completed,
  118. Percent: percent,
  119. }
  120. }
  121. if err := PushModel(req.Name, req.Username, req.Password, fn); err != nil {
  122. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  123. return
  124. }
  125. }()
  126. streamResponse(c, ch)
  127. }
  128. func create(c *gin.Context) {
  129. var req api.CreateRequest
  130. if err := c.ShouldBindJSON(&req); err != nil {
  131. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  132. return
  133. }
  134. // NOTE consider passing the entire Modelfile in the json instead of the path to it
  135. file, err := os.Open(req.Path)
  136. if err != nil {
  137. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  138. return
  139. }
  140. defer file.Close()
  141. ch := make(chan any)
  142. go func() {
  143. defer close(ch)
  144. fn := func(status string) {
  145. ch <- api.CreateProgress{
  146. Status: status,
  147. }
  148. }
  149. if err := CreateModel(req.Name, file, fn); err != nil {
  150. c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
  151. return
  152. }
  153. }()
  154. streamResponse(c, ch)
  155. }
  156. func Serve(ln net.Listener) error {
  157. r := gin.Default()
  158. r.GET("/", func(c *gin.Context) {
  159. c.String(http.StatusOK, "Ollama is running")
  160. })
  161. r.POST("/api/pull", pull)
  162. r.POST("/api/generate", generate)
  163. r.POST("/api/create", create)
  164. r.POST("/api/push", push)
  165. log.Printf("Listening on %s", ln.Addr())
  166. s := &http.Server{
  167. Handler: r,
  168. }
  169. return s.Serve(ln)
  170. }
  171. func streamResponse(c *gin.Context, ch chan any) {
  172. c.Stream(func(w io.Writer) bool {
  173. val, ok := <-ch
  174. if !ok {
  175. return false
  176. }
  177. bts, err := json.Marshal(val)
  178. if err != nil {
  179. return false
  180. }
  181. bts = append(bts, '\n')
  182. if _, err := w.Write(bts); err != nil {
  183. return false
  184. }
  185. return true
  186. })
  187. }