Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
fix: 2000 characters responses
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlolDev committed Dec 16, 2023
1 parent c27deed commit 5f837a2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
47 changes: 41 additions & 6 deletions src/bot/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,30 @@ async function buildInfo(
data.result = data.result.replaceAll("@everyone", "everyone").replaceAll("@here", "here");
// make a regex to replace all mentions of users or roles
data.result = data.result.replaceAll(/<&\d+>/g, "role").replaceAll(/<@\d+>/g, "user");
const characters = data.result.split("");

if (!data.done) {
if (lastUpdate + 1000 < Date.now() && !done) {
// if last update was more than 1 second ago
lastUpdate = Date.now();
await edit({
content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id
}>`,
});
if (characters.length > 2000) {
await edit({
content: `<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id
}>`,
// send result as a file
files: [
{
name: "chat.txt",
blob: new Blob([data.result], { type: "text/plain" }),
},
],
});
} else {
await edit({
content: `${data.result}<${loadingIndicator.emoji.animated ? "a" : ""}:${loadingIndicator.emoji.name}:${loadingIndicator.emoji.id
}>`,
});
}
}
} else {
done = true;
Expand Down Expand Up @@ -205,9 +221,28 @@ async function buildInfo(
// if last update was less than 1 second ago, wait 1 second
if (lastUpdate + 1000 > Date.now()) await delay(1000);
await chargePlan(data.cost, env, "chat", modelName);

await edit({
let contentR: {
content?: string;
files?: {
name: string;
blob: Blob;
}[];
} = {
content: `${data.result}`,
};
if (characters.length > 2000) {
contentR = {
content: "",
files: [
{
name: "chat.txt",
blob: new Blob([data.result], { type: "text/plain" }),
},
],
};
}
await edit({
...contentR,
components: [
{
type: MessageComponentTypes.ActionRow,
Expand Down
9 changes: 2 additions & 7 deletions src/bot/utils/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,11 @@ export async function addMessagesToConversation(conversation: Conversation, mess
const updatedConversation = {
history: {
datasetId: conversation.history.datasetId,
messages: [
...conversation.history.messages,
...messages.map((x) => ({
role: x.role,
content: x.content,
})),
],
messages: [...conversation.history.messages, ...messages],
},
last_update: Date.now(),
};
console.log(updatedConversation);
await update("conversations", conversation.id, updatedConversation);
}
export async function newConversation(messages: ConversationMessage[], userId: string, modelName: string) {
Expand Down

0 comments on commit 5f837a2

Please sign in to comment.