Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anthropicllm: Allow env var ANTHROPIC_MODEL for setting Claude model #971

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llms/anthropic/anthropicllm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
5 changes: 4 additions & 1 deletion llms/anthropic/anthropicllm_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions llms/anthropic/internal/anthropicclient/anthropicclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions llms/anthropic/internal/anthropicclient/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions llms/anthropic/internal/anthropicclient/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading