Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
dddb58a3
Unverified
Commit
dddb58a3
authored
Jul 02, 2024
by
Michael Yang
Committed by
GitHub
Jul 02, 2024
Browse files
Merge pull request #5051 from ollama/mxyng/capabilities
add model capabilities
parents
400056e1
da8e2a04
Changes
29
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
247 additions
and
0 deletions
+247
-0
template/openchat.gotmpl
template/openchat.gotmpl
+0
-0
template/phi-3.gotmpl
template/phi-3.gotmpl
+0
-0
template/solar-instruct.gotmpl
template/solar-instruct.gotmpl
+0
-0
template/starcoder2-instruct.gotmpl
template/starcoder2-instruct.gotmpl
+0
-0
template/template.go
template/template.go
+158
-0
template/template_test.go
template/template_test.go
+89
-0
template/testdata/templates.jsonl
template/testdata/templates.jsonl
+0
-0
template/vicuna.gotmpl
template/vicuna.gotmpl
+0
-0
template/zephyr.gotmpl
template/zephyr.gotmpl
+0
-0
No files found.
template
s
/openchat.gotmpl
→
template/openchat.gotmpl
View file @
dddb58a3
File moved
template
s
/phi-3.gotmpl
→
template/phi-3.gotmpl
View file @
dddb58a3
File moved
template
s
/solar-instruct.gotmpl
→
template/solar-instruct.gotmpl
View file @
dddb58a3
File moved
template
s
/starcoder2-instruct.gotmpl
→
template/starcoder2-instruct.gotmpl
View file @
dddb58a3
File moved
template
s
/template.go
→
template/template.go
View file @
dddb58a3
package
template
s
package
template
import
(
import
(
"bytes"
"bytes"
...
@@ -7,9 +7,14 @@ import (
...
@@ -7,9 +7,14 @@ import (
"errors"
"errors"
"io"
"io"
"math"
"math"
"slices"
"strings"
"sync"
"sync"
"text/template"
"text/template/parse"
"github.com/agnivade/levenshtein"
"github.com/agnivade/levenshtein"
"golang.org/x/exp/maps"
)
)
//go:embed index.json
//go:embed index.json
...
@@ -18,8 +23,8 @@ var indexBytes []byte
...
@@ -18,8 +23,8 @@ var indexBytes []byte
//go:embed *.gotmpl
//go:embed *.gotmpl
var
templatesFS
embed
.
FS
var
templatesFS
embed
.
FS
var
templatesOnce
=
sync
.
OnceValues
(
func
()
([]
*
Template
,
error
)
{
var
templatesOnce
=
sync
.
OnceValues
(
func
()
([]
*
named
,
error
)
{
var
templates
[]
*
Template
var
templates
[]
*
named
if
err
:=
json
.
Unmarshal
(
indexBytes
,
&
templates
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
(
indexBytes
,
&
templates
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -37,23 +42,23 @@ var templatesOnce = sync.OnceValues(func() ([]*Template, error) {
...
@@ -37,23 +42,23 @@ var templatesOnce = sync.OnceValues(func() ([]*Template, error) {
return
templates
,
nil
return
templates
,
nil
})
})
type
Template
struct
{
type
named
struct
{
Name
string
`json:"name"`
Name
string
`json:"name"`
Template
string
`json:"template"`
Template
string
`json:"template"`
Bytes
[]
byte
Bytes
[]
byte
}
}
func
(
t
Template
)
Reader
()
io
.
Reader
{
func
(
t
named
)
Reader
()
io
.
Reader
{
return
bytes
.
NewReader
(
t
.
Bytes
)
return
bytes
.
NewReader
(
t
.
Bytes
)
}
}
func
Named
Template
(
s
string
)
(
*
Template
,
error
)
{
func
Named
(
s
string
)
(
*
named
,
error
)
{
templates
,
err
:=
templatesOnce
()
templates
,
err
:=
templatesOnce
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
var
template
*
Template
var
template
*
named
score
:=
math
.
MaxInt
score
:=
math
.
MaxInt
for
_
,
t
:=
range
templates
{
for
_
,
t
:=
range
templates
{
if
s
:=
levenshtein
.
ComputeDistance
(
s
,
t
.
Template
);
s
<
score
{
if
s
:=
levenshtein
.
ComputeDistance
(
s
,
t
.
Template
);
s
<
score
{
...
@@ -68,3 +73,86 @@ func NamedTemplate(s string) (*Template, error) {
...
@@ -68,3 +73,86 @@ func NamedTemplate(s string) (*Template, error) {
return
nil
,
errors
.
New
(
"no matching template found"
)
return
nil
,
errors
.
New
(
"no matching template found"
)
}
}
type
Template
struct
{
*
template
.
Template
raw
string
}
func
(
t
*
Template
)
String
()
string
{
return
t
.
raw
}
var
DefaultTemplate
,
_
=
Parse
(
"{{ .Prompt }}"
)
func
Parse
(
s
string
)
(
*
Template
,
error
)
{
t
,
err
:=
template
.
New
(
""
)
.
Option
(
"missingkey=zero"
)
.
Parse
(
s
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Template
{
Template
:
t
,
raw
:
s
},
nil
}
func
(
t
*
Template
)
Vars
()
[]
string
{
var
vars
[]
string
for
_
,
n
:=
range
t
.
Tree
.
Root
.
Nodes
{
vars
=
append
(
vars
,
parseNode
(
n
)
...
)
}
set
:=
make
(
map
[
string
]
struct
{})
for
_
,
n
:=
range
vars
{
set
[
strings
.
ToLower
(
n
)]
=
struct
{}{}
}
vars
=
maps
.
Keys
(
set
)
slices
.
Sort
(
vars
)
return
vars
}
func
parseNode
(
n
parse
.
Node
)
[]
string
{
switch
n
:=
n
.
(
type
)
{
case
*
parse
.
ActionNode
:
return
parseNode
(
n
.
Pipe
)
case
*
parse
.
IfNode
:
names
:=
parseNode
(
n
.
Pipe
)
names
=
append
(
names
,
parseNode
(
n
.
List
)
...
)
if
n
.
ElseList
!=
nil
{
names
=
append
(
names
,
parseNode
(
n
.
ElseList
)
...
)
}
return
names
case
*
parse
.
RangeNode
:
names
:=
parseNode
(
n
.
Pipe
)
names
=
append
(
names
,
parseNode
(
n
.
List
)
...
)
if
n
.
ElseList
!=
nil
{
names
=
append
(
names
,
parseNode
(
n
.
ElseList
)
...
)
}
return
names
case
*
parse
.
WithNode
:
names
:=
parseNode
(
n
.
Pipe
)
names
=
append
(
names
,
parseNode
(
n
.
List
)
...
)
if
n
.
ElseList
!=
nil
{
names
=
append
(
names
,
parseNode
(
n
.
ElseList
)
...
)
}
return
names
case
*
parse
.
PipeNode
:
var
names
[]
string
for
_
,
c
:=
range
n
.
Cmds
{
for
_
,
a
:=
range
c
.
Args
{
names
=
append
(
names
,
parseNode
(
a
)
...
)
}
}
return
names
case
*
parse
.
ListNode
:
var
names
[]
string
for
_
,
n
:=
range
n
.
Nodes
{
names
=
append
(
names
,
parseNode
(
n
)
...
)
}
return
names
case
*
parse
.
FieldNode
:
return
n
.
Ident
}
return
nil
}
template
s
/template_test.go
→
template/template_test.go
View file @
dddb58a3
package
template
s
package
template
import
(
import
(
"bufio"
"bufio"
...
@@ -7,13 +7,14 @@ import (
...
@@ -7,13 +7,14 @@ import (
"io"
"io"
"os"
"os"
"path/filepath"
"path/filepath"
"slices"
"testing"
"testing"
"text/template"
"text/template"
"github.com/ollama/ollama/llm"
"github.com/ollama/ollama/llm"
)
)
func
Test
KVChatTemplate
(
t
*
testing
.
T
)
{
func
Test
Named
(
t
*
testing
.
T
)
{
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
"testdata"
,
"templates.jsonl"
))
f
,
err
:=
os
.
Open
(
filepath
.
Join
(
"testdata"
,
"templates.jsonl"
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
...
@@ -31,7 +32,7 @@ func TestKVChatTemplate(t *testing.T) {
...
@@ -31,7 +32,7 @@ func TestKVChatTemplate(t *testing.T) {
t
.
Run
(
k
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
k
,
func
(
t
*
testing
.
T
)
{
kv
:=
llm
.
KV
{
"tokenizer.chat_template"
:
v
}
kv
:=
llm
.
KV
{
"tokenizer.chat_template"
:
v
}
s
:=
kv
.
ChatTemplate
()
s
:=
kv
.
ChatTemplate
()
r
,
err
:=
Named
Template
(
s
)
r
,
err
:=
Named
(
s
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
...
@@ -57,3 +58,32 @@ func TestKVChatTemplate(t *testing.T) {
...
@@ -57,3 +58,32 @@ func TestKVChatTemplate(t *testing.T) {
}
}
}
}
}
}
func
TestParse
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
template
string
vars
[]
string
}{
{
"{{ .Prompt }}"
,
[]
string
{
"prompt"
}},
{
"{{ .System }} {{ .Prompt }}"
,
[]
string
{
"prompt"
,
"system"
}},
{
"{{ .System }} {{ .Prompt }} {{ .Response }}"
,
[]
string
{
"prompt"
,
"response"
,
"system"
}},
{
"{{ with .Tools }}{{ . }}{{ end }} {{ .System }} {{ .Prompt }}"
,
[]
string
{
"prompt"
,
"system"
,
"tools"
}},
{
"{{ range .Messages }}{{ .Role }} {{ .Content }}{{ end }}"
,
[]
string
{
"content"
,
"messages"
,
"role"
}},
{
"{{ range .Messages }}{{ if eq .Role
\"
system
\"
}}SYSTEM: {{ .Content }}{{ else if eq .Role
\"
user
\"
}}USER: {{ .Content }}{{ else if eq .Role
\"
assistant
\"
}}ASSISTANT: {{ .Content }}{{ end }}{{ end }}"
,
[]
string
{
"content"
,
"messages"
,
"role"
}},
{
"{{ .Prompt }} {{ .Suffix }}"
,
[]
string
{
"prompt"
,
"suffix"
}},
}
for
_
,
tt
:=
range
cases
{
t
.
Run
(
""
,
func
(
t
*
testing
.
T
)
{
tmpl
,
err
:=
Parse
(
tt
.
template
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
vars
:=
tmpl
.
Vars
()
if
!
slices
.
Equal
(
tt
.
vars
,
vars
)
{
t
.
Errorf
(
"expected %v, got %v"
,
tt
.
vars
,
vars
)
}
})
}
}
template
s
/testdata/templates.jsonl
→
template/testdata/templates.jsonl
View file @
dddb58a3
File moved
template
s
/vicuna.gotmpl
→
template/vicuna.gotmpl
View file @
dddb58a3
File moved
template
s
/zephyr.gotmpl
→
template/zephyr.gotmpl
View file @
dddb58a3
File moved
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment