-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathollama.go
More file actions
376 lines (329 loc) · 11.1 KB
/
Copy pathollama.go
File metadata and controls
376 lines (329 loc) · 11.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package deepseek
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
utils "github.com/cohesion-org/deepseek-go/utils"
api "github.com/ollama/ollama/api"
)
// IsOllamaRunning checks if the Ollama server is running by sending a GET request to the API
// endpoint. It returns true if the server is running and responds with a 200 OK status.
// http://localhost:11434/api/tags is the endpoint to check if the server is running.
func IsOllamaRunning() bool {
client := http.Client{
Timeout: 2 * time.Second,
}
resp, err := client.Get("http://localhost:11434/api/tags")
if err != nil {
return false
}
defer func() {
_ = resp.Body.Close()
}()
return resp.StatusCode == http.StatusOK
}
// OllamaStreamResponse represents the response format from Ollama when streaming chat completions.
type OllamaStreamResponse struct {
Model string `json:"model"` // Name of the model used for the response
CreatedAt string `json:"created_at"` // Timestamp when the response was created
Message api.Message `json:"message"` // Message content and role from Ollama's API
Done bool `json:"done"` // Indicates if the stream is finished
DoneReason string `json:"done_reason,omitempty"` // Optional reason for completion (e.g., "stop", "length")
TotalDuration int64 `json:"total_duration,omitempty"` // Total time taken for the request (nanoseconds)
LoadDuration int64 `json:"load_duration,omitempty"` // Time spent loading the model (nanoseconds)
PromptEvalCount int64 `json:"prompt_eval_count,omitempty"` // Number of tokens evaluated in the prompt
PromptEvalDuration int64 `json:"prompt_eval_duration,omitempty"` // Time spent evaluating the prompt (nanoseconds)
EvalCount int64 `json:"eval_count,omitempty"` // Number of tokens generated in the response
EvalDuration int64 `json:"eval_duration,omitempty"` // Time spent generating the response (nanoseconds)
}
// ollamaCompletionStream implements the ChatCompletionStream interface for Ollama.
type ollamaCompletionStream struct {
ctx context.Context // Context for stream cancellation
cancel context.CancelFunc // Function to cancel the context
resp *http.Response // HTTP response from the Ollama API
reader *bufio.Reader // Buffered reader for streaming response
}
// convertToOllamaMessages converts deepseek messages to ollama format
func convertToOllamaMessages(messages []ChatCompletionMessage) []api.Message {
converted := make([]api.Message, len(messages))
for i, msg := range messages {
converted[i] = api.Message{
Role: msg.Role,
Content: msg.Content,
}
}
return converted
}
// convertToOllamaMessages converts deepseek messages to ollama format
func convertToOllamaMessagesWithImage(messages []ChatCompletionMessageWithImage) (err error, _ []api.Message) {
converted := make([]api.Message, len(messages))
for i, msg := range messages {
switch content := msg.Content.(type) {
case []ContentItem:
// Join all text items into a single string
var text []string
var imageData []api.ImageData
for _, item := range content {
if item.Type == "text" {
text = append(text, item.Text)
} else if item.Type == "image_url" && item.Image != nil {
if urlStr, ok := item.Image.URL.(string); ok {
// Extract base64 data after the comma
parts := strings.Split(urlStr, ",")
if len(parts) != 2 {
return fmt.Errorf("invalid image URL format"), nil
}
base64Data, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return fmt.Errorf("error decoding to base64 %w", err), nil
}
imageData = append(imageData, base64Data)
}
}
}
converted[i] = api.Message{
Role: msg.Role,
Content: strings.Join(text, "\n"),
Images: imageData,
}
case string:
converted[i] = api.Message{
Role: msg.Role,
Content: content,
}
default:
return fmt.Errorf("unsupported content type: %T", content), nil
}
}
return nil, converted
}
// convertToDeepseekResponse converts ollama response to deepseek format
func convertToDeepseekResponse(response api.ChatResponse) *ChatCompletionResponse {
return &ChatCompletionResponse{
Model: response.Model,
Created: response.CreatedAt.Unix(),
Choices: []Choice{
{
Message: Message{
Role: response.Message.Role,
Content: response.Message.Content,
},
FinishReason: response.DoneReason,
},
},
Usage: Usage{
TotalTokens: response.PromptEvalCount + response.EvalCount,
},
}
}
// CreateOllamaChatCompletion sends a chat completion request to the Ollama API
// Note from maintainer: This is a wrapper around the Ollama API. It is not a direct implementation of deepseek-go.
func CreateOllamaChatCompletion(req *ChatCompletionRequest) (ChatCompletionResponse, error) {
if !IsOllamaRunning() {
return ChatCompletionResponse{}, fmt.Errorf("Ollama server is not running")
}
if req == nil {
return ChatCompletionResponse{}, fmt.Errorf("request cannot be nil")
}
client, err := api.ClientFromEnvironment()
if err != nil {
return ChatCompletionResponse{}, fmt.Errorf("failed to create client: %w", err)
}
var lastResponse api.ChatResponse
response := func(response api.ChatResponse) error {
lastResponse = response
return nil
}
stream := false
err = client.Chat(context.Background(), &api.ChatRequest{
Model: req.Model,
Messages: convertToOllamaMessages(req.Messages),
Stream: &stream,
}, response)
if err != nil {
return ChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
}
convertedResponse := convertToDeepseekResponse(lastResponse)
return *convertedResponse, nil
}
// CreateOllamaChatCompletionStream sends a chat completion request with stream enabled and returns the delta stream.
func CreateOllamaChatCompletionStream(
ctx context.Context,
request *StreamChatCompletionRequest,
) (*ollamaCompletionStream, error) {
if !IsOllamaRunning() {
return &ollamaCompletionStream{}, fmt.Errorf("Ollama server is not running")
}
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
c := Client{
BaseURL: "http://localhost:11434",
}
var s = true
// Convert messages to Ollama format
ollamaRequest := &api.ChatRequest{
Model: request.Model,
Messages: convertToOllamaMessages(request.Messages),
Stream: &s,
}
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(c.BaseURL).
SetPath("/api/chat/").
SetBodyFromStruct(ollamaRequest).
Build(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(c, req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
ctx, cancel := context.WithCancel(ctx)
stream := &ollamaCompletionStream{
ctx: ctx,
cancel: cancel,
resp: resp,
reader: bufio.NewReader(resp.Body),
}
return stream, nil
}
// Recv receives the next response from the Ollama stream
func (s *ollamaCompletionStream) Recv() (*StreamChatCompletionResponse, error) {
reader := s.reader
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
return nil, io.EOF
}
return nil, fmt.Errorf("error reading stream: %w", err)
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
var ollamaResp OllamaStreamResponse
if err := json.Unmarshal([]byte(line), &ollamaResp); err != nil {
return nil, fmt.Errorf("unmarshal error: %w, raw data: %s", err, line)
}
// Convert Ollama response to StreamChatCompletionResponse format
response := &StreamChatCompletionResponse{
Model: ollamaResp.Model,
Choices: []StreamChoices{
{
Index: 0,
Delta: StreamDelta{
Content: ollamaResp.Message.Content,
Role: ollamaResp.Message.Role,
},
FinishReason: ollamaResp.DoneReason,
},
},
}
if ollamaResp.Done && ollamaResp.Message.Content == "" {
return nil, io.EOF
}
return response, nil
}
}
// Close terminates the Ollama stream
func (s *ollamaCompletionStream) Close() error {
s.cancel()
err := s.resp.Body.Close()
if err != nil {
return fmt.Errorf("failed to close response body: %w", err)
}
return nil
}
// CreateOllamaChatCompletionWithImage sends a chat completion request with image to the Ollama API
// Note from maintainer: This is a wrapper around the Ollama API. It is not a direct implementation of deepseek-go.
func CreateOllamaChatCompletionWithImage(req *ChatCompletionRequestWithImage) (ChatCompletionResponse, error) {
if !IsOllamaRunning() {
return ChatCompletionResponse{}, fmt.Errorf("Ollama server is not running")
}
if req == nil {
return ChatCompletionResponse{}, fmt.Errorf("request cannot be nil")
}
client, err := api.ClientFromEnvironment()
if err != nil {
return ChatCompletionResponse{}, fmt.Errorf("failed to create client: %w", err)
}
var lastResponse api.ChatResponse
response := func(response api.ChatResponse) error {
lastResponse = response
return nil
}
stream := false
err, messages := convertToOllamaMessagesWithImage(req.Messages)
if err != nil {
return ChatCompletionResponse{}, fmt.Errorf("error converting messages: %w", err)
}
err = client.Chat(context.Background(), &api.ChatRequest{
Model: req.Model,
Messages: messages,
Stream: &stream,
}, response)
if err != nil {
return ChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
}
convertedResponse := convertToDeepseekResponse(lastResponse)
return *convertedResponse, nil
}
// CreateOllamaChatCompletionStreamWithImage sends a streaming chat completion request with image content and returns the delta stream.
func CreateOllamaChatCompletionStreamWithImage(
ctx context.Context,
request *StreamChatCompletionRequestWithImage,
) (*ollamaCompletionStream, error) {
if !IsOllamaRunning() {
return &ollamaCompletionStream{}, fmt.Errorf("Ollama server is not running")
}
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
err, messages := convertToOllamaMessagesWithImage(request.Messages)
if err != nil {
return nil, fmt.Errorf("error converting messages: %w", err)
}
var s = true
ollamaRequest := &api.ChatRequest{
Model: request.Model,
Messages: messages,
Stream: &s,
}
c := Client{
BaseURL: "http://localhost:11434",
}
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(c.BaseURL).
SetPath("/api/chat/").
SetBodyFromStruct(ollamaRequest).
Build(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(c, req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
ctx, cancel := context.WithCancel(ctx)
stream := &ollamaCompletionStream{
ctx: ctx,
cancel: cancel,
resp: resp,
reader: bufio.NewReader(resp.Body),
}
return stream, nil
}