model_test.go 8.6 KB

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