From 9a05faa170abd75ee33af04de5872b75b9c1a46b Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Thu, 27 Feb 2025 17:33:17 -0800 Subject: [PATCH] improvement(tool-calling): adding parameter-only mode --- providers/openai/index.ts | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/providers/openai/index.ts b/providers/openai/index.ts index 78a32a863b..1cea690a85 100644 --- a/providers/openai/index.ts +++ b/providers/openai/index.ts @@ -11,6 +11,9 @@ export const openaiProvider: ProviderConfig = { defaultModel: 'gpt-4o', executeRequest: async (request: ProviderRequest): Promise => { + // Add tool execution mode with default of 'parameters-only' + const toolExecutionMode = 'parameters-only' // 'sync', 'parameters-only' + if (!request.apiKey) { throw new Error('API key is required for OpenAI') } @@ -88,13 +91,39 @@ export const openaiProvider: ProviderConfig = { `[OpenAI Provider] First request took ${firstRequestEndTime - firstRequestStartTime}ms` ) + // Extract content and token information let content = currentResponse.choices[0]?.message?.content || '' let tokens = { prompt: currentResponse.usage?.prompt_tokens || 0, completion: currentResponse.usage?.completion_tokens || 0, total: currentResponse.usage?.total_tokens || 0, } - let toolCalls = [] + + // Extract tool calls if present + let toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls + const toolCalls = + toolCallsInResponse?.map((toolCall) => ({ + name: toolCall.function.name, + arguments: JSON.parse(toolCall.function.arguments), + })) || [] + + // If parameters-only mode or no tool calls, return immediately + if (toolExecutionMode === 'parameters-only' || !toolCalls.length) { + const endTime = Date.now() + console.log( + `[OpenAI Provider] Completed request at ${new Date(endTime).toISOString()} (${endTime - startTime}ms)` + ) + + return { + content, + model: request.model, + tokens, + toolCalls: toolCalls.length > 0 ? toolCalls : undefined, + // No tool results since we didn't execute them + } + } + + // If we reach here, we're in sync mode and need to execute tools let toolResults = [] let currentMessages = [...allMessages] let iterationCount = 0 @@ -103,7 +132,6 @@ export const openaiProvider: ProviderConfig = { try { while (iterationCount < MAX_ITERATIONS) { // Check for tool calls - const toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls if (!toolCallsInResponse || toolCallsInResponse.length === 0) { break } @@ -125,10 +153,6 @@ export const openaiProvider: ProviderConfig = { if (!result.success) continue toolResults.push(result.output) - toolCalls.push({ - name: toolName, - arguments: toolArgs, - }) // Add the tool call and result to messages currentMessages.push({ @@ -170,6 +194,9 @@ export const openaiProvider: ProviderConfig = { content = currentResponse.choices[0].message.content } + // Get new tool calls for next iteration + toolCallsInResponse = currentResponse.choices[0]?.message?.tool_calls + // Update token counts if (currentResponse.usage) { tokens.prompt += currentResponse.usage.prompt_tokens || 0