-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Quivr chatbot example #2827
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b40e3c
feat: Add Quivr chatbot example
StanGirard ab21f9d
Merge branch 'main' into feat/chainlit-core-example
StanGirard daf4f38
feat: Add streaming response to Quivr chatbot
StanGirard bec1194
chore: Update quivr-core and chainlit versions in requirements.txt
StanGirard 5e9bd68
Merge branch 'main' into feat/chainlit-core-example
StanGirard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Quivr Chatbot Example | ||
|
||
This example demonstrates how to create a simple chatbot using Quivr and Chainlit. The chatbot allows users to upload a text file and then ask questions about its content. | ||
|
||
## Prerequisites | ||
|
||
- Python 3.8 or higher | ||
|
||
## Installation | ||
|
||
1. Clone the repository or navigate to the `backend/core/examples/chatbot` directory. | ||
|
||
2. Install the required dependencies: | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Running the Chatbot | ||
|
||
1. Start the Chainlit server: | ||
|
||
``` | ||
chainlit run main.py | ||
``` | ||
|
||
2. Open your web browser and go to the URL displayed in the terminal (usually `http://localhost:8000`). | ||
|
||
## Using the Chatbot | ||
|
||
1. When the chatbot interface loads, you will be prompted to upload a text file. | ||
|
||
2. Click on the upload area and select a `.txt` file from your computer. The file size should not exceed 20MB. | ||
|
||
3. After uploading, the chatbot will process the file and inform you when it's ready. | ||
|
||
4. You can now start asking questions about the content of the uploaded file. | ||
|
||
5. Type your questions in the chat input and press Enter. The chatbot will respond based on the information in the uploaded file. | ||
|
||
## How It Works | ||
|
||
The chatbot uses the Quivr library to create a "brain" from the uploaded text file. This brain is then used to answer questions about the file's content. The Chainlit library provides the user interface and handles the chat interactions. | ||
|
||
Enjoy chatting with your documents! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Quivr Chatbot Example | ||
|
||
This example demonstrates how to create a simple chatbot using Quivr and Chainlit. The chatbot allows users to upload a text file and then ask questions about its content. | ||
|
||
## Prerequisites | ||
|
||
- Python 3.8 or higher | ||
|
||
## Installation | ||
|
||
1. Clone the repository or navigate to the `backend/core/examples/chatbot` directory. | ||
|
||
2. Install the required dependencies: | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Running the Chatbot | ||
|
||
1. Start the Chainlit server: | ||
|
||
``` | ||
chainlit run main.py | ||
``` | ||
|
||
2. Open your web browser and go to the URL displayed in the terminal (usually `http://localhost:8000`). | ||
|
||
## Using the Chatbot | ||
|
||
1. When the chatbot interface loads, you will be prompted to upload a text file. | ||
|
||
2. Click on the upload area and select a `.txt` file from your computer. The file size should not exceed 20MB. | ||
|
||
3. After uploading, the chatbot will process the file and inform you when it's ready. | ||
|
||
4. You can now start asking questions about the content of the uploaded file. | ||
|
||
5. Type your questions in the chat input and press Enter. The chatbot will respond based on the information in the uploaded file. | ||
|
||
## How It Works | ||
|
||
The chatbot uses the Quivr library to create a "brain" from the uploaded text file. This brain is then used to answer questions about the file's content. The Chainlit library provides the user interface and handles the chat interactions. | ||
|
||
Enjoy chatting with your documents! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import tempfile | ||
|
||
import chainlit as cl | ||
from quivr_core import Brain | ||
|
||
|
||
@cl.on_chat_start | ||
async def on_chat_start(): | ||
files = None | ||
|
||
# Wait for the user to upload a file | ||
while files is None: | ||
files = await cl.AskFileMessage( | ||
content="Please upload a text .txt file to begin!", | ||
accept=["text/plain"], | ||
max_size_mb=20, | ||
timeout=180, | ||
).send() | ||
|
||
file = files[0] | ||
|
||
msg = cl.Message(content=f"Processing `{file.name}`...", disable_feedback=True) | ||
await msg.send() | ||
|
||
with open(file.path, "r", encoding="utf-8") as f: | ||
text = f.read() | ||
|
||
with tempfile.NamedTemporaryFile( | ||
mode="w", suffix=".txt", delete=False | ||
) as temp_file: | ||
temp_file.write(text) | ||
temp_file.flush() | ||
temp_file_path = temp_file.name | ||
|
||
brain = Brain.from_files(name="user_brain", file_paths=[temp_file_path]) | ||
|
||
# Store the file path in the session | ||
cl.user_session.set("file_path", temp_file_path) | ||
|
||
# Let the user know that the system is ready | ||
msg.content = f"Processing `{file.name}` done. You can now ask questions!" | ||
await msg.update() | ||
|
||
cl.user_session.set("brain", brain) | ||
|
||
|
||
@cl.on_message | ||
async def main(message: cl.Message): | ||
brain = cl.user_session.get("brain") # type: Brain | ||
|
||
if brain is None: | ||
await cl.Message(content="Please upload a file first.").send() | ||
return | ||
|
||
# Prepare the message for streaming | ||
msg = cl.Message(content="") | ||
await msg.send() | ||
|
||
# Use the ask_stream method for streaming responses | ||
async for chunk in brain.ask_streaming(message.content): | ||
await msg.stream_token(chunk.answer) | ||
|
||
await msg.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
quivr-core==0.0.7 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quivr-core[base] should probably work which remove faiss, langchain-community, ...
|
||
langchain-community==0.2.6 | ||
faiss-cpu==1.8.0 | ||
langchain-openai==0.1.14 | ||
chainlit==1.0.0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tempfile are usually garbage collected as soon and the context is closed. I think we can pass that file.path directly to the brain 🤔 ?
If this doesn't work, maybe have the logic inside the context manager