A mini LangChain clone, built from scratch to understand how modern LLM frameworks work internally.
Project description
๐ EduChain
An educational Python framework inspired by LangChain โ built from scratch to understand how modern LLM frameworks work internally.
EduChain recreates the core building blocks of LangChain by hand โ including the parts most frameworks hide behind an import statement, like cosine similarity search, streaming source detection, and the agent reasoning loop. Instead of treating LLM frameworks as a black box, EduChain is about understanding the design patterns, abstractions, and architecture behind them well enough to build them yourself.
Note: EduChain is an educational project. It is not intended to replace LangChain or be feature-compatible with it.
โจ Features
Core Primitives
- โ
Runnable Abstraction (
invoke()/stream()/ainvoke()) - โ Prompt Templates
- โ Chat Model Wrapper
- โ Output Parsers (String, JSON)
- โ
RunnableSequence (with
|operator, auto-flattening) - โ RunnableParallel
- โ RunnablePassthrough
- โ RunnableLambda
Memory
- โ Chat History / Memory Module
Execution Models
- โ Streaming (token-by-token, smart source/transformer detection)
- โ
Async Execution (
ainvoke, true concurrency viaasyncio.gather) - โ Callback System (pluggable hooks โ no hardcoded logging)
Agentic Capabilities
- โ Tool Calling (auto-generated schemas from Python type hints)
- โ Vector Stores (in-memory, hand-written cosine similarity)
- โ RAG (Retrieval-Augmented Generation)
- โ Agents (multi-step tool-calling reasoning loop)
Engineering Fundamentals
- โ Input Validation across every component
- โ Modular Package Design
- โ 36/36 tests passing (happy paths + validation + full-stack integration)
๐ Project Structure
EduChain/ โ โโโ educhain/ โ โโโ core/ โ โ โโโ runnable.py โ Runnable (base class) โ โ โโโ sequence.py โ RunnableSequence โ โ โโโ parallel.py โ RunnableParallel โ โ โโโ passthrough.py โ RunnablePassthrough โ โ โโโ lambda_runnable.py โ RunnableLambda โ โ โโโ history.py โ MessageHistory (raw store) โ โ โโโ callbacks.py โ CallbackHandler, PrintCallbackHandler โ โ โโโ tool.py โ Tool โ โ โโโ vectorstore.py โ InMemoryVectorStore โ โ โโโ rag.py โ RAGChain โ โ โโโ agent.py โ Agent โ โโโ prompts/ โ โ โโโ prompt.py โ PromptTemplate โ โโโ models/ โ โ โโโ llm.py โ ChatModel โ โโโ output_parsers/ โ โ โโโ parser.py โ StringOutputParser, JsonOutputParser โ โโโ memory/ โ โโโ chat_history.py โ ChatMessageHistory (chain wrapper) โ โโโ test_all_features.py โ 36 tests, full coverage โโโ demo_all_features.py โ core primitives, real usage โโโ demo_async.py โ async execution + speed comparison โโโ demo_callbacks.py โ callback system โโโ demo_tools.py โ tool calling โโโ demo_vectorstore.py โ semantic search โโโ demo_rag.py โ retrieval-augmented generation โโโ demo_agent.py โ full agentic reasoning loop โ โโโ ROADMAP.md โโโ README.md โโโ requirements.txt โโโ LICENSE
๐ Architecture
Runnable
โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโดโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโ
โผ โผ โผ โผ โผ โผ
PromptTemplate ChatModel OutputParser Passthrough Lambda Agent โ โ โผ โผ (tools bound) ChatModel + Tools โ + Callbacks โผ โ RunnableSequence โโโโโ RAGChain โโโโโ InMemoryVectorStore โ โผ RunnableParallel โ โผ Memory & Chat History
Every component follows the same core interface:
invoke(input) # synchronous
stream(input) # chunked/streaming
ainvoke(input) # async
Because every component behaves consistently, they compose into flexible pipelines โ from a simple 3-step chain to a full agent that searches its own knowledge base.
โก Installation
Clone the repository
git clone https://github.com/CodeWithDks/EduChain.git
Move into the project
cd EduChain
Create a virtual environment
python -m venv venv
Activate it
Windows
venv\Scripts\activate
Linux / macOS
source venv/bin/activate
Install dependencies
pip install -r requirements.txt
Create a .env file
OPENAI_API_KEY=your_api_key_here
๐ Quick Start
Basic Chain
from educhain.prompts.prompt import PromptTemplate
from educhain.models.llm import ChatModel
from educhain.output_parsers.parser import StringOutputParser
prompt = PromptTemplate(
template="Explain {topic} in simple words.",
input_variables=["topic"]
)
model = ChatModel()
parser = StringOutputParser()
chain = prompt | model | parser
response = chain.invoke({"topic": "Artificial Intelligence"})
print(response)
Agent with Tools + RAG
from educhain.models.llm import ChatModel
from educhain.core.tool import Tool
from educhain.core.agent import Agent
def get_weather(city: str) -> str:
"""Get the current weather for a specific named city."""
return f"It's sunny in {city}"
weather_tool = Tool(get_weather)
model = ChatModel(tools=[weather_tool])
agent = Agent(model=model)
answer = agent.invoke("What's the weather like in Delhi?")
print(answer)
See demo_agent.py for a full example combining Tool Calling, RAG, and Callbacks together.
๐ Execution Flow
Basic chain:
Dictionary โ PromptTemplate โ Formatted Prompt โ ChatModel โ AIMessage โ OutputParser โ Final Response
Agent loop:
Question โ ChatModel (with tools) โ tool call requested? โ yes โโโโโโโโโดโโโโโโโโ no โ โ run the tool final answer โ feed result back, repeat (up to max_iterations)
๐งฉ Core Components
Runnable
The base abstraction of EduChain. Every component inherits from Runnable and implements invoke(), with optional stream() and ainvoke() overrides.
PromptTemplate
Formats prompts by replacing template variables with user input.
ChatModel
Wraps any LangChain-compatible chat model. Supports plain string prompts and full message-list conversations (needed for multi-turn tool calling). Optionally accepts tools=[...] to enable tool calling.
OutputParser
Transforms raw model outputs into clean Python objects โ StringOutputParser and JsonOutputParser.
RunnableSequence
Executes multiple runnables sequentially via the | operator, with smart streaming support that correctly detects which step is the actual streaming source vs. a downstream transformer.
RunnableParallel
Executes multiple independent chains โ concurrently via threads (invoke) or asyncio.gather (ainvoke).
Tool
Wraps a plain Python function as something an LLM can call โ auto-generates the JSON schema from type hints and docstrings, no hand-written schema required.
InMemoryVectorStore
Stores text + embeddings, retrieves the most relevant chunks for a query using hand-written cosine similarity search.
RAGChain
Combines a vector store with an existing chain โ retrieves relevant context, injects it into the prompt, generates a grounded answer.
Agent
Wraps a tool-bound ChatModel in a reasoning loop โ decides which tool to call, executes it, feeds the result back, and repeats until it reaches a final answer (bounded by max_iterations as a safety limit).
CallbackHandler
Pluggable observability hooks (on_step_start, on_step_end, on_error, etc.) โ swap in custom logging, timing, or monitoring logic without touching core chain code.
๐ Examples & Demos
demo_all_features.py โ core primitives working together demo_async.py โ async chains + speed comparison demo_callbacks.py โ built-in + custom callback handlers demo_tools.py โ tool detection, execution, full round trip demo_vectorstore.py โ semantic search across mixed topics demo_rag.py โ retrieval-augmented generation, grounded answers demo_agent.py โ full agentic loop, including RAG-as-a-tool
Run any demo directly:
python demo_agent.py
๐งช Tests
Run the full test suite (36 tests โ happy paths, validation, and full-stack integration):
python test_all_features.py
๐ฏ Learning Objectives
EduChain was built to understand the engineering principles behind modern LLM frameworks โ not just use them.
Topics covered include:
- Object-Oriented Programming & Abstract Base Classes
- Operator Overloading & Method Chaining
- Software Architecture & Dependency Injection
- Sequential and Parallel Pipelines (threads + asyncio)
- Streaming (source vs. transformer detection)
- Observer Pattern (Callback System)
- Function Calling / Tool Use (JSON schema generation from type hints)
- Vector Embeddings & Cosine Similarity (implemented by hand)
- Retrieval-Augmented Generation
- Agentic Reasoning Loops & Safety Limits (
max_iterations)
๐ฃ Roadmap
โ v1.0 โ Complete
- Runnable, PromptTemplate, ChatModel, OutputParser
- RunnableSequence, RunnableParallel, RunnablePassthrough, RunnableLambda
- Chat History / Memory
- Streaming
- Async Support
- Callback System
- Tool Calling
- Vector Store
- RAG Pipeline
- Agents
๐ก Future Directions (not committed)
- Persistent storage for memory & vector store (swap in-memory for a real DB)
- Pluggable vector store backends (FAISS/Chroma) behind the same interface
- Multi-agent coordination
- Streaming support inside the Agent loop
- Batch processing (
.batch())
See ROADMAP.md for full build history and design decisions.
๐ Documentation
Additional documentation is available in the docs/ directory and ROADMAP.md.
๐ค Contributing
Contributions, suggestions, and improvements are welcome. If you find a bug or have an idea for improving EduChain, feel free to open an issue or submit a pull request.
๐ License
This project is licensed under the MIT License.
๐ Acknowledgements
EduChain is inspired by the architecture and design principles of LangChain. This project is an educational reimplementation created to better understand how modern LLM frameworks are designed. It is not affiliated with or endorsed by the LangChain project.
๐จโ๐ป Author
Deepak Kumar Singh GitHub
If you found this project helpful, consider giving it a โญ on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file educhain_dks-1.0.0.tar.gz.
File metadata
- Download URL: educhain_dks-1.0.0.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1434ce301f60f4476487056772ca57c894bf59bdb1c44899712a1d8f342ba863
|
|
| MD5 |
59d44afe27efcc23fd5f7066b4b3f919
|
|
| BLAKE2b-256 |
6b6a6ed6e7749e8c1e0f732d187c95b910c629cbe9e3484151b769fc9b5ff4f8
|
File details
Details for the file educhain_dks-1.0.0-py3-none-any.whl.
File metadata
- Download URL: educhain_dks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 28.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d00c0a3f97c04ba23cdb0498e9c3828dbc41a824c77bb6e31e81287b34f96dae
|
|
| MD5 |
2d6ee7ef7d37cd8377615fcbca298004
|
|
| BLAKE2b-256 |
5e9d04218fc7f13b0c06eff10597ee53e771161a394a70b0c8a3b01c3f9b2278
|