Skip to content

Commit

Permalink
Merge pull request #115 from harmony-one/chat-stream
Browse files Browse the repository at this point in the history
add chat status while streaming
  • Loading branch information
theofandrich authored Aug 17, 2023
2 parents 43aef8c + 3db05d8 commit cf995b1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/modules/open-ai/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export const streamChatCompletion = async (
};
let completion = "";
let msgId = 0;
let showStatus = false;
return new Promise<string>(async (resolve, reject) => {
const res = await openai.createChatCompletion(
payload as CreateChatCompletionRequest,
Expand All @@ -176,6 +177,7 @@ export const streamChatCompletion = async (
for (const line of lines) {
const message = line.replace(/^data: /, "");
if (message === "[DONE]") {
showStatus = false;
if (!completion.endsWith(".")) {
if (msgId === 0) {
msgId = (await ctx.reply(completion)).message_id;
Expand All @@ -202,6 +204,13 @@ export const streamChatCompletion = async (
.editMessageText(ctx.chat?.id!, msgId, completion)
.catch((e: any) => console.log(e));
}
if (!showStatus) {
await ctx.replyWithChatAction("typing");
showStatus = true;
setTimeout(async () => {
showStatus = false;
}, 10000);
}
}
} catch (error) {
logger.error("Could not JSON parse stream message", message, error);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/qrcode/QRCodeBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Please add <URL> <PROMPT>
return
}

ctx.reply(`Generating...`);
// ctx.reply(`Generating...`);

const messageText = message;

Expand Down
6 changes: 3 additions & 3 deletions src/modules/sd-images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class SDImagesBot {
let idx = this.queue.findIndex(v => v === uuid);

if (idx !== 0) {
ctx.reply(`${author} you are the ${idx + 1}/${this.queue.length}. Please wait about ${idx * 30} sec`);
ctx.reply(`You are ${idx + 1} of ${this.queue.length}. Please wait ~${idx * 30}seconds`);
}

// waiting queue
Expand All @@ -119,7 +119,7 @@ export class SDImagesBot {
idx = this.queue.findIndex(v => v === uuid);
}

// ctx.reply(`${author} starting to generate your image`);
// ctx.reply(`Generating...`);
ctx.api.sendChatAction(ctx.chat?.id!,'upload_photo')
const imageBuffer = await this.sdNodeApi.generateImage(prompt);

Expand Down Expand Up @@ -161,7 +161,7 @@ export class SDImagesBot {
let idx = this.queue.findIndex(v => v === uuid);

if (idx !== 0) {
ctx.reply(`${author} you are the ${idx + 1}/${this.queue.length}. Please wait about ${idx * 30} sec`);
ctx.reply(`You are ${idx + 1} of ${this.queue.length}. Please wait ~${idx * 30}seconds`);
}

// waiting queue
Expand Down

0 comments on commit cf995b1

Please sign in to comment.