model_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package server
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "os"
  10. "path/filepath"
  11. "slices"
  12. "strings"
  13. "testing"
  14. "github.com/google/go-cmp/cmp"
  15. "github.com/ollama/ollama/api"
  16. "github.com/ollama/ollama/template"
  17. )
  18. func createZipFile(t *testing.T, name string) *os.File {
  19. t.Helper()
  20. f, err := os.CreateTemp(t.TempDir(), "")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. zf := zip.NewWriter(f)
  25. defer zf.Close()
  26. zh, err := zf.CreateHeader(&zip.FileHeader{Name: name})
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if _, err := io.Copy(zh, bytes.NewReader([]byte(""))); err != nil {
  31. t.Fatal(err)
  32. }
  33. return f
  34. }
  35. func TestExtractFromZipFile(t *testing.T) {
  36. cases := []struct {
  37. name string
  38. expect []string
  39. err error
  40. }{
  41. {
  42. name: "good",
  43. expect: []string{"good"},
  44. },
  45. {
  46. name: strings.Join([]string{"path", "..", "to", "good"}, string(os.PathSeparator)),
  47. expect: []string{filepath.Join("to", "good")},
  48. },
  49. {
  50. name: strings.Join([]string{"path", "..", "to", "..", "good"}, string(os.PathSeparator)),
  51. expect: []string{"good"},
  52. },
  53. {
  54. name: strings.Join([]string{"path", "to", "..", "..", "good"}, string(os.PathSeparator)),
  55. expect: []string{"good"},
  56. },
  57. {
  58. name: strings.Join([]string{"..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "..", "bad"}, string(os.PathSeparator)),
  59. err: zip.ErrInsecurePath,
  60. },
  61. {
  62. name: strings.Join([]string{"path", "..", "..", "to", "bad"}, string(os.PathSeparator)),
  63. err: zip.ErrInsecurePath,
  64. },
  65. }
  66. for _, tt := range cases {
  67. t.Run(tt.name, func(t *testing.T) {
  68. f := createZipFile(t, tt.name)
  69. defer f.Close()
  70. tempDir := t.TempDir()
  71. if err := extractFromZipFile(tempDir, f, func(api.ProgressResponse) {}); !errors.Is(err, tt.err) {
  72. t.Fatal(err)
  73. }
  74. var matches []string
  75. if err := filepath.Walk(tempDir, func(p string, fi os.FileInfo, err error) error {
  76. if err != nil {
  77. return err
  78. }
  79. if !fi.IsDir() {
  80. matches = append(matches, p)
  81. }
  82. return nil
  83. }); err != nil {
  84. t.Fatal(err)
  85. }
  86. var actual []string
  87. for _, match := range matches {
  88. rel, err := filepath.Rel(tempDir, match)
  89. if err != nil {
  90. t.Error(err)
  91. }
  92. actual = append(actual, rel)
  93. }
  94. if !slices.Equal(actual, tt.expect) {
  95. t.Fatalf("expected %d files, got %d", len(tt.expect), len(matches))
  96. }
  97. })
  98. }
  99. }
  100. type function struct {
  101. Name string `json:"name"`
  102. Arguments map[string]any `json:"arguments"`
  103. }
  104. func readFile(t *testing.T, base, name string) *bytes.Buffer {
  105. t.Helper()
  106. bts, err := os.ReadFile(filepath.Join(base, name))
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. return bytes.NewBuffer(bts)
  111. }
  112. func TestExecuteWithTools(t *testing.T) {
  113. p := filepath.Join("testdata", "tools")
  114. cases := []struct {
  115. model string
  116. output string
  117. ok bool
  118. }{
  119. {"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},
  120. {"mistral", `[TOOL_CALLS] [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]
  121. The temperature in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.`, true},
  122. {"mistral", `I'm not aware of that information. However, I can suggest searching for the weather using the "get_current_weather" function:
  123. [{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
  124. {"mistral", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  125. {"command-r-plus", "Action: ```json" + `
  126. [
  127. {
  128. "tool_name": "get_current_weather",
  129. "parameters": {
  130. "format": "fahrenheit",
  131. "location": "San Francisco, CA"
  132. }
  133. },
  134. {
  135. "tool_name": "get_current_weather",
  136. "parameters": {
  137. "format": "celsius",
  138. "location": "Toronto, Canada"
  139. }
  140. }
  141. ]
  142. ` + "```", true},
  143. {"command-r-plus", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  144. {"firefunction", ` functools[{"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}},{"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}]`, true},
  145. {"firefunction", " The weather in San Francisco, CA is 70°F and in Toronto, Canada is 20°C.", false},
  146. {"llama3-groq-tool-use", `<tool_call>
  147. {"name": "get_current_weather", "arguments": {"format":"fahrenheit","location":"San Francisco, CA"}}
  148. {"name": "get_current_weather", "arguments": {"format":"celsius","location":"Toronto, Canada"}}
  149. </tool_call>`, true},
  150. }
  151. var tools []api.Tool
  152. if err := json.Unmarshal(readFile(t, p, "tools.json").Bytes(), &tools); err != nil {
  153. t.Fatal(err)
  154. }
  155. var messages []api.Message
  156. if err := json.Unmarshal(readFile(t, p, "messages.json").Bytes(), &messages); err != nil {
  157. t.Fatal(err)
  158. }
  159. calls := []api.ToolCall{
  160. {
  161. Function: function{
  162. Name: "get_current_weather",
  163. Arguments: map[string]any{
  164. "format": "fahrenheit",
  165. "location": "San Francisco, CA",
  166. },
  167. },
  168. },
  169. {
  170. Function: function{
  171. Name: "get_current_weather",
  172. Arguments: map[string]any{
  173. "format": "celsius",
  174. "location": "Toronto, Canada",
  175. },
  176. },
  177. },
  178. }
  179. for _, tt := range cases {
  180. t.Run(tt.model, func(t *testing.T) {
  181. tmpl, err := template.Parse(readFile(t, p, fmt.Sprintf("%s.gotmpl", tt.model)).String())
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. t.Run("template", func(t *testing.T) {
  186. var actual bytes.Buffer
  187. if err := tmpl.Execute(&actual, template.Values{Tools: tools, Messages: messages}); err != nil {
  188. t.Fatal(err)
  189. }
  190. if diff := cmp.Diff(actual.String(), readFile(t, p, fmt.Sprintf("%s.out", tt.model)).String()); diff != "" {
  191. t.Errorf("mismatch (-got +want):\n%s", diff)
  192. }
  193. })
  194. t.Run("parse", func(t *testing.T) {
  195. m := &Model{Template: tmpl}
  196. actual, ok := m.parseToolCalls(tt.output)
  197. if ok != tt.ok {
  198. t.Fatalf("expected %t, got %t", tt.ok, ok)
  199. }
  200. if tt.ok {
  201. if diff := cmp.Diff(actual, calls); diff != "" {
  202. t.Errorf("mismatch (-got +want):\n%s", diff)
  203. }
  204. }
  205. })
  206. })
  207. }
  208. }