(m Message)
| 404 | } |
| 405 | |
| 406 | func (h *Handler) decorateEvent(m Message) error { |
| 407 | telemetry.Incr("bricksllm.message.handler.decorate_event.request", nil, 1) |
| 408 | |
| 409 | e, ok := m.Data.(*event.EventWithRequestAndContent) |
| 410 | if !ok { |
| 411 | telemetry.Incr("bricksllm.message.handler.decorate_event.message_data_parsing_error", nil, 1) |
| 412 | h.log.Debug("message contains data that cannot be converted to event with request and response format", zap.Any("data", m.Data)) |
| 413 | return errors.New("message data cannot be parsed as event with request and response") |
| 414 | } |
| 415 | |
| 416 | if e.Event.Path == "/api/providers/openai/v1/audio/speech" { |
| 417 | csr, ok := e.Request.(*goopenai.CreateSpeechRequest) |
| 418 | if !ok { |
| 419 | telemetry.Incr("bricksllm.message.handler.decorate_event.event_request_parsing_error", nil, 1) |
| 420 | h.log.Debug("event contains request that cannot be converted to anthropic completion request", zap.Any("data", m.Data)) |
| 421 | return errors.New("event request data cannot be parsed as anthropic completion request") |
| 422 | } |
| 423 | |
| 424 | if e.Event.Status == http.StatusOK { |
| 425 | cost, err := h.e.EstimateSpeechCost(csr.Input, string(csr.Model)) |
| 426 | if err != nil { |
| 427 | telemetry.Incr("bricksllm.message.handler.decorate_event.estimate_prompt_cost", nil, 1) |
| 428 | h.log.Debug("event contains request that cannot be converted to anthropic completion request", zap.Error(err)) |
| 429 | return err |
| 430 | } |
| 431 | |
| 432 | e.Event.CostInUsd = cost |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if e.Event.Path == "/api/providers/anthropic/v1/complete" { |
| 437 | cr, ok := e.Request.(*anthropic.CompletionRequest) |
| 438 | if !ok { |
| 439 | telemetry.Incr("bricksllm.message.handler.decorate_event.event_request_parsing_error", nil, 1) |
| 440 | h.log.Debug("event contains request that cannot be converted to anthropic completion request", zap.Any("data", m.Data)) |
| 441 | return errors.New("event request data cannot be parsed as anthropic completion request") |
| 442 | } |
| 443 | |
| 444 | tks := h.ae.Count(cr.Prompt) |
| 445 | tks += anthropicPromptMagicNum |
| 446 | |
| 447 | model := cr.Model |
| 448 | cost, err := h.ae.EstimatePromptCost(model, tks) |
| 449 | if err != nil { |
| 450 | telemetry.Incr("bricksllm.message.handler.decorate_event.estimate_prompt_cost", nil, 1) |
| 451 | h.log.Debug("event contains request that cannot be converted to anthropic completion request", zap.Error(err)) |
| 452 | return err |
| 453 | } |
| 454 | |
| 455 | completiontks := h.ae.Count(e.Content) |
| 456 | completiontks += anthropicCompletionMagicNum |
| 457 | |
| 458 | completionCost, err := h.ae.EstimateCompletionCost(model, completiontks) |
| 459 | if err != nil { |
| 460 | telemetry.Incr("bricksllm.message.handler.decorate_event.estimate_completion_cost_error", nil, 1) |
| 461 | return err |
| 462 | } |
| 463 |
no test coverage detected