Логирование: ревью всего кода и рефакторинг в соответствии с конвенциями
This commit is contained in:
+20
-17
@@ -11,6 +11,9 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.vakhrushev.me/av/jellybit/internal/logctx"
|
||||
"git.vakhrushev.me/av/jellybit/internal/logging"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -117,7 +120,7 @@ func (c *openAICompat) Complete(ctx context.Context, req Request) (Response, err
|
||||
return Response{}, fmt.Errorf("llm: marshal request: %w", err)
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
log := logctx.FromOr(ctx, c.log)
|
||||
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||||
if attempt > 1 {
|
||||
if err := c.wait(ctx, attempt); err != nil {
|
||||
@@ -125,31 +128,31 @@ 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()
|
||||
call := logging.ExtCall{
|
||||
Service: logging.ServiceLLM,
|
||||
Operation: "chat.completions",
|
||||
Start: time.Now(),
|
||||
Attempt: attempt,
|
||||
}
|
||||
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),
|
||||
call.Success(log, "model", resp.Model,
|
||||
"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)
|
||||
call.Failure(log, err, "model", c.model)
|
||||
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)
|
||||
if attempt < maxAttempts {
|
||||
call.Retry(log, err, "model", c.model)
|
||||
continue
|
||||
}
|
||||
// Последняя попытка тоже неуспешна — ретраи исчерпаны.
|
||||
call.Failure(log, err, "model", c.model)
|
||||
return Response{}, fmt.Errorf("llm: exhausted %d attempts: %w", maxAttempts, 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)
|
||||
return Response{}, fmt.Errorf("llm: exhausted %d attempts", maxAttempts)
|
||||
}
|
||||
|
||||
func (c *openAICompat) buildRequest(req Request) chatRequest {
|
||||
|
||||
Reference in New Issue
Block a user