Simplify the ChatCompletion calling loop
removes duplicate code in while loop
This commit is contained in:
parent
56869a2dc2
commit
124ac5cbf0
1 changed files with 17 additions and 23 deletions
|
@ -1,5 +1,5 @@
|
||||||
import DiscordApi, { GuildTextBasedChannel } from "discord.js";
|
import DiscordApi, { GuildTextBasedChannel } from "discord.js";
|
||||||
import { ChatCompletionRequestMessage } from "openai";
|
import { ChatCompletionRequestMessage, ChatCompletionResponseMessage } from "openai";
|
||||||
|
|
||||||
import { database, openai } from "./index";
|
import { database, openai } from "./index";
|
||||||
import Moderation from "./moderation";
|
import Moderation from "./moderation";
|
||||||
|
@ -241,27 +241,10 @@ async function executeFromQueue(channel: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenAImessages = toOpenAIMessages(messages);
|
OpenAImessages = toOpenAIMessages(messages);
|
||||||
let answer = await openai.createChatCompletion({
|
let generatedMessage: ChatCompletionResponseMessage | undefined = undefined;
|
||||||
...config.chatCompletionConfig,
|
let answer: Awaited<ReturnType<typeof openai.createChatCompletion>>;
|
||||||
messages: OpenAImessages,
|
|
||||||
// FIXME: don't use new instance of FunctionManager
|
|
||||||
functions: new FunctionManager().getFunctions(),
|
|
||||||
});
|
|
||||||
|
|
||||||
logUsedTokens(answer, message, ++functionRanCounter);
|
do {
|
||||||
|
|
||||||
let generatedMessage = answer.data.choices[0].message;
|
|
||||||
if (!generatedMessage) throw new Error("empty message received");
|
|
||||||
|
|
||||||
// handle function calls
|
|
||||||
while (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),
|
|
||||||
});
|
|
||||||
answer = await openai.createChatCompletion({
|
answer = await openai.createChatCompletion({
|
||||||
...config.chatCompletionConfig,
|
...config.chatCompletionConfig,
|
||||||
messages: OpenAImessages,
|
messages: OpenAImessages,
|
||||||
|
@ -272,8 +255,19 @@ async function executeFromQueue(channel: string) {
|
||||||
logUsedTokens(answer, message, ++functionRanCounter);
|
logUsedTokens(answer, message, ++functionRanCounter);
|
||||||
|
|
||||||
generatedMessage = answer.data.choices[0].message;
|
generatedMessage = answer.data.choices[0].message;
|
||||||
if (!generatedMessage) throw new Error("empty message received");
|
if (!generatedMessage) throw new Error("Empty message received");
|
||||||
|
|
||||||
|
// 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),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
} while (generatedMessage.function_call);
|
||||||
|
|
||||||
const answerContent = answer.data.choices[0].message?.content;
|
const answerContent = answer.data.choices[0].message?.content;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue