Compare commits
2 commits
124ac5cbf0
...
0e3962e110
Author | SHA1 | Date | |
---|---|---|---|
0e3962e110 | |||
000641bcfc |
2 changed files with 26 additions and 10 deletions
|
@ -260,12 +260,10 @@ async function executeFromQueue(channel: string) {
|
|||
// handle function calls
|
||||
if (generatedMessage.function_call) {
|
||||
OpenAImessages.push(generatedMessage);
|
||||
OpenAImessages.push({
|
||||
role: "function",
|
||||
name: generatedMessage.function_call.name,
|
||||
// FIXME: don't use new instance of FunctionManager
|
||||
content: new FunctionManager().handleFunction(generatedMessage.function_call),
|
||||
});
|
||||
OpenAImessages.push(
|
||||
new FunctionManager().handleFunction(generatedMessage.function_call)
|
||||
);
|
||||
}
|
||||
} while (generatedMessage.function_call);
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { ChatCompletionFunctions, ChatCompletionRequestMessageFunctionCall } from "openai";
|
||||
import { ChatCompletionFunctions, ChatCompletionRequestMessage, ChatCompletionRequestMessageFunctionCall } from "openai";
|
||||
|
||||
import config from "./config";
|
||||
|
||||
type parameterMap = {
|
||||
|
@ -60,15 +61,32 @@ export default class FunctionManager {
|
|||
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 {
|
||||
const parsedArguments = JSON.parse(request.arguments ?? "");
|
||||
return this.store.get(request.name ?? "")?.execute(parsedArguments);
|
||||
parsedArguments = JSON.parse(request.arguments ?? "");
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Function arguments raw: " + request.arguments);
|
||||
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue