Integrate Amazon Bedrock language models into your application using the AI SDK. Supports API key and SigV4 authentication with automatic fallback.
Integrate Amazon Bedrock language models into your application using the Vercel AI SDK. This skill provides step-by-step instructions for setting up and using the Amazon Bedrock provider with support for multiple authentication methods.
This skill guides you through:
Install the `@ai-sdk/amazon-bedrock` package and the core AI SDK:
```bash
npm i @ai-sdk/amazon-bedrock ai
```
The provider supports two authentication methods with automatic fallback:
**Option A: API Key Authentication (Recommended)**
Set environment variable:
```bash
AWS_BEARER_TOKEN_BEDROCK=your-api-key-here
```
Or configure directly in code:
```typescript
import { bedrock } from '@ai-sdk/amazon-bedrock';
const bedrockWithApiKey = bedrock.withSettings({
apiKey: process.env.AWS_BEARER_TOKEN_BEDROCK,
region: 'us-east-1', // Optional
});
```
**Option B: SigV4 Authentication (Automatic Fallback)**
Set standard AWS environment variables:
```bash
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_SESSION_TOKEN=your-session-token # Optional for temporary credentials
```
```typescript
import { bedrock } from '@ai-sdk/amazon-bedrock';
import { generateText } from 'ai';
```
Use any supported Bedrock model with the `generateText` function:
```typescript
const { text } = await generateText({
model: bedrock('anthropic.claude-3-haiku-20240307-v1:0'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(text);
```
Configure provider settings for specific regions or authentication:
```typescript
const customBedrock = bedrock.withSettings({
apiKey: 'your-api-key',
region: 'us-west-2',
});
const { text } = await generateText({
model: customBedrock('meta.llama3-8b-instruct-v1:0'),
prompt: 'Explain quantum computing in simple terms.',
});
```
The provider uses this authentication order:
1. API key from direct configuration (`withSettings()`)
2. API key from `AWS_BEARER_TOKEN_BEDROCK` environment variable
3. SigV4 authentication using AWS credential chain
**Basic text generation:**
```typescript
const { text } = await generateText({
model: bedrock('anthropic.claude-3-haiku-20240307-v1:0'),
prompt: 'What are the benefits of serverless architecture?',
});
```
**With streaming:**
```typescript
import { streamText } from 'ai';
const result = await streamText({
model: bedrock('anthropic.claude-3-sonnet-20240229-v1:0'),
prompt: 'Write a blog post about AI ethics.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
```
**With custom settings:**
```typescript
const result = await generateText({
model: bedrock('meta.llama3-70b-instruct-v1:0'),
prompt: 'Generate product descriptions for an e-commerce site.',
maxTokens: 500,
temperature: 0.7,
});
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/aws-bedrock-ai-sdk-integration/raw