model_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. file, err := os.CreateTemp(tempModels, "")
  125. if err != nil {
  126. t.Fatalf("failed to open file: %v", err)
  127. }
  128. defer file.Close()
  129. if err := llm.WriteGGUF(file, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
  130. t.Fatalf("failed to write gguf: %v", err)
  131. }
  132. if _, err := file.Seek(0, io.SeekStart); err != nil {
  133. t.Fatalf("failed to seek to start: %v", err)
  134. }
  135. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, "", func(api.ProgressResponse) {})
  136. if err != nil {
  137. t.Fatalf("failed to parse from file: %v", err)
  138. }
  139. if len(layers) != 1 {
  140. t.Fatalf("got %d != want 1", len(layers))
  141. }
  142. if _, err := file.Seek(0, io.SeekStart); err != nil {
  143. t.Fatalf("failed to seek to start: %v", err)
  144. }
  145. layers2, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file, layers[0].Digest, func(api.ProgressResponse) {})
  146. if err != nil {
  147. t.Fatalf("failed to parse from file: %v", err)
  148. }
  149. if len(layers2) != 1 {
  150. t.Fatalf("got %d != want 1", len(layers2))
  151. }
  152. if layers[0].Digest != layers2[0].Digest {
  153. t.Fatalf("got %s != want %s", layers[0].Digest, layers2[0].Digest)
  154. }
  155. if layers[0].Size != layers2[0].Size {
  156. t.Fatalf("got %d != want %d", layers[0].Size, layers2[0].Size)
  157. }
  158. if layers[0].MediaType != layers2[0].MediaType {
  159. t.Fatalf("got %v != want %v", layers[0].MediaType, layers2[0].MediaType)
  160. }
  161. }
  162. func TestParseLayerFromCopy(t *testing.T) {
  163. tempModels := t.TempDir()
  164. file2, err := os.CreateTemp(tempModels, "")
  165. if err != nil {
  166. t.Fatalf("failed to open file: %v", err)
  167. }
  168. defer file2.Close()
  169. for range 5 {
  170. if err := llm.WriteGGUF(file2, llm.KV{"general.architecture": "gemma"}, []llm.Tensor{}); err != nil {
  171. t.Fatalf("failed to write gguf: %v", err)
  172. }
  173. }
  174. if _, err := file2.Seek(0, io.SeekStart); err != nil {
  175. t.Fatalf("failed to seek to start: %v", err)
  176. }
  177. layers, err := parseFromFile(context.Background(), "model", []*layerGGML{}, file2, "", func(api.ProgressResponse) {})
  178. if err != nil {
  179. t.Fatalf("failed to parse from file: %v", err)
  180. }
  181. if len(layers) != 5 {
  182. t.Fatalf("got %d != want 5", len(layers))
  183. }
  184. }