Skip to content

Commit

Permalink
allow overriding template and system in /api/generate
Browse files Browse the repository at this point in the history
Fixes #297
Fixes #296
  • Loading branch information
jmorganca committed Aug 8, 2023
1 parent 5eb712f commit 8713ac2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
13 changes: 11 additions & 2 deletions server/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"net/http"
Expand All @@ -15,7 +16,6 @@ import (
"reflect"
"strconv"
"strings"
"text/template"

"github.com/jmorganca/ollama/api"
"github.com/jmorganca/ollama/parser"
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 8713ac2

Please sign in to comment.