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
5056bb9c
Commit
5056bb9c
authored
Jul 11, 2024
by
Michael Yang
Browse files
rename aggregate to contents
parent
57ec6901
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
7 deletions
+41
-7
template/template.go
template/template.go
+6
-5
template/template_test.go
template/template_test.go
+35
-2
No files found.
template/template.go
View file @
5056bb9c
...
...
@@ -103,15 +103,16 @@ var response = parse.ActionNode{
}
var
funcs
=
template
.
FuncMap
{
"aggregate"
:
func
(
v
[]
*
api
.
Message
,
role
string
)
string
{
var
aggregated
[]
string
// contents returns the contents of messages with an optional role filter
"contents"
:
func
(
v
[]
*
api
.
Message
,
role
...
string
)
string
{
var
parts
[]
string
for
_
,
m
:=
range
v
{
if
m
.
Role
==
role
{
aggregated
=
append
(
aggregated
,
m
.
Content
)
if
len
(
role
)
==
0
||
role
[
0
]
==
""
||
m
.
Role
==
role
[
0
]
{
parts
=
append
(
parts
,
m
.
Content
)
}
}
return
strings
.
Join
(
aggregated
,
"
\n\n
"
)
return
strings
.
Join
(
parts
,
"
\n\n
"
)
},
}
...
...
template/template_test.go
View file @
5056bb9c
...
...
@@ -216,7 +216,7 @@ func TestExecuteWithMessages(t *testing.T) {
{
"response"
,
`[INST] {{ if .System }}{{ .System }}
{{ end }}{{ .Prompt }}[/INST] {{ .Response }}`
},
{
"messages"
,
`{{- $system :=
aggregate $
.Messages "system" -}}
{
"messages"
,
`{{- $system :=
contents
.Messages "system" -}}
{{- range $index, $_ := .Messages }}
{{- if eq .Role "user" }}[INST] {{ if $system }}{{ $system }}
{{- $system = "" }}
...
...
@@ -243,7 +243,7 @@ func TestExecuteWithMessages(t *testing.T) {
{
"response"
,
`[INST] {{ if .System }}{{ .System }}
{{ end }}{{ .Prompt }}[/INST] {{ .Response }}`
},
{
"messages"
,
`{{- $system :=
aggregate $
.Messages "system" -}}
{
"messages"
,
`{{- $system :=
contents
.Messages "system" -}}
{{- range $index, $_ := .Messages }}
{{- if eq .Role "user" }}[INST] {{ if $system }}{{ $system }}
{{- $system = "" }}
...
...
@@ -363,3 +363,36 @@ Answer: `,
})
}
}
func
TestFuncs
(
t
*
testing
.
T
)
{
t
.
Run
(
"contents"
,
func
(
t
*
testing
.
T
)
{
cases
:=
map
[
string
]
string
{
""
:
"A
\n\n
B
\n\n
C
\n\n
D
\n\n
E
\n\n
F"
,
"system"
:
"A
\n\n
F"
,
"user"
:
"B
\n\n
E"
,
"assistant"
:
"C
\n\n
D"
,
}
s
:=
[]
*
api
.
Message
{
{
Role
:
"system"
,
Content
:
"A"
},
{
Role
:
"user"
,
Content
:
"B"
},
{
Role
:
"assistant"
,
Content
:
"C"
},
{
Role
:
"assistant"
,
Content
:
"D"
},
{
Role
:
"user"
,
Content
:
"E"
},
{
Role
:
"system"
,
Content
:
"F"
},
}
fn
,
ok
:=
funcs
[
"contents"
]
.
(
func
([]
*
api
.
Message
,
...
string
)
string
)
if
!
ok
{
t
.
Fatal
(
"contents is not a function"
)
}
for
k
,
v
:=
range
cases
{
t
.
Run
(
k
,
func
(
t
*
testing
.
T
)
{
if
diff
:=
cmp
.
Diff
(
fn
(
s
,
k
),
v
);
diff
!=
""
{
t
.
Errorf
(
"mismatch (-got +want):
\n
%s"
,
diff
)
}
})
}
})
}
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