model_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  64. var tools []api.Tool
  65. if err := json.Unmarshal(readFile(t, p, "tools.json").Bytes(), &tools); err != nil {
  66. t.Fatal(err)
  67. }
  68. var messages []api.Message
  69. if err := json.Unmarshal(readFile(t, p, "messages.json").Bytes(), &messages); err != nil {
  70. t.Fatal(err)
  71. }
  72. calls := []api.ToolCall{
  73. {
  74. Function: api.ToolCallFunction{
  75. Name: "get_current_weather",
  76. Arguments: api.ToolCallFunctionArguments{
  77. "format": "fahrenheit",
  78. "location": "San Francisco, CA",
  79. },
  80. },
  81. },
  82. {
  83. Function: api.ToolCallFunction{
  84. Name: "get_current_weather",
  85. Arguments: api.ToolCallFunctionArguments{
  86. "format": "celsius",
  87. "location": "Toronto, Canada",
  88. },
  89. },
  90. },
  91. }
  92. for _, tt := range cases {
  93. t.Run(tt.model, func(t *testing.T) {
  94. tmpl, err := template.Parse(readFile(t, p, fmt.Sprintf("%s.gotmpl", tt.model)).String())
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. t.Run("template", func(t *testing.T) {
  99. var actual bytes.Buffer
  100. if err := tmpl.Execute(&actual, template.Values{Tools: tools, Messages: messages}); err != nil {
  101. t.Fatal(err)
  102. }
  103. if diff := cmp.Diff(actual.String(), readFile(t, p, fmt.Sprintf("%s.out", tt.model)).String()); diff != "" {
  104. t.Errorf("mismatch (-got +want):\n%s", diff)
  105. }
  106. })
  107. t.Run("parse", func(t *testing.T) {
  108. m := &Model{Template: tmpl}
  109. actual, ok := m.parseToolCalls(tt.output)
  110. if ok != tt.ok {
  111. t.Fatalf("expected %t, got %t", tt.ok, ok)
  112. }
  113. if tt.ok {
  114. if diff := cmp.Diff(actual, calls); diff != "" {
  115. t.Errorf("mismatch (-got +want):\n%s", diff)
  116. }
  117. }
  118. })
  119. })
  120. }
  121. }
  122. func TestParseFromFileFromLayer(t *testing.T) {
  123. tempModels := t.TempDir()
  124. t.Setenv("OLLAMA_MODELS", tempModels)
  125. file, err := os.CreateTemp(tempModels, "")
  126. if err != nil {
  127. t.Fatalf("failed to open file: %v", err)
  128. }
  129. defer file.Close()
  130. if err := llm.WriteGGUF(file, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
  131. t.Fatalf("failed to write gguf: %v", err)
  132. }
  133. if _, err := file.Seek(0, io.SeekStart); err != nil {
  134. t.Fatalf("failed to seek to start: %v", err)
  135. }
  136. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, "", func(api.ProgressResponse) {})
  137. if err != nil {
  138. t.Fatalf("failed to parse from file: %v", err)
  139. }
  140. if len(layers) != 1 {
  141. t.Fatalf("got %d != want 1", len(layers))
  142. }
  143. if _, err := file.Seek(0, io.SeekStart); err != nil {
  144. t.Fatalf("failed to seek to start: %v", err)
  145. }
  146. layers2, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, layers[0].Digest, func(api.ProgressResponse) {})
  147. if err != nil {
  148. t.Fatalf("failed to parse from file: %v", err)
  149. }
  150. if len(layers2) != 1 {
  151. t.Fatalf("got %d != want 1", len(layers2))
  152. }
  153. if layers[0].Digest != layers2[0].Digest {
  154. t.Fatalf("got %s != want %s", layers[0].Digest, layers2[0].Digest)
  155. }
  156. if layers[0].Size != layers2[0].Size {
  157. t.Fatalf("got %d != want %d", layers[0].Size, layers2[0].Size)
  158. }
  159. if layers[0].MediaType != layers2[0].MediaType {
  160. t.Fatalf("got %v != want %v", layers[0].MediaType, layers2[0].MediaType)
  161. }
  162. }
  163. func TestParseLayerFromCopy(t *testing.T) {
  164. tempModels := t.TempDir()
  165. t.Setenv("OLLAMA_MODELS", tempModels)
  166. file2, err := os.CreateTemp(tempModels, "")
  167. if err != nil {
  168. t.Fatalf("failed to open file: %v", err)
  169. }
  170. defer file2.Close()
  171. for range 5 {
  172. if err := llm.WriteGGUF(file2, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
  173. t.Fatalf("failed to write gguf: %v", err)
  174. }
  175. }
  176. if _, err := file2.Seek(0, io.SeekStart); err != nil {
  177. t.Fatalf("failed to seek to start: %v", err)
  178. }
  179. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file2, "", func(api.ProgressResponse) {})
  180. if err != nil {
  181. t.Fatalf("failed to parse from file: %v", err)
  182. }
  183. if len(layers) != 5 {
  184. t.Fatalf("got %d != want 5", len(layers))
  185. }
  186. }