LangChain Chat and Chroma Embeddings Integration
A two-part Python sandbox exploring stateful terminal chat with ConversationSummaryMemory and local retrieval-augmented generation (RAG) using Chroma.
- Published
- Updated
- Reading
- 2 min
- Author
- Sudhanva Narayana
Overview
tchat is a two-part Python sandbox exploring memory and retrieval patterns in LangChain: a terminal chat loop that compresses long conversation histories with ConversationSummaryMemory, and a local retrieval-augmented generation (RAG) pipeline backed by Chroma embeddings.
LangChain Chat Application
Features
- OpenAI Chat Model: Uses
ChatOpenAIfor interactive terminal turns. - Conversation Summary Memory: Uses
ConversationSummaryMemory(persisted viaFileChatMessageHistory) so long sessions stay within token budgets instead of replaying every raw message. - Structured Chat Prompts: Combines
ChatPromptTemplate,MessagesPlaceholder, andHumanMessagePromptTemplateto inject rolling summaries cleanly into each turn. - Environment Management: Loads API keys from
.envviapython-dotenv.
How It Works
- Environment Setup: Reads
OPENAI_API_KEYfrom.env. - Model Initialization: Instantiates
ChatOpenAI. - Memory Management: Summarizes prior turns and merges them into the prompt context.
- Prompt Configuration: Builds a chat template with placeholders for history and user input.
- Interactive Loop: Reads user prompts from stdin and prints contextual responses until exit.
Usage
- Install dependencies (
python-dotenv,langchain,openai). - Clone the repository.
- Add your
OPENAI_API_KEYto.env. - Run
python main.py.
Chroma Embeddings for Text Files
Features
- Document Chunking: Loads source text with
TextLoaderand splits it into fixed-size chunks viaCharacterTextSplitter. - Vector Embeddings: Generates embeddings with
OpenAIEmbeddings. - Chroma Vector Store: Persists embeddings locally in Chroma for fast similarity search.
- Retrieval QA: Pairs a custom duplicate-filtering retriever with
RetrievalQAandChatOpenAIto answer questions grounded infacts.txt.
How It Works
main.py: Loads and chunksfacts.txt, computes embeddings, initializes the local Chroma index, and runs a quick similarity search check.prompt.py: ConnectsChatOpenAIto the persisted Chroma collection through aRetrievalQAchain to answer user questions.
Usage
- Set up your Python environment and
.envcredentials. - Clone the repository and drop your source text into
facts.txt. - Run
main.pyonce to build the Chroma index, then runprompt.pyto query it.
For the full source code, see tchat on GitHub.
Related work
Production ML context
See how this topic connects to production ML systems, infrastructure, and inference.