Skip to content

KonulJ/Memory-Enabled-Chatbot-using-LangChain-Groq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memory-Enabled Chatbot

Python LangChain Groq Streamlit License

A conversational AI chatbot with multi-session memory, multi-language support, and token-aware history trimming — built with LangChain LCEL and Groq.

The key differentiator from a basic chatbot: each named session keeps fully independent memory, the model responds in the user's chosen language, and a sliding token window prevents context overflow on long conversations.


Features

  • Multi-session memory — run parallel conversations under different session IDs with fully isolated history (RunnableWithMessageHistory)
  • Multi-language responses — English, German, or Turkish, switched per session without clearing history
  • Token-aware trimming — adjustable memory window (50–500 tokens) trims oldest messages first while always keeping the system prompt
  • Streaming output — responses stream token-by-token via st.write_stream
  • Secure API key handling — credentials loaded from .env, never hardcoded

Architecture

User Input
    │
    ▼
Streamlit UI  ──►  RunnableWithMessageHistory
                         │           │
                    trim_messages   session store
                         │           (per session_id)
                         ▼
                   ChatPromptTemplate
                   (system + history + input)
                         │
                         ▼
                      ChatGroq  ──►  streaming response

Project Structure

memory-chatbot/
├── app.py            # Streamlit app — multi-session UI, chain, streaming
├── requirements.txt  # Python dependencies
├── 1-chatbots.ipynb  # Learning notebook — step-by-step implementation walkthrough
├── .env              # API key (git-ignored)
└── README.md

Getting Started

Prerequisites

1. Clone the repository

git clone https://github.com/KonulJ/Memory-Enabled-Chatbot-using-LangChain-Groq.git
cd Memory-Enabled-Chatbot-using-LangChain-Groq

2. Install dependencies

pip install -r requirements.txt

3. Set up your API key

GROQ_API_KEY=your_groq_api_key_here

4. Run the app

streamlit run app.py

How It Works

  1. The user picks a session ID and language in the sidebar.
  2. Each session ID maps to its own ChatMessageHistory stored in Streamlit session state.
  3. On each turn, trim_messages slides a token window over the history — recent messages are kept, old ones dropped, the system prompt always preserved.
  4. A ChatPromptTemplate injects the trimmed history and language into the prompt, then ChatGroq streams the response token-by-token.
  5. Switching session ID instantly loads a different conversation — the previous one is untouched.

Key Concepts Demonstrated

Concept Implementation
Stateful LCEL chains RunnableWithMessageHistory
Multi-session isolation session_id-keyed ChatMessageHistory store
Token budget management trim_messages with strategy="last"
Multi-language prompting {language} variable in system prompt
Streaming UI st.write_stream + chain.stream()

Roadmap

  • Persistent storage (SQLite/PostgreSQL)
  • RAG integration for document-aware sessions
  • Voice input/output
  • Export chat history to .txt / .json

License

MIT


Built by Konul Jafarova

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors