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

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

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

15
	"github.com/ollama/ollama/api"
Josh's avatar
Josh committed
16
	"github.com/ollama/ollama/llm"
Michael Yang's avatar
Michael Yang committed
17
	"github.com/ollama/ollama/template"
18
19
)

Michael Yang's avatar
Michael Yang committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
36
		ok     bool
Michael Yang's avatar
Michael Yang committed
37
	}{
Michael Yang's avatar
Michael Yang committed
38
		{"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
39
40
		{"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
41
42
43
44
45
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
		{"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
63
64
65
66
` + "```", 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},
67
68
69
70
		{"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},
71
		{"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},
72
		{"nemotron", `<toolcall>{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]} </toolcall>`, true},
Michael Yang's avatar
Michael Yang committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	}

	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{
		{
87
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
88
				Name: "get_current_weather",
89
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
90
91
92
93
94
95
					"format":   "fahrenheit",
					"location": "San Francisco, CA",
				},
			},
		},
		{
96
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
97
				Name: "get_current_weather",
98
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
					"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
127
128
				if ok != tt.ok {
					t.Fatalf("expected %t, got %t", tt.ok, ok)
Michael Yang's avatar
Michael Yang committed
129
130
				}

Michael Yang's avatar
Michael Yang committed
131
132
133
134
				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
135
136
137
138
139
				}
			})
		})
	}
}
Josh's avatar
Josh committed
140
141
142

func TestParseFromFileFromLayer(t *testing.T) {
	tempModels := t.TempDir()
143
	t.Setenv("OLLAMA_MODELS", tempModels)
Josh's avatar
Josh committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157

	file, err := os.CreateTemp(tempModels, "")
	if err != nil {
		t.Fatalf("failed to open file: %v", err)
	}
	defer file.Close()
	if err := llm.WriteGGUF(file, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
		t.Fatalf("failed to write gguf: %v", err)
	}

	if _, err := file.Seek(0, io.SeekStart); err != nil {
		t.Fatalf("failed to seek to start: %v", err)
	}

158
	layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, "", func(api.ProgressResponse) {})
Josh's avatar
Josh committed
159
160
161
162
163
164
165
166
167
168
169
170
	if err != nil {
		t.Fatalf("failed to parse from file: %v", err)
	}

	if len(layers) != 1 {
		t.Fatalf("got %d != want 1", len(layers))
	}

	if _, err := file.Seek(0, io.SeekStart); err != nil {
		t.Fatalf("failed to seek to start: %v", err)
	}

171
	layers2, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, layers[0].Digest, func(api.ProgressResponse) {})
Josh's avatar
Josh committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
	if err != nil {
		t.Fatalf("failed to parse from file: %v", err)
	}
	if len(layers2) != 1 {
		t.Fatalf("got %d != want 1", len(layers2))
	}

	if layers[0].Digest != layers2[0].Digest {
		t.Fatalf("got %s != want %s", layers[0].Digest, layers2[0].Digest)
	}

	if layers[0].Size != layers2[0].Size {
		t.Fatalf("got %d != want %d", layers[0].Size, layers2[0].Size)
	}

	if layers[0].MediaType != layers2[0].MediaType {
		t.Fatalf("got %v != want %v", layers[0].MediaType, layers2[0].MediaType)
	}
}

func TestParseLayerFromCopy(t *testing.T) {
	tempModels := t.TempDir()
194
	t.Setenv("OLLAMA_MODELS", tempModels)
Josh's avatar
Josh committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

	file2, err := os.CreateTemp(tempModels, "")
	if err != nil {
		t.Fatalf("failed to open file: %v", err)
	}
	defer file2.Close()

	for range 5 {
		if err := llm.WriteGGUF(file2, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
			t.Fatalf("failed to write gguf: %v", err)
		}
	}

	if _, err := file2.Seek(0, io.SeekStart); err != nil {
		t.Fatalf("failed to seek to start: %v", err)
	}

212
	layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file2, "", func(api.ProgressResponse) {})
Josh's avatar
Josh committed
213
214
215
216
217
218
219
220
	if err != nil {
		t.Fatalf("failed to parse from file: %v", err)
	}

	if len(layers) != 5 {
		t.Fatalf("got %d != want 5", len(layers))
	}
}
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

func TestParseObjects(t *testing.T) {
	tests := []struct {
		input string
		want  []map[string]any
	}{
		{
			input: `[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`,
			want: []map[string]any{
				{"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
				{"name": "get_current_weather", "arguments": map[string]any{"format": "celsius", "location": "Toronto, Canada"}},
			},
		},
		{
			input: `<toolcall>{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}} </toolcall>`,
			want: []map[string]any{
				{"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
			},
		},
		{
			input: `<toolcall>{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}} </toolcall> <toolcall>{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, ON"}} </toolcall>`,
			want: []map[string]any{
				{"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
				{"name": "get_current_weather", "arguments": map[string]any{"format": "celsius", "location": "Toronto, ON"}},
			},
		},
		{
			input: `{"name": "get_current_weather", "arguments": `,
			want:  nil,
		},
	}

	for _, tc := range tests {
		t.Run(tc.input, func(t *testing.T) {
			got := parseObjects(tc.input)

			if diff := cmp.Diff(got, tc.want); diff != "" {
				t.Errorf("mismatch (-got +want):\n%s", diff)
			}
		})
	}
}