fix(ollama): improve error handling and add timeouts
- 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:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user