|
| 1 | +import { bedrockText } from '../src/index' |
| 2 | +import { z } from 'zod' |
| 3 | +import { readFileSync } from 'fs' |
| 4 | +import { join, dirname } from 'path' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | +import { chat } from '@tanstack/ai' |
| 7 | + |
| 8 | +// Load environment variables from .env.local manually |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 10 | +try { |
| 11 | + const envContent = readFileSync(join(__dirname, '.env.local'), 'utf-8') |
| 12 | + envContent.split('\n').forEach((line) => { |
| 13 | + const match = line.match(/^([^=]+)=(.*)$/) |
| 14 | + if (match) { |
| 15 | + process.env[match[1].trim()] = match[2].trim() |
| 16 | + } |
| 17 | + }) |
| 18 | +} catch (e) { |
| 19 | + // .env.local not found |
| 20 | +} |
| 21 | + |
| 22 | +async function testBedrockNovaToolCalling() { |
| 23 | + console.log('Testing Bedrock tool calling (Amazon Nova Pro)\n') |
| 24 | + |
| 25 | + const stream = await chat({ |
| 26 | + adapter: bedrockText('us.amazon.nova-pro-v1:0', { |
| 27 | + region: process.env.AWS_REGION || 'us-west-2', |
| 28 | + credentials: { |
| 29 | + accessKeyId: process.env.AWS_ACCESS_KEY_ID || '', |
| 30 | + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '', |
| 31 | + } |
| 32 | + }), |
| 33 | + messages: [ |
| 34 | + { |
| 35 | + role: 'user', |
| 36 | + content: 'Use the `get_temperature` tool to find the temperature in New York and explain why it is the way it is.', |
| 37 | + }, |
| 38 | + ], |
| 39 | + tools: [ |
| 40 | + { |
| 41 | + name: 'get_temperature', |
| 42 | + description: 'Get the current temperature for a specific location', |
| 43 | + inputSchema: z.object({ |
| 44 | + location: z.string().describe('The city or location'), |
| 45 | + unit: z.enum(['celsius', 'fahrenheit']).describe('The temperature unit'), |
| 46 | + }), |
| 47 | + execute: async ({ location, unit }: { location: string; unit: string }) => { |
| 48 | + console.log(`\n[TOOL Temperature] Fetching for ${location}...`) |
| 49 | + return { |
| 50 | + temperature: 45, |
| 51 | + unit: unit, |
| 52 | + condition: 'Cloudy', |
| 53 | + } |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
| 57 | + stream: true, |
| 58 | + }) |
| 59 | + |
| 60 | + let finalContent = '' |
| 61 | + let hasThinking = false |
| 62 | + let toolCallCount = 0 |
| 63 | + |
| 64 | + console.log('--- Stream Output ---') |
| 65 | + for await (const chunk of stream) { |
| 66 | + if (chunk.type === 'thinking') { |
| 67 | + hasThinking = true |
| 68 | + } else if (chunk.type === 'content') { |
| 69 | + process.stdout.write(chunk.delta) |
| 70 | + finalContent += chunk.delta |
| 71 | + } else if (chunk.type === 'tool_call') { |
| 72 | + toolCallCount++ |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + console.log('--- Test Results ---') |
| 77 | + console.log('Thinking detected:', hasThinking) |
| 78 | + console.log('Tool calls:', toolCallCount) |
| 79 | + console.log('Final content length:', finalContent.length) |
| 80 | + |
| 81 | + if (!hasThinking) { |
| 82 | + console.warn('Warning: No thinking blocks detected') |
| 83 | + } |
| 84 | + |
| 85 | + if (finalContent.includes('<thinking>')) { |
| 86 | + console.error('Test failed: Thinking tags found in final content') |
| 87 | + process.exit(1) |
| 88 | + } |
| 89 | + |
| 90 | + if (!finalContent || finalContent.trim().length === 0) { |
| 91 | + console.error('Test failed: No final content - model should explain the temperature') |
| 92 | + process.exit(1) |
| 93 | + } |
| 94 | + |
| 95 | + console.log('Test passed') |
| 96 | +} |
| 97 | + |
| 98 | +testBedrockNovaToolCalling() |
0 commit comments