Добавил логи
This commit is contained in:
+22
-1
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -26,10 +27,11 @@ type openAICompat struct {
|
||||
apiKey string
|
||||
model string
|
||||
retryWait time.Duration // базовая пауза между ретраями (0 в тестах)
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// newOpenAICompat собирает клиент из конфига.
|
||||
func newOpenAICompat(cfg Config) (*openAICompat, error) {
|
||||
func newOpenAICompat(cfg Config, logger *slog.Logger) (*openAICompat, error) {
|
||||
if cfg.BaseURL == "" {
|
||||
return nil, fmt.Errorf("llm: empty base_url")
|
||||
}
|
||||
@@ -51,12 +53,16 @@ func newOpenAICompat(cfg Config) (*openAICompat, error) {
|
||||
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
|
||||
}
|
||||
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
return &openAICompat{
|
||||
endpoint: strings.TrimRight(cfg.BaseURL, "/") + "/chat/completions",
|
||||
hc: &http.Client{Timeout: timeout, Transport: transport},
|
||||
apiKey: cfg.APIKey,
|
||||
model: cfg.Model,
|
||||
retryWait: baseRetryWait,
|
||||
log: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -119,15 +125,30 @@ func (c *openAICompat) Complete(ctx context.Context, req Request) (Response, err
|
||||
}
|
||||
}
|
||||
|
||||
c.log.Debug("llm: request",
|
||||
"endpoint", c.endpoint, "model", c.model,
|
||||
"attempt", attempt, "max_attempts", maxAttempts)
|
||||
start := time.Now()
|
||||
resp, retryable, err := c.do(ctx, body)
|
||||
if err == nil {
|
||||
c.log.Debug("llm: response ok",
|
||||
"model", resp.Model, "attempt", attempt,
|
||||
"duration", time.Since(start),
|
||||
"total_tokens", resp.Usage.TotalTokens, "cost", resp.Usage.Cost)
|
||||
return resp, nil
|
||||
}
|
||||
lastErr = err
|
||||
if !retryable {
|
||||
c.log.Error("llm: request failed (non-retryable)",
|
||||
"model", c.model, "attempt", attempt, "duration", time.Since(start), "err", err)
|
||||
return Response{}, err
|
||||
}
|
||||
c.log.Warn("llm: request failed, will retry",
|
||||
"model", c.model, "attempt", attempt, "max_attempts", maxAttempts,
|
||||
"duration", time.Since(start), "err", err)
|
||||
}
|
||||
c.log.Error("llm: all attempts exhausted",
|
||||
"model", c.model, "max_attempts", maxAttempts, "err", lastErr)
|
||||
return Response{}, fmt.Errorf("llm: exhausted %d attempts: %w", maxAttempts, lastErr)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user