Compare commits

..

No commits in common. "0e3962e1104bf07dd80eac0884159ac635a63e81" and "124ac5cbf044675bfdf99c85e43c317e379b778f" have entirely different histories.

2 changed files with 10 additions and 26 deletions

View file

@ -260,10 +260,12 @@ 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({
role: "function",
name: generatedMessage.function_call.name,
// FIXME: don't use new instance of FunctionManager // FIXME: don't use new instance of FunctionManager
OpenAImessages.push( content: new FunctionManager().handleFunction(generatedMessage.function_call),
new FunctionManager().handleFunction(generatedMessage.function_call) });
);
} }
} while (generatedMessage.function_call); } while (generatedMessage.function_call);

View file

@ -1,5 +1,4 @@
import { ChatCompletionFunctions, ChatCompletionRequestMessage, ChatCompletionRequestMessageFunctionCall } from "openai"; import { ChatCompletionFunctions, ChatCompletionRequestMessageFunctionCall } from "openai";
import config from "./config"; import config from "./config";
type parameterMap = { type parameterMap = {
@ -61,32 +60,15 @@ export default class FunctionManager {
return rvalue; return rvalue;
} }
public handleFunction(request: ChatCompletionRequestMessageFunctionCall): ChatCompletionRequestMessage { public handleFunction(request: ChatCompletionRequestMessageFunctionCall) {
// 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 {
parsedArguments = JSON.parse(request.arguments ?? ""); const 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),
};
} }
} }