Skip to content

Commit

Permalink
Merge pull request #243 from harmony-one/dev
Browse files Browse the repository at this point in the history
Dev Merge to Master (9/05)
  • Loading branch information
theofandrich authored Sep 5, 2023
2 parents 24adbec + 78aafad commit 0a61b5e
Show file tree
Hide file tree
Showing 19 changed files with 770 additions and 1,382 deletions.
1,078 changes: 296 additions & 782 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@walletconnect/sign-client": "^2.9.2",
"axios": "^1.4.0",
"bignumber.js": "^9.1.1",
"cheerio": "^1.0.0-rc.12",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
"express": "^4.18.2",
Expand Down
28 changes: 28 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { autoRetry } from "@grammyjs/auto-retry";
import { run } from "@grammyjs/runner";
import { runBotHeartBit } from "./monitoring/monitoring";
import { BotPaymentLog } from "./database/stats.service";
import { getChatMemberInfo } from "./modules/open-ai/utils/web-crawler";

const logger = pino({
name: "bot",
Expand Down Expand Up @@ -437,6 +438,15 @@ bot.command("love", (ctx) => {
});
});

bot.command('stop', (ctx) => {
logger.info("/stop command");
ctx.session.openAi.chatGpt.chatConversation = [];
ctx.session.openAi.chatGpt.usage = 0;
ctx.session.openAi.chatGpt.price = 0;
ctx.session.translate.enable = false;
ctx.session.translate.languages = []
ctx.session.oneCountry.lastDomain = ""
})
// bot.command("memo", (ctx) => {
// ctx.reply(MEMO.text, {
// parse_mode: "Markdown",
Expand All @@ -451,6 +461,24 @@ bot.command("love", (ctx) => {
// });
// });

bot.on("msg:new_chat_members", async (ctx) => {
try {
const newMembers = (await ctx.message?.new_chat_members) || [];
newMembers.forEach(async (m) => {
const user = await getChatMemberInfo(m.username!);
if (user.displayName) {
await ctx.reply(
`Hi everyone! Welcome to ${user.displayName} (@${user.username})${
user.bio && ": " + user.bio
}`
);
}
});
} catch (e: any) {
logger.error(`Error when welcoming new chat memmber ${e.toString()}`);
}
});

bot.on("message", onMessage);
bot.on("callback_query:data", onCallback);

Expand Down
26 changes: 19 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,30 @@ export default {
},
},
chatGpt: {
wordCountBetween: process.env.WORD_COUNT_BETWEEN ? parseInt(process.env.WORD_COUNT_BETWEEN) : 100,
wordCountBetween: process.env.WORD_COUNT_BETWEEN
? parseInt(process.env.WORD_COUNT_BETWEEN)
: 100,
priceAdjustment: process.env.PRICE_ADJUSTMENT
? parseInt(process.env.PRICE_ADJUSTMENT)
: 2,
isEnabled: Boolean(parseInt(process.env.CHAT_GPT_ENABLED || "1")),
isTypingEnabled: Boolean(parseInt(process.env.TYPING_STATUS_ENABLED || "1")),
isTypingEnabled: Boolean(
parseInt(process.env.TYPING_STATUS_ENABLED || "1")
),
//hard coded gpt-4
// model: "gpt-4",
model: process.env.OPENAI_MODEL ?? "gpt-4",
chatPrefix: process.env.GROUP_PREFIX
? process.env.GROUP_PREFIX.split(",")
: ["?", ">"],
prefixes: {
chatPrefix: process.env.ASK_PREFIX
? process.env.ASK_PREFIX.split(",")
: ["a.","?",">","."],
dallePrefix: process.env.DALLE_PREFIX
? process.env.DALLE_PREFIX.split(",")
: ["d."],
newPrefix: process.env.NEW_PREFIX
? process.env.NEW_PREFIX.split(",")
: ["n."],
},
minimumBalance: process.env.MIN_BALANCE
? parseInt(process.env.MIN_BALANCE)
: 0,
Expand Down Expand Up @@ -130,6 +142,6 @@ export default {
creditsAmount: "100",
},
betteruptime: {
botHeartBitId: process.env.BOT_HEARTBIT_ID || ''
}
botHeartBitId: process.env.BOT_HEARTBIT_ID || "",
},
};
4 changes: 2 additions & 2 deletions src/modules/open-ai/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function alterGeneratedImg(
}
}

export async function chatCompilation(
export async function chatCompletion(
conversation: ChatConversation[],
model = config.openAi.chatGpt.model,
limitTokens = true
Expand Down Expand Up @@ -200,7 +200,7 @@ export async function improvePrompt(promptText: string, model: string) {
const prompt = `Improve this picture description using max 100 words and don't add additional text to the image: ${promptText} `;
try {
const conversation = [{ role: "user", content: prompt }];
const response = await chatCompilation(conversation, model);
const response = await chatCompletion(conversation, model);
return response.completion;
} catch (e: any) {
throw e;
Expand Down
17 changes: 7 additions & 10 deletions src/modules/open-ai/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const imgGen = async (
imgs.map(async (img: any) => {
await ctx
.replyWithPhoto(img.url, {
caption: `/DALLE ${prompt}`,
caption: `/dalle ${prompt}`,
})
.catch((e) => {
throw e;
Expand Down Expand Up @@ -106,12 +106,7 @@ export const alterImg = async (
const { chatId, prompt, numImages, imgSize, filePath } = data;
try {
ctx.chatAction = "upload_photo";
const imgs = await alterGeneratedImg(
prompt!,
filePath!,
ctx,
imgSize!
);
const imgs = await alterGeneratedImg(prompt!, filePath!, ctx, imgSize!);
if (imgs) {
imgs!.map(async (img: any) => {
await ctx.replyWithPhoto(img.url).catch((e) => {
Expand All @@ -125,7 +120,10 @@ export const alterImg = async (
}
};

export const promptGen = async (data: ChatGptPayload, chat: ChatConversation[]) => {
export const promptGen = async (
data: ChatGptPayload,
chat: ChatConversation[]
) => {
const { conversation, ctx, model } = data;
try {
let msgId = (await ctx.reply("...")).message_id;
Expand Down Expand Up @@ -159,8 +157,7 @@ export const promptGen = async (data: ChatGptPayload, chat: ChatConversation[])
conversation.push({ content: completion, role: "system" });
ctx.session.openAi.chatGpt.usage += promptTokens + completionTokens;
ctx.session.openAi.chatGpt.price += price;
chat = [...conversation!]
// ctx.session.openAi.chatGpt.chatConversation = [...conversation!];
chat = [...conversation!];
return price;
}
return 0;
Expand Down
Loading

0 comments on commit 0a61b5e

Please sign in to comment.