allow overriding template and system in /api/generate

Fixes #297
Fixes #296
This commit is contained in:
Jeffrey Morgan 2023-08-08 00:55:34 -04:00
parent 5eb712f962
commit 8713ac23a8
2 changed files with 16 additions and 5 deletions

View file

@ -35,6 +35,8 @@ func (e StatusError) Error() string {
type GenerateRequest struct {
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"`

View file

@ -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