Gen AI project · LLM Applications
LLM Chatbot
A from-scratch multi-turn chatbot on Gemini 2.0 Flash with conversation memory and a model layer that can be swapped out.
Overview
A small, readable chatbot built directly on Google’s google-genai SDK, with no LangChain or other framework. The aim was to understand what a “chat” with an LLM actually is under the hood. In particular: how a model that is stateless between requests can remember what you said three messages ago.
The key idea: conversation memory
An LLM API call has no memory of its own. To make a conversation feel continuous, the app has to resend the earlier turns with every new message. This project does that explicitly:
- Store the history. Streamlit’s
session_stateholds achat_historylist that survives page reruns. - Open a chat with it. On every interaction, a Gemini chat session is created seeded with that history, using the SDK’s own
Contentobjects withuserandmodelroles. - Record each exchange. When you ask a question, the answer comes back and both your message and the reply are appended to the history.
- Show the transcript. The full conversation is displayed below the latest answer.
So when you ask “What did I ask you earlier?”, the model can answer, because it has been given the whole conversation every time.
Design choices
- Model-agnostic. The model is a single constant (
gemini-2.0-flash), so switching to another Gemini model is a one-line change, and the history-handling pattern carries over to other chat APIs. - Minimal dependencies. Only three:
streamlitfor the UI,google-genaifor the model andpython-dotenvfor the API key. The whole app is easy to read end to end.
Running it
pdm install
echo "GOOGLE_API_KEY=<your key>" > .env
pdm run streamlit run app.py
The repository includes a short demo GIF of a multi-turn conversation.