@flowlab/all
Version:
A cool library focusing on handling various flows
44 lines (38 loc) • 1.95 kB
text/typescript
import { BaseAINode, BaseAINodeInput } from './baseAINode';
import { INodeContext } from '@flowlab/core';
import { IAIProvider, AIModelOptions } from '../types';
interface FunctionCallingNodeInput extends BaseAINodeInput {
promptTemplate: string;
functions: any[]; // Array of function definitions (e.g., OpenAI format)
}
export class FunctionCallingNode extends BaseAINode {
id = 'FunctionCallingNode';
// MARK: 执行AI
protected async executeAI(context: INodeContext, provider: IAIProvider, renderedPrompt: string): Promise<Record<string, any>> {
const nodeInput = context.input as FunctionCallingNodeInput;
if (!Array.isArray(nodeInput.functions) || nodeInput.functions.length === 0) {
throw new Error("Missing or invalid 'functions' array input for FunctionCallingNode");
}
const options = nodeInput.modelOptions;
const { functionCall, text, usage } = await provider.decideFunctionCall(renderedPrompt, nodeInput.functions, options, context);
// Output structure indicates if a function was requested or just text returned
return {
functionCall: functionCall, // { name: 'funcName', arguments: { arg1: 'val1' } } or undefined
responseText: text, // Text response if no function called
usage
};
}
// MARK: 验证输入
validateInput(input: Record<string, any>): boolean {
if (!super.validateInput(input)) return false;
if (!input.promptTemplate || typeof input.promptTemplate !== 'string') {
console.error(`Validation Error [${this.id}]: Missing or invalid 'promptTemplate'.`);
return false;
}
if (!Array.isArray(input.functions) || input.functions.length === 0) {
console.error(`Validation Error [${this.id}]: Missing or invalid 'functions' array.`);
return false;
}
return true;
}
}