|
| 1 | +import { createOpenAI } from '../src/index' |
| 2 | +import { readFileSync } from 'fs' |
| 3 | +import { join, dirname } from 'path' |
| 4 | +import { fileURLToPath } from 'url' |
| 5 | + |
| 6 | +// Load environment variables from .env.local manually |
| 7 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 8 | +try { |
| 9 | + const envContent = readFileSync(join(__dirname, '.env.local'), 'utf-8') |
| 10 | + envContent.split('\n').forEach((line) => { |
| 11 | + const match = line.match(/^([^=]+)=(.*)$/) |
| 12 | + if (match) { |
| 13 | + process.env[match[1].trim()] = match[2].trim() |
| 14 | + } |
| 15 | + }) |
| 16 | +} catch (e) { |
| 17 | + // .env.local not found, will use process.env |
| 18 | +} |
| 19 | + |
| 20 | +const apiKey = process.env.OPENAI_API_KEY |
| 21 | + |
| 22 | +if (!apiKey) { |
| 23 | + console.error('❌ OPENAI_API_KEY not found in .env.local') |
| 24 | + process.exit(1) |
| 25 | +} |
| 26 | + |
| 27 | +async function testToolWithOptionalParameters() { |
| 28 | + console.log('🚀 Testing OpenAI tool calling with OPTIONAL parameters\n') |
| 29 | + |
| 30 | + const adapter = createOpenAI(apiKey) |
| 31 | + |
| 32 | + // Create a tool with optional parameters (unit is optional) |
| 33 | + const getTemperatureTool = { |
| 34 | + type: 'function' as const, |
| 35 | + function: { |
| 36 | + name: 'get_temperature', |
| 37 | + description: 'Get the current temperature for a specific location', |
| 38 | + parameters: { |
| 39 | + type: 'object', |
| 40 | + properties: { |
| 41 | + location: { |
| 42 | + type: 'string', |
| 43 | + description: 'The city or location to get the temperature for', |
| 44 | + }, |
| 45 | + unit: { |
| 46 | + type: 'string', |
| 47 | + enum: ['celsius', 'fahrenheit'], |
| 48 | + description: |
| 49 | + 'The temperature unit (optional, defaults to fahrenheit)', |
| 50 | + }, |
| 51 | + }, |
| 52 | + required: ['location'], // unit is optional |
| 53 | + }, |
| 54 | + }, |
| 55 | + execute: async (args: any) => { |
| 56 | + console.log( |
| 57 | + '✅ Tool executed with arguments:', |
| 58 | + JSON.stringify(args, null, 2), |
| 59 | + ) |
| 60 | + |
| 61 | + if (!args || !args.location) { |
| 62 | + console.error('❌ ERROR: Location argument is missing!') |
| 63 | + return 'Error: Location is required' |
| 64 | + } |
| 65 | + |
| 66 | + const unit = args.unit || 'fahrenheit' |
| 67 | + console.log(` - location: "${args.location}"`) |
| 68 | + console.log( |
| 69 | + ` - unit: "${unit}" (${args.unit ? 'provided' : 'defaulted'})`, |
| 70 | + ) |
| 71 | + |
| 72 | + return `The temperature in ${args.location} is 72°${unit === 'celsius' ? 'C' : 'F'}` |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + const messages = [ |
| 77 | + { |
| 78 | + role: 'user' as const, |
| 79 | + content: |
| 80 | + 'What is the temperature in Paris? Use the get_temperature tool.', |
| 81 | + }, |
| 82 | + ] |
| 83 | + |
| 84 | + console.log('📤 Sending request with tool:') |
| 85 | + console.log(' Tool name:', getTemperatureTool.function.name) |
| 86 | + console.log( |
| 87 | + ' Required params:', |
| 88 | + getTemperatureTool.function.parameters.required, |
| 89 | + ) |
| 90 | + console.log(' Optional params:', ['unit']) |
| 91 | + console.log(' User message:', messages[0].content) |
| 92 | + console.log() |
| 93 | + |
| 94 | + try { |
| 95 | + console.log('📥 Streaming response...\n') |
| 96 | + |
| 97 | + let toolCallFound = false |
| 98 | + let toolCallArguments: any = null |
| 99 | + let toolExecuted = false |
| 100 | + let finalResponse = '' |
| 101 | + |
| 102 | + // @ts-ignore - using internal chat method |
| 103 | + const stream = adapter.chatStream({ |
| 104 | + model: 'gpt-4o-mini', |
| 105 | + messages, |
| 106 | + tools: [getTemperatureTool], |
| 107 | + }) |
| 108 | + |
| 109 | + for await (const chunk of stream) { |
| 110 | + if (chunk.type === 'tool_call') { |
| 111 | + toolCallFound = true |
| 112 | + toolCallArguments = chunk.toolCall.function.arguments |
| 113 | + console.log('🔧 Tool call detected!') |
| 114 | + console.log(' Name:', chunk.toolCall.function.name) |
| 115 | + console.log(' Arguments (raw):', toolCallArguments) |
| 116 | + |
| 117 | + // Parse if it's a string |
| 118 | + if (typeof toolCallArguments === 'string') { |
| 119 | + try { |
| 120 | + const parsed = JSON.parse(toolCallArguments) |
| 121 | + toolCallArguments = parsed |
| 122 | + } catch (e) { |
| 123 | + console.error(' ❌ Failed to parse arguments:', e) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // Execute the tool |
| 128 | + if (getTemperatureTool.execute) { |
| 129 | + console.log('\n🔨 Executing tool...') |
| 130 | + try { |
| 131 | + const result = await getTemperatureTool.execute(toolCallArguments) |
| 132 | + toolExecuted = true |
| 133 | + console.log(' Result:', result) |
| 134 | + } catch (error) { |
| 135 | + console.error(' ❌ Tool execution error:', error) |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (chunk.type === 'content') { |
| 141 | + finalResponse += chunk.delta |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + console.log('\n' + '='.repeat(60)) |
| 146 | + console.log('📊 Test Summary:') |
| 147 | + console.log(' Tool call found:', toolCallFound ? '✅' : '❌') |
| 148 | + console.log(' Arguments received:', toolCallArguments ? '✅' : '❌') |
| 149 | + console.log(' Tool executed:', toolExecuted ? '✅' : '❌') |
| 150 | + console.log( |
| 151 | + ' Location provided:', |
| 152 | + toolCallArguments?.location ? '✅' : '❌', |
| 153 | + ) |
| 154 | + console.log('='.repeat(60)) |
| 155 | + |
| 156 | + if (!toolCallFound) { |
| 157 | + console.error('\n❌ FAIL: No tool call was detected') |
| 158 | + process.exit(1) |
| 159 | + } |
| 160 | + |
| 161 | + if (!toolCallArguments || !toolCallArguments.location) { |
| 162 | + console.error('\n❌ FAIL: Tool arguments missing or invalid') |
| 163 | + process.exit(1) |
| 164 | + } |
| 165 | + |
| 166 | + if (!toolExecuted) { |
| 167 | + console.error('\n❌ FAIL: Tool was not executed successfully') |
| 168 | + process.exit(1) |
| 169 | + } |
| 170 | + |
| 171 | + console.log( |
| 172 | + '\n✅ SUCCESS: Tool calling with optional parameters works correctly!', |
| 173 | + ) |
| 174 | + process.exit(0) |
| 175 | + } catch (error: any) { |
| 176 | + console.error('\n❌ ERROR:', error.message) |
| 177 | + console.error('Stack:', error.stack) |
| 178 | + process.exit(1) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +testToolWithOptionalParameters() |
0 commit comments