fix(ollama): improve error handling and add timeouts
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

- Add timeouts (30s) and retries to resty client
- Add debug logging for Ollama requests and responses
- Import time package for timeout configuration
- This should fix 500 errors and provide better error messages
This commit is contained in:
2026-04-06 15:05:31 -04:00
parent ba4c4af2f8
commit 1b5cd2815e
+17 -2
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"strings"
"time"
"gophergate/internal/config"
"gophergate/internal/models"
@@ -19,8 +20,14 @@ type OllamaProvider struct {
}
func NewOllamaProvider(cfg config.OllamaConfig) *OllamaProvider {
client := resty.New()
// Set reasonable timeouts for local Ollama server
client.SetTimeout(30 * time.Second)
client.SetRetryCount(2)
client.SetRetryWaitTime(1 * time.Second)
return &OllamaProvider{
client: resty.New(),
client: client,
config: cfg,
}
}
@@ -36,25 +43,33 @@ func (p *OllamaProvider) ChatCompletion(ctx context.Context, req *models.Unified
}
body := BuildOllamaBody(req, messagesJSON, false)
url := fmt.Sprintf("%s/chat/completions", p.config.BaseURL)
// Log request for debugging
fmt.Printf("[Ollama] Request to %s with model %s\n", url, req.Model)
resp, err := p.client.R().
SetContext(ctx).
SetBody(body).
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
Post(url)
if err != nil {
fmt.Printf("[Ollama] Request error: %v\n", err)
return nil, fmt.Errorf("request failed: %w", err)
}
if !resp.IsSuccess() {
fmt.Printf("[Ollama] API error %d: %s\n", resp.StatusCode(), resp.String())
return nil, fmt.Errorf("Ollama API error (%d): %s", resp.StatusCode(), resp.String())
}
var respJSON map[string]interface{}
if err := json.Unmarshal(resp.Body(), &respJSON); err != nil {
fmt.Printf("[Ollama] Parse error: %v\n", err)
return nil, fmt.Errorf("failed to parse response: %w", err)
}
fmt.Printf("[Ollama] Success response for model %s\n", req.Model)
return ParseOllamaResponse(respJSON, req.Model)
}