Lesson 7: Adding Chat History
Now that our chatbot is working, we want to add chat history. This will allow OpenAI to see all previous messages and understand the whole conversation context:
Adding Chat History
We will build this chat history using a SQLite database. We will create a new file called chat_history.py
file with the following code:
Note: We will use SQLite database for this, as we want to write to something other than the database we use for the chatbot source. We assume the chat we are building has read-only access to the database to ensure no data changes.
src/chat_history.py
import osimport sqlite3 def check_if_storage_folder_exists(): if not os.path.exists("storage"): os.makedirs("storage") def retrieve_history(identifier): check_if_storage_folder_exists() database = sqlite3.connect("storage/chat_database.db") cursor = database.cursor() if not cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='chat_history'").fetchone(): cursor.execute("CREATE TABLE chat_history (identifier text, sender text, message text)") database.commit() cursor.execute("SELECT * FROM chat_history WHERE identifier = ?", [identifier]) history = cursor.fetchall() database.close() return history def write_history(identifier, sender, message): check_if_storage_folder_exists() database = sqlite3.connect("storage/chat_database.db") cursor = database.cursor() cursor.execute("INSERT INTO chat_history VALUES (?, ?, ?)", (identifier, sender, message)) database.commit() database.close()
The idea here is simple - we have two functions:
-
check_if_storage_folder_exists
- this function will check if the storage folder exists and create it if it doesn't (this is where our SQLite database will be stored) -
retrieve_history
- this function will retrieve the chat history based on the identifier -
write_history
- this function will write the chat history based on the identifier, sender, and message
This has prepared our chat history logic, and now we need to implement it into our chatbot.
Implementing Chat History
We will create a chatbot.py
file to use the chat history. While handling the chat history, this file will act as a middleman between our request and the AI engine. Here's what will happen inside:
- We need to accept the identifier as a parameter in the
make_ai_request
function - this will be used to identify the chat history - We need to retrieve the chat history based on the identifier
- We need to form the chat history into a string and add it to the prompt
- We need to write the chat history to the database
- We need to write the assistant's response to the database
Here's how that looks:
chatbot.py
import ai_engineimport chat_history def make_ai_request(question, identifier): # Find the chat log in SQlite database history = chat_history.retrieve_history(identifier) # If the chat log is more than 1 message, add the chat history to the prompt chat_messages = "" if len(history) > 1: chat_messages = "Chat history:" + '\n'.join([f'{message[1]}: {message[2]}\n' for message in history]) # Add the chat history to the prompt chat_history.write_history(identifier, "user", question) ai_response = ai_engine.generate_response(question, chat_messages) chat_history.write_history(identifier, "assistant", ai_response) return ai_response
Next, we need to modify our app.py
file to use the chatbot.py
file and pass the identifier to the make_ai_request
function:
app.py
import ai_engineimport chatbot question = "Do you have any cars newer than 2018?"identifier = "123" print(ai_engine.generate_response(question))print(chatbot.make_ai_request(question, identifier))
When we run our chatbot, we should see a new file in the storage
folder called chat_database.db
. This file will contain the chat history. You can view it via a SQLite database viewer:
If we check our generated prompt, we should see the chat history added to the prompt:
# ... personal_access_tokens has columns: id (bigint), tokenable_type (varchar), tokenable_id (bigint), name (varchar), token (varchar), abilities (text), last_used_at (timestamp), expires_at (timestamp), created_at (timestamp), updated_at (timestamp)users has columns: id (bigint), name (varchar), email (varchar), email_verified_at (timestamp), password (varchar), remember_token (varchar), created_at (timestamp), updated_at (timestamp) Chat history:user: Do you have any cars newer than 2018? assistant: Yes, we do. We have a 2020 Volkswagen Passat which is a hatchback with front-wheel drive (FWD), a 2023 Volvo S90 which is a sedan with all-wheel drive (AWD), and a 2023 Mercedes C63 AMG which is a sedan with rear-wheel drive (RWD). Question: "Do you have any cars newer than 2018?"SQLQuery: SELECT * FROM cars WHERE year > '2018'; # ...
That's it. We have implemented a chat history. We can see the whole conversation context and send it to OpenAI if we have the same identifier.
- Intro: Structure and Preparation
- AI Engine and Main Python Script
- Front-end JavaScript Widget
- Bonus
No comments or questions yet...