-
Notifications
You must be signed in to change notification settings - Fork 1
/
ollamatea_embedding.go
293 lines (246 loc) · 7.61 KB
/
ollamatea_embedding.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// OllamaTea Copyright (c) 2024 Neomantra Corp
package ollamatea
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
tea "github.com/charmbracelet/bubbletea"
ollama "github.com/ollama/ollama/api"
)
//////////////////////////////////////////////////////////////////////////////
// BubbleTea messages
type StartEmbedMsg struct {
ID int64 // ID is the session ID to start
}
type StopEmbedMsg struct {
ID int64 // ID is the session ID to stop
}
// EmbedResponseMsg is the message generated each time there is a reply from Ollama.
// The information contained is only partial.
// To check what has been received so far in the request, check [Session.Response()]
// To focus solely on full responses, listen for GenerateDoneMsg.
type EmbedResponseMsg struct {
ID int64 // ID is the generation session ID corresponding to the Response
CreatedAt time.Time // CreatedAt is the timestamp of the response.
Response ollama.EmbedResponse // Response is the EmbedResponse from Ollama
}
// EmbedErrorMsg is the message generated when the generation is complete.
// It contains the complete response along with [Context], which may be set on
// [Session.Context] to carry on the conversation.
type EmbedErrorMsg struct {
ID int64 // ID is the generation session ID corresponding to the Response
CreatedAt time.Time // CreatedAt is the timestamp of the response.
Error error // Error is the reason the model stopped generating text.
}
///////////////////////////////////////////////////////////////////////////////
// EmbedSession holds the data for an OllamaTea Embed, both its request and response
// See https://github.com/ollama/ollama/blob/main/api/types.go#L248
type EmbedSession struct {
Host string // Ollama Host -- really the service's URL
Model string // Ollama LLM model. See https://ollama.com/library
Options map[string]interface{} // Options lists model-specific options.
Input any // Input is the input to embed.
KeepAlive *time.Duration // KeepAlive controls how long the model will stay loaded in memory following this request.
Truncate *bool // Truncate the end of each input to fit within context length
// Private
ctx context.Context
cancelFunc context.CancelFunc
id int64 // Unique Session ID
lastError error // Last error
isEmbedding bool // Currently inferencing? Only one per session
response *ollama.EmbedResponse // Ollama embed response
}
// NewEmbedSession returns a new Session with the default values.
func NewEmbedSession(opts ...EmbedOption) EmbedSession {
s := EmbedSession{
Host: DefaultHost(),
Model: DefaultModel(),
Input: nil,
id: nextSessionID(),
isEmbedding: false,
}
for _, opt := range opts {
opt(&s)
}
return s
}
// EmbedOption is a functional option for configuring a EmbedSession.
// Various With* functions, such as WitHost, are available to set specific fields.
type EmbedOption func(*EmbedSession)
// WithHost is an EmbedOption to set the Host field.
func WithHost(host string) EmbedOption {
return func(s *EmbedSession) {
s.Host = host
}
}
// WithModel is an EmbedOption to set the Model field.
func WithModel(model string) EmbedOption {
return func(s *EmbedSession) {
s.Model = model
}
}
// WithInput is an EmbedOption to set the Input field.
func WithInput(input any) EmbedOption {
return func(s *EmbedSession) {
s.Input = input
}
}
// WithKeepAlive is an EmbedOption to set the Duration of the KeepAlive field.
func WithKeepAlive(d time.Duration) EmbedOption {
return func(s *EmbedSession) {
if d == 0 {
s.KeepAlive = nil
} else {
s.KeepAlive = &d
}
}
}
// WithTruncate is an EmbedOption to indicate truncation.
func WithTruncate(trunc bool) EmbedOption {
return func(s *EmbedSession) {
s.Truncate = &trunc
}
}
// ID returns the ID of the EmbedSession
func (s *EmbedSession) ID() int64 {
return s.id
}
// IsEmbedding returns whether the EmbedSession is currently embedding
func (s *EmbedSession) IsEmbedding() bool {
return s.isEmbedding
}
// Response returns the last EmbedResponse, if any.
func (s *EmbedSession) Response() *ollama.EmbedResponse {
return s.response
}
// Error returns the last error, if any
func (s *EmbedSession) Error() error {
return s.lastError
}
// ClearReponse clears the current response
func (s *EmbedSession) ClearResponse() {
s.response = nil
}
// ClearError clears the current error
func (s *EmbedSession) ClearError() {
s.lastError = nil
}
// StartEmbedMsg returns a StartEmbedMsg for the EmbedSession
func (s *EmbedSession) StartEmbedMsg() tea.Msg {
return StartEmbedMsg{ID: s.id}
}
// StartEmbedCmd returns a command to start emebedding for the EmbedSession
func (s *EmbedSession) StartEmbedCmd() tea.Cmd {
return func() tea.Msg {
return StartEmbedMsg{ID: s.id}
}
}
//////////////////////////////////////////////////////////////////////////////
// BubbleTea interface
// Init handles the initialization of an EmbedSession
// Currently does nothing
func (m *EmbedSession) Init() tea.Cmd {
return nil
}
// Update handles BubbleTea messages for the EmbedSession
// This is for starting/stopping/updating generation.
func (m *EmbedSession) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case StartEmbedMsg:
if msg.ID != m.id {
return m, nil
}
if m.isEmbedding {
// Cancel current inference
if m.cancelFunc != nil {
m.cancelFunc()
m.cancelFunc = nil
}
m.ctx = nil
m.isEmbedding = false
}
return m, m.startEmbeddingCmd()
case StopEmbedMsg:
if msg.ID != m.id {
return m, nil
}
if m.cancelFunc != nil {
m.cancelFunc()
m.cancelFunc = nil
}
m.ctx = nil
m.isEmbedding = false
return m, nil
case EmbedResponseMsg:
m.response = &msg.Response
m.lastError = nil
return m, nil
case EmbedErrorMsg:
m.response = nil
m.lastError = msg.Error
return m, nil
}
return m, nil
}
// View renders the Sessions's view.
// This is will either be an error message, a "..." waiting string, or the Ollama response.
// We often set up other components for the TUI chrome and ignore this View.
func (m *EmbedSession) View() string {
if m.lastError != nil {
return fmt.Sprintf("ERROR: %s", m.lastError.Error())
}
return "" // m.Response()
}
//////////////////////////////////////////////////////////////////////////////
// startEmbeddingCmd is a tea.Msg wrapper for startEmbedding
func (s *EmbedSession) startEmbeddingCmd() tea.Cmd {
return func() tea.Msg {
return s.startEmbedding()
}
}
// startEmbedding starts embedding for a Session
// Performs the actual Ollama /embed call
func (s *EmbedSession) startEmbedding() tea.Msg {
if s.isEmbedding {
return nil
}
s.isEmbedding = true
s.ctx, s.cancelFunc = context.WithCancel(context.Background())
ollamaURL, err := url.Parse(s.Host)
if err != nil {
s.lastError = err
s.isEmbedding = false
return makeEmbedErrorMsg(s.id, err)
}
ollamaClient := ollama.NewClient(ollamaURL, http.DefaultClient)
req := &ollama.EmbedRequest{
Model: s.Model,
Input: s.Input,
// TODO KeepAlive: &ollama.Duration{},
// TODO Truncate: new(bool),
Options: s.Options,
}
resp, err := ollamaClient.Embed(s.ctx, req)
if err != nil {
s.lastError = err
return makeEmbedErrorMsg(s.id, err)
}
return makeEmbedResponseMsg(s.id, resp)
}
//////////////////////////////////////////////////////////////////////////////
func makeEmbedResponseMsg(id int64, resp *ollama.EmbedResponse) tea.Msg {
return EmbedResponseMsg{
ID: id,
CreatedAt: time.Now(),
Response: *resp,
}
}
func makeEmbedErrorMsg(id int64, err error) tea.Msg {
return EmbedErrorMsg{
ID: id,
CreatedAt: time.Now(),
Error: err,
}
}