From 8713ac23a86e1eaf8b94f93bb0d51d13b0225d87 Mon Sep 17 00:00:00 2001 From: Jeffrey Morgan Date: Tue, 8 Aug 2023 00:55:34 -0400 Subject: [PATCH] allow overriding `template` and `system` in `/api/generate` Fixes #297 Fixes #296 --- api/types.go | 8 +++++--- server/images.go | 13 +++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/types.go b/api/types.go index dee53d50..90b964a3 100644 --- a/api/types.go +++ b/api/types.go @@ -33,9 +33,11 @@ func (e StatusError) Error() string { } type GenerateRequest struct { - Model string `json:"model"` - Prompt string `json:"prompt"` - Context []int `json:"context,omitempty"` + Model string `json:"model"` + Prompt string `json:"prompt"` + System string `json:"system"` + Template string `json:"template"` + Context []int `json:"context,omitempty"` Options map[string]interface{} `json:"options"` } diff --git a/server/images.go b/server/images.go index f566c7dd..e06a40a1 100644 --- a/server/images.go +++ b/server/images.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "html/template" "io" "log" "net/http" @@ -15,7 +16,6 @@ import ( "reflect" "strconv" "strings" - "text/template" "github.com/jmorganca/ollama/api" "github.com/jmorganca/ollama/parser" @@ -37,7 +37,12 @@ type Model struct { } func (m *Model) Prompt(request api.GenerateRequest) (string, error) { - tmpl, err := template.New("").Parse(m.Template) + t := m.Template + if request.Template != "" { + t = request.Template + } + + tmpl, err := template.New("").Parse(t) if err != nil { return "", err } @@ -56,6 +61,10 @@ func (m *Model) Prompt(request api.GenerateRequest) (string, error) { vars.Prompt = request.Prompt vars.Context = request.Context + if request.System != "" { + vars.System = request.System + } + var sb strings.Builder if err := tmpl.Execute(&sb, vars); err != nil { return "", err