60255
Zn
Zoea-Nova / files
Zoea-Nova / internal / provider / ollama.go
12.5 KB
1package provider 2 3import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "errors" 8 "fmt" 9 "io" 10 "net/http" 11 "strings" 12 "time" 13 14 "github.com/rs/zerolog/log" 15 openai "github.com/sashabaranov/go-openai" 16) 17 18// OllamaProvider implements the Provider interface for Ollama. 19type OllamaProvider struct { 20 name string 21 client *openai.Client 22 baseURL string 23 httpClient *http.Client 24 model string 25 temperature float64 26} 27 28var ollamaRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second} 29 30// NewOllama creates a new Ollama provider. 31// Ollama exposes an OpenAI-compatible API at /v1. 32func NewOllama(endpoint, model string) *OllamaProvider { 33 return NewOllamaWithTemp("ollama", endpoint, model, 0.7) 34} 35 36func NewOllamaWithTemp(name string, endpoint, model string, temperature float64) *OllamaProvider { 37 config := openai.DefaultConfig("") 38 baseURL := strings.TrimRight(endpoint, "/") + "/v1" 39 config.BaseURL = baseURL 40 41 return &OllamaProvider{ 42 name: name, 43 client: openai.NewClientWithConfig(config), 44 baseURL: baseURL, 45 httpClient: &http.Client{}, 46 model: model, 47 temperature: temperature, 48 } 49} 50 51// Name returns the provider identifier. 52func (p *OllamaProvider) Name() string { 53 return p.name 54} 55 56// Chat sends messages and returns the complete response. 57func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string, error) { 58 resp, err := p.createChatCompletion(ctx, ollamaChatRequest{ 59 Model: p.model, 60 Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)), 61 Temperature: float32(p.temperature), 62 }) 63 if err != nil { 64 return "", err 65 } 66 67 if len(resp.Choices) == 0 { 68 return "", errors.New("no response choices") 69 } 70 71 return resp.Choices[0].Message.Content, nil 72} 73 74// ChatWithTools sends messages with available tools and returns response with potential tool calls. 75func (p *OllamaProvider) ChatWithTools(ctx context.Context, messages []Message, tools []Tool) (*ChatResponse, error) { 76 resp, err := p.createChatCompletion(ctx, ollamaChatRequest{ 77 Model: p.model, 78 Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)), 79 Tools: toOllamaTools(tools), 80 Temperature: float32(p.temperature), 81 }) 82 if err != nil { 83 return nil, err 84 } 85 86 if len(resp.Choices) == 0 { 87 return nil, errors.New("no response choices") 88 } 89 90 choice := resp.Choices[0] 91 result := &ChatResponse{ 92 Content: choice.Message.Content, 93 Reasoning: choice.Message.reasoning(), 94 } 95 96 // Extract tool calls if present 97 if len(choice.Message.ToolCalls) > 0 { 98 result.ToolCalls = make([]ToolCall, len(choice.Message.ToolCalls)) 99 for i, tc := range choice.Message.ToolCalls { 100 result.ToolCalls[i] = ToolCall{ 101 ID: tc.ID, 102 Name: tc.Function.Name, 103 Arguments: json.RawMessage(tc.Function.Arguments), 104 } 105 } 106 } 107 108 return result, nil 109} 110 111type chatCompletionResponse struct { 112 Choices []chatCompletionChoice `json:"choices"` 113} 114 115type chatCompletionChoice struct { 116 Message chatCompletionMessage `json:"message"` 117} 118 119type chatCompletionMessage struct { 120 Role string `json:"role"` 121 Content string `json:"content"` 122 Reasoning string `json:"reasoning,omitempty"` 123 ReasoningContent string `json:"reasoning_content,omitempty"` 124 ToolCalls []chatCompletionToolCall `json:"tool_calls,omitempty"` 125} 126 127type chatCompletionToolCall struct { 128 ID string `json:"id"` 129 Type string `json:"type"` 130 Function chatCompletionFunction `json:"function"` 131} 132 133type chatCompletionFunction struct { 134 Name string `json:"name"` 135 Arguments string `json:"arguments"` 136} 137 138// OLLAMA-SPECIFIC TYPES 139// These types are custom to Ollama and should NOT be used by other providers. 140// They differ from OpenAI standard in structure and field names. 141 142type ollamaChatRequest struct { 143 Model string `json:"model"` 144 Messages []ollamaReqMessage `json:"messages"` 145 Tools []ollamaReqTool `json:"tools,omitempty"` 146 Temperature float32 `json:"temperature,omitempty"` 147} 148 149type ollamaReqMessage struct { 150 Role string `json:"role"` 151 Content string `json:"content"` 152 ToolCallID string `json:"tool_call_id,omitempty"` 153 ToolCalls []ollamaReqToolCall `json:"tool_calls,omitempty"` 154} 155 156type ollamaReqTool struct { 157 Type string `json:"type"` 158 Function ollamaReqFunction `json:"function"` 159} 160 161type ollamaReqFunction struct { 162 Name string `json:"name"` 163 Description string `json:"description"` 164 Parameters map[string]interface{} `json:"parameters"` 165} 166 167type ollamaReqToolCall struct { 168 ID string `json:"id"` 169 Type string `json:"type"` 170 Function ollamaReqFuncCall `json:"function"` 171} 172 173type ollamaReqFuncCall struct { 174 Name string `json:"name"` 175 Arguments string `json:"arguments"` 176} 177 178// reasoning extracts reasoning content from Ollama response. 179// 180// OLLAMA-SPECIFIC: Ollama may return reasoning in either "reasoning" or "reasoning_content" fields. 181// This is an Ollama extension not present in standard OpenAI Chat Completions API. 182func (m chatCompletionMessage) reasoning() string { 183 if m.Reasoning != "" { 184 return m.Reasoning 185 } 186 return m.ReasoningContent 187} 188 189func (p *OllamaProvider) createChatCompletion(ctx context.Context, req ollamaChatRequest) (*chatCompletionResponse, error) { 190 body, err := json.Marshal(req) 191 if err != nil { 192 return nil, err 193 } 194 195 url := p.baseURL + "/chat/completions" 196 197 log.Debug(). 198 Str("provider", "ollama"). 199 Str("model", p.model). 200 Int("messages", len(req.Messages)). 201 Int("body_bytes", len(body)). 202 Msg("Sending request to Ollama") 203 204 maxRetries := len(ollamaRetryDelays) 205 var lastErr error 206 for attempt := 0; attempt <= maxRetries; attempt++ { 207 if attempt > 0 { 208 delay := ollamaRetryDelays[attempt-1] 209 log.Warn(). 210 Str("provider", "ollama"). 211 Int("attempt", attempt). 212 Dur("delay", delay). 213 Msg("Retrying Ollama request after transient error") 214 215 select { 216 case <-time.After(delay): 217 case <-ctx.Done(): 218 return nil, ctx.Err() 219 } 220 } 221 222 // Log at Info level for first attempt, Debug for retries 223 if attempt == 0 { 224 log.Info(). 225 Str("provider", "ollama"). 226 Str("model", p.model). 227 Msg("Ollama request started") 228 } 229 230 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) 231 if err != nil { 232 return nil, err 233 } 234 httpReq.Header.Set("Content-Type", "application/json") 235 236 resp, err := p.httpClient.Do(httpReq) 237 if err != nil { 238 // Do not retry on context cancellation or timeout 239 if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { 240 return nil, err 241 } 242 243 lastErr = err 244 continue 245 } 246 247 if resp.StatusCode == 429 || resp.StatusCode == 500 || resp.StatusCode == 502 || 248 resp.StatusCode == 503 || resp.StatusCode == 504 { 249 payload, _ := io.ReadAll(resp.Body) 250 resp.Body.Close() 251 lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload))) 252 253 log.Warn(). 254 Str("provider", "ollama"). 255 Int("status", resp.StatusCode). 256 Int("attempt", attempt+1). 257 Str("body", string(payload)). 258 Msg("Ollama retryable error") 259 continue 260 } 261 262 if resp.StatusCode < 200 || resp.StatusCode >= 300 { 263 payload, _ := io.ReadAll(resp.Body) 264 resp.Body.Close() 265 266 log.Error(). 267 Str("provider", "ollama"). 268 Int("status", resp.StatusCode). 269 Str("body", string(payload)). 270 Msg("Ollama non-retryable error") 271 272 return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload))) 273 } 274 275 bodyBytes, err := io.ReadAll(resp.Body) 276 resp.Body.Close() 277 if err != nil { 278 return nil, fmt.Errorf("read response body: %w", err) 279 } 280 281 var decoded chatCompletionResponse 282 if err := json.Unmarshal(bodyBytes, &decoded); err != nil { 283 return nil, fmt.Errorf("decode response: %w", err) 284 } 285 286 // Log successful request at Info level for visibility 287 log.Info(). 288 Str("provider", "ollama"). 289 Str("model", p.model). 290 Int("status", resp.StatusCode). 291 Int("attempt", attempt+1). 292 Msg("Ollama request successful") 293 294 return &decoded, nil 295 } 296 297 log.Error(). 298 Str("provider", "ollama"). 299 Str("model", p.model). 300 Int("max_retries", maxRetries). 301 Int("total_attempts", maxRetries+1). 302 Err(lastErr). 303 Msg("Ollama request failed after all retries") 304 return nil, fmt.Errorf("request failed after %d retries: %w", maxRetries, lastErr) 305} 306 307// Stream sends messages and returns a channel that streams response chunks. 308func (p *OllamaProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) { 309 stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{ 310 Model: p.model, 311 Messages: toOpenAIMessages(messages), 312 Temperature: float32(p.temperature), 313 }) 314 if err != nil { 315 return nil, err 316 } 317 318 ch := make(chan StreamChunk) 319 go func() { 320 defer close(ch) 321 defer stream.Close() 322 323 for { 324 resp, err := stream.Recv() 325 if errors.Is(err, io.EOF) { 326 ch <- StreamChunk{Done: true} 327 return 328 } 329 if err != nil { 330 ch <- StreamChunk{Err: err} 331 return 332 } 333 334 if len(resp.Choices) > 0 { 335 ch <- StreamChunk{Content: resp.Choices[0].Delta.Content} 336 } 337 } 338 }() 339 340 return ch, nil 341} 342 343// toOllamaMessages converts provider messages to Ollama's custom request format. 344// 345// OLLAMA-SPECIFIC: Uses custom ollamaReqMessage type instead of OpenAI SDK types. 346// Ollama has its own message format and tool call structure. 347func toOllamaMessages(messages []Message) []ollamaReqMessage { 348 result := make([]ollamaReqMessage, len(messages)) 349 for i, m := range messages { 350 msg := ollamaReqMessage{ 351 Role: m.Role, 352 Content: m.Content, 353 } 354 355 if m.ToolCallID != "" { 356 msg.ToolCallID = m.ToolCallID 357 } 358 359 if len(m.ToolCalls) > 0 { 360 msg.ToolCalls = make([]ollamaReqToolCall, len(m.ToolCalls)) 361 for j, tc := range m.ToolCalls { 362 msg.ToolCalls[j] = ollamaReqToolCall{ 363 ID: tc.ID, 364 Type: "function", 365 Function: ollamaReqFuncCall{ 366 Name: tc.Name, 367 Arguments: string(tc.Arguments), 368 }, 369 } 370 } 371 } 372 373 result[i] = msg 374 } 375 return result 376} 377 378func toOllamaTools(tools []Tool) []ollamaReqTool { 379 result := make([]ollamaReqTool, len(tools)) 380 for i, t := range tools { 381 var params map[string]interface{} 382 if len(t.Parameters) > 0 { 383 if err := json.Unmarshal(t.Parameters, ¶ms); err != nil { 384 params = map[string]interface{}{ 385 "type": "object", 386 "properties": map[string]interface{}{}, 387 } 388 } 389 } 390 if params == nil { 391 params = map[string]interface{}{ 392 "type": "object", 393 "properties": map[string]interface{}{}, 394 } 395 } 396 397 result[i] = ollamaReqTool{ 398 Type: "function", 399 Function: ollamaReqFunction{ 400 Name: t.Name, 401 Description: t.Description, 402 Parameters: params, 403 }, 404 } 405 } 406 return result 407} 408 409// mergeConsecutiveSystemMessagesOllama merges consecutive system messages into a single message. 410// 411// OLLAMA-SPECIFIC BEHAVIOR: 412// Unlike OpenAI, Ollama allows system messages anywhere in the conversation. 413// This function merges consecutive system messages IN PLACE without moving them to the start. 414// This preserves Ollama's flexible system message handling. 415// 416// DO NOT use this function for OpenAI-compatible providers - use mergeSystemMessagesOpenAI instead. 417func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaReqMessage { 418 if len(messages) == 0 { 419 return messages 420 } 421 422 result := make([]ollamaReqMessage, 0, len(messages)) 423 var systemBuffer strings.Builder 424 inSystemRun := false 425 426 for i, msg := range messages { 427 if msg.Role == "system" { 428 // Start or continue system message accumulation 429 if inSystemRun { 430 systemBuffer.WriteString("\n\n") 431 } else { 432 inSystemRun = true 433 } 434 systemBuffer.WriteString(msg.Content) 435 } else { 436 // Flush accumulated system messages if any 437 if inSystemRun { 438 result = append(result, ollamaReqMessage{ 439 Role: "system", 440 Content: systemBuffer.String(), 441 }) 442 systemBuffer.Reset() 443 inSystemRun = false 444 } 445 // Add non-system message 446 result = append(result, msg) 447 } 448 449 // Flush at end of array 450 if i == len(messages)-1 && inSystemRun { 451 result = append(result, ollamaReqMessage{ 452 Role: "system", 453 Content: systemBuffer.String(), 454 }) 455 } 456 } 457 458 log.Debug(). 459 Int("original_count", len(messages)). 460 Int("merged_count", len(result)). 461 Msg("Ollama: Merged consecutive system messages") 462 463 return result 464} 465 466// Close closes idle HTTP connections 467func (p *OllamaProvider) Close() error { 468 if p.httpClient != nil { 469 p.httpClient.CloseIdleConnections() 470 } 471 return nil 472}