routes.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ch := make(chan any)
  51. go stream(c, ch)
  52. templateNames := make([]string, 0, len(templates.Templates()))
  53. for _, template := range templates.Templates() {
  54. templateNames = append(templateNames, template.Name())
  55. }
  56. match, _ := matchRankOne(path.Base(req.Model), templateNames)
  57. if template := templates.Lookup(match); template != nil {
  58. var sb strings.Builder
  59. if err := template.Execute(&sb, req); err != nil {
  60. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  61. return
  62. }
  63. req.Prompt = sb.String()
  64. }
  65. llm, err := llama.New(req.Model, req.Options)
  66. if err != nil {
  67. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  68. return
  69. }
  70. defer llm.Close()
  71. fn := func(r api.GenerateResponse) {
  72. r.Model = req.Model
  73. r.CreatedAt = time.Now().UTC()
  74. if r.Done {
  75. r.TotalDuration = time.Since(start)
  76. }
  77. ch <- r
  78. }
  79. if err := llm.Predict(req.Context, req.Prompt, fn); err != nil {
  80. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  81. return
  82. }
  83. }
  84. func pull(c *gin.Context) {
  85. var req api.PullRequest
  86. if err := c.ShouldBindJSON(&req); err != nil {
  87. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  88. return
  89. }
  90. remote, err := getRemote(req.Model)
  91. if err != nil {
  92. c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
  93. return
  94. }
  95. // check if completed file exists
  96. fi, err := os.Stat(remote.FullName())
  97. switch {
  98. case errors.Is(err, os.ErrNotExist):
  99. // noop, file doesn't exist so create it
  100. case err != nil:
  101. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  102. return
  103. default:
  104. c.JSON(http.StatusOK, api.PullProgress{
  105. Total: fi.Size(),
  106. Completed: fi.Size(),
  107. Percent: 100,
  108. })
  109. return
  110. }
  111. ch := make(chan any)
  112. go stream(c, ch)
  113. fn := func(total, completed int64) {
  114. ch <- api.PullProgress{
  115. Total: total,
  116. Completed: completed,
  117. Percent: float64(completed) / float64(total) * 100,
  118. }
  119. }
  120. if err := saveModel(remote, fn); err != nil {
  121. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  122. return
  123. }
  124. }
  125. func Serve(ln net.Listener) error {
  126. r := gin.Default()
  127. r.GET("/", func(c *gin.Context) {
  128. c.String(http.StatusOK, "Ollama is running")
  129. })
  130. r.POST("/api/pull", pull)
  131. r.POST("/api/generate", generate)
  132. log.Printf("Listening on %s", ln.Addr())
  133. s := &http.Server{
  134. Handler: r,
  135. }
  136. return s.Serve(ln)
  137. }
  138. func matchRankOne(source string, targets []string) (bestMatch string, bestRank int) {
  139. bestRank = math.MaxInt
  140. for _, target := range targets {
  141. if rank := fuzzy.LevenshteinDistance(source, target); bestRank > rank {
  142. bestRank = rank
  143. bestMatch = target
  144. }
  145. }
  146. return
  147. }
  148. func stream(c *gin.Context, ch chan any) {
  149. c.Stream(func(w io.Writer) bool {
  150. val, ok := <-ch
  151. if !ok {
  152. return false
  153. }
  154. bts, err := json.Marshal(val)
  155. if err != nil {
  156. return false
  157. }
  158. bts = append(bts, '\n')
  159. if _, err := w.Write(bts); err != nil {
  160. return false
  161. }
  162. return true
  163. })
  164. }