diff --git a/llms/anthropic/anthropicllm.go b/llms/anthropic/anthropicllm.go index 2d236ebe5..537209eab 100644 --- a/llms/anthropic/anthropicllm.go +++ b/llms/anthropic/anthropicllm.go @@ -49,6 +49,7 @@ func New(opts ...Option) (*LLM, error) { func newClient(opts ...Option) (*anthropicclient.Client, error) { options := &options{ token: os.Getenv(tokenEnvVarName), + model: os.Getenv(modelEnvVarName), baseURL: anthropicclient.DefaultBaseURL, httpClient: http.DefaultClient, } diff --git a/llms/anthropic/anthropicllm_option.go b/llms/anthropic/anthropicllm_option.go index 75ae25ded..bab2be66d 100644 --- a/llms/anthropic/anthropicllm_option.go +++ b/llms/anthropic/anthropicllm_option.go @@ -6,6 +6,7 @@ import ( const ( tokenEnvVarName = "ANTHROPIC_API_KEY" //nolint:gosec + modelEnvVarName = "ANTHROPIC_MODEL" //nolint:gosec ) // MaxTokensAnthropicSonnet35 is the header value for specifying the maximum number of tokens @@ -34,7 +35,9 @@ func WithToken(token string) Option { } } -// WithModel passes the Anthropic model to the client. +// WithModel passes the Anthropic model to the client. If not set, the model +// is read from the ANTHROPIC_MODEL environment variable. If that is not set, +// `defaultModel` in the anthropicclient package is used. func WithModel(model string) Option { return func(opts *options) { opts.model = model diff --git a/llms/anthropic/internal/anthropicclient/anthropicclient.go b/llms/anthropic/internal/anthropicclient/anthropicclient.go index cbe9fd9bf..c94cfe852 100644 --- a/llms/anthropic/internal/anthropicclient/anthropicclient.go +++ b/llms/anthropic/internal/anthropicclient/anthropicclient.go @@ -22,8 +22,8 @@ var ErrEmptyResponse = errors.New("empty response") // Client is a client for the Anthropic API. type Client struct { token string - Model string baseURL string + model string httpClient Doer @@ -69,9 +69,9 @@ func WithAnthropicBetaHeader(val string) Option { // New returns a new Anthropic client. func New(token string, model string, baseURL string, opts ...Option) (*Client, error) { c := &Client{ - Model: model, token: token, baseURL: strings.TrimSuffix(baseURL, "/"), + model: model, } for _, opt := range opts { diff --git a/llms/anthropic/internal/anthropicclient/completions.go b/llms/anthropic/internal/anthropicclient/completions.go index 86d754991..7c91899ea 100644 --- a/llms/anthropic/internal/anthropicclient/completions.go +++ b/llms/anthropic/internal/anthropicclient/completions.go @@ -46,8 +46,8 @@ func (c *Client) setCompletionDefaults(payload *completionPayload) { case payload.Model != "": // If no model is set in the payload, take the one specified in the client. - case c.Model != "": - payload.Model = c.Model + case c.model != "": + payload.Model = c.model // Fallback: use the default model default: payload.Model = defaultModel diff --git a/llms/anthropic/internal/anthropicclient/messages.go b/llms/anthropic/internal/anthropicclient/messages.go index 10a0ca400..464152ae9 100644 --- a/llms/anthropic/internal/anthropicclient/messages.go +++ b/llms/anthropic/internal/anthropicclient/messages.go @@ -157,8 +157,8 @@ func (c *Client) setMessageDefaults(payload *messagePayload) { case payload.Model != "": // If no model is set in the payload, take the one specified in the client. - case c.Model != "": - payload.Model = c.Model + case c.model != "": + payload.Model = c.model // Fallback: use the default model default: payload.Model = defaultModel