From de3ab00f3dca0bdbe3e8f1867cc3a0cb6547b4c7 Mon Sep 17 00:00:00 2001 From: Stan Girard Date: Thu, 2 Nov 2023 09:38:07 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20telegram=20(#1559)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added connectors and doc --- README.md | 9 --- connectors/telegram_bot/.env.example | 5 ++ connectors/telegram_bot/main.py | 73 +++++++++++++++++++ .../Developers/connectors/_category_.json | 8 ++ docs/docs/Developers/connectors/telegram.md | 34 +++++++++ docs/docs/Developers/run_fully_local.md | 2 +- 6 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 connectors/telegram_bot/.env.example create mode 100644 connectors/telegram_bot/main.py create mode 100644 docs/docs/Developers/connectors/_category_.json create mode 100644 docs/docs/Developers/connectors/telegram.md diff --git a/README.md b/README.md index 7bc5bfe38..352a0d70f 100644 --- a/README.md +++ b/README.md @@ -35,13 +35,6 @@ Quivr, your second brain, utilizes the power of GenerativeAI to store and retrie https://github.com/StanGirard/quivr/assets/19614572/a6463b73-76c7-4bc0-978d-70562dca71f5 -## Disclaimer ⚠️ - -For a little while, Quivr will be only compatible with OpenAI API. - -If you want to use a Local LLM please refer to [v0.0.46](https://github.com/StanGirard/quivr/releases/tag/v0.0.46). - -This is due to us preparing a big feature and needing to clean the code a bit. ## Getting Started 🚀 @@ -112,8 +105,6 @@ Additionally, you'll need a [Supabase](https://supabase.com/) account for: > _The `NEXT_PUBLIC_BACKEND_URL` is set to localhost:5050 for the docker. Update it if you are running the backend on a different machine._ - > _To activate vertexAI with PaLM from GCP follow the instructions [here](https://python.langchain.com/en/latest/modules/models/llms/integrations/google_vertex_ai_palm.html) and update `backend/.env`- It is an advanced feature, please be expert in GCP before trying to use it_ - - Change variables in `backend/.env` - Change variables in `frontend/.env` diff --git a/connectors/telegram_bot/.env.example b/connectors/telegram_bot/.env.example new file mode 100644 index 000000000..9b4d7457e --- /dev/null +++ b/connectors/telegram_bot/.env.example @@ -0,0 +1,5 @@ +TELEGRAM_BOT_TOKEN=XXXX +QUIVR_TOKEN=XXXX +QUIVR_CHAT_ID=1XXXX +QUIVR_BRAIN_ID=XXXX +QUIVR_URL=XXXX \ No newline at end of file diff --git a/connectors/telegram_bot/main.py b/connectors/telegram_bot/main.py new file mode 100644 index 000000000..1c20d2c02 --- /dev/null +++ b/connectors/telegram_bot/main.py @@ -0,0 +1,73 @@ +import logging +import os + +import requests +from dotenv import load_dotenv +from telegram import Update +from telegram.ext import ( + ApplicationBuilder, + CommandHandler, + ContextTypes, + MessageHandler, + filters, +) + +load_dotenv() # Load variables from .env file + +logging.basicConfig( + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO +) + +telegram_bot_token = os.getenv("TELEGRAM_BOT_TOKEN", "") +quivr_token = os.getenv("QUIVR_TOKEN", "") +quivr_chat_id = os.getenv("QUIVR_CHAT_ID", "") +quivr_brain_id = os.getenv("QUIVR_BRAIN_ID", "") +quivr_url = ( + os.getenv("QUIVR_URL", "https://api.quivr.app") + + f"/chat/{quivr_chat_id}/question?brain_id={quivr_brain_id}" +) + +headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + quivr_token, +} + + +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): + await context.bot.send_message( + chat_id=update.effective_chat.id, + text="I'm Quiv's bot and can answer any question. Please ask your question.", + ) + + +async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE): + user_message = update.message.text + response = requests.post( + quivr_url, headers=headers, json={"question": user_message} + ) + if response.status_code == 200: + quivr_response = response.json().get( + "assistant", "Sorry, I couldn't understand that." + ) + await context.bot.send_message( + chat_id=update.effective_chat.id, text=quivr_response + ) + else: + # Log or print the response for debugging + print(f"Error: {response.status_code}, {response.text}") + await context.bot.send_message( + chat_id=update.effective_chat.id, + text=f"Sorry, there was an error processing your request. {response.text}", + ) + + +if __name__ == "__main__": + application = ApplicationBuilder().token(telegram_bot_token).build() + + start_handler = CommandHandler("start", start) + message_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), handle_message) + + application.add_handler(start_handler) + application.add_handler(message_handler) + + application.run_polling() diff --git a/docs/docs/Developers/connectors/_category_.json b/docs/docs/Developers/connectors/_category_.json new file mode 100644 index 000000000..878900ea7 --- /dev/null +++ b/docs/docs/Developers/connectors/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "📲 Connect Quivr to ...", + "position": 3, + "link": { + "type": "generated-index", + "description": "Connect Quivr to anything" + } +} \ No newline at end of file diff --git a/docs/docs/Developers/connectors/telegram.md b/docs/docs/Developers/connectors/telegram.md new file mode 100644 index 000000000..b618aed38 --- /dev/null +++ b/docs/docs/Developers/connectors/telegram.md @@ -0,0 +1,34 @@ +--- +title: Telegram +--- + +## Load a Telegram chat + +- You can export your Telegram chat history using the [Telegram Desktop](https://desktop.telegram.org/) app. +- Go to Settings > Advanced > Export Telegram data +- Select the chat you want to export +- Select the format `Machine-readable JSON` +- Click `Export` +- Rename the `json` to `.telegram` +- Go to [Quivr.app](https://quivr.app/) and upload the file to a brain +- You can now search your Telegram chat history! + +## Create a telegram bot + +- Go to [BotFather](https://t.me/botfather) and create a new bot +- Copy the token +- Go to `/connectors/telegram` and copy-paste the .env.example file +- `TELEGRAM_BOT_TOKEN` The token you copied from BotFather +- `QUIVR_TOKEN` The API Key of Quivr you can find in your profile +- `QUIVR_CHAT_ID` Create a new chat in Quivr and copy the ID from the URL +- `QUIVR_BRAIN_ID` Copy the id of the brain on which you want to ask question to +- `QUIVR_URL` The URL of the **API** of the Quivr instance you want to use + +Enjoy ! 🎉 + +
+ +
\ No newline at end of file diff --git a/docs/docs/Developers/run_fully_local.md b/docs/docs/Developers/run_fully_local.md index 4a28850cb..d7b4a8e3d 100644 --- a/docs/docs/Developers/run_fully_local.md +++ b/docs/docs/Developers/run_fully_local.md @@ -1,5 +1,5 @@ --- -sidebar_position: 3 +sidebar_position: 4 title: 📍 Run Quivr fully locally ---