Blog #ai #openai #portfolio

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

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 ChatOpenAI for interactive terminal turns.
  • Conversation Summary Memory: Uses ConversationSummaryMemory (persisted via FileChatMessageHistory) so long sessions stay within token budgets instead of replaying every raw message.
  • Structured Chat Prompts: Combines ChatPromptTemplate, MessagesPlaceholder, and HumanMessagePromptTemplate to inject rolling summaries cleanly into each turn.
  • Environment Management: Loads API keys from .env via python-dotenv.

How It Works

  1. Environment Setup: Reads OPENAI_API_KEY from .env.
  2. Model Initialization: Instantiates ChatOpenAI.
  3. Memory Management: Summarizes prior turns and merges them into the prompt context.
  4. Prompt Configuration: Builds a chat template with placeholders for history and user input.
  5. Interactive Loop: Reads user prompts from stdin and prints contextual responses until exit.

Usage

  1. Install dependencies (python-dotenv, langchain, openai).
  2. Clone the repository.
  3. Add your OPENAI_API_KEY to .env.
  4. Run python main.py.

Chroma Embeddings for Text Files

Features

  • Document Chunking: Loads source text with TextLoader and splits it into fixed-size chunks via CharacterTextSplitter.
  • 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 RetrievalQA and ChatOpenAI to answer questions grounded in facts.txt.

How It Works

  • main.py: Loads and chunks facts.txt, computes embeddings, initializes the local Chroma index, and runs a quick similarity search check.
  • prompt.py: Connects ChatOpenAI to the persisted Chroma collection through a RetrievalQA chain to answer user questions.

Usage

  1. Set up your Python environment and .env credentials.
  2. Clone the repository and drop your source text into facts.txt.
  3. Run main.py once to build the Chroma index, then run prompt.py to 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.