diff --git a/app/pkg/anthropic/client.go b/app/pkg/anthropic/client.go index 376cc16..94ea83d 100644 --- a/app/pkg/anthropic/client.go +++ b/app/pkg/anthropic/client.go @@ -48,7 +48,14 @@ func NewClient(cfg config.Config) (*anthropic.Client, error) { return nil, err } - client := anthropic.NewClient(apiKey, anthropic.WithHTTPClient(httpClient)) + opts := make([]anthropic.ClientOption, 0) + + if cfg.Anthropic.BaseURL != "" { + log.Info("Using custom Anthropic base URL", "baseURL", cfg.Anthropic.BaseURL) + opts = append(opts, anthropic.WithBaseURL(cfg.Anthropic.BaseURL)) + } + opts = append(opts, anthropic.WithHTTPClient(httpClient)) + client := anthropic.NewClient(apiKey, opts...) return client, nil } diff --git a/app/pkg/anthropic/client_test.go b/app/pkg/anthropic/client_test.go new file mode 100644 index 0000000..ac0e7ed --- /dev/null +++ b/app/pkg/anthropic/client_test.go @@ -0,0 +1,57 @@ +package anthropic + +import ( + "context" + "os" + "testing" + + "github.com/jlewi/foyle/app/pkg/config" + "github.com/liushuangls/go-anthropic/v2" + "google.golang.org/protobuf/proto" +) + +func Test_AnthropicClient(t *testing.T) { + if os.Getenv("GITHUB_ACTIONS") != "" { + t.Skipf("TestAnthropicClient is a manual test that is skipped in CICD") + } + + // N.B since this test isn't using a valid endpoint we don't expect it to succceed + // But hopefully the error message will let us confirm that the baseURL is being used + + // Test non standard base URL + cfg := config.Config{ + Anthropic: &config.AnthropicConfig{ + APIKeyFile: "/Users/jlewi/secrets/anthropic.key", + BaseURL: "https://localhost:8844", + }, + } + + client, err := NewClient(cfg) + if err != nil { + t.Fatalf("Failed to create Anthropic client: %v", err) + } + + message := "Say hello a funny way" + messages := []anthropic.Message{ + {Role: anthropic.RoleUser, + Content: []anthropic.MessageContent{ + {Type: anthropic.MessagesContentTypeText, + Text: proto.String(message), + }, + }, + }, + } + + request := anthropic.MessagesRequest{ + Model: cfg.GetModel(), + Messages: messages, + MaxTokens: 2000, + Temperature: proto.Float32(temperature), + System: "You are a helper", + } + + _, err = client.CreateMessages(context.Background(), request) + if err != nil { + t.Logf("Error: %+v", err) + } +} diff --git a/app/pkg/config/config.go b/app/pkg/config/config.go index 6aa6f84..ff0594f 100644 --- a/app/pkg/config/config.go +++ b/app/pkg/config/config.go @@ -130,6 +130,9 @@ type OpenAIConfig struct { type AnthropicConfig struct { // APIKeyFile is the path to the file containing the API key APIKeyFile string `json:"apiKeyFile" yaml:"apiKeyFile"` + + // BaseURL is the baseURL for the API. + BaseURL string `json:"baseURL" yaml:"baseURL"` } type ReplicateConfig struct {