@@ -13,11 +13,8 @@ import {
1313 type BaseFunctionsArgs ,
1414 RunnableToolFunction ,
1515} from './RunnableFunction' ;
16- import { ChatCompletionFunctionRunnerParams , ChatCompletionToolRunnerParams } from './ChatCompletionRunner' ;
17- import {
18- ChatCompletionStreamingFunctionRunnerParams ,
19- ChatCompletionStreamingToolRunnerParams ,
20- } from './ChatCompletionStreamingRunner' ;
16+ import { ChatCompletionToolRunnerParams } from './ChatCompletionRunner' ;
17+ import { ChatCompletionStreamingToolRunnerParams } from './ChatCompletionStreamingRunner' ;
2118import { isAssistantMessage , isFunctionMessage , isToolMessage } from './chatCompletionUtils' ;
2219import { BaseEvents , EventStream } from './EventStream' ;
2320import { ParsedChatCompletion } from '../resources/beta/chat/completions' ;
@@ -266,91 +263,6 @@ export class AbstractChatCompletionRunner<
266263 return await this . _createChatCompletion ( client , params , options ) ;
267264 }
268265
269- protected async _runFunctions < FunctionsArgs extends BaseFunctionsArgs > (
270- client : OpenAI ,
271- params :
272- | ChatCompletionFunctionRunnerParams < FunctionsArgs >
273- | ChatCompletionStreamingFunctionRunnerParams < FunctionsArgs > ,
274- options ?: RunnerOptions ,
275- ) {
276- const role = 'function' as const ;
277- const { function_call = 'auto' , stream, ...restParams } = params ;
278- const singleFunctionToCall = typeof function_call !== 'string' && function_call ?. name ;
279- const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || { } ;
280-
281- const functionsByName : Record < string , RunnableFunction < any > > = { } ;
282- for ( const f of params . functions ) {
283- functionsByName [ f . name || f . function . name ] = f ;
284- }
285-
286- const functions : ChatCompletionCreateParams . Function [ ] = params . functions . map (
287- ( f ) : ChatCompletionCreateParams . Function => ( {
288- name : f . name || f . function . name ,
289- parameters : f . parameters as Record < string , unknown > ,
290- description : f . description ,
291- } ) ,
292- ) ;
293-
294- for ( const message of params . messages ) {
295- this . _addMessage ( message , false ) ;
296- }
297-
298- for ( let i = 0 ; i < maxChatCompletions ; ++ i ) {
299- const chatCompletion : ChatCompletion = await this . _createChatCompletion (
300- client ,
301- {
302- ...restParams ,
303- function_call,
304- functions,
305- messages : [ ...this . messages ] ,
306- } ,
307- options ,
308- ) ;
309- const message = chatCompletion . choices [ 0 ] ?. message ;
310- if ( ! message ) {
311- throw new OpenAIError ( `missing message in ChatCompletion response` ) ;
312- }
313- if ( ! message . function_call ) return ;
314- const { name, arguments : args } = message . function_call ;
315- const fn = functionsByName [ name ] ;
316- if ( ! fn ) {
317- const content = `Invalid function_call: ${ JSON . stringify ( name ) } . Available options are: ${ functions
318- . map ( ( f ) => JSON . stringify ( f . name ) )
319- . join ( ', ' ) } . Please try again`;
320-
321- this . _addMessage ( { role, name, content } ) ;
322- continue ;
323- } else if ( singleFunctionToCall && singleFunctionToCall !== name ) {
324- const content = `Invalid function_call: ${ JSON . stringify ( name ) } . ${ JSON . stringify (
325- singleFunctionToCall ,
326- ) } requested. Please try again`;
327-
328- this . _addMessage ( { role, name, content } ) ;
329- continue ;
330- }
331-
332- let parsed ;
333- try {
334- parsed = isRunnableFunctionWithParse ( fn ) ? await fn . parse ( args ) : args ;
335- } catch ( error ) {
336- this . _addMessage ( {
337- role,
338- name,
339- content : error instanceof Error ? error . message : String ( error ) ,
340- } ) ;
341- continue ;
342- }
343-
344- // @ts -expect-error it can't rule out `never` type.
345- const rawContent = await fn . function ( parsed , this ) ;
346- const content = this . #stringifyFunctionCallResult( rawContent ) ;
347-
348- this . _addMessage ( { role, name, content } ) ;
349-
350- if ( singleFunctionToCall ) return ;
351- }
352- }
353-
354266 protected async _runTools < FunctionsArgs extends BaseFunctionsArgs > (
355267 client : OpenAI ,
356268 params :
0 commit comments