Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

t# LangChain & LangGraph Projects

A collection of 6 small projects exploring LangChain and LangGraph — from a one-line OpenAI call to a self-correcting RAG pipeline. Each project lives in its own folder with its own requirements.txt and .env file.


What's Inside

# Project What It Does
1 simple_message/ Basic LLM calls — direct, with output parser, with prompt template, and served via FastAPI.
2 chating_history/ A chatbot that remembers the conversation using RunnableWithMessageHistory.
3 vector_store/ Embed documents, store them in Chroma, and run similarity search.
4 rag_project/ A simple RAG pipeline: load a blog post, chunk it, retrieve, answer.
5 agents/ A ReAct agent that can search the web with Tavily.
6 advanced_rag/ Self-correcting RAG with LangGraph: routing, document grading, hallucination check, web fallback.

The projects are roughly ordered from easy to hard. If you're new to LangChain, follow them in order.


Setup

You'll need Python 3.10+. Each folder has its own dependencies, so install them per project:

# Clone the repo
git clone <repo-url>
cd langchain-projects

# Pick a project
cd simple_message

# (Optional) Create a virtual environment
python -m venv venv
source venv/bin/activate          # Windows: venv\Scripts\activate

# Install
pip install -r requirements.txt

Then run the script:

python simplemessage.py

API Keys (.env files) — Important!

Every project needs at least an OpenAI API key. Some need extra keys (Tavily for web search, LangSmith for tracing). Each folder already contains a .env file with the key names — you just need to fill in the values.

⚠️ Never commit real API keys to GitHub. Add .env to your .gitignore (one is already provided in advanced_rag/).

Where to get the keys

Key Used by Get it from
OPENAI_API_KEY All projects https://platform.openai.com/api-keys
TAVILY_API_KEY agents/, advanced_rag/ https://tavily.com (free tier available)
LANGCHAIN_API_KEY LangSmith tracing (optional) https://smith.langchain.com
LANGCHAIN_TRACING_V2 Set to true to enable tracing
LANGCHAIN_PROJECT Project name shown in LangSmith Any string, e.g. my-rag

What each project needs

Project Required keys Optional keys
simple_message/ OPENAI_API_KEY LangSmith vars
chating_history/ OPENAI_API_KEY LangSmith vars
vector_store/ OPENAI_API_KEY LangSmith vars
rag_project/ OPENAI_API_KEY LangSmith vars
agents/ OPENAI_API_KEY, TAVILY_API_KEY LangSmith vars
advanced_rag/ OPENAI_API_KEY, TAVILY_API_KEY LangSmith vars

Example .env

OPENAI_API_KEY=sk-...
TAVILY_API_KEY=tvly-...
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=ls__...
LANGCHAIN_PROJECT=my-langchain-project

load_dotenv() is called at the top of every script, so the keys are picked up automatically.


Project Details

1. simple_message

Four small scripts that build up the basic LangChain pattern step by step:

  • simplemessage.py — the simplest case: send messages, get a response.
  • simplemessagewithoutparser.py — same, but pipe the output through StrOutputParser.
  • simplemessagewithtemplates.py — use ChatPromptTemplate so the prompt is reusable.
  • serve.py — wrap the chain in a FastAPI app with LangServe.

Run any of them with python <filename>.py. For serve.py, open http://localhost:8080/chain/playground after starting it.

2. chating_history

A chatbot that keeps track of the conversation. Uses InMemoryChatMessageHistory with a session_id so multiple users can have separate conversations. The session ID is currently hardcoded — change it to support multiple users.

python main.py
> Hello!
> What did I just say?

3. vector_store

Embeds 5 short documents about pets, stores them in Chroma, and shows three ways to query: by text, by text with score, and by vector. main.py extends this into a full retrieval-augmented chain that answers questions using only the stored context.

4. rag_project

A classic RAG pipeline:

  1. Load a blog post about LLM agents using WebBaseLoader.
  2. Split it into chunks with RecursiveCharacterTextSplitter.
  3. Embed and store in Chroma.
  4. Retrieve relevant chunks for a question.
  5. Generate an answer with the retrieved context.

The output streams as it's generated.

5. agents

A ReAct-style agent built with create_react_agent from LangGraph. The agent has one tool — Tavily web search — and can decide when to use it. Try asking it about current weather or recent news, things the LLM doesn't know on its own.

6. advanced_rag

The most complex project. A self-correcting RAG pipeline built as a LangGraph state machine. The flow:

  1. Route — decide whether the question goes to the vector store or directly to web search.
  2. Retrieve — get documents from Chroma.
  3. Grade documents — check whether the retrieved docs are actually relevant.
  4. Generate — produce an answer.
  5. Hallucination check — verify the answer is grounded in the docs.
  6. Answer check — verify the answer addresses the question.
  7. Fall back to web search if any check fails.

The graph is in graph/graph.py; nodes and chains are in their own modules. Run python main.py to test it end-to-end.


Common Terms

Term What It Means
Chain A pipeline of steps connected with |, e.g. prompt | model | parser.
Runnable The base interface for anything that can be .invoke()-d in LangChain.
Embedding A vector that represents the meaning of a piece of text.
Vector store A database that lets you search documents by semantic similarity.
RAG Retrieval-Augmented Generation — fetch relevant docs, then ask the LLM to answer using them.
ReAct agent An agent that alternates between Reasoning (thinking) and Acting (using a tool).
LangGraph A library for building LLM workflows as state machines.

Tips

  • Costs add up. Each call to OpenAIEmbeddings or ChatOpenAI costs money. The advanced RAG project re-builds the vector store on every run — add a persist_directory check to skip re-embedding.
  • LangSmith helps a lot. Set LANGCHAIN_TRACING_V2=true and you'll see every step of every chain in https://smith.langchain.com — invaluable for debugging.
  • Watch your model name. GPT-4 is much pricier than GPT-3.5. For simple translations or routing, gpt-3.5-turbo is more than enough.

License

Provided for educational purposes.


Built With

LangChain · LangGraph · LangServe · LangSmith · OpenAI · Chroma · Tavily · FastAPI

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages