model_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. "github.com/google/go-cmp/cmp"
  12. "github.com/ollama/ollama/api"
  13. "github.com/ollama/ollama/fs/ggml"
  14. "github.com/ollama/ollama/template"
  15. )
  16. func readFile(t *testing.T, base, name string) *bytes.Buffer {
  17. t.Helper()
  18. bts, err := os.ReadFile(filepath.Join(base, name))
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. return bytes.NewBuffer(bts)
  23. }
  24. func TestExecuteWithTools(t *testing.T) {
  25. p := filepath.Join("testdata", "tools")
  26. cases := []struct {
  27. model string
  28. output string
  29. ok bool
  30. }{
  31. {"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},
  32. {"mistral", `[TOOL_CALLS] [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]
  33. The temperature in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.`, true},
  34. {"mistral", `[TOOL_CALLS] [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"To }]`, false},
  35. {"mistral", `I'm not aware of that information. However, I can suggest searching for the weather using the "get_current_weather" function:
  36. [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
  37. {"mistral", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  38. {"command-r-plus", "Action: ```json" + `
  39. [
  40. {
  41. "tool_name": "get_current_weather",
  42. "parameters": {
  43. "format": "fahrenheit",
  44. "location": "San Francisco, CA"
  45. }
  46. },
  47. {
  48. "tool_name": "get_current_weather",
  49. "parameters": {
  50. "format": "celsius",
  51. "location": "Toronto, Canada"
  52. }
  53. }
  54. ]
  55. ` + "```", true},
  56. {"command-r-plus", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  57. {"firefunction", ` functools[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
  58. {"firefunction", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  59. {"llama3-groq-tool-use", `<tool_call>
  60. {"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}}
  61. {"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}
  62. </tool_call>`, true},
  63. {"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},
  64. {"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},
  65. }
  66. var tools []api.Tool
  67. if err := json.Unmarshal(readFile(t, p, "tools.json").Bytes(), &tools); err != nil {
  68. t.Fatal(err)
  69. }
  70. var messages []api.Message
  71. if err := json.Unmarshal(readFile(t, p, "messages.json").Bytes(), &messages); err != nil {
  72. t.Fatal(err)
  73. }
  74. calls := []api.ToolCall{
  75. {
  76. Function: api.ToolCallFunction{
  77. Name: "get_current_weather",
  78. Arguments: api.ToolCallFunctionArguments{
  79. "format": "fahrenheit",
  80. "location": "San Francisco, CA",
  81. },
  82. },
  83. },
  84. {
  85. Function: api.ToolCallFunction{
  86. Name: "get_current_weather",
  87. Arguments: api.ToolCallFunctionArguments{
  88. "format": "celsius",
  89. "location": "Toronto, Canada",
  90. },
  91. },
  92. },
  93. }
  94. for _, tt := range cases {
  95. t.Run(tt.model, func(t *testing.T) {
  96. tmpl, err := template.Parse(readFile(t, p, fmt.Sprintf("%s.gotmpl", tt.model)).String())
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. t.Run("template", func(t *testing.T) {
  101. var actual bytes.Buffer
  102. if err := tmpl.Execute(&actual, template.Values{Tools: tools, Messages: messages}); err != nil {
  103. t.Fatal(err)
  104. }
  105. if diff := cmp.Diff(actual.String(), readFile(t, p, fmt.Sprintf("%s.out", tt.model)).String()); diff != "" {
  106. t.Errorf("mismatch (-got +want):\n%s", diff)
  107. }
  108. })
  109. t.Run("parse", func(t *testing.T) {
  110. m := &Model{Template: tmpl}
  111. actual, ok := m.parseToolCalls(tt.output)
  112. if ok != tt.ok {
  113. t.Fatalf("expected %t, got %t", tt.ok, ok)
  114. }
  115. if tt.ok {
  116. if diff := cmp.Diff(actual, calls); diff != "" {
  117. t.Errorf("mismatch (-got +want):\n%s", diff)
  118. }
  119. }
  120. })
  121. })
  122. }
  123. }
  124. func TestParseFromFileFromLayer(t *testing.T) {
  125. tempModels := t.TempDir()
  126. t.Setenv("OLLAMA_MODELS", tempModels)
  127. file, err := os.CreateTemp(tempModels, "")
  128. if err != nil {
  129. t.Fatalf("failed to open file: %v", err)
  130. }
  131. defer file.Close()
  132. if err := ggml.WriteGGUF(file, ggml.KV{"general.architecture": "gemma"}, []ggml.Tensor{}); err != nil {
  133. t.Fatalf("failed to write gguf: %v", err)
  134. }
  135. if _, err := file.Seek(0, io.SeekStart); err != nil {
  136. t.Fatalf("failed to seek to start: %v", err)
  137. }
  138. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, "", func(api.ProgressResponse) {})
  139. if err != nil {
  140. t.Fatalf("failed to parse from file: %v", err)
  141. }
  142. if len(layers) != 1 {
  143. t.Fatalf("got %d != want 1", len(layers))
  144. }
  145. if _, err := file.Seek(0, io.SeekStart); err != nil {
  146. t.Fatalf("failed to seek to start: %v", err)
  147. }
  148. layers2, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, layers[0].Digest, func(api.ProgressResponse) {})
  149. if err != nil {
  150. t.Fatalf("failed to parse from file: %v", err)
  151. }
  152. if len(layers2) != 1 {
  153. t.Fatalf("got %d != want 1", len(layers2))
  154. }
  155. if layers[0].Digest != layers2[0].Digest {
  156. t.Fatalf("got %s != want %s", layers[0].Digest, layers2[0].Digest)
  157. }
  158. if layers[0].Size != layers2[0].Size {
  159. t.Fatalf("got %d != want %d", layers[0].Size, layers2[0].Size)
  160. }
  161. if layers[0].MediaType != layers2[0].MediaType {
  162. t.Fatalf("got %v != want %v", layers[0].MediaType, layers2[0].MediaType)
  163. }
  164. }
  165. func TestParseLayerFromCopy(t *testing.T) {
  166. tempModels := t.TempDir()
  167. t.Setenv("OLLAMA_MODELS", tempModels)
  168. file2, err := os.CreateTemp(tempModels, "")
  169. if err != nil {
  170. t.Fatalf("failed to open file: %v", err)
  171. }
  172. defer file2.Close()
  173. for range 5 {
  174. if err := ggml.WriteGGUF(file2, ggml.KV{"general.architecture": "gemma"}, []ggml.Tensor{}); err != nil {
  175. t.Fatalf("failed to write gguf: %v", err)
  176. }
  177. }
  178. if _, err := file2.Seek(0, io.SeekStart); err != nil {
  179. t.Fatalf("failed to seek to start: %v", err)
  180. }
  181. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file2, "", func(api.ProgressResponse) {})
  182. if err != nil {
  183. t.Fatalf("failed to parse from file: %v", err)
  184. }
  185. if len(layers) != 5 {
  186. t.Fatalf("got %d != want 5", len(layers))
  187. }
  188. }
  189. func TestParseObjects(t *testing.T) {
  190. tests := []struct {
  191. input string
  192. want []map[string]any
  193. }{
  194. {
  195. input: `[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`,
  196. want: []map[string]any{
  197. {"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
  198. {"name": "get_current_weather", "arguments": map[string]any{"format": "celsius", "location": "Toronto, Canada"}},
  199. },
  200. },
  201. {
  202. input: `<toolcall>{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}} </toolcall>`,
  203. want: []map[string]any{
  204. {"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
  205. },
  206. },
  207. {
  208. 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>`,
  209. want: []map[string]any{
  210. {"name": "get_current_weather", "arguments": map[string]any{"format": "fahrenheit", "location": "San Francisco, CA"}},
  211. {"name": "get_current_weather", "arguments": map[string]any{"format": "celsius", "location": "Toronto, ON"}},
  212. },
  213. },
  214. {
  215. input: `{"name": "get_current_weather", "arguments": `,
  216. want: nil,
  217. },
  218. }
  219. for _, tc := range tests {
  220. t.Run(tc.input, func(t *testing.T) {
  221. got := parseObjects(tc.input)
  222. if diff := cmp.Diff(got, tc.want); diff != "" {
  223. t.Errorf("mismatch (-got +want):\n%s", diff)
  224. }
  225. })
  226. }
  227. }