ollama/server/routes.go

164 lines
3.3 KiB
Go
Raw Normal View History

package server
import (
2023-07-06 18:33:29 +00:00
"embed"
2023-07-06 17:40:11 +00:00
"encoding/json"
"fmt"
"io"
"log"
2023-07-07 00:03:00 +00:00
"math"
"net"
"net/http"
2023-07-06 17:40:11 +00:00
"path"
"runtime"
2023-07-06 17:40:11 +00:00
"strings"
"text/template"
"github.com/gin-gonic/gin"
2023-07-06 17:40:11 +00:00
"github.com/lithammer/fuzzysearch/fuzzy"
2023-07-03 20:32:48 +00:00
"github.com/jmorganca/ollama/api"
2023-07-06 17:40:11 +00:00
"github.com/jmorganca/ollama/llama"
)
2023-07-06 18:33:29 +00:00
//go:embed templates/*
var templatesFS embed.FS
var templates = template.Must(template.ParseFS(templatesFS, "templates/*.prompt"))
2023-07-06 17:40:11 +00:00
2023-07-05 19:37:33 +00:00
func generate(c *gin.Context) {
2023-07-04 04:47:00 +00:00
// TODO: these should be request parameters
gpulayers := 1
tokens := 512
threads := runtime.NumCPU()
2023-07-05 19:37:33 +00:00
var req api.GenerateRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
2023-07-06 22:43:04 +00:00
if remoteModel, _ := getRemote(req.Model); remoteModel != nil {
req.Model = remoteModel.FullName()
}
2023-07-06 18:14:32 +00:00
model, err := llama.New(req.Model, llama.EnableF16Memory, llama.SetContext(128), llama.EnableEmbeddings, llama.SetGPULayers(gpulayers))
2023-07-05 19:37:33 +00:00
if err != nil {
fmt.Println("Loading the model failed:", err.Error())
return
}
2023-07-06 18:14:32 +00:00
defer model.Free()
2023-07-06 17:40:11 +00:00
templateNames := make([]string, 0, len(templates.Templates()))
for _, template := range templates.Templates() {
templateNames = append(templateNames, template.Name())
}
2023-07-07 00:03:00 +00:00
match, _ := matchRankOne(path.Base(req.Model), templateNames)
2023-07-06 17:40:11 +00:00
if template := templates.Lookup(match); template != nil {
var sb strings.Builder
if err := template.Execute(&sb, req); err != nil {
fmt.Println("Prompt template failed:", err.Error())
return
}
req.Prompt = sb.String()
}
2023-07-05 19:37:33 +00:00
ch := make(chan string)
2023-07-04 04:47:00 +00:00
2023-07-05 19:37:33 +00:00
go func() {
defer close(ch)
2023-07-06 18:14:32 +00:00
_, err := model.Predict(req.Prompt, llama.Debug, llama.SetTokenCallback(func(token string) bool {
2023-07-05 19:37:33 +00:00
ch <- token
return true
}), llama.SetTokens(tokens), llama.SetThreads(threads), llama.SetTopK(90), llama.SetTopP(0.86), llama.SetStopWords("llama"))
2023-07-04 04:47:00 +00:00
if err != nil {
2023-07-05 19:37:33 +00:00
panic(err)
2023-07-04 04:47:00 +00:00
}
2023-07-05 19:37:33 +00:00
}()
2023-07-04 04:47:00 +00:00
2023-07-05 19:37:33 +00:00
c.Stream(func(w io.Writer) bool {
2023-07-06 17:40:11 +00:00
token, ok := <-ch
2023-07-05 19:37:33 +00:00
if !ok {
return false
}
2023-07-06 17:40:11 +00:00
2023-07-06 19:27:50 +00:00
resp := api.GenerateResponse{
Response: token,
2023-07-06 17:40:11 +00:00
}
bts, err := json.Marshal(resp)
if err != nil {
return false
}
bts = append(bts, '\n')
if _, err := w.Write(bts); err != nil {
return false
}
2023-07-05 19:37:33 +00:00
return true
})
2023-07-05 19:37:33 +00:00
}
func Serve(ln net.Listener) error {
r := gin.Default()
2023-07-06 16:24:49 +00:00
r.POST("api/pull", func(c *gin.Context) {
var req api.PullRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
2023-07-06 18:18:40 +00:00
progressCh := make(chan api.PullProgress)
2023-07-06 16:24:49 +00:00
go func() {
defer close(progressCh)
if err := pull(req.Model, progressCh); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
}()
c.Stream(func(w io.Writer) bool {
progress, ok := <-progressCh
if !ok {
return false
}
2023-07-06 19:30:36 +00:00
bts, err := json.Marshal(progress)
if err != nil {
return false
}
bts = append(bts, '\n')
if _, err := w.Write(bts); err != nil {
return false
}
2023-07-06 16:24:49 +00:00
return true
})
})
2023-07-05 19:37:33 +00:00
r.POST("/api/generate", generate)
log.Printf("Listening on %s", ln.Addr())
s := &http.Server{
Handler: r,
}
return s.Serve(ln)
}
2023-07-06 17:40:11 +00:00
func matchRankOne(source string, targets []string) (bestMatch string, bestRank int) {
2023-07-07 00:03:00 +00:00
bestRank = math.MaxInt
2023-07-06 17:40:11 +00:00
for _, target := range targets {
2023-07-07 00:03:00 +00:00
if rank := fuzzy.LevenshteinDistance(source, target); bestRank > rank {
2023-07-06 17:40:11 +00:00
bestRank = rank
bestMatch = target
}
}
return
}