interactive_test.go 3.52 KB
Newer Older
1
2
3
package cmd

import (
4
	"bytes"
5
	"testing"
6
	"text/template"
7
8

	"github.com/stretchr/testify/assert"
Michael Yang's avatar
lint  
Michael Yang committed
9
	"github.com/stretchr/testify/require"
10

11
	"github.com/ollama/ollama/api"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
)

func TestExtractFilenames(t *testing.T) {
	// Unix style paths
	input := ` some preamble 
 ./relative\ path/one.png inbetween1 ./not a valid two.jpg inbetween2
/unescaped space /three.jpeg inbetween3 /valid\ path/dir/four.png "./quoted with spaces/five.svg`
	res := extractFileNames(input)
	assert.Len(t, res, 5)
	assert.Contains(t, res[0], "one.png")
	assert.Contains(t, res[1], "two.jpg")
	assert.Contains(t, res[2], "three.jpeg")
	assert.Contains(t, res[3], "four.png")
	assert.Contains(t, res[4], "five.svg")
	assert.NotContains(t, res[4], '"')
	assert.NotContains(t, res, "inbtween")

	// Windows style paths
	input = ` some preamble
 c:/users/jdoe/one.png inbetween1 c:/program files/someplace/two.jpg inbetween2 
 /absolute/nospace/three.jpeg inbetween3 /absolute/with space/four.png inbetween4
./relative\ path/five.svg inbetween5 "./relative with/spaces/six.png inbetween6
d:\path with\spaces\seven.svg inbetween7 c:\users\jdoe\eight.png inbetween8 
 d:\program files\someplace\nine.png inbetween9 "E:\program files\someplace\ten.svg some ending
`
	res = extractFileNames(input)
	assert.Len(t, res, 10)
	assert.NotContains(t, res, "inbtween")
	assert.Contains(t, res[0], "one.png")
	assert.Contains(t, res[0], "c:")
	assert.Contains(t, res[1], "two.jpg")
	assert.Contains(t, res[1], "c:")
	assert.Contains(t, res[2], "three.jpeg")
	assert.Contains(t, res[3], "four.png")
	assert.Contains(t, res[4], "five.svg")
	assert.Contains(t, res[5], "six.png")
	assert.Contains(t, res[6], "seven.svg")
	assert.Contains(t, res[6], "d:")
	assert.Contains(t, res[7], "eight.png")
	assert.Contains(t, res[7], "c:")
	assert.Contains(t, res[8], "nine.png")
	assert.Contains(t, res[8], "d:")
	assert.Contains(t, res[9], "ten.svg")
	assert.Contains(t, res[9], "E:")
}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

func TestModelfileBuilder(t *testing.T) {
	opts := runOptions{
		Model:    "hork",
		System:   "You are part horse and part shark, but all hork. Do horklike things",
		Template: "This is a template.",
		Messages: []api.Message{
			{Role: "user", Content: "Hey there hork!"},
			{Role: "assistant", Content: "Yes it is true, I am half horse, half shark."},
		},
		Options: map[string]interface{}{},
	}

	opts.Options["temperature"] = 0.9
	opts.Options["seed"] = 42
	opts.Options["penalize_newline"] = false
	opts.Options["stop"] = []string{"hi", "there"}

	mf := buildModelfile(opts)
	expectedModelfile := `FROM {{.Model}}
SYSTEM """{{.System}}"""
TEMPLATE """{{.Template}}"""
PARAMETER penalize_newline false
PARAMETER seed 42
PARAMETER stop [hi there]
PARAMETER temperature 0.9

MESSAGE user """Hey there hork!"""
MESSAGE assistant """Yes it is true, I am half horse, half shark."""
`

	tmpl, err := template.New("").Parse(expectedModelfile)
Michael Yang's avatar
lint  
Michael Yang committed
89
	require.NoError(t, err)
90
91
92

	var buf bytes.Buffer
	err = tmpl.Execute(&buf, opts)
Michael Yang's avatar
lint  
Michael Yang committed
93
	require.NoError(t, err)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	assert.Equal(t, buf.String(), mf)

	opts.ParentModel = "horseshark"
	mf = buildModelfile(opts)
	expectedModelfile = `FROM {{.ParentModel}}
SYSTEM """{{.System}}"""
TEMPLATE """{{.Template}}"""
PARAMETER penalize_newline false
PARAMETER seed 42
PARAMETER stop [hi there]
PARAMETER temperature 0.9

MESSAGE user """Hey there hork!"""
MESSAGE assistant """Yes it is true, I am half horse, half shark."""
`

	tmpl, err = template.New("").Parse(expectedModelfile)
Michael Yang's avatar
lint  
Michael Yang committed
111
	require.NoError(t, err)
112
113
114

	var parentBuf bytes.Buffer
	err = tmpl.Execute(&parentBuf, opts)
Michael Yang's avatar
lint  
Michael Yang committed
115
	require.NoError(t, err)
116
117
	assert.Equal(t, parentBuf.String(), mf)
}