diff --git a/pkg/providers/anthropic/chat.go b/pkg/providers/anthropic/chat.go index 570b5e9c..9038666e 100644 --- a/pkg/providers/anthropic/chat.go +++ b/pkg/providers/anthropic/chat.go @@ -68,14 +68,11 @@ func (c *Client) Chat(ctx context.Context, request *schemas.ChatRequest) (*schem chatRequest := c.createChatRequestSchema(request) chatResponse, err := c.doChatRequest(ctx, chatRequest) + if err != nil { return nil, err } - if len(chatResponse.ModelResponse.Message.Content) == 0 { - return nil, ErrEmptyResponse - } - return chatResponse, nil } @@ -100,11 +97,11 @@ func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*sche } req.Header.Set("x-api-key", string(c.config.APIKey)) // must be in lower case - req.Header.Set("anthropic-version", "2023-06-01") + req.Header.Set("anthropic-version") req.Header.Set("Content-Type", "application/json") // TODO: this could leak information from messages which may not be a desired thing to have - c.telemetry.Logger.Debug( + c.tel.L().Debug( "Anthropic chat request", zap.String("chat_url", c.chatURL), zap.Any("payload", payload), @@ -124,21 +121,23 @@ func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*sche // Read the response body into a byte slice bodyBytes, err := io.ReadAll(resp.Body) if err != nil { - c.telemetry.Logger.Error("Failed to read anthropic chat response", zap.Error(err)) + c.tel.L().Error("Failed to read anthropic chat response", zap.Error(err)) return nil, err } // Parse the response JSON var anthropicResponse ChatCompletion - c.telemetry.L().Debug("Anthropic Raw Response", zap.String("resp", string(bodyBytes))) - err = json.Unmarshal(bodyBytes, &anthropicResponse) if err != nil { - c.telemetry.Logger.Error("Failed to parse anthropic chat response", zap.Error(err)) + c.tel.L().Error("Failed to parse anthropic chat response", zap.Error(err)) return nil, err } + if len(anthropicResponse.Content) == 0 { + return nil, ErrEmptyResponse + } + completion := anthropicResponse.Content[0] usage := anthropicResponse.Usage diff --git a/pkg/providers/anthropic/client.go b/pkg/providers/anthropic/client.go index 1ab1f584..1af21262 100644 --- a/pkg/providers/anthropic/client.go +++ b/pkg/providers/anthropic/client.go @@ -22,11 +22,12 @@ var ( type Client struct { baseURL string chatURL string + apiVersion string chatRequestTemplate *ChatRequest errMapper *ErrorMapper config *Config httpClient *http.Client - telemetry *telemetry.Telemetry + tel *telemetry.Telemetry } // NewClient creates a new OpenAI client for the OpenAI API. @@ -39,6 +40,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel * c := &Client{ baseURL: providerConfig.BaseURL, chatURL: chatURL, + apiVersion: providerConfig.APIVersion, config: providerConfig, chatRequestTemplate: NewChatRequestFromConfig(providerConfig), errMapper: NewErrorMapper(tel), @@ -50,7 +52,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel * MaxIdleConnsPerHost: 2, }, }, - telemetry: tel, + tel: tel, } return c, nil diff --git a/pkg/providers/anthropic/config.go b/pkg/providers/anthropic/config.go index 4a2711fb..0de765ac 100644 --- a/pkg/providers/anthropic/config.go +++ b/pkg/providers/anthropic/config.go @@ -37,6 +37,7 @@ func (p *Params) UnmarshalYAML(unmarshal func(interface{}) error) error { type Config struct { BaseURL string `yaml:"baseUrl" json:"baseUrl" validate:"required"` + APIVersion string `yaml:"apiVersion" json:"apiVersion" validate:"required"` ChatEndpoint string `yaml:"chatEndpoint" json:"chatEndpoint" validate:"required"` Model string `yaml:"model" json:"model" validate:"required"` APIKey fields.Secret `yaml:"api_key" json:"-" validate:"required"` @@ -49,6 +50,7 @@ func DefaultConfig() *Config { return &Config{ BaseURL: "https://api.anthropic.com/v1", + APIVersion: "2023-06-01", ChatEndpoint: "/messages", Model: "claude-instant-1.2", DefaultParams: &defaultParams,