From 858282929c3aa822d8767fbd1648f68314514272 Mon Sep 17 00:00:00 2001 From: geoffsee <> Date: Tue, 8 Jul 2025 11:47:46 -0400 Subject: [PATCH] Refactor `chat-stream-provider` to simplify tool structure. Optimize `WeatherTool` implementation with enriched function schema. --- .../ai/src/providers/chat-stream-provider.ts | 30 ++----------------- packages/ai/src/tools/basic.ts | 21 +++++++++++-- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/packages/ai/src/providers/chat-stream-provider.ts b/packages/ai/src/providers/chat-stream-provider.ts index 7589498..2534224 100644 --- a/packages/ai/src/providers/chat-stream-provider.ts +++ b/packages/ai/src/providers/chat-stream-provider.ts @@ -1,7 +1,7 @@ import { OpenAI } from 'openai'; import ChatSdk from '../chat-sdk/chat-sdk.ts'; -import { BasicValueTool, WeatherTool } from '../tools/basic.ts'; +import { WeatherTool } from '../tools/basic.ts'; import type { GenericEnv } from '../types'; export interface CommonProviderParams { @@ -37,31 +37,7 @@ export abstract class BaseChatProvider implements ChatStreamProvider { const client = this.getOpenAIClient(param); - // const tools = [WeatherTool]; - const tools = [ - { - type: 'function', - function: { - name: 'getCurrentTemperature', - description: 'Get the current temperature for a specific location', - parameters: { - type: 'object', - properties: { - location: { - type: 'string', - description: 'The city and state, e.g., San Francisco, CA', - }, - unit: { - type: 'string', - enum: ['Celsius', 'Fahrenheit'], - description: "The temperature unit to use. Infer this from the user's location.", - }, - }, - required: ['location', 'unit'], - }, - }, - }, - ]; + const tools = [WeatherTool]; const getCurrentTemp = (location: string) => { return '20C'; @@ -89,7 +65,7 @@ export abstract class BaseChatProvider implements ChatStreamProvider { const toolCalls: any[] = []; for await (const chunk of stream as unknown as AsyncIterable) { - console.log('chunk', chunk); + // console.log('chunk', chunk); // Handle tool calls if (chunk.choices[0]?.delta?.tool_calls) { diff --git a/packages/ai/src/tools/basic.ts b/packages/ai/src/tools/basic.ts index 632d38a..756eac3 100644 --- a/packages/ai/src/tools/basic.ts +++ b/packages/ai/src/tools/basic.ts @@ -34,8 +34,23 @@ export const WeatherTool = { required: ['location'], additionalProperties: false, }, - function: async (params: { location: string }) => { - console.log('[WeatherTool] Getting weather for:', params.location); - return { temperature: '25°C' }; + function: { + name: 'getCurrentTemperature', + description: 'Get the current temperature for a specific location', + parameters: { + type: 'object', + properties: { + location: { + type: 'string', + description: 'The city and state, e.g., San Francisco, CA', + }, + unit: { + type: 'string', + enum: ['Celsius', 'Fahrenheit'], + description: "The temperature unit to use. Infer this from the user's location.", + }, + }, + required: ['location', 'unit'], + }, }, };