🪨 Bedrock Wrapper
Overview
Bedrock Wrapper is a Node.js library that provides OpenAI-compatible API objects for AWS Bedrock's serverless inference LLMs. It allows you to use AWS Bedrock models with the same interface you're familiar with from OpenAI, making it easy to switch between providers or use them together.
Features
- OpenAI-compatible API interface
- Support for multiple Bedrock models
- Streaming responses
- TypeScript support
- Automatic retries and error handling
- Custom model parameters
Installation
npm install bedrock-wrapper
AWS Configuration
First, configure your AWS credentials:
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_REGION=your_region
Basic Usage
import { BedrockWrapper } from 'bedrock-wrapper';
const bedrock = new BedrockWrapper({
model: 'anthropic.claude-v2'
});
const response = await bedrock.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello!' }
]
});
Streaming
const stream = await bedrock.chat.completions.create({
messages: [{ role: 'user', content: 'Tell me a story.' }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
Supported Models
- Anthropic Claude:
- anthropic.claude-v2
- anthropic.claude-v1
- anthropic.claude-instant-v1
- Amazon Titan:
- amazon.titan-text-express-v1
- amazon.titan-text-lite-v1
- AI21 Labs:
- ai21.j2-ultra-v1
- ai21.j2-mid-v1
Advanced Configuration
Custom Model Parameters
const bedrock = new BedrockWrapper({
model: 'anthropic.claude-v2',
modelParams: {
temperature: 0.7,
top_p: 0.9,
max_tokens: 1000
}
});
Retry Configuration
const bedrock = new BedrockWrapper({
model: 'anthropic.claude-v2',
maxRetries: 3,
retryDelay: 1000
});
Error Handling
try {
const response = await bedrock.chat.completions.create({
messages: [{ role: 'user', content: 'Hello!' }]
});
} catch (error) {
if (error.code === 'ThrottlingException') {
// Handle rate limiting
} else if (error.code === 'ValidationException') {
// Handle validation errors
}
console.error('Error:', error);
}
Contributing
We welcome contributions! Please check our contribution guidelines for more information.
License
This project is licensed under the MIT License - see the LICENSE file for details.