Skip to content

Commit

Permalink
Merge pull request #130 from harmony-one/ask35
Browse files Browse the repository at this point in the history
add ask35 logic + add/delete message to end typing status
  • Loading branch information
theofandrich authored Aug 21, 2023
2 parents 06728f7 + 6fd26f3 commit f3d70e1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ bot.catch((err) => {
logger.error(`Error while handling update ${ctx.update.update_id}:`);
const e = err.error;
if (e instanceof GrammyError) {
console.log(e)
logger.error("Error in request:", e.description);
} else if (e instanceof HttpError) {
logger.error("Could not contact Telegram:", e);
Expand Down
37 changes: 30 additions & 7 deletions src/modules/open-ai/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,30 @@ export const streamChatCompletion = async (
try {
const payload = {
model: model,
max_tokens: limitTokens
? config.openAi.imageGen.completions.maxTokens
: undefined,
max_tokens: 800,
// limitTokens
// ? config.openAi.imageGen.completions.maxTokens
// : undefined,
temperature: config.openAi.imageGen.completions.temperature,
messages: conversation,
stream: true,
};
let completion = "";
let msgId = 0;
let msgId = (
await ctx.reply(
`_${ctx.session.openAi.chatGpt.model.toLocaleUpperCase()}_`,
{
parse_mode: "Markdown",
}
)
).message_id;
ctx.chatAction = "typing";
return new Promise<string>(async (resolve, reject) => {
const res = await openai.createChatCompletion(
payload as CreateChatCompletionRequest,
{ responseType: "stream" }
);
let wordCount = 0;
//@ts-ignore
res.data.on("data", async (data: any) => {
const lines = data
Expand All @@ -181,27 +191,40 @@ export const streamChatCompletion = async (
if (!completion.endsWith(".")) {
if (msgId === 0) {
msgId = (await ctx.reply(completion)).message_id;
resolve(completion);
return;
}
}
await ctx.api
.editMessageText(ctx.chat?.id!, msgId, completion)
.catch((e: any) => console.log(e));
// const msgIdEnd = (
// await ctx.reply(`_done_`, {
// // with ${ctx.session.openAi.chatGpt.model.toLocaleUpperCase()}
// parse_mode: "Markdown",
// })
// ).message_id;
ctx.api.deleteMessage(ctx.chat?.id!, msgId); // msgIdEnd);
ctx.reply(completion);
resolve(completion);
return;
}
try {
wordCount++;
const parsed = JSON.parse(message);
// console.log(parsed.choices[0].delta.content, wordCount)
completion +=
parsed.choices[0].delta.content !== undefined
? parsed.choices[0].delta.content
: "";
if (parsed.choices[0].delta.content === ".") {
if (msgId === 0) {
msgId = (await ctx.reply(completion)).message_id;
ctx.chatAction = "typing";
} else {
// msgId = (await ctx.reply(completion)).message_id;
// ctx.chatAction = "typing";
} else if (wordCount > 30) {
completion = completion.replaceAll("..", "");
completion += "..";
wordCount = 0;
ctx.api
.editMessageText(ctx.chat?.id!, msgId, completion)
.catch((e: any) => console.log(e));
Expand Down
6 changes: 3 additions & 3 deletions src/modules/open-ai/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export const promptGen = async (data: ChatGptPayload) => {
return price
}
return 0
} catch (e) {
logger.error("promptGen Error", e);
throw "There was an error while generating the image";
} catch (e: any) {
logger.error(`promptGen Error ${e.toString()}`);
throw e;
}
};
19 changes: 17 additions & 2 deletions src/modules/open-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { alterImg, imgGen, imgGenEnhanced, promptGen } from "./controller";
import { Logger, pino } from "pino";
import { appText } from "./utils/text";
import { chatService } from "../../database/services";
import { ChatGPTModelsEnum } from "./types";

export const SupportedCommands = {
// chat: {
Expand All @@ -23,6 +24,11 @@ export const SupportedCommands = {
groupParams: ">0",
privateParams: ">0",
},
ask35: {
name: "ask35",
groupParams: ">0",
privateParams: ">0",
},
gpt4: {
name: "gpt4",
groupParams: ">0",
Expand Down Expand Up @@ -224,6 +230,13 @@ export class OpenAIBot {
// }

if (ctx.hasCommand(SupportedCommands.ask.name)) {
ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4
await this.onChat(ctx);
return;
}

if (ctx.hasCommand(SupportedCommands.ask35.name)) {
ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_35_TURBO
await this.onChat(ctx);
return;
}
Expand Down Expand Up @@ -361,7 +374,9 @@ export class OpenAIBot {
await ctx.reply(msg, { parse_mode: "Markdown" });
return;
}

// if (chatConversation.length === 0) {
// ctx.reply(`_Using model ${ctx.session.openAi.chatGpt.model}_`,{ parse_mode: "Markdown" })
// }
chatConversation.push({
role: "user",
content: `${this.hasPrefix(prompt) ? prompt.slice(1) : prompt}.`,
Expand Down Expand Up @@ -389,7 +404,7 @@ export class OpenAIBot {
}
} catch (error: any) {
ctx.chatAction = null;
this.logger.error(error.toString());
this.logger.error(`onChat: ${error.toString()}`);
await ctx.reply("Error handling your request");
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class BotPayments {
}

public toONE(amount: BigNumber, roundCeil = true) {
console.log(amount, amount.toFixed())
const value = this.web3.utils.fromWei(amount.toFixed(), 'ether')
if(roundCeil) {
return Math.ceil(+value)
Expand Down Expand Up @@ -346,6 +347,7 @@ export class BotPayments {
const freeCredits = await chatService.getBalance(accountId)
const addressBalance = await this.getAddressBalance(account.address);
const balance = addressBalance.plus(freeCredits)
console.log('balance',balance)
const balanceOne = this.toONE(balance, false);
ctx.reply(
`Your credits in ONE tokens: ${balanceOne.toFixed(2)}
Expand Down

0 comments on commit f3d70e1

Please sign in to comment.