From 38174f33b777392fe4f0cbd3e2f361588ae2d410 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sun, 8 Sep 2024 13:02:51 +0530 Subject: [PATCH 1/4] feat: Add group command to Telegram integration This commit introduces a new `/ask` command for the Telegram integration, enabling users to interact with the chatbot in group chats. The command retrieves the message following `/ask`, sends it to the chatbot for processing, and then replies to the group with the chatbot's response. The previous restriction of private chat interaction is removed, allowing for a more flexible and collaborative usage of the chatbot within groups. --- app/ui/package.json | 2 +- package.json | 2 +- server/src/integration/telegram.ts | 118 +++++++++++++++++------------ 3 files changed, 71 insertions(+), 51 deletions(-) diff --git a/app/ui/package.json b/app/ui/package.json index 1422d768..812153b6 100644 --- a/app/ui/package.json +++ b/app/ui/package.json @@ -1,7 +1,7 @@ { "name": "app", "private": true, - "version": "1.11.0", + "version": "1.11.1", "type": "module", "scripts": { "dev": "vite", diff --git a/package.json b/package.json index 00db9f0d..a141a028 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dialoqbase", - "version": "1.11.0", + "version": "1.11.1", "description": "Create chatbots with ease", "scripts": { "ui:dev": "pnpm run --filter ui dev", diff --git a/server/src/integration/telegram.ts b/server/src/integration/telegram.ts index 7a2af602..ddbf5911 100644 --- a/server/src/integration/telegram.ts +++ b/server/src/integration/telegram.ts @@ -11,6 +11,10 @@ import * as fs from "fs/promises"; import { convertOggToWave } from "../utils/ffmpeg"; import { telegramFormat } from "../utils/telegram-format"; type DialoqBaseContext = FileFlavor; + +const groupCommand = process.env.DQ_TG_GROUP_COMMAND || "ask" +const groupCommandRegex = new RegExp(`^\\/${groupCommand}\\s(.*)`); + export default class TelegramBot { static get clients() { return this._clients.values(); @@ -38,18 +42,12 @@ export default class TelegramBot { ]); bot.command("start", async (ctx) => { - if (ctx.chat.type !== "private") { - return ctx.reply("Hi, I can only work in private chats."); - } await ctx.replyWithChatAction("typing"); const message = await welcomeMessage(identifier); return await ctx.reply(message); }); bot.command("ping", (ctx) => ctx.reply("pong")); bot.command("clear", async (ctx) => { - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } await ctx.replyWithChatAction("typing"); if (!ctx?.from?.id) { return await ctx.reply("I can't find your user id"); @@ -60,55 +58,43 @@ export default class TelegramBot { ); return await ctx.reply(response); }); - bot.on("message:text", async (ctx) => { - // check it's a group chat - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } - await ctx.replyWithChatAction("typing"); - // set messaging type - const user_id = ctx.from.id; - const message = await telegramBotHandler( - identifier, - ctx.message.text, - user_id - ); - - if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { - return await ctx.reply(message); - } - - return await ctx.reply(telegramFormat(message), - { - parse_mode: "HTML", - }); - }); - - bot.on("message:voice", async (ctx) => { + bot.hears(groupCommandRegex, async (ctx) => { try { - if (ctx.chat.type !== "private") { - return ctx.reply("I can only work in private chats."); - } - await ctx.replyWithChatAction("typing"); + const user_id = ctx.from.id; + const [, message] = ctx.match + if (!message) { + return await ctx.reply("Please provide a question after /ask"); + } + const response = await telegramBotHandler( + identifier, + message, + user_id + ); - const file = await ctx.getFile(); - const path = await file.download(); - - const audioWav = await convertOggToWave(path); - const audio = await fs.readFile(audioWav); - - const response = await convertTextToAudio(audio); + if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { + return await ctx.reply(response); + } + return await ctx.reply(telegramFormat(response), + { + parse_mode: "HTML", + }); + } catch (e) { + console.log(e) + return await ctx.reply("Something went wrong") + } + }); + bot.on("message:text", async (ctx) => { + if (ctx.chat.type === "private") { + await ctx.replyWithChatAction("typing"); const user_id = ctx.from.id; - const message = await telegramBotHandler( identifier, - response.text, + ctx.message.text, user_id ); - if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { return await ctx.reply(message); } @@ -117,9 +103,43 @@ export default class TelegramBot { { parse_mode: "HTML", }); - } catch (error) { - console.log(error); - return await ctx.reply("Opps! Something went wrong"); + } + }); + + bot.on("message:voice", async (ctx) => { + if (ctx.chat.type == "private") { + try { + await ctx.replyWithChatAction("typing"); + + const file = await ctx.getFile(); + const path = await file.download(); + + const audioWav = await convertOggToWave(path); + const audio = await fs.readFile(audioWav); + + const response = await convertTextToAudio(audio); + + const user_id = ctx.from.id; + + const message = await telegramBotHandler( + identifier, + response.text, + user_id + ); + + + if (process.env.DB_TELEGEAM_PARSE_MODE === "normal") { + return await ctx.reply(message); + } + + return await ctx.reply(telegramFormat(message), + { + parse_mode: "HTML", + }); + } catch (error) { + console.log(error); + return await ctx.reply("Opps! Something went wrong"); + } } }); @@ -150,4 +170,4 @@ export default class TelegramBot { return false; } } -} +} \ No newline at end of file From 0457fbb45b2994ccd7889972fa4d61ee41dfc279 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Thu, 19 Sep 2024 18:16:28 +0530 Subject: [PATCH 2/4] feat: Add advanced bot configuration options --- .../handlers/api/v1/bot/bot/api.handler.ts | 2 ++ server/src/handlers/api/v1/bot/bot/types.ts | 10 ++++++ server/src/schema/api/v1/bot/bot/index.ts | 32 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/server/src/handlers/api/v1/bot/bot/api.handler.ts b/server/src/handlers/api/v1/bot/bot/api.handler.ts index bda42af7..ff780150 100644 --- a/server/src/handlers/api/v1/bot/bot/api.handler.ts +++ b/server/src/handlers/api/v1/bot/bot/api.handler.ts @@ -29,6 +29,7 @@ export const createBotAPIHandler = async ( question_generator_prompt, system_prompt, temperature, + options } = request.body; const prisma = request.server.prisma; @@ -112,6 +113,7 @@ export const createBotAPIHandler = async ( const bot = await prisma.bot.create({ data: { + ...options, name, embedding: embeddingInfo.model_id, model: modelInfo.model_id, diff --git a/server/src/handlers/api/v1/bot/bot/types.ts b/server/src/handlers/api/v1/bot/bot/types.ts index 84f21c20..d532b742 100644 --- a/server/src/handlers/api/v1/bot/bot/types.ts +++ b/server/src/handlers/api/v1/bot/bot/types.ts @@ -96,6 +96,16 @@ export interface CreateBotAPIRequest { system_prompt?: string; question_generator_prompt?: string; temperature?: number; + options?: { + noOfDocumentsToRetrieve?: number, + noOfChatHistoryInContext?: number, + publicBotPwdProtected? : boolean, + semanticSearchSimilarityScore?: "none" |"0.2" |"0.5" |"0.7" + autoResetSession?: boolean, + internetSearchEnabled?: boolean + use_hybrid_search?: boolean + autoSyncDataSources?: boolean + } }; } diff --git a/server/src/schema/api/v1/bot/bot/index.ts b/server/src/schema/api/v1/bot/bot/index.ts index d3c3649f..16c9d1fa 100644 --- a/server/src/schema/api/v1/bot/bot/index.ts +++ b/server/src/schema/api/v1/bot/bot/index.ts @@ -244,6 +244,38 @@ export const createBotAPISchema: FastifySchema = { temperature: { type: "number", }, + options: { + type: "object", + properties: { + noOfDocumentsToRetrieve: { + type: "number", + }, + noOfChatHistoryInContext: { + type: "number", + }, + semanticSearchSimilarityScore: { + type: "string", + enum: ["none", "0.2", "0.5", "0.7"], + default: "none" + }, + autoResetSession: { + type: "boolean", + default: false, + }, + internetSearchEnabled: { + type: "boolean", + default: false, + }, + use_hybrid_search: { + type: "boolean", + default: false, + }, + autoSyncDataSources: { + type: "boolean", + default: false, + }, + }, + }, }, }, }; From 70d8e5f105de77f57610d58ab78679dd13434d94 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Thu, 19 Sep 2024 18:45:58 +0530 Subject: [PATCH 3/4] Fix: Remove unnecessary padding in `BotLayout` Allows for a consistent layout across different pages within the bot view by removing the default padding in `BotLayout`. This enables a more unified look and feel while providing flexibility to control the padding as needed for specific scenarios. --- app/ui/src/App.tsx | 4 +- app/ui/src/Layout/BotLayout.tsx | 90 ++++++++++++++++----------------- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/ui/src/App.tsx b/app/ui/src/App.tsx index f8eed14d..d1b39c48 100644 --- a/app/ui/src/App.tsx +++ b/app/ui/src/App.tsx @@ -54,7 +54,7 @@ const router = createHashRouter([ { path: "/bot/:id", element: ( - + ), @@ -70,7 +70,7 @@ const router = createHashRouter([ { path: "/bot/:id/playground/:history_id", element: ( - + ), diff --git a/app/ui/src/Layout/BotLayout.tsx b/app/ui/src/Layout/BotLayout.tsx index 11e32596..a8dbbc99 100644 --- a/app/ui/src/Layout/BotLayout.tsx +++ b/app/ui/src/Layout/BotLayout.tsx @@ -62,9 +62,11 @@ function classNames(...classes) { export default function BotLayout({ children, + asideSpace = "md:ml-16", }: { children: React.ReactNode; noPadding?: boolean; + asideSpace?: string; }) { const [sidebarOpen, setSidebarOpen] = useState(false); const params = useParams<{ id: string }>(); @@ -190,7 +192,7 @@ export default function BotLayout({
-
+
-
- {children} - {/*
-
-
*/} -
-
- -
-
-
- -
+ +
+ +
{children}
From c24f6b77e53e010305f95ba27ddf873b6ecd8990 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Thu, 19 Sep 2024 18:52:09 +0530 Subject: [PATCH 4/4] feat: Improve clipboard copy functionality The clipboard copy functionality has been improved across several components, utilizing a centralized utility function to handle clipboard operations for enhanced consistency and reliability. --- .../components/Bot/Integration/IntegrationForm.tsx | 5 +++-- app/ui/src/components/Bot/Playground/Message.tsx | 5 +++-- app/ui/src/components/Common/CopyBtn.tsx | 3 ++- app/ui/src/components/Common/Markdown.tsx | 5 +++-- app/ui/src/components/Common/Mermaid.tsx | 5 +++-- app/ui/src/utils/clipboard.ts | 12 ++++++++++++ 6 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 app/ui/src/utils/clipboard.ts diff --git a/app/ui/src/components/Bot/Integration/IntegrationForm.tsx b/app/ui/src/components/Bot/Integration/IntegrationForm.tsx index 1f810eb6..8c0e80f1 100644 --- a/app/ui/src/components/Bot/Integration/IntegrationForm.tsx +++ b/app/ui/src/components/Bot/Integration/IntegrationForm.tsx @@ -8,6 +8,7 @@ import axios from "axios"; import { ClipboardIcon } from "@heroicons/react/24/outline"; // import Switch from antd as AntdSwitch import { Switch as AntdSwitch } from "antd"; +import { clipbardCopy } from "../../../utils/clipboard"; //@ts-ignore function classNames(...classes) { @@ -163,8 +164,8 @@ export const IntegrationForm: React.FC = ({ onClose, data }) => {