model_test.go 6.61 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},
Michael Yang's avatar
Michael Yang committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	}

	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{
		{
86
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
87
				Name: "get_current_weather",
88
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
89
90
91
92
93
94
					"format":   "fahrenheit",
					"location": "San Francisco, CA",
				},
			},
		},
		{
95
			Function: api.ToolCallFunction{
Michael Yang's avatar
Michael Yang committed
96
				Name: "get_current_weather",
97
				Arguments: api.ToolCallFunctionArguments{
Michael Yang's avatar
Michael Yang committed
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
123
124
125
					"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
126
127
				if ok != tt.ok {
					t.Fatalf("expected %t, got %t", tt.ok, ok)
Michael Yang's avatar
Michael Yang committed
128
129
				}

Michael Yang's avatar
Michael Yang committed
130
131
132
133
				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
134
135
136
137
138
				}
			})
		})
	}
}
Josh's avatar
Josh committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

func TestParseFromFileFromLayer(t *testing.T) {
	tempModels := t.TempDir()

	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)
	}

156
	layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, "", func(api.ProgressResponse) {})
Josh's avatar
Josh committed
157
158
159
160
161
162
163
164
165
166
167
168
	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)
	}

169
	layers2, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, layers[0].Digest, func(api.ProgressResponse) {})
Josh's avatar
Josh committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	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()

	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)
	}

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

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