A comprehensive AWS Bedrock plugin for Firebase Genkit Go that provides text generation, image generation, and embedding capabilities using AWS Bedrock foundation models via the Converse API.
- Text Generation: Support for multiple foundation models via AWS Bedrock Converse API
- Image Generation: Support for image generation models like Amazon Titan Image Generator
- Embeddings: Support for text embedding models from Amazon Titan and Cohere
- Streaming: Full streaming support for real-time responses
- Tool Calling: Complete function calling capabilities with schema validation and type conversion
- Multimodal Support: Support for text + image inputs (vision models)
- Schema Management: Automatic conversion between Genkit and AWS Bedrock schemas
- Type Safety: Robust type conversion for tool parameters (handles AWS document.Number types)
- Anthropic Claude 3/3.5/4: Haiku, Sonnet, Opus (all versions)
- Amazon Nova: Micro, Lite, Pro
- Meta Llama: 3.1/3.2/3.3 (8B, 70B, 405B)
- Mistral AI: Large, 7B models
- Amazon Titan: Text Express, Premier
- Amazon Titan Image Generator v1
- Amazon Titan Image Generator v2 (preview)
- Amazon Titan Embeddings: Text v1/v2, Multimodal v1
- Cohere: Embed English/Multilingual v3
- All Claude 3/3.5/4 models
- Amazon Nova models
go get github.com/xavidop/genkit-aws-bedrock-go
package main
import (
"context"
"log"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
bedrock "github.com/xavidop/genkit-aws-bedrock-go"
)
func main() {
ctx := context.Background()
bedrockPlugin := &bedrock.Bedrock{
Region: "us-east-1",
}
// Initialize Genkit
g := genkit.Init(ctx,
genkit.WithPlugins(bedrockPlugin),
genkit.WithDefaultModel("bedrock/anthropic.claude-3-haiku-20240307-v1:0"), // Set default model
)
bedrock.DefineCommonModels(bedrockPlugin, g) // Optional: Define common models for easy access
log.Println("Starting basic Bedrock example...")
// Example: Generate text (basic usage)
response, err := genkit.Generate(ctx, g,
ai.WithPrompt("What are the key benefits of using AWS Bedrock for AI applications?"),
)
if err != nil {
log.Printf("Error generating text: %v", err)
} else {
log.Printf("Generated response: %s", response.Text())
}
log.Println("Basic Bedrock example completed")
}
package main
import (
"context"
"log"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
bedrock "github.com/xavidop/genkit-aws-bedrock-go"
)
func main() {
ctx := context.Background()
// Initialize Bedrock plugin
bedrockPlugin := &bedrock.Bedrock{
Region: "us-east-1", // Optional, defaults to AWS_REGION or us-east-1
}
// Initialize Genkit
g := genkit.Init(ctx,
genkit.WithPlugins(bedrockPlugin),
)
// Define a Claude 3 model
claudeModel := bedrockPlugin.DefineModel(g, bedrock.ModelDefinition{
Name: "anthropic.claude-3-haiku-20240307-v1:0",
Type: "text",
}, nil)
// Generate text
response, err := genkit.Generate(ctx, g,
ai.WithModel(claudeModel),
ai.WithMessages(ai.NewUserMessage(
ai.NewTextPart("Hello! How are you?"),
)),
)
if err != nil {
log.Fatal(err)
}
log.Println(response.Text())
}
The plugin supports various configuration options:
bedrockPlugin := &bedrock.Bedrock{
Region: "us-west-2", // AWS region
MaxRetries: 3, // Max retry attempts
RequestTimeout: 30 * time.Second, // Request timeout
AWSConfig: customAWSConfig, // Custom AWS config (optional)
}
Option | Type | Default | Description |
---|---|---|---|
Region |
string |
"us-east-1" |
AWS region for Bedrock |
MaxRetries |
int |
3 |
Maximum retry attempts |
RequestTimeout |
time.Duration |
30s |
Request timeout |
AWSConfig |
*aws.Config |
nil |
Custom AWS configuration |
The plugin uses the standard AWS SDK v2 configuration methods:
-
Environment Variables:
export AWS_ACCESS_KEY_ID="your-access-key" export AWS_SECRET_ACCESS_KEY="your-secret-key" export AWS_REGION="us-east-1"
-
AWS Credentials File (
~/.aws/credentials
):[default] aws_access_key_id = your-access-key aws_secret_access_key = your-secret-key region = us-east-1
-
IAM Roles (when running on AWS services like EC2, ECS, Lambda)
-
AWS SSO/CLI (
aws configure sso
)
Create an IAM policy with these permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": [
"arn:aws:bedrock:*::foundation-model/*"
]
}
]
}
Some models require additional access requests:
- Go to AWS Bedrock Console β Model Access
- Request access for specific models (e.g., Claude, Llama)
- Wait for approval (usually instant for most models)
The repository includes comprehensive examples:
examples/basic/
- Simple text generationexamples/streaming/
- Real-time streaming responsesexamples/tool_calling/
- Function calling with multiple toolsexamples/image_generation/
- Image generation and file savingexamples/embeddings/
- Text embeddings and similarityexamples/multimodal/
- Vision models with image inputsexamples/advanced_schemas/
- Complex tool schemas and validation
# Clone the repository
git clone https://github.com/xavidop/genkit-aws-bedrock-go
cd genkit-aws-bedrock
# Run basic example
cd examples/basic
go run main.go
# Run tool calling example
cd ../tool_calling
go run main.go
# Run image generation example
cd ../image_generation
go run main.go
- Schema Validation: Automatic validation and type conversion
- Type Safety: Handles AWS
document.Number
to Go numeric types - Complex Schemas: Support for nested objects, arrays, enums
- Error Handling: Robust error handling and fallbacks
- Input: Supports base64 data URLs and binary data
- Output: Returns images as base64 data URLs
- Formats: PNG, JPEG, WebP, GIF support
- Vision: Text + image inputs for multimodal models
- Real-time: Token-by-token streaming responses
- Efficient: Low-latency streaming with proper buffering
- Error Handling: Stream error handling and recovery
- AWS Document Types: Handles
document.Number
,document.String
- Schema Mapping: JSON Schema to AWS Bedrock schema conversion
- Parameter Types: String to number/boolean conversion for tools
// For development/testing
config := map[string]interface{}{
"temperature": 0.1, // Low for consistent results
"maxOutputTokens": 1000, // Reasonable limit
}
// For creative tasks
config := map[string]interface{}{
"temperature": 0.8, // Higher for creativity
"topP": 0.9, // Nucleus sampling
"maxOutputTokens": 2000, // Allow longer responses
}
// For tool calling
config := map[string]interface{}{
"temperature": 0.1, // Low for consistent tool usage
"maxOutputTokens": 1000, // Tools need space for responses
}
response, err := genkit.Generate(ctx, g, /* options */)
if err != nil {
// Handle specific AWS errors
if strings.Contains(err.Error(), "ValidationException") {
log.Printf("Request validation error: %v", err)
} else if strings.Contains(err.Error(), "ThrottlingException") {
log.Printf("Rate limit exceeded: %v", err)
// Implement retry logic
} else {
log.Printf("Generation error: %v", err)
}
return
}
-
"Access Denied" Errors
- Verify IAM permissions include
bedrock:InvokeModel
- Check AWS credentials are configured correctly
- Ensure you're using the correct AWS region
- Verify IAM permissions include
-
"Model Not Found" Errors
- Request model access in AWS Bedrock Console
- Verify model name spelling and version
- Check model availability in your region
-
"Validation Exception" Errors
- Check tool schema definitions
- Verify parameter types match schema
- Ensure required fields are provided
-
"Throttling Exception" Errors
- Implement exponential backoff retry logic
- Consider upgrading to higher rate limits
- Distribute requests across time
This project follows Semantic Versioning and uses automated releases based on Conventional Commits.
-
Automatic Version Bumping: Commit messages determine version increments
feat:
β Minor version bump (new features)fix:
β Patch version bump (bug fixes)BREAKING CHANGE:
β Major version bump (breaking changes)
-
Automated Changelog: Release notes are automatically generated from commit messages
-
GitHub Releases: Tagged releases with compiled artifacts and documentation
When contributing, use conventional commit format:
# Feature (minor bump)
feat(models): add support for Claude 3.5 Sonnet
# Bug fix (patch bump)
fix(streaming): resolve timeout issue with long responses
# Breaking change (major bump)
feat(api)!: change model configuration interface
BREAKING CHANGE: Model configuration now requires explicit region specification.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Follow Conventional Commits format
- Commit your changes (
git commit -m 'feat: add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Apache 2.0 - see LICENSE file for details.
- Firebase Genkit team for the excellent Go framework
- AWS Bedrock team for the comprehensive AI model platform
- The open source community for inspiration and feedback
Built with β€οΈ for the Firebase Genkit Go community