This project is a Document Chatbot that uses Streamlit for the user interface and the Ollama model for question-answering tasks. The chatbot ingests a document and then answers questions based on the information in the document.
- Python 3.9 or higher
- Streamlit 1.31.1 or higher
- Ollama model from the Langchain library
- Mistral model
- Clone the repository:
git clone https://github.com/timpara/docuchat.git
- Navigate to the project directory:
cd docuchat
- Install the dependencies:
poetry install
- Install the Ollama model:
ollama pull mistral
- Start the Streamlit server:
streamlit run app.py
- Open your web browser and go to
http://localhost:8501
. - Upload a document using the file uploader. The supported file types are PDF, Markdown (md), Text (txt), Word (docx, doc).
- After the document is ingested, you can ask questions in the text input field labeled "Message". The chatbot will answer based on the information in the uploaded document.
The chatbot uses the Ollama model for question-answering tasks. When a document is uploaded, the ChatDocument
class ingests the document, splits it into chunks, and stores it in a vector store. The ChatDocument
class also sets up a retriever and a chat model chain.
When a question is asked, the ChatDocument
class uses the retriever to find relevant context in the vector store. The context and the question are then passed to the chat model chain, which generates an answer.
The Streamlit interface provides a user-friendly way to interact with the chatbot. It allows you to upload documents, ask questions, and view the chatbot's answers.
The Document Chatbot works in the following way:
-
Ingestion: When a document is uploaded, the
ChatDocument
class ingests the document. This is done in theingest
method. The document is loaded using theUnstructuredMarkdownLoader
class from thelangchain_community.document_loaders
module. The document is then split into chunks using theMarkdownTextSplitter
class from thelangchain.text_splitter
module. These chunks are then filtered to remove complex metadata. -
Vector Store Creation: The chunks are then used to create a vector store using the
Chroma
class from thelangchain.vectorstores
module. TheChroma
class uses theFastEmbedEmbeddings
class from thelangchain.embeddings
module to create embeddings for the chunks. These embeddings are then stored in the vector store. -
Retriever Setup: The vector store is then used to set up a retriever. The retriever is used to find relevant context in the vector store when a question is asked. The retriever is set up to return the top 3 results that have a similarity score of at least 0.1.
-
Chat Model Chain Setup: A chat model chain is then set up. The chat model chain is a sequence of operations that are performed to generate an answer. The chat model chain consists of the retriever, a prompt, the Ollama model, and an output parser. The retriever provides the context, the prompt formats the context and the question, the Ollama model generates an answer, and the output parser formats the answer.
-
Question Answering: When a question is asked, the
ChatDocument
class uses the chat model chain to generate an answer. TheChatDocument
class uses the retriever to find relevant context in the vector store. The context and the question are then passed to the chat model chain, which generates an answer.
The Streamlit interface provides a user-friendly way to interact with the chatbot. It allows you to upload documents, ask questions, and view the chatbot's answers. The interface is defined in the app.py
file. The interface uses the streamlit
and streamlit_chat
modules to create a web page with a file uploader and a chat interface. The file uploader is used to upload documents, and the chat interface is used to ask questions and view answers.