This is the Chatbot component, responsible for receiving a natural language query from the user then performing a hybrid KNN and keyword search in CrateDB to identify relevant information from a set of PDF files that were previously ingested using the Data Extractor.
Results are presented back to the user in natural language by sending them to an LLM.
First, you'll need to get a local copy of the code, set up a CrateDB database and get an OpenAI API key. Follow the instructions in the main README, returning here when you're ready.
You should create and activate a Python Virtual Environment to install this project's dependencies into. To do this, run the following commands:
cd chatbot
python -m venv venv
. ./venv/bin/activateNow install the dependencies that this project requires:
pip install -r requirements.txtThe chatbot uses Spacy to perform natural language processing on queries from the user. You'll need to download Spacy's English pipeline model before continuing:
python -m spacy download en_core_web_smThe chatbot has several configuration parameters. These are all defined in a .env file and should be considered secrets, don't commit them to source control!
To get you started, we've provided a template file env.example. Create a .env file by running the following command:
cp env.example .envThen edit .env and make the following changes:
- Set the value of
CRATEDB_URLto:https://<hostname>:4200/_sqlif you are using a cloud database, replacing<hostname>with the host name, which looks something likesome-host-name.gke1.us-central1.gcp.cratedb.net.http://localhost:4200/_sqlif you're using Docker.
- Set the value of
CRATEDB_USERNAMEtoadminif you are using a cloud database, orcrateif you are using Docker. - Set the value of
CRATEDB_PASSWORDto your database password if you are using a cloud database, or leave it blank if you are using Docker. - Set the value of
OPENAI_API_KEYto your OpenAI API key.
Save your changes before attempting to run the chatbot.
The chatbot has two interfaces. One has a basic terminal prompt, the other is a web application using the Streamlit framework.
Start the chatbot's terminal interface with the following command:
python chatbot.pyThe interface will appear in the terminal window.
If you'd prefer to run the web interface, use this command:
streamlit run chatbot-with-ui.pyYour browser should open a new tab with the chatbot interface in it. If it doesn't, point your browser at http://localhost:8501/ to see it.
Once you've started the chatbot, ask it a question using natual language. For example you might ask:
"How does CrateDB fit into the AI ecosystem, specifically in the area of knowledge assistants?"
The chatbot will generate its answer by performing hybrid search queries against the chunked PDF text and image data stored in CrateDB and feeding the responses as context to an LLM.
If you're using the Streamlit web interface, each link to a source document is clickable and should open the document for you on the page referenced.