6119
My
mysis / files
mysis / internal / provider / ollama.go
12.9 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 if err := resp.Body.Close(); err != nil { 251 log.Warn().Err(err).Msg("Failed to close response body") 252 } 253 lastErr = fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload))) 254 255 log.Warn(). 256 Str("provider", "ollama"). 257 Int("status", resp.StatusCode). 258 Int("attempt", attempt+1). 259 Str("body", string(payload)). 260 Msg("Ollama retryable error") 261 continue 262 } 263 264 if resp.StatusCode < 200 || resp.StatusCode >= 300 { 265 payload, _ := io.ReadAll(resp.Body) 266 if err := resp.Body.Close(); err != nil { 267 log.Warn().Err(err).Msg("Failed to close response body") 268 } 269 270 log.Error(). 271 Str("provider", "ollama"). 272 Int("status", resp.StatusCode). 273 Str("body", string(payload)). 274 Msg("Ollama non-retryable error") 275 276 return nil, fmt.Errorf("chat completion status %d: %s", resp.StatusCode, strings.TrimSpace(string(payload))) 277 } 278 279 bodyBytes, readErr := io.ReadAll(resp.Body) 280 if err := resp.Body.Close(); err != nil { 281 log.Warn().Err(err).Msg("Failed to close response body") 282 } 283 if readErr != nil { 284 return nil, fmt.Errorf("read response body: %w", readErr) 285 } 286 287 var decoded chatCompletionResponse 288 if err := json.Unmarshal(bodyBytes, &decoded); err != nil { 289 return nil, fmt.Errorf("decode response: %w", err) 290 } 291 292 // Log successful request at Info level for visibility 293 log.Info(). 294 Str("provider", "ollama"). 295 Str("model", p.model). 296 Int("status", resp.StatusCode). 297 Int("attempt", attempt+1). 298 Msg("Ollama request successful") 299 300 return &decoded, nil 301 } 302 303 log.Error(). 304 Str("provider", "ollama"). 305 Str("model", p.model). 306 Int("max_retries", maxRetries). 307 Int("total_attempts", maxRetries+1). 308 Err(lastErr). 309 Msg("Ollama request failed after all retries") 310 return nil, fmt.Errorf("request failed after %d retries: %w", maxRetries, lastErr) 311} 312 313// Stream sends messages and returns a channel that streams response chunks. 314func (p *OllamaProvider) Stream(ctx context.Context, messages []Message) (<-chan StreamChunk, error) { 315 stream, err := p.client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{ 316 Model: p.model, 317 Messages: toOpenAIMessages(messages), 318 Temperature: float32(p.temperature), 319 }) 320 if err != nil { 321 return nil, err 322 } 323 324 ch := make(chan StreamChunk) 325 go func() { 326 defer close(ch) 327 defer func() { 328 if err := stream.Close(); err != nil { 329 log.Warn().Err(err).Msg("Failed to close stream") 330 } 331 }() 332 333 for { 334 resp, err := stream.Recv() 335 if errors.Is(err, io.EOF) { 336 ch <- StreamChunk{Done: true} 337 return 338 } 339 if err != nil { 340 ch <- StreamChunk{Err: err} 341 return 342 } 343 344 if len(resp.Choices) > 0 { 345 ch <- StreamChunk{Content: resp.Choices[0].Delta.Content} 346 } 347 } 348 }() 349 350 return ch, nil 351} 352 353// toOllamaMessages converts provider messages to Ollama's custom request format. 354// 355// OLLAMA-SPECIFIC: Uses custom ollamaReqMessage type instead of OpenAI SDK types. 356// Ollama has its own message format and tool call structure. 357func toOllamaMessages(messages []Message) []ollamaReqMessage { 358 result := make([]ollamaReqMessage, len(messages)) 359 for i, m := range messages { 360 msg := ollamaReqMessage{ 361 Role: m.Role, 362 Content: m.Content, 363 } 364 365 if m.ToolCallID != "" { 366 msg.ToolCallID = m.ToolCallID 367 } 368 369 if len(m.ToolCalls) > 0 { 370 msg.ToolCalls = make([]ollamaReqToolCall, len(m.ToolCalls)) 371 for j, tc := range m.ToolCalls { 372 msg.ToolCalls[j] = ollamaReqToolCall{ 373 ID: tc.ID, 374 Type: "function", 375 Function: ollamaReqFuncCall{ 376 Name: tc.Name, 377 Arguments: string(tc.Arguments), 378 }, 379 } 380 } 381 } 382 383 result[i] = msg 384 } 385 return result 386} 387 388func toOllamaTools(tools []Tool) []ollamaReqTool { 389 result := make([]ollamaReqTool, len(tools)) 390 for i, t := range tools { 391 var params map[string]interface{} 392 if len(t.Parameters) > 0 { 393 if err := json.Unmarshal(t.Parameters, ¶ms); err != nil { 394 params = map[string]interface{}{ 395 "type": "object", 396 "properties": map[string]interface{}{}, 397 } 398 } 399 } 400 if params == nil { 401 params = map[string]interface{}{ 402 "type": "object", 403 "properties": map[string]interface{}{}, 404 } 405 } 406 407 result[i] = ollamaReqTool{ 408 Type: "function", 409 Function: ollamaReqFunction{ 410 Name: t.Name, 411 Description: t.Description, 412 Parameters: params, 413 }, 414 } 415 } 416 return result 417} 418 419// mergeConsecutiveSystemMessagesOllama merges consecutive system messages into a single message. 420// 421// OLLAMA-SPECIFIC BEHAVIOR: 422// Unlike OpenAI, Ollama allows system messages anywhere in the conversation. 423// This function merges consecutive system messages IN PLACE without moving them to the start. 424// This preserves Ollama's flexible system message handling. 425// 426// DO NOT use this function for OpenAI-compatible providers - use mergeSystemMessagesOpenAI instead. 427func mergeConsecutiveSystemMessagesOllama(messages []ollamaReqMessage) []ollamaReqMessage { 428 if len(messages) == 0 { 429 return messages 430 } 431 432 result := make([]ollamaReqMessage, 0, len(messages)) 433 var systemBuffer strings.Builder 434 inSystemRun := false 435 436 for i, msg := range messages { 437 if msg.Role == "system" { 438 // Start or continue system message accumulation 439 if inSystemRun { 440 systemBuffer.WriteString("\n\n") 441 } else { 442 inSystemRun = true 443 } 444 systemBuffer.WriteString(msg.Content) 445 } else { 446 // Flush accumulated system messages if any 447 if inSystemRun { 448 result = append(result, ollamaReqMessage{ 449 Role: "system", 450 Content: systemBuffer.String(), 451 }) 452 systemBuffer.Reset() 453 inSystemRun = false 454 } 455 // Add non-system message 456 result = append(result, msg) 457 } 458 459 // Flush at end of array 460 if i == len(messages)-1 && inSystemRun { 461 result = append(result, ollamaReqMessage{ 462 Role: "system", 463 Content: systemBuffer.String(), 464 }) 465 } 466 } 467 468 log.Debug(). 469 Int("original_count", len(messages)). 470 Int("merged_count", len(result)). 471 Msg("Ollama: Merged consecutive system messages") 472 473 return result 474} 475 476// Close closes idle HTTP connections 477func (p *OllamaProvider) Close() error { 478 if p.httpClient != nil { 479 p.httpClient.CloseIdleConnections() 480 } 481 return nil 482}