model_test.go 4.42 KB
Newer Older
1
2
3
4
package server

import (
	"bytes"
Michael Yang's avatar
Michael Yang committed
5
6
	"encoding/json"
	"fmt"
7
8
9
10
	"os"
	"path/filepath"
	"testing"

Michael Yang's avatar
Michael Yang committed
11
	"github.com/google/go-cmp/cmp"
Michael Yang's avatar
lint  
Michael Yang committed
12

13
	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
14
	"github.com/ollama/ollama/template"
15
16
)

Michael Yang's avatar
Michael Yang committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
func readFile(t *testing.T, base, name string) *bytes.Buffer {
	t.Helper()

	bts, err := os.ReadFile(filepath.Join(base, name))
	if err != nil {
		t.Fatal(err)
	}

	return bytes.NewBuffer(bts)
}

func TestExecuteWithTools(t *testing.T) {
	p := filepath.Join("testdata", "tools")
	cases := []struct {
		model  string
		output string
Michael Yang's avatar
Michael Yang committed
33
		ok     bool
Michael Yang's avatar
Michael Yang committed
34
	}{
Michael Yang's avatar
Michael Yang committed
35
		{"mistral", `[TOOL_CALLS]  [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
Michael Yang's avatar
Michael Yang committed
36
37
		{"mistral", `[TOOL_CALLS]  [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]

Michael Yang's avatar
Michael Yang committed
38
39
40
41
42
The temperature in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.`, true},
		{"mistral", `I'm not aware of that information. However, I can suggest searching for the weather using the "get_current_weather" function:

		[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
		{"mistral", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
Michael Yang's avatar
Michael Yang committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
		{"command-r-plus", "Action: ```json" + `
[
    {
        "tool_name": "get_current_weather",
        "parameters": {
            "format": "fahrenheit",
            "location": "San Francisco, CA"
        }
    },
    {
        "tool_name": "get_current_weather",
        "parameters": {
            "format": "celsius",
            "location": "Toronto, Canada"
        }
    }
]
Michael Yang's avatar
Michael Yang committed
60
61
62
63
` + "```", true},
		{"command-r-plus", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
		{"firefunction", ` functools[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
		{"firefunction", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
64
65
66
67
		{"llama3-groq-tool-use", `<tool_call>
{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}}
{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}
</tool_call>`, true},
68
		{"xlam", `{"tool_calls": [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]}`, true},
Michael Yang's avatar
Michael Yang committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	}

	var tools []api.Tool
	if err := json.Unmarshal(readFile(t, p, "tools.json").Bytes(), &tools); err != nil {
		t.Fatal(err)
	}

	var messages []api.Message
	if err := json.Unmarshal(readFile(t, p, "messages.json").Bytes(), &messages); err != nil {
		t.Fatal(err)
	}

	calls := []api.ToolCall{
		{
83
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
84
				Name: "get_current_weather",
85
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
86
87
88
89
90
91
					"format":   "fahrenheit",
					"location": "San Francisco, CA",
				},
			},
		},
		{
92
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
93
				Name: "get_current_weather",
94
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
					"format":   "celsius",
					"location": "Toronto, Canada",
				},
			},
		},
	}

	for _, tt := range cases {
		t.Run(tt.model, func(t *testing.T) {
			tmpl, err := template.Parse(readFile(t, p, fmt.Sprintf("%s.gotmpl", tt.model)).String())
			if err != nil {
				t.Fatal(err)
			}

			t.Run("template", func(t *testing.T) {
				var actual bytes.Buffer
				if err := tmpl.Execute(&actual, template.Values{Tools: tools, Messages: messages}); err != nil {
					t.Fatal(err)
				}

				if diff := cmp.Diff(actual.String(), readFile(t, p, fmt.Sprintf("%s.out", tt.model)).String()); diff != "" {
					t.Errorf("mismatch (-got +want):\n%s", diff)
				}
			})

			t.Run("parse", func(t *testing.T) {
				m := &Model{Template: tmpl}
				actual, ok := m.parseToolCalls(tt.output)
Michael Yang's avatar
Michael Yang committed
123
124
				if ok != tt.ok {
					t.Fatalf("expected %t, got %t", tt.ok, ok)
Michael Yang's avatar
Michael Yang committed
125
126
				}

Michael Yang's avatar
Michael Yang committed
127
128
129
130
				if tt.ok {
					if diff := cmp.Diff(actual, calls); diff != "" {
						t.Errorf("mismatch (-got +want):\n%s", diff)
					}
Michael Yang's avatar
Michael Yang committed
131
132
133
134
135
				}
			})
		})
	}
}