interactive_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Template: "This is a template.",
  57. Messages: []api.Message{
  58. {Role: "user", Content: "Hey there hork!"},
  59. {Role: "assistant", Content: "Yes it is true, I am half horse, half shark."},
  60. },
  61. Options: map[string]interface{}{},
  62. }
  63. opts.Options["temperature"] = 0.9
  64. opts.Options["seed"] = 42
  65. opts.Options["penalize_newline"] = false
  66. opts.Options["stop"] = []string{"hi", "there"}
  67. mf := buildModelfile(opts)
  68. expectedModelfile := `FROM {{.Model}}
  69. SYSTEM """{{.System}}"""
  70. TEMPLATE """{{.Template}}"""
  71. PARAMETER penalize_newline false
  72. PARAMETER seed 42
  73. PARAMETER stop [hi there]
  74. PARAMETER temperature 0.9
  75. MESSAGE user """Hey there hork!"""
  76. MESSAGE assistant """Yes it is true, I am half horse, half shark."""
  77. `
  78. tmpl, err := template.New("").Parse(expectedModelfile)
  79. require.NoError(t, err)
  80. var buf bytes.Buffer
  81. err = tmpl.Execute(&buf, opts)
  82. require.NoError(t, err)
  83. assert.Equal(t, buf.String(), mf)
  84. opts.ParentModel = "horseshark"
  85. mf = buildModelfile(opts)
  86. expectedModelfile = `FROM {{.ParentModel}}
  87. SYSTEM """{{.System}}"""
  88. TEMPLATE """{{.Template}}"""
  89. PARAMETER penalize_newline false
  90. PARAMETER seed 42
  91. PARAMETER stop [hi there]
  92. PARAMETER temperature 0.9
  93. MESSAGE user """Hey there hork!"""
  94. MESSAGE assistant """Yes it is true, I am half horse, half shark."""
  95. `
  96. tmpl, err = template.New("").Parse(expectedModelfile)
  97. require.NoError(t, err)
  98. var parentBuf bytes.Buffer
  99. err = tmpl.Execute(&parentBuf, opts)
  100. require.NoError(t, err)
  101. assert.Equal(t, parentBuf.String(), mf)
  102. }