Spaces:
Runtime error
Runtime error
File size: 7,586 Bytes
cfc0db8 56724c7 8aaf6b2 386d73a 8aaf6b2 3234c2a 6ec321a 5ccaabd 386d73a 6a8232f 5ccaabd 6a8232f 5ccaabd 6a8232f 784c769 5ccaabd cfc0db8 56724c7 507e0ff 6ec321a 507e0ff 56724c7 cfc0db8 56724c7 cfc0db8 51eee05 cfc0db8 51eee05 cfc0db8 30daf06 56724c7 f47d3bd 3e355cf d07d5ac cfc0db8 d07d5ac cfc0db8 d07d5ac f47d3bd cfc0db8 f47d3bd ece814c f47d3bd 784c769 ece814c 4978dd5 ece814c 784c769 56724c7 4283a03 ece814c 56724c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# π€ Megabots
[![Tests](https://github.com/momegas/qnabot/actions/workflows/python-package.yml/badge.svg)](https://github.com/momegas/qnabot/actions/workflows/python-package.yml)
![](https://dcbadge.vercel.app/api/server/zkqDWk5S7P?style=flat&n&compact=true)
π€ 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 π«΅
- π Join us on Discord: https://discord.gg/zkqDWk5S7P
- βοΈ Work is managed in this project: https://github.com/users/momegas/projects/5/views/2
The Megabots library can be used to create bots that:
- βοΈ are production ready, in minutes
- ποΈ can answer questions over documents
- π§ββοΈ can act as personal assistants and use tools (Coming soon)
- π£οΈ can accept voice as an input (Coming soon)
- π validate and correct the outputs of large language models using [guardrails](https://github.com/ShreyaR/guardrails) (Coming soon)
- π° semanticly cache LLM Queries and reduce your LLM API Costs by 10x using (Coming soon)
- ποΈ are mega-easily to train (Coming soon)
π€ 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, [FastAPI](https://fastapi.tiangolo.com/) 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.
## Getting started
Note: This is a work in progress. The API might change.
```bash
pip install megabots
```
```python
from megabots import bot
import os
os.environ["OPENAI_API_KEY"] = "my key"
# Create a bot π with one line of code. Automatically loads your data from ./index or index.pkl.
qnabot = bot("qna-over-docs")
# Ask a question
answer = bot.ask("How do I use this bot?")
# Save the index to save costs (GPT is used to create the index)
bot.save_index("index.pkl")
# Load the index from a previous run
qnabot = bot("qna-over-docs", index="./index.pkl")
# Or create the index from a directory of documents
qnabot = bot("qna-over-docs", index="./index")
# Change the model
qnabot = bot("qna-over-docs", model="text-davinci-003")
# Change the prompt
prompt_template = "Be humourous in your responses. Question: {question}\nContext: {context}, Answer:"
prompt_variables=["question", "context"]
qnabot = bot("qna-over-docs", prompt_template=prompt_template, prompt_variables=prompt_variables)
```
You can also create a FastAPI app that will expose the bot as an API using the create_app function.
Assuming you file is called `main.py` run `uvicorn main:app --reload` to run the API locally.
You should then be able to visit `http://localhost:8000/docs` to see the API documentation.
```python
from megabots import bot, create_api
app = create_app(bot("qna-over-docs"))
```
You can expose a gradio UI for the bot using `create_interface` function.
Assuming your file is called `ui.py` run `gradio qnabot/ui.py` to run the UI locally.
You should then be able to visit `http://127.0.0.1:7860` to see the API documentation.
```python
from megabots import bot, create_interface
demo = create_interface(bot("qna-over-docs"))
```
## Customising bot
The `bot` function should serve as the starting point for creating and customising your bot. Below is a list of the available arguments in `bot`.
| Argument | Description |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| task | The type of bot to create. Available options: `qna-over-docs`. More comming soon |
| 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` |
| 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. |
| prompt_template | A string template for the prompt, which defines the format of the question and context passed to the model. The template should include placeholders for the variables specified in `prompt_variables`. |
| prompt_variables | A list of variables to be used in the prompt template. These variables are replaced with actual values when the bot processes a query. |
| 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. |
## How QnA bot works
Large 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."
In 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.
`qna-over-docs` uses FAISS to create an index of documents and GPT to generate answers.
```mermaid
sequenceDiagram
actor User
participant API
participant LLM
participant Vectorstore
participant IngestionEngine
participant DataLake
autonumber
Note over API, DataLake: Ingestion phase
loop Every X time
IngestionEngine ->> DataLake: Load documents
DataLake -->> IngestionEngine: Return data
IngestionEngine -->> IngestionEngine: Split documents and Create embeddings
IngestionEngine ->> Vectorstore: Store documents and embeddings
end
Note over API, DataLake: Generation phase
User ->> API: Receive user question
API ->> Vectorstore: Lookup documents in the index relevant to the question
API ->> API: Construct a prompt from the question and any relevant documents
API ->> LLM: Pass the prompt to the model
LLM -->> API: Get response from model
API -->> User: Return response
```
|