Skip to content

Commit

Permalink
#183: parametrized the API version
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Mar 19, 2024
1 parent fb96526 commit ba2afc1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
19 changes: 9 additions & 10 deletions pkg/providers/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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")

Check failure on line 100 in pkg/providers/anthropic/chat.go

View workflow job for this annotation

GitHub Actions / Vulnerability Check

not enough arguments in call to req.Header.Set

Check failure on line 100 in pkg/providers/anthropic/chat.go

View workflow job for this annotation

GitHub Actions / Build

not enough arguments in call to req.Header.Set

Check failure on line 100 in pkg/providers/anthropic/chat.go

View workflow job for this annotation

GitHub Actions / Tests

not enough arguments in call to req.Header.Set
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),
Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions pkg/providers/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand All @@ -50,7 +52,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
MaxIdleConnsPerHost: 2,
},
},
telemetry: tel,
tel: tel,
}

return c, nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/anthropic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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,
Expand Down

0 comments on commit ba2afc1

Please sign in to comment.