Amazon Bedrock
Amazon Bedrock is the current provider implementation in this project.
This provider now uses the official AWS SDK for Go v2 for credentials, SigV4 signing, non-stream requests (Converse), and real-time streaming (ConverseStream).
Install Bedrock module:
go get github.com/iamanishx/go-ai/provider/bedrockCreate Provider
import "github.com/iamanishx/go-ai/provider/bedrock"
bedrockProvider := bedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1", Profile: "myprofile",})Authentication Options
AWS profile
bedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1", Profile: "myprofile",})Environment variables
export AWS_REGION=us-east-1export AWS_ACCESS_KEY_ID=your-keyexport AWS_SECRET_ACCESS_KEY=your-secretbedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1",})Static credentials
bedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1", AccessKeyID: "YOUR_ACCESS_KEY_ID", SecretAccessKey: "YOUR_SECRET_ACCESS_KEY", SessionToken: "OPTIONAL_SESSION_TOKEN",})Custom credential provider
bedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1", CredentialProvider: &bedrock.SharedConfigCredentialProvider{ Profile: "myprofile", },})Default chain
bedrock.Create(bedrock.BedrockProviderSettings{ Region: "us-east-1", CredentialProvider: bedrock.NewDefaultCredentialProviderChain(),})Credential resolution follows AWS SDK defaults when explicit settings are not provided.
Generate Text
model := bedrockProvider.Chat("anthropic.claude-3-sonnet-20240229-v1:0")
result, err := model.GenerateText(ctx, provider.GenerateTextOptions{ Prompt: "Hello, how are you?", System: "You are a helpful assistant.", MaxTokens: 1000, Temperature: 0.7,})
if err != nil { panic(err)}
_ = result.TextStream Text
stream, err := model.StreamText(ctx, provider.GenerateTextOptions{ Prompt: "Write a short poem about Go",})
if err != nil { panic(err)}
for part := range stream { switch part.Type { case "text-delta": fmt.Print(part.Text) case "tool-call": fmt.Printf("\n[tool call: %s]\n", part.ToolName) case "finish": fmt.Printf("\n[finish: %s]\n", part.FinishReason) case "error": fmt.Printf("\n[error: %v]\n", part.Error) }}Supported Settings
| Option | Type | Description |
|---|---|---|
Region | string | AWS region (defaults to us-east-1) |
Profile | string | AWS profile name |
AccessKeyID | string | Static access key ID |
SecretAccessKey | string | Static secret access key |
SessionToken | string | Optional session token |
APIKey | string | Bearer token alternative |
BaseURL | string | Custom Bedrock endpoint |
Headers | map[string]string | Extra request headers |
CredentialProvider | CredentialProvider | Custom credentials source |
When GenerateTextOptions.MaxTokens is not set, the Bedrock request defaults to 4096.
StreamText uses Bedrock ConverseStream, so text-delta events are emitted incrementally.
Built-in Credential Providers
EnvCredentialProviderSharedConfigCredentialProviderWebIdentityCredentialProviderStaticCredentialProviderDefaultCredentialProviderChain