{"id":13910566,"url":"https://github.com/momegas/megabots","last_synced_at":"2025-04-04T17:09:57.208Z","repository":{"id":152600708,"uuid":"626597211","full_name":"momegas/megabots","owner":"momegas","description":"🤖 State-of-the-art, production ready LLM apps made mega-easy, so you don't have to build them from scratch 🤯 Create a bot, now 🫵","archived":false,"fork":false,"pushed_at":"2023-06-10T22:15:29.000Z","size":144,"stargazers_count":349,"open_issues_count":15,"forks_count":35,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-03-28T16:11:06.931Z","etag":null,"topics":["chatbot","faiss","fastapi","gpt-35-turbo","gpt-4","information-retrieval","langchain","llama","natural-language-processing","nlp","pinecone","prompt-engineering","python","question-answering","s3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/momegas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null}},"created_at":"2023-04-11T19:38:38.000Z","updated_at":"2025-02-11T21:35:46.000Z","dependencies_parsed_at":"2023-08-23T09:40:01.607Z","dependency_job_id":null,"html_url":"https://github.com/momegas/megabots","commit_stats":null,"previous_names":["momegas/qnabot"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momegas%2Fmegabots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momegas%2Fmegabots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momegas%2Fmegabots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momegas%2Fmegabots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/momegas","download_url":"https://codeload.github.com/momegas/megabots/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247217222,"owners_count":20903009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","faiss","fastapi","gpt-35-turbo","gpt-4","information-retrieval","langchain","llama","natural-language-processing","nlp","pinecone","prompt-engineering","python","question-answering","s3"],"created_at":"2024-08-07T00:01:34.167Z","updated_at":"2025-04-04T17:09:57.192Z","avatar_url":"https://github.com/momegas.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# 🤖 Megabots\n\n[![Tests](https://github.com/momegas/qnabot/actions/workflows/python-package.yml/badge.svg)](https://github.com/momegas/qnabot/actions/workflows/python-package.yml)\n[![Python Version](https://img.shields.io/badge/python-%203.10%20-blue.svg)](#supported-python-versions)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![License](https://img.shields.io/badge/License-MIT-informational.svg)](https://github.com/momegas/megabots/blob/main/LICENCE)\n![](https://dcbadge.vercel.app/api/server/zkqDWk5S7P?style=flat\u0026n\u0026compact=true)\n\n🤖 Megabots provides State-of-the-art, production ready LLM apps made mega-easy, so you don't have to build them from scratch 🤯 Create a bot, now 🫵\n\n- 👉 Join us on Discord: https://discord.gg/zkqDWk5S7P\n- ✈️ Work is managed in this project: https://github.com/users/momegas/projects/5/views/2\n- 🤖 Documentation bot: https://huggingface.co/spaces/momegas/megabots\n\n**The Megabots library can be used to create bots that:**\n\n- ⌚️ are production ready, in minutes\n- 🗂️ can answer questions over documents\n- 💾 can connect to vector databases\n- 🎖️ automatically expose the bot as a rebust API using FastAPI (early release)\n- 🏓 automatically expose the bot as a UI using Gradio\n\n🤖 Megabots is backed by some of the most famous tools for productionalising AI. It uses [LangChain](https://docs.langchain.com/docs/) for managing LLM chains, [langchain-serve](https://github.com/jina-ai/langchain-serve) to create a production ready API, [Gradio](https://gradio.app/) to create a UI. At the moment it uses [OpenAI](https://openai.com/) to generate answers, but we plan to support other LLMs in the future.\n\n## Getting started\n\nNote: This is a work in progress. The API might change.\n\n```bash\npip install megabots\n```\n\n```python\nfrom megabots import bot\nimport os\n\nos.environ[\"OPENAI_API_KEY\"] = \"my key\"\n\n# Create a bot 👉 with one line of code. Automatically loads your data from ./index or index.pkl.\n# Keep in mind that you need to have one or another.\nqnabot = bot(\"qna-over-docs\")\n\n# Ask a question\nanswer = qnabot.ask(\"How do I use this bot?\")\n\n# Save the index to save costs (GPT is used to create the index)\nqnabot.save_index(\"index.pkl\")\n\n# Load the index from a previous run\nqnabot = bot(\"qna-over-docs\", index=\"./index.pkl\")\n\n# Or create the index from a directory of documents\nqnabot = bot(\"qna-over-docs\", index=\"./index\")\n\n# Change the model\nqnabot = bot(\"qna-over-docs\", model=\"text-davinci-003\")\n```\n\n## Changing the bot's prompt\n\nYou can change the bots promnpt to customize it to your needs. In the `qna-over-docs` type of bot you will need to pass 2 variables for the `context` (knwoledge searched from the index) and the `question` (the human question).\n\n```python\nfrom megabots import bot\n\nprompt = \"\"\"\nUse the following pieces of context to answer the question at the end.\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\nAnswer in the style of Tony Stark.\n\n{context}\n\nQuestion: {question}\nHelpful humorous answer:\"\"\"\n\nqnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", prompt=prompt)\n\nqnabot.ask(\"what was the first roster of the avengers?\")\n```\n\n## Working with memory\n\nYou can easily add memory to your `bot` using the `memory` parameter. It accepts a string with the type of the memory to be used. This defaults to some sane dafaults.\nShould you need more configuration, you can use the `memory` function and pass the type of memory and the configuration you need.\n\n```python\nfrom megabots import bot\n\nqnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", memory=\"conversation-buffer\")\n\nprint(qnabot.ask(\"who is iron man?\"))\nprint(qnabot.ask(\"was he in the first roster?\"))\n# Bot should understand who \"he\" refers to.\n```\n\nOr using the `memory`factory function\n\n```python\nfrom megabots import bot, memory\n\nmem(\"conversation-buffer-window\", k=5)\n\nqnabot = bot(\"qna-over-docs\", index=\"./index.pkl\", memory=mem)\n\nprint(qnabot.ask(\"who is iron man?\"))\nprint(qnabot.ask(\"was he in the first roster?\"))\n```\n\nNOTE: For the `qna-over-docs` bot, when using memory and passing your custom prompt, it is important to remember to pass one more variable to your custom prompt to facilitate for chat history. The variable name is `history`.\n\n```python\nfrom megabots import bot\n\nprompt = \"\"\"\nUse the following pieces of context to answer the question at the end.\nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\n{history}\nHuman: {question}\nAI:\"\"\"\n\nqnabot = bot(\"qna-over-docs\", prompt=prompt, index=\"./index.pkl\", memory=\"conversation-buffer\")\n\nprint(qnabot.ask(\"who is iron man?\"))\nprint(qnabot.ask(\"was he in the first roster?\"))\n```\n\n## Using Megabots with Milvus (more DBs comming soon)\n\nMegabots `bot` can also use Milvus as a backend for its search engine. You can find an example of how to do it below.\n\nIn order to run Milvus you need to follow [this guide](https://milvus.io/docs/example_code.md) to download a docker compose file and run it.\nThe command is:\n\n```bash\nwget https://raw.githubusercontent.com/milvus-io/pymilvus/v2.2.7/examples/hello_milvus.py\n```\n\nYou can then [install Attu](https://milvus.io/docs/attu_install-docker.md) as a management tool for Milvus\n\n```python\nfrom megabots import bot\n\n# Attach a vectorstore by passing the name of the database. Default port for milvus is 19530 and default host is localhost\n# Point it to your files directory so that it can index the files and add them to the vectorstore\nbot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=\"milvus\")\n\nbot.ask(\"what was the first roster of the avengers?\")\n```\n\nOr use the `vectorstore` factory function for more customisation\n\n```python\n\nfrom megabots import bot, vectorstore\n\nmilvus = vectorstore(\"milvus\", host=\"localhost\", port=19530)\n\nbot = bot(\"qna-over-docs\", index=\"./examples/files/\", vectorstore=milvus)\n```\n\n## Exposing an API with [langchain-serve](https://github.com/jina-ai/langchain-serve)\n\nYou can also expose the bot endpoints locally using langchain-serve. A sample file `api.py` is provided in the `megabots` folder.\n\nTo expose the API locally, you can do \n```bash\nlc-serve deploy local megabots.api\n```\n\nYou should then be able to visit `http://localhost:8000/docs` to see \u0026 interact with the API documentation.\n\nTo deploy your API to the cloud, you can do and connect to the API using the endpoint provided in the output.\n```bash\nlc-serve deploy jcloud megabots.api\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eShow command output\u003c/summary\u003e\n\n```text\n╭──────────────┬──────────────────────────────────────────────────────────────────────────────────────╮\n│ App ID       │                                 langchain-dec14439a6                                 │\n├──────────────┼──────────────────────────────────────────────────────────────────────────────────────┤\n│ Phase        │                                       Serving                                        │\n├──────────────┼──────────────────────────────────────────────────────────────────────────────────────┤\n│ Endpoint     │                      https://langchain-dec14439a6.wolf.jina.ai                       │\n├──────────────┼──────────────────────────────────────────────────────────────────────────────────────┤\n│ App logs     │                               dashboards.wolf.jina.ai                                │\n├──────────────┼──────────────────────────────────────────────────────────────────────────────────────┤\n│ Swagger UI   │                    https://langchain-dec14439a6.wolf.jina.ai/docs                    │\n├──────────────┼──────────────────────────────────────────────────────────────────────────────────────┤\n│ OpenAPI JSON │                https://langchain-dec14439a6.wolf.jina.ai/openapi.json                │\n╰──────────────┴──────────────────────────────────────────────────────────────────────────────────────╯\n```\n\u003c/details\u003e\n\n\nYou can read more about langchain-serve [here](https://github.com/jina-ai/langchain-server).\n\n## Exposing a Gradio chat-like interface\n\nYou can expose a gradio UI for the bot using `create_interface` function.\nAssuming your file is called `ui.py` run `gradio qnabot/ui.py` to run the UI locally.\nYou should then be able to visit `http://127.0.0.1:7860` to see the API documentation.\n\n```python\nfrom megabots import bot, create_interface\n\ndemo = create_interface(bot(\"qna-over-docs\"))\n```\n\n## Customising bot\n\nThe `bot` function should serve as the starting point for creating and customising your bot. Below is a list of the available arguments in `bot`.\n\n| Argument    | Description                                                                                                                                                                                                                                                                                |\n| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| task        | The type of bot to create. Available options: `qna-over-docs`. More comming soon                                                                                                                                                                                                           |\n| index       | Specifies the index to use for the bot. It can either be a saved index file (e.g., `index.pkl`) or a directory of documents (e.g., `./index`). In the case of the directory the index will be automatically created. If no index is specified `bot` will look for `index.pkl` or `./index` |\n| model       | The name of the model to use for the bot. You can specify a different model by providing its name, like \"text-davinci-003\". Supported models: `gpt-3.5-turbo` (default),`text-davinci-003` More comming soon.                                                                              |\n| prompt      | A string template for the prompt, which defines the format of the question and context passed to the model. The template should include placeholder variables like so: `context`, `{question}` and in the case of using memory `history`.                                                  |\n| memory      | The type of memory to be used by the bot. Can be a string with the type of the memory or you can use `memory` factory function. Supported memories: `conversation-buffer`, `conversation-buffer-window`                                                                                    |\n| vectorstore | The vectorstore to be used for the index. Can be a string with the name of the databse or you can use `vectorstore` factory function. Supported DBs: `milvus`.                                                                                                                             |\n\n| sources | When `sources` is `True` the bot will also include sources in the response. A known [issue](https://github.com/hwchase17/langchain/issues/2858) exists, where if you pass a custom prompt with sources the code breaks. |\n\n## How QnA bot works\n\nLarge language models (LLMs) are powerful, but they can't answer questions about documents they haven't seen. If you want to use an LLM to answer questions about documents it was not trained on, you have to give it information about those documents. To solve this, we use \"retrieval augmented generation.\"\n\nIn simple terms, when you have a question, you first search for relevant documents. Then, you give the documents and the question to the language model to generate an answer. To make this work, you need your documents in a searchable format (an index). This process involves two main steps: (1) preparing your documents for easy querying, and (2) using the retrieval augmented generation method.\n\n`qna-over-docs` uses FAISS to create an index of documents and GPT to generate answers.\n\n```mermaid\nsequenceDiagram\n    actor User\n    participant API\n    participant LLM\n    participant Vectorstore\n    participant IngestionEngine\n    participant DataLake\n    autonumber\n\n    Note over API, DataLake: Ingestion phase\n    loop Every X time\n    IngestionEngine -\u003e\u003e DataLake: Load documents\n    DataLake --\u003e\u003e IngestionEngine: Return data\n    IngestionEngine --\u003e\u003e IngestionEngine: Split documents and Create embeddings\n    IngestionEngine -\u003e\u003e Vectorstore: Store documents and embeddings\n    end\n\n    Note over API, DataLake: Generation phase\n\n    User -\u003e\u003e API: Receive user question\n    API -\u003e\u003e Vectorstore: Lookup documents in the index relevant to the question\n    API -\u003e\u003e API: Construct a prompt from the question and any relevant documents\n    API -\u003e\u003e LLM: Pass the prompt to the model\n    LLM --\u003e\u003e API: Get response from model\n    API --\u003e\u003e User: Return response\n\n```\n\n## How to contribute?\n\nWe welcome any suggestions, problem reports, and contributions!\nFor any changes you would like to make to this project, we invite you to submit an [issue](https://github.com/momegas/megabots/issues).\n\nFor more information, see [`CONTRIBUTING`](https://github.com/momegas/megabots/blob/main/CONTRIBUTING.md) instructions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomegas%2Fmegabots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmomegas%2Fmegabots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomegas%2Fmegabots/lists"}