routes.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "github.com/gin-gonic/gin"
  16. "github.com/lithammer/fuzzysearch/fuzzy"
  17. "github.com/jmorganca/ollama/api"
  18. "github.com/jmorganca/ollama/llama"
  19. )
  20. //go:embed templates/*
  21. var templatesFS embed.FS
  22. var templates = template.Must(template.ParseFS(templatesFS, "templates/*.prompt"))
  23. func cacheDir() string {
  24. home, err := os.UserHomeDir()
  25. if err != nil {
  26. panic(err)
  27. }
  28. return path.Join(home, ".ollama")
  29. }
  30. func generate(c *gin.Context) {
  31. req := api.GenerateRequest{
  32. Options: api.DefaultOptions(),
  33. }
  34. if err := c.ShouldBindJSON(&req); err != nil {
  35. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  36. return
  37. }
  38. if remoteModel, _ := getRemote(req.Model); remoteModel != nil {
  39. req.Model = remoteModel.FullName()
  40. }
  41. if _, err := os.Stat(req.Model); err != nil {
  42. if !errors.Is(err, os.ErrNotExist) {
  43. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  44. return
  45. }
  46. req.Model = path.Join(cacheDir(), "models", req.Model+".bin")
  47. }
  48. ch := make(chan any)
  49. go stream(c, ch)
  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. fn := func(s string) {
  70. ch <- api.GenerateResponse{Response: s}
  71. }
  72. if err := llm.Predict(req.Prompt, fn); err != nil {
  73. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  74. return
  75. }
  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. remote, err := getRemote(req.Model)
  84. if err != nil {
  85. c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
  86. return
  87. }
  88. ch := make(chan any)
  89. go stream(c, ch)
  90. fn := func(total, completed int64) {
  91. ch <- api.PullProgress{
  92. Total: total,
  93. Completed: completed,
  94. Percent: float64(total) / float64(completed) * 100,
  95. }
  96. }
  97. if err := saveModel(remote, fn); err != nil {
  98. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  99. return
  100. }
  101. }
  102. func Serve(ln net.Listener) error {
  103. r := gin.Default()
  104. r.GET("/", func(c *gin.Context) {
  105. c.String(http.StatusOK, "Ollama is running")
  106. })
  107. r.POST("api/pull", pull)
  108. r.POST("/api/generate", generate)
  109. log.Printf("Listening on %s", ln.Addr())
  110. s := &http.Server{
  111. Handler: r,
  112. }
  113. return s.Serve(ln)
  114. }
  115. func matchRankOne(source string, targets []string) (bestMatch string, bestRank int) {
  116. bestRank = math.MaxInt
  117. for _, target := range targets {
  118. if rank := fuzzy.LevenshteinDistance(source, target); bestRank > rank {
  119. bestRank = rank
  120. bestMatch = target
  121. }
  122. }
  123. return
  124. }
  125. func stream(c *gin.Context, ch chan any) {
  126. c.Stream(func(w io.Writer) bool {
  127. val, ok := <-ch
  128. if !ok {
  129. return false
  130. }
  131. bts, err := json.Marshal(val)
  132. if err != nil {
  133. return false
  134. }
  135. bts = append(bts, '\n')
  136. if _, err := w.Write(bts); err != nil {
  137. return false
  138. }
  139. return true
  140. })
  141. }