interactive_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package cmd
  2. import (
  3. "bytes"
  4. "testing"
  5. "text/template"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "github.com/ollama/ollama/api"
  9. )
  10. func TestExtractFilenames(t *testing.T) {
  11. // Unix style paths
  12. input := ` some preamble
  13. ./relative\ path/one.png inbetween1 ./not a valid two.jpg inbetween2
  14. /unescaped space /three.jpeg inbetween3 /valid\ path/dir/four.png "./quoted with spaces/five.svg`
  15. res := extractFileNames(input)
  16. assert.Len(t, res, 5)
  17. assert.Contains(t, res[0], "one.png")
  18. assert.Contains(t, res[1], "two.jpg")
  19. assert.Contains(t, res[2], "three.jpeg")
  20. assert.Contains(t, res[3], "four.png")
  21. assert.Contains(t, res[4], "five.svg")
  22. assert.NotContains(t, res[4], '"')
  23. assert.NotContains(t, res, "inbtween")
  24. // Windows style paths
  25. input = ` some preamble
  26. c:/users/jdoe/one.png inbetween1 c:/program files/someplace/two.jpg inbetween2
  27. /absolute/nospace/three.jpeg inbetween3 /absolute/with space/four.png inbetween4
  28. ./relative\ path/five.svg inbetween5 "./relative with/spaces/six.png inbetween6
  29. d:\path with\spaces\seven.svg inbetween7 c:\users\jdoe\eight.png inbetween8
  30. d:\program files\someplace\nine.png inbetween9 "E:\program files\someplace\ten.svg some ending
  31. `
  32. res = extractFileNames(input)
  33. assert.Len(t, res, 10)
  34. assert.NotContains(t, res, "inbtween")
  35. assert.Contains(t, res[0], "one.png")
  36. assert.Contains(t, res[0], "c:")
  37. assert.Contains(t, res[1], "two.jpg")
  38. assert.Contains(t, res[1], "c:")
  39. assert.Contains(t, res[2], "three.jpeg")
  40. assert.Contains(t, res[3], "four.png")
  41. assert.Contains(t, res[4], "five.svg")
  42. assert.Contains(t, res[5], "six.png")
  43. assert.Contains(t, res[6], "seven.svg")
  44. assert.Contains(t, res[6], "d:")
  45. assert.Contains(t, res[7], "eight.png")
  46. assert.Contains(t, res[7], "c:")
  47. assert.Contains(t, res[8], "nine.png")
  48. assert.Contains(t, res[8], "d:")
  49. assert.Contains(t, res[9], "ten.svg")
  50. assert.Contains(t, res[9], "E:")
  51. }
  52. func TestModelfileBuilder(t *testing.T) {
  53. opts := runOptions{
  54. Model: "hork",
  55. System: "You are part horse and part shark, but all hork. Do horklike things",
  56. Messages: []api.Message{
  57. {Role: "user", Content: "Hey there hork!"},
  58. {Role: "assistant", Content: "Yes it is true, I am half horse, half shark."},
  59. },
  60. Options: map[string]interface{}{},
  61. }
  62. opts.Options["temperature"] = 0.9
  63. opts.Options["seed"] = 42
  64. opts.Options["penalize_newline"] = false
  65. opts.Options["stop"] = []string{"hi", "there"}
  66. mf := buildModelfile(opts)
  67. expectedModelfile := `FROM {{.Model}}
  68. SYSTEM """{{.System}}"""
  69. PARAMETER penalize_newline false
  70. PARAMETER seed 42
  71. PARAMETER stop [hi there]
  72. PARAMETER temperature 0.9
  73. MESSAGE user """Hey there hork!"""
  74. MESSAGE assistant """Yes it is true, I am half horse, half shark."""
  75. `
  76. tmpl, err := template.New("").Parse(expectedModelfile)
  77. require.NoError(t, err)
  78. var buf bytes.Buffer
  79. err = tmpl.Execute(&buf, opts)
  80. require.NoError(t, err)
  81. assert.Equal(t, buf.String(), mf)
  82. opts.ParentModel = "horseshark"
  83. mf = buildModelfile(opts)
  84. expectedModelfile = `FROM {{.ParentModel}}
  85. SYSTEM """{{.System}}"""
  86. PARAMETER penalize_newline false
  87. PARAMETER seed 42
  88. PARAMETER stop [hi there]
  89. PARAMETER temperature 0.9
  90. MESSAGE user """Hey there hork!"""
  91. MESSAGE assistant """Yes it is true, I am half horse, half shark."""
  92. `
  93. tmpl, err = template.New("").Parse(expectedModelfile)
  94. require.NoError(t, err)
  95. var parentBuf bytes.Buffer
  96. err = tmpl.Execute(&parentBuf, opts)
  97. require.NoError(t, err)
  98. assert.Equal(t, parentBuf.String(), mf)
  99. }