Skip to content

Commit

Permalink
fix(openai.js): bugs of chatWithAI
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonictw committed Jun 11, 2024
1 parent db53fe6 commit bd3b96e
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/clients/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,39 @@ async function chatWithAI(chatId, prompt) {
if (!chatHistoryMapper.has(chatId)) {
chatHistoryMapper.set(chatId, []);
}
const chatHistory = chatHistoryMapper.get(chatId);

const messages = [
...chatHistory,
...prependPrompts,
{
role: "user",
content: prompt,
},
];
const chatHistory = chatHistoryMapper.get(chatId);
const userPromptMessage = {
role: "user",
content: prompt,
};

const response = await client.chat.completions.create({
model: chatModel,
messages,
messages: [
...chatHistory,
...prependPrompts,
userPromptMessage,
],
});

const choice = choose(response.choices);
const content = choice.message.content;
chatHistory.push({
const reply = choice.message.content;
const assistantReplyMessage = {
role: "assistant",
content,
});
content: reply,
};

chatHistory.push(
userPromptMessage,
assistantReplyMessage,
);
if (chatHistory.length > 30) {
chatHistory.shift();
chatHistory.shift();
}

return choice.message.content;
return reply;
}

exports.useClient = () => client;
Expand Down

0 comments on commit bd3b96e

Please sign in to comment.