Spaces:
Runtime error
Runtime error
File size: 3,214 Bytes
56724c7 8aaf6b2 56724c7 507e0ff 56724c7 4283a03 56724c7 51eee05 56724c7 3e355cf d07d5ac 3e355cf d07d5ac 0eed37b d07d5ac f4e889a 0eed37b 51eee05 56724c7 4283a03 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 |
# QnA Bot
[](https://github.com/momegas/qnabot/actions/workflows/python-package.yml)
Create a question answering over docs bot with one line of code:
```bash
pip install qnabot
```
```python
from qnabot import QnABot
import os
os.environ["OPENAI_API_KEY"] = "my key"
# Create a bot π with one line of code
bot = QnABot(directory="./mydata")
# 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.pickle")
# Load the index from a previous run
bot = QnABot(directory="./mydata", index="index.pickle")
```
You can also create a FastAPI app that will expose the bot as an API.
Just 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 qnabot import QnABot, create_app
app = create_app(QnABot("./mydata"))
```
### Features
- [x] Create a question answering bot over your documents with one line of code using GPT
- [x] Save / load index to reduce costs (Open AI embedings are used to create the index)
- [x] Local data source (directory of documents) or S3 data source
- [x] FAISS for storing vectors / index
- [x] Expose bot over API using FastAPI
- [ ] Support for other vector databases (e.g. Weaviate, Pinecone)
- [ ] Customise prompt
- [ ] Support for LLaMA model
- [ ] Support for Anthropic models
- [ ] CLI / UI
### Here's how it 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.
`QnABot` 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
```
|