From 14712cac8873bc1c7ff11fcd25e4e7c8ebf6eece Mon Sep 17 00:00:00 2001 From: Sidharth Mohanty Date: Fri, 10 Nov 2023 00:14:47 +0530 Subject: [PATCH] Deploy remaining bots and fix schema validation (#927) --- configs/opensearch.yaml | 4 +-- docs/examples/telegram_bot.mdx | 33 ++++++++++++++++++++++--- docs/examples/whatsapp_bot.mdx | 15 +++++++++--- embedchain/utils.py | 17 ++++--------- examples/telegram_bot/.env.example | 2 ++ examples/telegram_bot/Dockerfile | 11 +++++++++ examples/telegram_bot/telegram_bot.py | 2 +- examples/telegram_bot/variables.env | 2 -- examples/whatsapp_bot/.env.example | 1 + examples/whatsapp_bot/Dockerfile | 11 +++++++++ examples/whatsapp_bot/variables.env | 1 - examples/whatsapp_bot/whatsapp_bot.py | 2 +- tests/test_utils.py | 35 +++++++++++++++++++++++++++ 13 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 examples/telegram_bot/.env.example create mode 100644 examples/telegram_bot/Dockerfile delete mode 100644 examples/telegram_bot/variables.env create mode 100644 examples/whatsapp_bot/.env.example create mode 100644 examples/whatsapp_bot/Dockerfile delete mode 100644 examples/whatsapp_bot/variables.env create mode 100644 tests/test_utils.py diff --git a/configs/opensearch.yaml b/configs/opensearch.yaml index 23c3e76e..4918775e 100644 --- a/configs/opensearch.yaml +++ b/configs/opensearch.yaml @@ -1,7 +1,7 @@ app: config: id: 'my-app' - log_level: 'WARN' + log_level: 'WARNING' collect_metrics: true collection_name: 'my-app' @@ -30,4 +30,4 @@ embedder: provider: openai config: model: 'text-embedding-ada-002' - deployment_name: null + deployment_name: 'my-app' diff --git a/docs/examples/telegram_bot.mdx b/docs/examples/telegram_bot.mdx index be5dbf05..14f17e90 100644 --- a/docs/examples/telegram_bot.mdx +++ b/docs/examples/telegram_bot.mdx @@ -1,21 +1,46 @@ --- -title: '📱 Telegram Bot' +title: "📱 Telegram Bot" --- ### 🖼️ Template Setup -- Fork [this](https://replit.com/@taranjeetio/EC-Telegram-Bot-Template?v=1#README.md) replit template. -- Set your `OPENAI_API_KEY` in Secrets. - Open the Telegram app and search for the `BotFather` user. - Start a chat with BotFather and use the `/newbot` command to create a new bot. - Follow the instructions to choose a name and username for your bot. - Once the bot is created, BotFather will provide you with a unique token for your bot. -- Set this token as `TELEGRAM_BOT_TOKEN` in Secrets. + + + + ```bash + docker run --name telegram-bot -e OPENAI_API_KEY=sk-xxx -e TELEGRAM_BOT_TOKEN=xxx -p 8000:8000 embedchain/telegram-bot + ``` + + + If you wish to use **Docker**, you would need to host your bot on a server. + You can use [ngrok](https://ngrok.com/) to expose your localhost to the + internet and then set the webhook using the ngrok URL. + + + + + + Fork **[this](https://replit.com/@taranjeetio/EC-Telegram-Bot-Template?v=1#README.md)** replit template. + + + - Set your `OPENAI_API_KEY` in Secrets. + - Set the unique token as `TELEGRAM_BOT_TOKEN` in Secrets. + + + + + - Click on `Run` in the replit container and a URL will get generated for your bot. - Now set your webhook by running the following link in your browser: + ```url https://api.telegram.org/bot/setWebhook?url= ``` + - When you get a successful response in your browser, your bot is ready to be used. ### 🚀 Usage Instructions diff --git a/docs/examples/whatsapp_bot.mdx b/docs/examples/whatsapp_bot.mdx index 3d7553f2..16a8c504 100644 --- a/docs/examples/whatsapp_bot.mdx +++ b/docs/examples/whatsapp_bot.mdx @@ -12,10 +12,19 @@ pip install --upgrade embedchain 2. Launch your WhatsApp bot: + + + ```bash + docker run --name whatsapp-bot -e OPENAI_API_KEY=sk-xxx -p 8000:8000 embedchain/whatsapp-bot + ``` + + + ```bash + python -m embedchain.bots.whatsapp --port 5000 + ``` + + -```bash -python -m embedchain.bots.whatsapp --port 5000 -``` If your bot needs to be accessible online, use your machine's public IP or DNS. Otherwise, employ a proxy server like [ngrok](https://ngrok.com/) to make your local bot accessible. diff --git a/embedchain/utils.py b/embedchain/utils.py index 801371f2..455430e3 100644 --- a/embedchain/utils.py +++ b/embedchain/utils.py @@ -138,8 +138,7 @@ def detect_datatype(source: Any) -> DataType: formatted_source = format_source(str(source), 30) if url: - from langchain.document_loaders.youtube import \ - ALLOWED_NETLOCK as YOUTUBE_ALLOWED_NETLOCS + from langchain.document_loaders.youtube import ALLOWED_NETLOCK as YOUTUBE_ALLOWED_NETLOCS if url.netloc in YOUTUBE_ALLOWED_NETLOCS: logging.debug(f"Source of `{formatted_source}` detected as `youtube_video`.") @@ -309,7 +308,7 @@ def validate_yaml_config(config_data): "gpt4all", "jina", "llama2", - "vertex_ai", + "vertexai", ), Optional("config"): { Optional("model"): str, @@ -329,23 +328,17 @@ def validate_yaml_config(config_data): Optional("provider"): Or( "chroma", "elasticsearch", "opensearch", "pinecone", "qdrant", "weaviate", "zilliz" ), - Optional("config"): { - Optional("collection_name"): str, - Optional("dir"): str, - Optional("allow_reset"): bool, - Optional("host"): str, - Optional("port"): str, - }, + Optional("config"): object, # TODO: add particular config schema for each provider }, Optional("embedder"): { - Optional("provider"): Or("openai", "gpt4all", "huggingface", "vertexai"), + Optional("provider"): Or("openai", "gpt4all", "huggingface", "vertexai", "azure_openai"), Optional("config"): { Optional("model"): Optional(str), Optional("deployment_name"): Optional(str), }, }, Optional("embedding_model"): { - Optional("provider"): Or("openai", "gpt4all", "huggingface", "vertexai"), + Optional("provider"): Or("openai", "gpt4all", "huggingface", "vertexai", "azure_openai"), Optional("config"): { Optional("model"): str, Optional("deployment_name"): str, diff --git a/examples/telegram_bot/.env.example b/examples/telegram_bot/.env.example new file mode 100644 index 00000000..cd80d5ef --- /dev/null +++ b/examples/telegram_bot/.env.example @@ -0,0 +1,2 @@ +TELEGRAM_BOT_TOKEN= +OPENAI_API_KEY= diff --git a/examples/telegram_bot/Dockerfile b/examples/telegram_bot/Dockerfile new file mode 100644 index 00000000..aed7b62e --- /dev/null +++ b/examples/telegram_bot/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11-slim + +WORKDIR /usr/src/ +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +EXPOSE 8000 + +CMD ["python", "telegram_bot.py"] diff --git a/examples/telegram_bot/telegram_bot.py b/examples/telegram_bot/telegram_bot.py index d81824dd..8ff8892b 100644 --- a/examples/telegram_bot/telegram_bot.py +++ b/examples/telegram_bot/telegram_bot.py @@ -63,4 +63,4 @@ def send_message(chat_id, text): if __name__ == "__main__": - app.run(host="0.0.0.0", port=5000, debug=False) + app.run(host="0.0.0.0", port=8000, debug=False) diff --git a/examples/telegram_bot/variables.env b/examples/telegram_bot/variables.env deleted file mode 100644 index b4880368..00000000 --- a/examples/telegram_bot/variables.env +++ /dev/null @@ -1,2 +0,0 @@ -TELEGRAM_BOT_TOKEN="" -OPENAI_API_KEY="" \ No newline at end of file diff --git a/examples/whatsapp_bot/.env.example b/examples/whatsapp_bot/.env.example new file mode 100644 index 00000000..e570b8b5 --- /dev/null +++ b/examples/whatsapp_bot/.env.example @@ -0,0 +1 @@ +OPENAI_API_KEY= diff --git a/examples/whatsapp_bot/Dockerfile b/examples/whatsapp_bot/Dockerfile new file mode 100644 index 00000000..528f2eea --- /dev/null +++ b/examples/whatsapp_bot/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.11-slim + +WORKDIR /usr/src/ +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY . . + +EXPOSE 8000 + +CMD ["python", "whatsapp_bot.py"] diff --git a/examples/whatsapp_bot/variables.env b/examples/whatsapp_bot/variables.env deleted file mode 100644 index da672599..00000000 --- a/examples/whatsapp_bot/variables.env +++ /dev/null @@ -1 +0,0 @@ -OPENAI_API_KEY="" \ No newline at end of file diff --git a/examples/whatsapp_bot/whatsapp_bot.py b/examples/whatsapp_bot/whatsapp_bot.py index d4d13a40..50f9c16d 100644 --- a/examples/whatsapp_bot/whatsapp_bot.py +++ b/examples/whatsapp_bot/whatsapp_bot.py @@ -48,4 +48,4 @@ def query(message): if __name__ == "__main__": - app.run(host="0.0.0.0", port=5000, debug=False) + app.run(host="0.0.0.0", port=8000, debug=False) diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..c0dd2eb1 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,35 @@ +import yaml +from embedchain.utils import validate_yaml_config + +CONFIG_YAMLS = [ + "configs/anthropic.yaml", + "configs/azure_openai.yaml", + "configs/chroma.yaml", + "configs/chunker.yaml", + "configs/cohere.yaml", + "configs/full-stack.yaml", + "configs/gpt4all.yaml", + "configs/huggingface.yaml", + "configs/jina.yaml", + "configs/llama2.yaml", + "configs/opensearch.yaml", + "configs/opensource.yaml", + "configs/pinecone.yaml", + "configs/vertexai.yaml", + "configs/weaviate.yaml", +] + + +class TestAllConfigYamls: + def test_all_config_yamls(self): + """Test that all config yamls are valid.""" + for config_yaml in CONFIG_YAMLS: + with open(config_yaml, "r") as f: + config = yaml.safe_load(f) + assert config is not None + + try: + validate_yaml_config(config) + except Exception as e: + print(f"Error in {config_yaml}: {e}") + raise e