Compare commits

..

2 commits

Author SHA1 Message Date
0e3962e110 Handle unregistered function calls 2023-07-30 01:51:40 +02:00
000641bcfc Do not catch function execution error when catching errors of json parsing.
Errors from model function calling should propagate up instead of being catched there.
2023-07-30 01:43:41 +02:00
2 changed files with 26 additions and 10 deletions

View file

@ -260,12 +260,10 @@ async function executeFromQueue(channel: string) {
// handle function calls // handle function calls
if (generatedMessage.function_call) { if (generatedMessage.function_call) {
OpenAImessages.push(generatedMessage); OpenAImessages.push(generatedMessage);
OpenAImessages.push({ // FIXME: don't use new instance of FunctionManager
role: "function", OpenAImessages.push(
name: generatedMessage.function_call.name, new FunctionManager().handleFunction(generatedMessage.function_call)
// FIXME: don't use new instance of FunctionManager );
content: new FunctionManager().handleFunction(generatedMessage.function_call),
});
} }
} while (generatedMessage.function_call); } while (generatedMessage.function_call);

View file

@ -1,4 +1,5 @@
import { ChatCompletionFunctions, ChatCompletionRequestMessageFunctionCall } from "openai"; import { ChatCompletionFunctions, ChatCompletionRequestMessage, ChatCompletionRequestMessageFunctionCall } from "openai";
import config from "./config"; import config from "./config";
type parameterMap = { type parameterMap = {
@ -60,15 +61,32 @@ export default class FunctionManager {
return rvalue; return rvalue;
} }
public handleFunction(request: ChatCompletionRequestMessageFunctionCall) { public handleFunction(request: ChatCompletionRequestMessageFunctionCall): ChatCompletionRequestMessage {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let parsedArguments: any;
const functionToRun = this.store.get(request.name ?? "");
// check if the function is registered
if (!functionToRun) {
return {
role: "system",
content: "Only use functions that were provided to you",
};
}
try { try {
const parsedArguments = JSON.parse(request.arguments ?? ""); parsedArguments = JSON.parse(request.arguments ?? "");
return this.store.get(request.name ?? "")?.execute(parsedArguments);
} }
catch (e) { catch (e) {
console.error("Function arguments raw: " + request.arguments); console.error("Function arguments raw: " + request.arguments);
throw new Error(`Failed to parse the function JSON arguments when running function [${request.name}]`, {cause: e}); throw new Error(`Failed to parse the function JSON arguments when running function [${request.name}]`, {cause: e});
} }
return {
role: "function",
name: request.name,
content: functionToRun.execute(parsedArguments),
};
} }
} }