@natiwo/bedrock
v0.1.0
Published
AWS Bedrock client for AI model integration with Claude, Llama, Titan and more
Maintainers
Readme
@natiwo/bedrock
Generic AWS Bedrock client for AI model integration supporting Claude, Llama, Titan, and more.
Features
- Multi-Model Support: Claude, Llama, Titan, Jamba, Command, Mistral
- Streaming: Real-time response streaming
- Type-Safe: Full TypeScript support with strict typing
- Tool Calling: Function calling for supported models (Claude 3+)
- Vision: Image input support for capable models
- Cost Estimation: Built-in pricing calculator
- Error Handling: Comprehensive error types
- Zero Dependencies: Only requires AWS SDK
Installation
pnpm add @natiwo/bedrock @aws-sdk/client-bedrock-runtimeQuick Start
import { BedrockClient } from '@natiwo/bedrock';
const client = new BedrockClient({
region: 'us-east-2',
profile: 'default', // or use credentials
});
const response = await client.invoke({
model: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
messages: [
{
role: 'user',
content: 'Explain quantum computing in simple terms',
},
],
maxTokens: 1024,
temperature: 0.7,
});
console.log(response.content[0].text);
console.log(`Cost: $${estimateCost(response.model, response.usage)}`);Streaming
const stream = client.invokeStream({
model: 'anthropic.claude-3-5-haiku-20241022-v1:0',
messages: [{ role: 'user', content: 'Write a poem about code' }],
});
for await (const chunk of stream) {
if (chunk.type === 'content_block_delta' && chunk.delta?.text) {
process.stdout.write(chunk.delta.text);
}
}Tool Calling (Function Calling)
const response = await client.invoke({
model: 'anthropic.claude-3-5-sonnet-20241022-v2:0',
messages: [
{
role: 'user',
content: 'What is the weather in San Francisco?',
},
],
tools: [
{
name: 'get_weather',
description: 'Get current weather for a location',
input_schema: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
},
required: ['location'],
},
},
],
});
if (response.stopReason === 'tool_use') {
const toolUse = response.content.find((c) => c.type === 'tool_use');
console.log(`Calling tool: ${toolUse.name}`, toolUse.input);
}Supported Models
Anthropic Claude
anthropic.claude-3-5-sonnet-20241022-v2:0(recommended - balanced)anthropic.claude-3-5-haiku-20241022-v1:0(fast & cheap)anthropic.claude-3-opus-20240229-v1:0(most powerful)
Meta Llama
meta.llama3-1-70b-instruct-v1:0meta.llama3-2-3b-instruct-v1:0
Amazon Titan
amazon.titan-text-premier-v1:0amazon.titan-text-express-v1
Others
- AI21 Jamba
- Cohere Command
- Mistral AI
Utilities
import {
estimateCost,
getRecommendedModel,
supportsTools,
supportsVision,
getMaxContextTokens,
} from '@natiwo/bedrock';
// Get recommended model for use case
const model = getRecommendedModel('balanced'); // 'fast' | 'balanced' | 'powerful'
// Check capabilities
if (supportsTools(model)) {
console.log('Model supports function calling');
}
if (supportsVision(model)) {
console.log('Model supports image inputs');
}
// Get limits
console.log(`Max context: ${getMaxContextTokens(model)} tokens`);Configuration
Using AWS Profile
const client = new BedrockClient({
region: 'us-east-2',
profile: 'my-profile',
});Using Credentials
const client = new BedrockClient({
region: 'us-east-2',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});Environment Variables
AWS_REGION=us-east-2
AWS_PROFILE=defaultError Handling
import { BedrockError } from '@natiwo/bedrock';
try {
const response = await client.invoke({ ... });
} catch (error) {
if (error instanceof BedrockError) {
console.error(`Bedrock Error: ${error.message}`);
console.error(`Status: ${error.statusCode}`);
console.error(`Request ID: ${error.requestId}`);
}
}Cost Estimation
import { estimateCost } from '@natiwo/bedrock';
const response = await client.invoke({ ... });
const cost = estimateCost(response.model, response.usage);
console.log(`Input tokens: ${response.usage.inputTokens}`);
console.log(`Output tokens: ${response.usage.outputTokens}`);
console.log(`Estimated cost: $${cost.toFixed(6)}`);License
MIT © Claudiomar Emanuel Estevam
Author
Claudiomar Emanuel Estevam
- Email: [email protected]
- GitHub: @claudioeestevam
