Skip to main content
In this section, you will learn how to connect your LangChain chat assistant to a local SQLite database to store and retrieve conversation history.

Objectives

  1. Understand SQL-backed persistent history tracking.
  2. Store message loops dynamically in a local SQLite file using SQLChatMessageHistory.
  3. Retrieve and output logs for a specific session ID from a completely separate script.

SQLite Persistence

Unlike cloud databases, SQLite requires zero credentials or network configurations. It saves all logs locally inside a standard .db database file.

Goal

Implement a live terminal chat loop that saves queries to a local SQLite database, and create a second script to retrieve and print those saved conversation logs.

Sample Input

Sample Output

Automatic storage of messages inside the local SQLite database.

Plan

  • Saver Script:
    1. Import SQLChatMessageHistory from langchain_community.chat_message_histories.
    2. Instantiate the history object using the local database connection.
    3. Start a console chat loop, adding inputs and replies to the history.
  • Retriever Script:
    1. Connect to the same SQLite database.
    2. Iterate through and print the history messages.

Code Implementation

1. Conversation Loop & Saver (5a_chat_model_save_message_history_sqlite.py)
Letโ€™s build the SQLite saver application incrementally step-by-step:
Step 1: Imports and Setup
Plan:
  1. Import environment variable loader load_dotenv from dotenv.
  2. Import SQLChatMessageHistory from langchain_community.chat_message_histories.
  3. Import the unified model initializer init_chat_model from langchain.chat_models.
Code Implementation:
Step 2: Initialize Database Connection
Plan:
  1. Set up a unique SESSION_ID string to identify this chat session.
  2. Define the local connection URI sqlite:///chat_history.db.
  3. Instantiate the SQLChatMessageHistory object.
Code Implementation:
Step 3: Setup Chat Model
Plan:
  1. Initialize a Groq-provided Llama model (llama-3.3-70b-versatile) using the core abstraction helper init_chat_model.
Code Implementation:
Step 4: Execute Interactive Chat Loop
Plan:
  1. Prompt user queries from the console in a while True loop.
  2. Call .add_user_message() to persist the input in the SQL database.
  3. Call model.invoke() passing the current full message array.
  4. Call .add_ai_message() to persist the modelโ€™s answer.
Code Implementation:
Combined Saver Code
Combining all the steps above gives the final completed script:
2. Session Retriever Script (5b_chat_model_retrieve_message_history_sqlite.py)
Below is the retriever script that loads the SQL log independently and outputs the session chat logs:

Exercise: Session Config Checker ๐Ÿ”

Goal

Write a python utility function check_logs(session_id: str) that connects to our local SQLite database and prints how many human vs AI messages are currently stored for the given session.

Sample Input

Sample Output

Plan

  1. Instantiate the SQLChatMessageHistory inside the function.
  2. Iterate through .messages and count human and ai instances.
  3. Print the formatted totals.

Practice & Exercises

To practice, open the interactive notebook:

Practice & Exercises

Practice configuring SQL databases locally for persistent conversation sessions.๐Ÿ’ป VS Code | ๐Ÿš€ Colab | ๐Ÿ“ฅ Download