@@ -2,20 +2,24 @@ import OpenAI from 'openai';
22
33const TOKEN_REPEAT_COUNT = 2048 ;
44
5- const calculatorTool = {
5+ const weatherTool = {
66 type : "function" ,
77 function : {
8- name : "calculate " ,
9- description : "Calculate a mathematical expression " ,
8+ name : "get_current_weather " ,
9+ description : "Get the current weather in a given location " ,
1010 parameters : {
1111 type : "object" ,
1212 properties : {
13- expression : {
13+ location : {
1414 type : "string" ,
15- description : "The mathematical expression to calculate"
15+ description : "The city and state, e.g. San Francisco, CA"
16+ } ,
17+ unit : {
18+ type : "string" ,
19+ enum : [ "celsius" , "fahrenheit" ]
1620 }
1721 } ,
18- required : [ "expression " ]
22+ required : [ "location" , "unit "]
1923 }
2024 }
2125} ;
@@ -121,14 +125,6 @@ function processNonStreamingResponse(completion: any): { complete: any } {
121125 return { complete : completion . choices [ 0 ] . message } ;
122126}
123127
124- function executeCalculator ( expression : string ) : number {
125- try {
126- return eval ( expression ) ;
127- } catch ( error ) {
128- throw new Error ( `Failed to calculate expression: ${ expression } ` ) ;
129- }
130- }
131-
132128export async function CreateChatCompletion ( params : {
133129 client : OpenAI ,
134130 model : string ,
@@ -148,10 +144,12 @@ export async function CreateChatCompletion(params: {
148144
149145 const responses : Array < { stream ?: any [ ] , complete : any } > = [ ] ;
150146
151- const baseMessage = useTools ? "What is 50+50? Use the tool." : "What is 50+50?" ;
147+ const baseMessage = useTools
148+ ? "What is the weather in San Francisco in Celsius? Tell me your plan, then use the tool."
149+ : "What is the weather in San Francisco in Celsius?" ;
152150 const userMessage = baseMessage ;
153151 const messages = [ { role : "user" , content : userMessage } ] ;
154- const tools = useTools ? [ calculatorTool , searchWebTool ] : undefined ;
152+ const tools = useTools ? [ weatherTool , searchWebTool ] : undefined ;
155153
156154 const completion = await makeRequest ( client , model , stream , testProviderCache , cacheTriggerToken , messages , tools ) ;
157155
@@ -163,16 +161,15 @@ export async function CreateChatCompletion(params: {
163161
164162 if ( useTools && firstResponse . complete . tool_calls && firstResponse . complete . tool_calls . length > 0 ) {
165163 const toolCall = firstResponse . complete . tool_calls [ 0 ] ;
166- if ( toolCall . function . name === "calculate" ) {
167- const expression = JSON . parse ( toolCall . function . arguments ) . expression ;
168- const result = executeCalculator ( expression ) ;
164+ if ( toolCall . function . name === "get_current_weather" ) {
165+ const result = "It is sunny at 30 degrees Celsius in San Francisco." ;
169166
170167 const finalMessages = [
171168 ...messages ,
172169 firstResponse . complete ,
173170 {
174171 role : "tool" ,
175- content : result . toString ( ) ,
172+ content : result ,
176173 tool_call_id : toolCall . id
177174 }
178175 ] ;
0 commit comments