interactive_test.go 3.5 KB

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