-
Notifications
You must be signed in to change notification settings - Fork 0
/
ollama_test.go
183 lines (155 loc) · 3.82 KB
/
ollama_test.go
1
2
3
4
5
6
7
8
9
10
11
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ollama
import (
"crypto/sha256"
"fmt"
"net/url"
"strings"
"testing"
)
var LLM *Ollama
const (
_Model = "phi3.5"
_Message = "Respond only with the text \"Hello I am here to assist you.\""
)
func init() {
uri, _ := url.Parse("http://ollama.home.local:11434")
LLM = New(*uri)
}
func TestGenerateStream(t *testing.T) {
streamedResponses := make([]GenerateResponse, 0)
resp, err := LLM.Generate(
LLM.Generate.WithModel(_Model),
LLM.Generate.WithSeed(123),
LLM.Generate.WithPrompt(_Message),
LLM.Generate.WithKeepAlive("5m"),
LLM.Generate.WithTemperature(0.1),
LLM.Generate.WithStream(true, 512000, func(r *GenerateResponse, err error) {
if err != nil {
t.Errorf("Generate streaming returned an error: %s", err)
} else {
streamedResponses = append(streamedResponses, *r)
}
}),
)
if err != nil {
t.Errorf("Generate returned an error: %s", err)
return
}
result := ""
for _, response := range streamedResponses {
result += response.Response
}
if resp.Response != result {
t.Errorf("Expected \"%s\", got \"%s\"", resp.Response, result)
return
}
resp, err = LLM.Generate(
LLM.Generate.WithModel(_Model),
LLM.Generate.WithSeed(123),
LLM.Generate.WithPrompt(_Message),
LLM.Generate.WithKeepAlive("5m"),
LLM.Generate.WithTemperature(0.1),
)
if err != nil {
t.Errorf("Generate returned an error: %s", err)
return
}
if resp.Response != result {
t.Errorf("Expected \"%s\", got \"%s\"", resp.Response, result)
return
}
}
func TestChatStream(t *testing.T) {
streamedResponses := make([]ChatResponse, 0)
resp, err := LLM.Chat(
nil,
LLM.Chat.WithModel(_Model),
LLM.Chat.WithSeed(123),
LLM.Chat.WithMessage(Message{Role: pointer("user"), Content: pointer(_Message)}),
LLM.Chat.WithKeepAlive("5m"),
LLM.Chat.WithTemperature(0.1),
LLM.Chat.WithStream(true, 512000, func(r *ChatResponse, err error) {
if err != nil {
t.Errorf("Chat streaming returned an error: %s", err)
} else {
streamedResponses = append(streamedResponses, *r)
}
}),
)
if err != nil {
t.Errorf("Chat returned an error: %s", err)
return
}
result := ""
for _, response := range streamedResponses {
result += *response.Message.Content
}
if *resp.Message.Content != result {
t.Errorf("Expected \"%s\", got \"%s\"", *resp.Message.Content, result)
return
}
resp, err = LLM.Chat(
nil,
LLM.Chat.WithModel(_Model),
LLM.Chat.WithSeed(123),
LLM.Chat.WithMessage(Message{Role: pointer("user"), Content: pointer(_Message)}),
LLM.Chat.WithKeepAlive("5m"),
LLM.Chat.WithTemperature(0.1),
)
if err != nil {
t.Errorf("Chat returned an error: %s", err)
return
}
if *resp.Message.Content != result {
t.Errorf("Expected \"%s\", got \"%s\"", *resp.Message.Content, result)
return
}
}
func TestBlobs(t *testing.T) {
data := []byte{0x0, 0x1, 0xF}
digest := fmt.Sprintf("sha256:%x", sha256.Sum256(data))
err := LLM.Blobs.Create(digest, data)
if err != nil {
t.Errorf("Blobs returned an error: %s", err)
return
}
err = LLM.Blobs.Check(digest)
if err != nil {
t.Errorf("Blobs returned an error: %s", err)
return
}
}
func TestModelsActions(t *testing.T) {
err := LLM.Models.Copy(_Model, _Model+"-test")
if err != nil {
t.Errorf("Models returned an error: %s", err)
return
}
models, err := LLM.Models.List()
if err != nil {
t.Errorf("Models returned an error: %s", err)
return
}
found := false
for _, model := range models.Models {
if strings.HasPrefix(model.Name, _Model+"-test") {
found = true
}
}
if !found {
t.Errorf("Models did not find test model")
return
}
_, err = LLM.Models.ShowInfo(
LLM.Models.ShowInfo.WithModel(_Model + "-test"),
)
if err != nil {
t.Errorf("Models returned an error: %s", err)
return
}
err = LLM.Models.Delete(_Model + "-test")
if err != nil {
t.Errorf("Models returned an error: %s", err)
return
}
}