mirror of
https://github.com/ollama/ollama.git
synced 2026-12-04 08:32:11 +00:00
188 lines
6.2 KiB
Go
188 lines
6.2 KiB
Go
package harmony
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ollama/ollama/api"
|
|
)
|
|
|
|
func TestHarmonyMessageHandlerRecipients(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantContent string
|
|
wantThinking string
|
|
wantFunction string
|
|
wantArgs string
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "builtin python",
|
|
input: " to=python<|channel|>analysis<|message|>print(1 + 1)",
|
|
wantThinking: "print(1 + 1)",
|
|
},
|
|
{
|
|
name: "builtin recipient after channel",
|
|
input: "<|channel|>analysis to=python<|message|>print(1 + 1)",
|
|
wantThinking: "print(1 + 1)",
|
|
},
|
|
{
|
|
name: "browser analysis still uses JSON",
|
|
input: " to=browser.search<|channel|>analysis<|message|>{\"query\":\"weather\"}",
|
|
wantFunction: "browser.search",
|
|
wantArgs: `{"query":"weather"}`,
|
|
},
|
|
{
|
|
name: "browser commentary still uses JSON",
|
|
input: "<|channel|>commentary to=browser.search<|message|>{\"query\":\"weather\"}",
|
|
wantFunction: "browser.search",
|
|
wantArgs: `{"query":"weather"}`,
|
|
},
|
|
{
|
|
name: "container still uses JSON",
|
|
input: " to=container.exec<|channel|>analysis<|message|>{\"cmd\":\"echo hello\"}",
|
|
wantFunction: "container.exec",
|
|
wantArgs: `{"cmd":"echo hello"}`,
|
|
},
|
|
{
|
|
name: "malformed container arguments still fail",
|
|
input: " to=container.exec<|channel|>analysis<|message|>echo hello",
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "malformed browser arguments still fail",
|
|
input: " to=browser.search<|channel|>analysis<|message|>weather",
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "builtin commentary is ignored",
|
|
input: "<|channel|>commentary to=python<|message|>print(1 + 1)",
|
|
},
|
|
{
|
|
name: "builtin between analysis and final",
|
|
input: "<|channel|>analysis<|message|>Calculating.\n<|end|><|start|>assistant to=python<|channel|>analysis<|message|>print(1 + 1)<|end|><|start|>assistant<|channel|>final<|message|>2",
|
|
wantContent: "2",
|
|
wantThinking: "Calculating.\nprint(1 + 1)",
|
|
},
|
|
{
|
|
name: "final after ignored builtin",
|
|
input: "<|channel|>commentary to=python<|message|>print(1 + 1)<|end|><|start|>assistant<|channel|>final<|message|>Hello",
|
|
wantContent: "Hello",
|
|
},
|
|
{
|
|
name: "function after builtin",
|
|
input: " to=python<|channel|>analysis<|message|>print(1 + 1)<|end|><|start|>assistant<|channel|>commentary to=functions.calculate<|message|>{\"x\":1}",
|
|
wantThinking: "print(1 + 1)",
|
|
wantFunction: "calculate",
|
|
wantArgs: `{"x":1}`,
|
|
},
|
|
{
|
|
name: "function named python",
|
|
input: "<|channel|>commentary to=functions.python<|message|>{\"code\":\"print(1 + 1)\"}",
|
|
wantFunction: "python",
|
|
wantArgs: `{"code":"print(1 + 1)"}`,
|
|
},
|
|
{
|
|
name: "function named python on analysis",
|
|
input: " to=functions.python<|channel|>analysis<|message|>{\"code\":\"print(1 + 1)\"}",
|
|
wantFunction: "python",
|
|
wantArgs: `{"code":"print(1 + 1)"}`,
|
|
},
|
|
{
|
|
name: "bare function name",
|
|
input: "<|channel|>commentary to=calculate<|message|>{\"x\":1}",
|
|
wantFunction: "calculate",
|
|
wantArgs: `{"x":1}`,
|
|
},
|
|
{
|
|
name: "function sharing builtin prefix",
|
|
input: "<|channel|>commentary to=python_helper<|message|>{\"x\":1}",
|
|
wantFunction: "python_helper",
|
|
wantArgs: `{"x":1}`,
|
|
},
|
|
{
|
|
name: "malformed function arguments still fail",
|
|
input: "<|channel|>commentary to=functions.python<|message|>print(1 + 1)",
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "commentary without recipient",
|
|
input: "<|channel|>commentary<|message|>Let me check.",
|
|
wantContent: "Let me check.",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
for _, withTools := range []bool{false, true} {
|
|
// Completion strips the <|call|> stop token and signals done separately.
|
|
for _, chunkSize := range []int{len(tt.input), 1, 7} {
|
|
t.Run(fmt.Sprintf("%s/tools=%t/chunk=%d", tt.name, withTools, chunkSize), func(t *testing.T) {
|
|
h := NewHarmonyMessageHandler()
|
|
var tools []api.Tool
|
|
if withTools {
|
|
for _, name := range []string{"python", "calculate", "python_helper"} {
|
|
tools = append(tools, api.Tool{Type: "function", Function: api.ToolFunction{Name: name}})
|
|
}
|
|
}
|
|
h.Init(tools, nil, nil)
|
|
|
|
var content, thinking strings.Builder
|
|
var calls []api.ToolCall
|
|
var parseErr error
|
|
for start := 0; start < len(tt.input); start += chunkSize {
|
|
end := min(start+chunkSize, len(tt.input))
|
|
c, th, tc, err := h.Add(tt.input[start:end], end == len(tt.input))
|
|
if err != nil && end != len(tt.input) {
|
|
t.Fatal(err)
|
|
}
|
|
content.WriteString(c)
|
|
thinking.WriteString(th)
|
|
calls = append(calls, tc...)
|
|
parseErr = err
|
|
}
|
|
if tt.wantError {
|
|
if parseErr == nil || !strings.Contains(parseErr.Error(), "error parsing tool call") {
|
|
t.Fatalf("expected tool-call parsing error, got %v", parseErr)
|
|
}
|
|
return
|
|
}
|
|
if parseErr != nil {
|
|
t.Fatal(parseErr)
|
|
}
|
|
if content.String() != tt.wantContent || thinking.String() != tt.wantThinking {
|
|
t.Errorf("got content=%q thinking=%q, want content=%q thinking=%q", content.String(), thinking.String(), tt.wantContent, tt.wantThinking)
|
|
}
|
|
if tt.wantFunction == "" {
|
|
if len(calls) != 0 {
|
|
t.Fatalf("unexpected tool calls: %v", calls)
|
|
}
|
|
} else if len(calls) != 1 || calls[0].Function.Name != tt.wantFunction || calls[0].Function.Arguments.String() != tt.wantArgs {
|
|
t.Fatalf("got calls %v, want %s(%s)", calls, tt.wantFunction, tt.wantArgs)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHarmonyBuiltinPythonStreamsThinking(t *testing.T) {
|
|
h := NewHarmonyMessageHandler()
|
|
h.Init(nil, nil, nil)
|
|
for _, step := range []struct {
|
|
input string
|
|
done bool
|
|
want string
|
|
}{
|
|
{" to=python<|channel|>analysis<|message|>print(", false, "print("},
|
|
{"1 + 1)", false, "1 + 1)"},
|
|
{"", true, ""},
|
|
} {
|
|
content, thinking, calls, err := h.Add(step.input, step.done)
|
|
if err != nil || content != "" || thinking != step.want || len(calls) != 0 {
|
|
t.Fatalf("input %q: got content=%q thinking=%q calls=%v err=%v, want thinking=%q", step.input, content, thinking, calls, err, step.want)
|
|
}
|
|
}
|
|
}
|