Refactor chat-stream-provider to simplify tool structure. Optimize WeatherTool implementation with enriched function schema.

This commit is contained in:
geoffsee
2025-07-08 11:47:46 -04:00
committed by Geoff Seemueller
parent 06b6a68b9b
commit 858282929c
2 changed files with 21 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
import { OpenAI } from 'openai'; import { OpenAI } from 'openai';
import ChatSdk from '../chat-sdk/chat-sdk.ts'; 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'; import type { GenericEnv } from '../types';
export interface CommonProviderParams { export interface CommonProviderParams {
@@ -37,31 +37,7 @@ export abstract class BaseChatProvider implements ChatStreamProvider {
const client = this.getOpenAIClient(param); const client = this.getOpenAIClient(param);
// const tools = [WeatherTool]; 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 getCurrentTemp = (location: string) => { const getCurrentTemp = (location: string) => {
return '20C'; return '20C';
@@ -89,7 +65,7 @@ export abstract class BaseChatProvider implements ChatStreamProvider {
const toolCalls: any[] = []; const toolCalls: any[] = [];
for await (const chunk of stream as unknown as AsyncIterable<any>) { for await (const chunk of stream as unknown as AsyncIterable<any>) {
console.log('chunk', chunk); // console.log('chunk', chunk);
// Handle tool calls // Handle tool calls
if (chunk.choices[0]?.delta?.tool_calls) { if (chunk.choices[0]?.delta?.tool_calls) {

View File

@@ -34,8 +34,23 @@ export const WeatherTool = {
required: ['location'], required: ['location'],
additionalProperties: false, additionalProperties: false,
}, },
function: async (params: { location: string }) => { function: {
console.log('[WeatherTool] Getting weather for:', params.location); name: 'getCurrentTemperature',
return { temperature: '25°C' }; 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'],
},
}, },
}; };