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.
- 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
User Input
│
▼
Streamlit UI ──► RunnableWithMessageHistory
│ │
trim_messages session store
│ (per session_id)
▼
ChatPromptTemplate
(system + history + input)
│
▼
ChatGroq ──► streaming response
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
- Python 3.8+
- Free Groq API key from console.groq.com
git clone https://github.com/KonulJ/Memory-Enabled-Chatbot-using-LangChain-Groq.git
cd Memory-Enabled-Chatbot-using-LangChain-Groqpip install -r requirements.txtGROQ_API_KEY=your_groq_api_key_herestreamlit run app.py- The user picks a session ID and language in the sidebar.
- Each session ID maps to its own
ChatMessageHistorystored in Streamlit session state. - On each turn,
trim_messagesslides a token window over the history — recent messages are kept, old ones dropped, the system prompt always preserved. - A
ChatPromptTemplateinjects the trimmed history and language into the prompt, thenChatGroqstreams the response token-by-token. - Switching session ID instantly loads a different conversation — the previous one is untouched.
| 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() |
- Persistent storage (SQLite/PostgreSQL)
- RAG integration for document-aware sessions
- Voice input/output
- Export chat history to
.txt/.json
MIT
Built by Konul Jafarova