Skip to content

Commit

Permalink
Add BaseURL for Anthropic configuration. (#336)
Browse files Browse the repository at this point in the history
This will allow Anthropic requests to be issued via a proxy.
  • Loading branch information
jlewi authored Nov 11, 2024
1 parent af95d2f commit c523d81
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/pkg/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
57 changes: 57 additions & 0 deletions app/pkg/anthropic/client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 3 additions & 0 deletions app/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c523d81

Please sign in to comment.