@@ -1,32 +1,23 @@
11 package provider
22
33 import (
4- "bytes"
54 "context"
65 "encoding/json"
7- "errors"
8- "fmt"
9- "io"
106 "net/http"
117 "strings"
12- "time"
138
149 "github.com/rs/zerolog/log"
15- openai "github.com/sashabaranov/go-openai"
1610 )
1711
1812 // OllamaProvider implements the Provider interface for Ollama.
1913 type OllamaProvider struct {
2014 name string
21- client *openai.Client
2215 baseURL string
2316 httpClient *http.Client
2417 model string
2518 temperature float64
2619 }
2720
28-var ollamaRetryDelays = []time.Duration{5 * time.Second, 10 * time.Second, 15 * time.Second}
29-
3021 // NewOllama creates a new Ollama provider.
3122 // Ollama exposes an OpenAI-compatible API at /v1.
3223 func NewOllama(endpoint, model string) *OllamaProvider {
@@ -34,13 +25,10 @@ func NewOllama(endpoint, model string) *OllamaProvider {
3425 }
3526
3627 func NewOllamaWithTemp(name string, endpoint, model string, temperature float64) *OllamaProvider {
37- config := openai.DefaultConfig("")
3828 baseURL := strings.TrimRight(endpoint, "/") + "/v1"
39- config.BaseURL = baseURL
4029
4130 return &OllamaProvider{
4231 name: name,
43- client: openai.NewClientWithConfig(config),
4432 baseURL: baseURL,
4533 httpClient: &http.Client{},
4634 model: model,
@@ -53,86 +41,39 @@ func (p *OllamaProvider) Name() string {
5341 return p.name
5442 }
5543
56-// Chat sends messages and returns the complete response.
57-func (p *OllamaProvider) Chat(ctx context.Context, messages []Message) (string, error) {
58- resp, err := p.createChatCompletion(ctx, ollamaChatRequest{
44+// ChatStream sends messages with optional tools and returns a channel of streaming events.
45+func (p *OllamaProvider) ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
46+ req := ollamaChatRequest{
5947 Model: p.model,
6048 Messages: mergeConsecutiveSystemMessagesOllama(toOllamaMessages(messages)),
49+ Tools: toOllamaTools(tools),
6150 Temperature: float32(p.temperature),
62- })
63- if err != nil {
64- return "", err
51+ Stream: true,
6552 }
66-
67- if len(resp.Choices) == 0 {
68- return "", errors.New("no response choices")
53+ body, err := json.Marshal(req)
54+ if err != nil {
55+ return nil, err
6956 }
7057
71- return resp.Choices[0].Message.Content, nil
72-}
73-
74-// ChatWithTools sends messages with available tools and returns response with potential tool calls.
75-func (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),
58+ reader, err := httpDoSSE(ctx, httpRequestConfig{
59+ client: p.httpClient,
60+ url: p.baseURL + "/chat/completions",
61+ body: body,
62+ provider: "ollama",
63+ model: p.model,
8164 })
8265 if err != nil {
8366 return nil, err
8467 }
8568
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-
111-type chatCompletionResponse struct {
112- Choices []chatCompletionChoice `json:"choices"`
113-}
114-
115-type chatCompletionChoice struct {
116- Message chatCompletionMessage `json:"message"`
117-}
118-
119-type 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-
127-type chatCompletionToolCall struct {
128- ID string `json:"id"`
129- Type string `json:"type"`
130- Function chatCompletionFunction `json:"function"`
131-}
69+ ch := make(chan StreamEvent)
70+ go func() {
71+ defer close(ch)
72+ defer reader.Close()
73+ parseSSEStream(ctx, reader, ch)
74+ }()
13275
133-type chatCompletionFunction struct {
134- Name string `json:"name"`
135- Arguments string `json:"arguments"`
76+ return ch, nil
13677 }
13778
13879 // OLLAMA-SPECIFIC TYPES
@@ -144,6 +85,7 @@ type ollamaChatRequest struct {
14485 Messages []ollamaReqMessage `json:"messages"`
14586 Tools []ollamaReqTool `json:"tools,omitempty"`
14687 Temperature float32 `json:"temperature,omitempty"`
88+ Stream bool `json:"stream"`
14789 }
14890
14991 type ollamaReqMessage struct {
@@ -175,181 +117,6 @@ type ollamaReqFuncCall struct {
175117 Arguments string `json:"arguments"`
176118 }
177119
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.
182-func (m chatCompletionMessage) reasoning() string {
183- if m.Reasoning != "" {
184- return m.Reasoning
185- }
186- return m.ReasoningContent
187-}
188-
189-func (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.
314-func (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-
353120 // toOllamaMessages converts provider messages to Ollama's custom request format.
354121 //
355122 // OLLAMA-SPECIFIC: Uses custom ollamaReqMessage type instead of OpenAI SDK types.