-
-
Notifications
You must be signed in to change notification settings - Fork 113
feat(ai-bedrock): add AWS Bedrock adapter with ConverseStream and tool-calling support #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srmurali002
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
srmurali002:feat/ai-bedrock-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@tanstack/ai-bedrock": minor | ||
| --- | ||
|
|
||
| Add Amazon Bedrock adapter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Bedrock Live Tests | ||
|
|
||
| These tests verify that the Bedrock adapter correctly handles tool calling and multimodal inputs with various models (Nova, Claude). | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Create a `.env.local` file in this directory with your AWS credentials: | ||
|
|
||
| ``` | ||
| AWS_ACCESS_KEY_ID=... | ||
| AWS_SECRET_ACCESS_KEY=... | ||
| AWS_REGION=us-east-1 | ||
| ``` | ||
|
|
||
| 2. Install dependencies: | ||
| ```bash | ||
| pnpm install | ||
| ``` | ||
|
|
||
| ## Tests | ||
|
|
||
| ### `tool-test.ts` | ||
| Tests basic tool calling with Claude 3.5 Sonnet. | ||
|
|
||
| ### `tool-test-nova.ts` | ||
| Tests Amazon Nova Pro with multimodal inputs (if applicable) and tool calling. | ||
|
|
||
| ## Running Tests | ||
|
|
||
| ```bash | ||
| # Run Claude tool test | ||
| pnpm test | ||
|
|
||
| # Run Nova tool test | ||
| pnpm test:nova | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "name": "@tanstack/ai-bedrock-live-tests", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "tsx tool-test.ts", | ||
| "test:nova": "tsx tool-test-nova.ts" | ||
| }, | ||
| "devDependencies": { | ||
| "tsx": "^4.7.1", | ||
| "typescript": "^5.4.2", | ||
| "zod": "^4.2.1" | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
packages/typescript/ai-bedrock/live-tests/tool-test-haiku.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // import 'dotenv/config' | ||
| import { bedrockText } from '../src/bedrock-chat' | ||
| import { z } from 'zod' | ||
| import { chat } from '@tanstack/ai' | ||
|
|
||
| function throwMissingEnv(name: string): never { | ||
| throw new Error(`Missing required environment variable: ${name}`) | ||
| } | ||
|
|
||
| async function main() { | ||
| const accessKeyId = process.env.AWS_ACCESS_KEY_ID ?? throwMissingEnv('AWS_ACCESS_KEY_ID') | ||
| const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY ?? throwMissingEnv('AWS_SECRET_ACCESS_KEY') | ||
|
|
||
| const modelId = 'anthropic.claude-haiku-4-5-20251001-v1:0' | ||
| console.log(`Running tool test for: ${modelId}`) | ||
|
|
||
| const stream = await chat({ | ||
| adapter: bedrockText(modelId, { | ||
| region: process.env.AWS_REGION || 'us-east-1', | ||
| credentials: { | ||
| accessKeyId, | ||
| secretAccessKey, | ||
| }, | ||
| }), | ||
| modelOptions: { | ||
| thinking: { | ||
| type: 'enabled', | ||
| budget_tokens: 1024 | ||
| } | ||
| }, | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: 'Use the `get_weather` tool to find the weather in New York and explain it.', | ||
| }, | ||
| ], | ||
| tools: [ | ||
| { | ||
| name: 'get_weather', | ||
| description: 'Get the current weather in a location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The city and state, e.g. New York, NY'), | ||
| }), | ||
| execute: async ({ location }) => { | ||
| console.log(`\n[TOOL Weather] Fetching weather for ${location}...`) | ||
| return { | ||
| temperature: 45, | ||
| unit: 'F', | ||
| condition: 'Cloudy', | ||
| } | ||
| }, | ||
| }, | ||
| ], | ||
| stream: true | ||
| }) | ||
|
|
||
| let finalContent = '' | ||
| let hasThinking = false | ||
| let toolCallCount = 0 | ||
|
|
||
| console.log('--- Stream Output ---') | ||
| for await (const chunk of stream) { | ||
| if (chunk.type === 'thinking') { | ||
| hasThinking = true | ||
| } else if (chunk.type === 'content') { | ||
| process.stdout.write(chunk.delta) | ||
| finalContent += chunk.delta | ||
| } else if (chunk.type === 'tool_call') { | ||
| toolCallCount++ | ||
| } | ||
| } | ||
|
|
||
| console.log('--- Results ---') | ||
| console.log('Thinking:', hasThinking) | ||
| console.log('Tool calls:', toolCallCount) | ||
| console.log('Content length:', finalContent.length) | ||
|
|
||
| if (!finalContent || finalContent.trim().length === 0) { | ||
| console.error('Test failed: No final content') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| console.log('Test passed') | ||
| } | ||
|
|
||
| main().catch(console.error) |
105 changes: 105 additions & 0 deletions
105
packages/typescript/ai-bedrock/live-tests/tool-test-nova.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { bedrockText } from '../src/index' | ||
| import { z } from 'zod' | ||
| import { readFileSync } from 'fs' | ||
| import { join, dirname } from 'path' | ||
| import { fileURLToPath } from 'url' | ||
| import { chat } from '@tanstack/ai' | ||
|
|
||
| // Load environment variables from .env.local manually | ||
| const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
| try { | ||
| const envContent = readFileSync(join(__dirname, '.env.local'), 'utf-8') | ||
| envContent.split('\n').forEach((line) => { | ||
| const match = line.match(/^([^=]+)=(.*)$/) | ||
| if (match) { | ||
| process.env[match[1].trim()] = match[2].trim() | ||
| } | ||
| }) | ||
| } catch (e) { | ||
| // .env.local not found | ||
| } | ||
|
|
||
| function throwMissingEnv(name: string): never { | ||
| throw new Error(`Missing required environment variable: ${name}`) | ||
| } | ||
|
|
||
| async function testBedrockNovaToolCalling() { | ||
| console.log('Testing Bedrock tool calling (Amazon Nova Pro)\n') | ||
|
|
||
| const accessKeyId = process.env.AWS_ACCESS_KEY_ID ?? throwMissingEnv('AWS_ACCESS_KEY_ID') | ||
| const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY ?? throwMissingEnv('AWS_SECRET_ACCESS_KEY') | ||
|
|
||
| const stream = await chat({ | ||
| adapter: bedrockText('us.amazon.nova-pro-v1:0', { | ||
| region: process.env.AWS_REGION || 'us-east-1', | ||
| credentials: { | ||
| accessKeyId, | ||
| secretAccessKey, | ||
| } | ||
| }), | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: 'Use the `get_temperature` tool to find the temperature in New York and explain why it is the way it is.', | ||
| }, | ||
| ], | ||
| tools: [ | ||
| { | ||
| name: 'get_temperature', | ||
| description: 'Get the current temperature for a specific location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The city or location'), | ||
| unit: z.enum(['celsius', 'fahrenheit']).describe('The temperature unit'), | ||
| }), | ||
| execute: async ({ location, unit }: { location: string; unit: string }) => { | ||
| console.log(`\n[TOOL Temperature] Fetching for ${location}...`) | ||
| return { | ||
| temperature: 45, | ||
| unit: unit, | ||
| condition: 'Cloudy', | ||
| } | ||
| }, | ||
| }, | ||
| ], | ||
| stream: true, | ||
| }) | ||
|
|
||
| let finalContent = '' | ||
| let hasThinking = false | ||
| let toolCallCount = 0 | ||
|
|
||
| console.log('--- Stream Output ---') | ||
| for await (const chunk of stream) { | ||
| if (chunk.type === 'thinking') { | ||
| hasThinking = true | ||
| } else if (chunk.type === 'content') { | ||
| process.stdout.write(chunk.delta) | ||
| finalContent += chunk.delta | ||
| } else if (chunk.type === 'tool_call') { | ||
| toolCallCount++ | ||
| } | ||
| } | ||
|
|
||
| console.log('--- Test Results ---') | ||
| console.log('Thinking detected:', hasThinking) | ||
| console.log('Tool calls:', toolCallCount) | ||
| console.log('Final content length:', finalContent.length) | ||
|
|
||
| if (!hasThinking) { | ||
| console.warn('Warning: No thinking blocks detected') | ||
| } | ||
|
|
||
| if (finalContent.includes('<thinking>')) { | ||
| console.error('Test failed: Thinking tags found in final content') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| if (!finalContent || finalContent.trim().length === 0) { | ||
| console.error('Test failed: No final content - model should explain the temperature') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| console.log('Test passed') | ||
| } | ||
|
|
||
| testBedrockNovaToolCalling().catch(console.error) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider asserting that the tool was actually invoked.
This test specifically validates "Bedrock tool calling" but doesn't assert that
toolCallCount > 0. The test could pass even if the model responds without calling the tool, missing the core behavior under test.🐛 Suggested assertion
console.log('--- Test Results ---') console.log('Thinking detected:', hasThinking) console.log('Tool calls:', toolCallCount) console.log('Final content length:', finalContent.length) + if (toolCallCount === 0) { + console.error('Test failed: No tool calls detected - model should have called get_temperature') + process.exit(1) + } + if (!hasThinking) { console.warn('Warning: No thinking blocks detected') }📝 Committable suggestion
🤖 Prompt for AI Agents