Implement RAG agents with LangGraph
From 0 to an AI engineer PRO - A system. Designing the ideal fine-tuning dataset generation pipeline.
This week’s topics:
Implement RAG agents with LangGraph
From 0 to an AI engineer PRO - A system
Designing the ideal fine-tuning dataset generation pipeline
Implement RAG agents with LangGraph
Here's the best piece of advice you need to build real-world agents:
"Stop thinking in prompts; start thinking in graphs."
Because under the hood, serious agentic systems aren’t just string manipulation.
They’re structured, dynamic workflows.
And that’s exactly how the PhiloAgent works...
It’s not a prompt wrapped in a Python script.
It’s a full agentic RAG system -
Let’s break it down...
We use a stateful execution graph to drive our philosopher NPCs.
Here’s how:
1/ Conversation Node
Handles the primary logic.
It merges incoming messages, current state, and philosopher identity (style, tone, perspective) to generate the next reply.
2/ Retrieval Tool Node
If the agent needs more information, it calls a MongoDB-powered vector search to fetch relevant facts about the philosopher's life and work.
This turns simple RAG into agentic RAG, since the LLM dynamically chooses tool calls.
3/ Summarize Context Node
We summarize long-retrieved passages before injecting them into the prompt.
This keeps prompts clean and focused, avoiding dumping in whole Wikipedia pages.
4/ Summarize Conversation Node
If the conversation gets long, we summarize and trim earlier messages and keep only recent context, while preserving meaning.
We need the summary for the agent to be consistent and reference early topics from the conversation.
This helps keep the context window short and focused, lowering costs and latency and improving accuracy.
5/ End Node
Wraps up the cycle.
Memory is updated, context evolves, and the agent grows with every message.
Here’s the implementation details:
The short-term memory is kept as a Pydantic in-memory state: the PhilosopherState
Tool orchestration with LangChain
Low-latency LLMs, such as Llama 70B, served by Groq
Smaller 8B models used for summarization tasks
Prompt templates are dynamically generated per philosopher
Served as a real-time REST API through FastAPI & WebSockets to power the game UI
Monitoring + evaluation wired through Opik by Comet ML
In short:
Agents come alive through structure, memory, tools, and flow control.
You can adapt this exact system to build:
Context-aware assistants
Multi-turn RAG copilots
NPCs, tutors, or internal tools that think and retrieve
We walk through every step (with code) in Lesson 2 of the PhiloAgents course ↓
From 0 to an AI engineer PRO - A system (Affiliate)
Most people don’t need another tutorial.
They need a system upgrade.
Because here’s the truth:
Learning Python is easy. Understanding LLMs is fascinating. But stitching everything together to build a real GenAI application?
That’s where most people stall.
Scattered blog posts and YouTube videos can only get you so far.
What you need is:
The right sequence
The right tools
And a roadmap that doesn’t waste your time
That’s why we recommend
and bundle, where they pack all their top courses into one end-to-end curriculum.
The goal?
To help you go from novice to expert without the noise.
Whether you’re new to Python, dabbling in LLMs, or trying to build production-ready GenAI apps...
This bundle gives you the full stack:
Python for GenAI
LLM fundamentals (prompting, RAG, agents)
Advanced app building (production systems)
A playbook to avoid hallucinations, breakdowns, and bloated AI architectures
Taking you from 0 knowledge in Python to building end-to-end LLM applications.
You’ll finish with projects you can demo, skills companies actually hire for -
And a working understanding of how to build with GenAI from scratch.
Already know Python? There’s a leaner bundle with just the GenAI courses, too.
P.S. Use code PAUL_15 to support us and get 15% off
Designing the ideal fine-tuning dataset generation pipeline
Here's what training and fine-tuning dozens of ML models taught me:
Creating high-quality fine-tuning datasets it’s a lot trickier that it looks....
The true complexity lies in balancing data diversity, complexity, and quality.
So, what's the ideal dataset generation pipeline?
1/ Data Extraction
Start by gathering raw data that fits your use case.
It may not be in the right format initially, but transforming it into instruction-answer pairs is the first step in aligning it with fine-tuning needs.
2/ Data Deduplication
Remove duplicates to avoid overfitting.
Use techniques like MD5/SHA-256 for exact duplicates or MinHash/semantic similarity to ensure the dataset contains unique, diverse examples.
3/ Data Decontamination
Ensure no overlap between your train, validation, and test sets.
This prevents data leakage - which could lead to inflated scores during evaluation.
4/ Data Quality Evaluation
Filter out low-quality samples based on heuristics or LLM-based reviews.
Your model needs only high-quality examples to learn from.
5/ Data Exploration
Analyze your dataset using techniques like clustering to find concentrations around specific topics.
This also helps you identify potential gaps that need addressing.
6/ Data Generation
When you find gaps, generate new data manually or through distillation from more knowledgeable models.
This helps cover corner cases or rare scenarios that are important for your application.
7/ Data Augmentation
Enhance your dataset by evolving existing instructions and answers.
Make them more complex or nuanced to boost both the quantity and quality of your data without starting from scratch.
Takeaway:
By following these seven steps, you can build a dataset generation pipeline that ensures high accuracy, diversity, and complexity.
Check out the article below for learning how to implement the ideal fine-tuning dataset generation pipeline ↓
Whenever you’re ready, there are 3 ways we can help you:
Perks: Exclusive discounts on our recommended learning resources
(books, live courses, self-paced courses and learning platforms).
The LLM Engineer’s Handbook: Our bestseller book on teaching you an end-to-end framework for building production-ready LLM and RAG applications, from data collection to deployment (get up to 20% off using our discount code).
Free open-source courses: Master production AI with our end-to-end open-source courses, which reflect real-world AI projects and cover everything from system architecture to data collection, training and deployment.
Images
If not otherwise stated, all images are created by the author.