routes.go 4.2 KB

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