Skip to content

Commit 8969ba7

Browse files
committed
feat(bedrock): add support for basic auth with api key on bedrock
1 parent 0423ae5 commit 8969ba7

7 files changed

Lines changed: 141 additions & 9 deletions

File tree

providers/anthropic/anthropic.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func WithHTTPClient(client option.HTTPClient) Option {
122122

123123
func (a *provider) LanguageModel(modelID string) (fantasy.LanguageModel, error) {
124124
clientOptions := make([]option.RequestOption, 0, 5+len(a.options.headers))
125-
if a.options.apiKey != "" {
125+
if a.options.apiKey != "" && !a.options.useBedrock {
126126
clientOptions = append(clientOptions, option.WithAPIKey(a.options.apiKey))
127127
}
128128
if a.options.baseURL != "" {
@@ -157,10 +157,10 @@ func (a *provider) LanguageModel(modelID string) (fantasy.LanguageModel, error)
157157
)
158158
}
159159
if a.options.useBedrock {
160-
if a.options.skipAuth {
160+
if a.options.skipAuth || a.options.apiKey != "" {
161161
clientOptions = append(
162162
clientOptions,
163-
bedrock.WithConfig(dummyBedrockConfig),
163+
bedrock.WithConfig(bedrockBasicAuthConfig(a.options.apiKey)),
164164
)
165165
} else {
166166
clientOptions = append(

providers/anthropic/bedrock.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package anthropic
22

33
import (
4-
"context"
4+
"cmp"
5+
"os"
56

67
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/smithy-go/auth/bearer"
79
)
810

9-
var dummyBedrockConfig = aws.Config{
10-
Region: "us-east-1",
11-
Credentials: aws.CredentialsProviderFunc(func(context.Context) (aws.Credentials, error) {
12-
return aws.Credentials{}, nil
13-
}),
11+
func bedrockBasicAuthConfig(apiKey string) aws.Config {
12+
return aws.Config{
13+
Region: cmp.Or(os.Getenv("AWS_REGION"), "us-east-1"),
14+
BearerAuthTokenProvider: bearer.StaticTokenProvider{Token: bearer.Token{Value: apiKey}},
15+
}
1416
}

providers/bedrock/bedrock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func New(opts ...Option) fantasy.Provider {
3636
)
3737
}
3838

39+
// WithAPIKey sets the access token for the Bedrock provider.
40+
func WithAPIKey(apiKey string) Option {
41+
return func(o *options) {
42+
o.anthropicOptions = append(o.anthropicOptions, anthropic.WithAPIKey(apiKey))
43+
}
44+
}
45+
3946
// WithHeaders sets the headers for the Bedrock provider.
4047
func WithHeaders(headers map[string]string) Option {
4148
return func(o *options) {

providertests/.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FANTASY_ANTHROPIC_API_KEY=
22
FANTASY_AZURE_API_KEY=
33
FANTASY_AZURE_BASE_URL=
4+
FANTASY_BEDROCK_API_KEY=
45
FANTASY_GEMINI_API_KEY=
56
FANTASY_GROQ_API_KEY=
67
FANTASY_OPENAI_API_KEY=

providertests/bedrock_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package providertests
22

33
import (
44
"net/http"
5+
"os"
56
"testing"
67

78
"charm.land/fantasy"
@@ -17,6 +18,10 @@ func TestBedrockCommon(t *testing.T) {
1718
})
1819
}
1920

21+
func TestBedrockBasicAuth(t *testing.T) {
22+
testSimple(t, builderPair{"bedrock-anthropic-claude-3-sonnet", buildersBedrockBasicAuth, nil})
23+
}
24+
2025
func builderBedrockClaude3Sonnet(r *recorder.Recorder) (fantasy.LanguageModel, error) {
2126
provider := bedrock.New(
2227
bedrock.WithHTTPClient(&http.Client{Transport: r}),
@@ -40,3 +45,12 @@ func builderBedrockClaude3Haiku(r *recorder.Recorder) (fantasy.LanguageModel, er
4045
)
4146
return provider.LanguageModel("us.anthropic.claude-3-haiku-20240307-v1:0")
4247
}
48+
49+
func buildersBedrockBasicAuth(r *recorder.Recorder) (fantasy.LanguageModel, error) {
50+
provider := bedrock.New(
51+
bedrock.WithHTTPClient(&http.Client{Transport: r}),
52+
bedrock.WithAPIKey(os.Getenv("FANTASY_BEDROCK_API_KEY")),
53+
bedrock.WithSkipAuth(true),
54+
)
55+
return provider.LanguageModel("us.anthropic.claude-3-sonnet-20240229-v1:0")
56+
}

providertests/testdata/TestBedrockBasicAuth/simple.yaml

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

providertests/testdata/TestBedrockBasicAuth/simple_streaming.yaml

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)