educhain-dks 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. educhain_dks-1.0.0/LICENSE +21 -0
  2. educhain_dks-1.0.0/PKG-INFO +407 -0
  3. educhain_dks-1.0.0/README.md +380 -0
  4. educhain_dks-1.0.0/educhain/__init__.py +35 -0
  5. educhain_dks-1.0.0/educhain/core/__init__.py +5 -0
  6. educhain_dks-1.0.0/educhain/core/agent.py +124 -0
  7. educhain_dks-1.0.0/educhain/core/callbacks.py +83 -0
  8. educhain_dks-1.0.0/educhain/core/history.py +55 -0
  9. educhain_dks-1.0.0/educhain/core/lambda_runnable.py +49 -0
  10. educhain_dks-1.0.0/educhain/core/parallel.py +98 -0
  11. educhain_dks-1.0.0/educhain/core/passthrough.py +34 -0
  12. educhain_dks-1.0.0/educhain/core/rag.py +103 -0
  13. educhain_dks-1.0.0/educhain/core/runnable.py +101 -0
  14. educhain_dks-1.0.0/educhain/core/sequence.py +170 -0
  15. educhain_dks-1.0.0/educhain/core/tool.py +133 -0
  16. educhain_dks-1.0.0/educhain/core/vectorstore.py +145 -0
  17. educhain_dks-1.0.0/educhain/memory/__init__.py +4 -0
  18. educhain_dks-1.0.0/educhain/memory/chat_history.py +82 -0
  19. educhain_dks-1.0.0/educhain/models/__init__.py +3 -0
  20. educhain_dks-1.0.0/educhain/models/llm.py +172 -0
  21. educhain_dks-1.0.0/educhain/output_parsers/__init__.py +3 -0
  22. educhain_dks-1.0.0/educhain/output_parsers/parser.py +105 -0
  23. educhain_dks-1.0.0/educhain/prompts/__init__.py +3 -0
  24. educhain_dks-1.0.0/educhain/prompts/prompt.py +70 -0
  25. educhain_dks-1.0.0/educhain_dks.egg-info/PKG-INFO +407 -0
  26. educhain_dks-1.0.0/educhain_dks.egg-info/SOURCES.txt +30 -0
  27. educhain_dks-1.0.0/educhain_dks.egg-info/dependency_links.txt +1 -0
  28. educhain_dks-1.0.0/educhain_dks.egg-info/requires.txt +3 -0
  29. educhain_dks-1.0.0/educhain_dks.egg-info/top_level.txt +1 -0
  30. educhain_dks-1.0.0/pyproject.toml +41 -0
  31. educhain_dks-1.0.0/setup.cfg +4 -0
  32. educhain_dks-1.0.0/tests/test_all_features.py +1024 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Deepak Kumar Singh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,407 @@
1
+ Metadata-Version: 2.4
2
+ Name: educhain-dks
3
+ Version: 1.0.0
4
+ Summary: A mini LangChain clone, built from scratch to understand how modern LLM frameworks work internally.
5
+ Author-email: Deepak Singh <deepakniu09@gmail.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/CodeWithDks/EduChain
8
+ Project-URL: Repository, https://github.com/CodeWithDks/EduChain
9
+ Project-URL: Documentation, https://github.com/CodeWithDks/EduChain/tree/main/docs
10
+ Project-URL: Issues, https://github.com/CodeWithDks/EduChain/issues
11
+ Keywords: llm,langchain,ai,agents,rag,education,tool-calling
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: langchain-core>=0.3.0
24
+ Requires-Dist: langchain-openai>=0.2.0
25
+ Requires-Dist: python-dotenv>=1.0.0
26
+ Dynamic: license-file
27
+
28
+ # ๐Ÿš€ EduChain
29
+ ![Python](https://img.shields.io/badge/Python-3.10+-blue.svg)
30
+ ![License](https://img.shields.io/badge/License-MIT-green.svg)
31
+ ![Status](https://img.shields.io/badge/Status-v1.0-success.svg)
32
+ ![Tests](https://img.shields.io/badge/Tests-36%2F36%20passing-brightgreen.svg)
33
+
34
+ > **An educational Python framework inspired by LangChain โ€” built from scratch to understand how modern LLM frameworks work internally.**
35
+
36
+ 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.
37
+
38
+ > **Note:** EduChain is an educational project. It is **not** intended to replace LangChain or be feature-compatible with it.
39
+
40
+ ---
41
+
42
+ ## โœจ Features
43
+
44
+ **Core Primitives**
45
+ - โœ… Runnable Abstraction (`invoke()` / `stream()` / `ainvoke()`)
46
+ - โœ… Prompt Templates
47
+ - โœ… Chat Model Wrapper
48
+ - โœ… Output Parsers (String, JSON)
49
+ - โœ… RunnableSequence (with `|` operator, auto-flattening)
50
+ - โœ… RunnableParallel
51
+ - โœ… RunnablePassthrough
52
+ - โœ… RunnableLambda
53
+
54
+ **Memory**
55
+ - โœ… Chat History / Memory Module
56
+
57
+ **Execution Models**
58
+ - โœ… Streaming (token-by-token, smart source/transformer detection)
59
+ - โœ… Async Execution (`ainvoke`, true concurrency via `asyncio.gather`)
60
+ - โœ… Callback System (pluggable hooks โ€” no hardcoded logging)
61
+
62
+ **Agentic Capabilities**
63
+ - โœ… Tool Calling (auto-generated schemas from Python type hints)
64
+ - โœ… Vector Stores (in-memory, hand-written cosine similarity)
65
+ - โœ… RAG (Retrieval-Augmented Generation)
66
+ - โœ… Agents (multi-step tool-calling reasoning loop)
67
+
68
+ **Engineering Fundamentals**
69
+ - โœ… Input Validation across every component
70
+ - โœ… Modular Package Design
71
+ - โœ… 36/36 tests passing (happy paths + validation + full-stack integration)
72
+
73
+ ---
74
+
75
+ # ๐Ÿ“‚ Project Structure
76
+
77
+ EduChain/
78
+ โ”‚
79
+ โ”œโ”€โ”€ educhain/
80
+ โ”‚ โ”œโ”€โ”€ core/
81
+ โ”‚ โ”‚ โ”œโ”€โ”€ runnable.py โ†’ Runnable (base class)
82
+ โ”‚ โ”‚ โ”œโ”€โ”€ sequence.py โ†’ RunnableSequence
83
+ โ”‚ โ”‚ โ”œโ”€โ”€ parallel.py โ†’ RunnableParallel
84
+ โ”‚ โ”‚ โ”œโ”€โ”€ passthrough.py โ†’ RunnablePassthrough
85
+ โ”‚ โ”‚ โ”œโ”€โ”€ lambda_runnable.py โ†’ RunnableLambda
86
+ โ”‚ โ”‚ โ”œโ”€โ”€ history.py โ†’ MessageHistory (raw store)
87
+ โ”‚ โ”‚ โ”œโ”€โ”€ callbacks.py โ†’ CallbackHandler, PrintCallbackHandler
88
+ โ”‚ โ”‚ โ”œโ”€โ”€ tool.py โ†’ Tool
89
+ โ”‚ โ”‚ โ”œโ”€โ”€ vectorstore.py โ†’ InMemoryVectorStore
90
+ โ”‚ โ”‚ โ”œโ”€โ”€ rag.py โ†’ RAGChain
91
+ โ”‚ โ”‚ โ””โ”€โ”€ agent.py โ†’ Agent
92
+ โ”‚ โ”œโ”€โ”€ prompts/
93
+ โ”‚ โ”‚ โ””โ”€โ”€ prompt.py โ†’ PromptTemplate
94
+ โ”‚ โ”œโ”€โ”€ models/
95
+ โ”‚ โ”‚ โ””โ”€โ”€ llm.py โ†’ ChatModel
96
+ โ”‚ โ”œโ”€โ”€ output_parsers/
97
+ โ”‚ โ”‚ โ””โ”€โ”€ parser.py โ†’ StringOutputParser, JsonOutputParser
98
+ โ”‚ โ””โ”€โ”€ memory/
99
+ โ”‚ โ””โ”€โ”€ chat_history.py โ†’ ChatMessageHistory (chain wrapper)
100
+ โ”‚
101
+ โ”œโ”€โ”€ test_all_features.py โ†’ 36 tests, full coverage
102
+ โ”œโ”€โ”€ demo_all_features.py โ†’ core primitives, real usage
103
+ โ”œโ”€โ”€ demo_async.py โ†’ async execution + speed comparison
104
+ โ”œโ”€โ”€ demo_callbacks.py โ†’ callback system
105
+ โ”œโ”€โ”€ demo_tools.py โ†’ tool calling
106
+ โ”œโ”€โ”€ demo_vectorstore.py โ†’ semantic search
107
+ โ”œโ”€โ”€ demo_rag.py โ†’ retrieval-augmented generation
108
+ โ”œโ”€โ”€ demo_agent.py โ†’ full agentic reasoning loop
109
+ โ”‚
110
+ โ”œโ”€โ”€ ROADMAP.md
111
+ โ”œโ”€โ”€ README.md
112
+ โ”œโ”€โ”€ requirements.txt
113
+ โ””โ”€โ”€ LICENSE
114
+
115
+
116
+ ---
117
+
118
+ # ๐Ÿ› Architecture
119
+ Runnable
120
+ โ”‚
121
+ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
122
+ โ–ผ โ–ผ โ–ผ โ–ผ โ–ผ โ–ผ
123
+
124
+ PromptTemplate ChatModel OutputParser Passthrough Lambda Agent
125
+ โ”‚ โ”‚
126
+ โ–ผ โ–ผ
127
+ (tools bound) ChatModel + Tools
128
+ โ”‚ + Callbacks
129
+ โ–ผ โ”‚
130
+ RunnableSequence โ—„โ”€โ”€โ”€โ”€ RAGChain โ—„โ”€โ”€โ”€โ”€ InMemoryVectorStore
131
+ โ”‚
132
+ โ–ผ
133
+ RunnableParallel
134
+ โ”‚
135
+ โ–ผ
136
+ Memory & Chat History
137
+
138
+
139
+ Every component follows the same core interface:
140
+
141
+ ```python
142
+ invoke(input) # synchronous
143
+ stream(input) # chunked/streaming
144
+ ainvoke(input) # async
145
+ ```
146
+
147
+ 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.
148
+
149
+ ---
150
+
151
+ # โšก Installation
152
+
153
+ Clone the repository
154
+
155
+ ```bash
156
+ git clone https://github.com/CodeWithDks/EduChain.git
157
+ ```
158
+
159
+ Move into the project
160
+
161
+ ```bash
162
+ cd EduChain
163
+ ```
164
+
165
+ Create a virtual environment
166
+
167
+ ```bash
168
+ python -m venv venv
169
+ ```
170
+
171
+ Activate it
172
+
173
+ ### Windows
174
+
175
+ ```bash
176
+ venv\Scripts\activate
177
+ ```
178
+
179
+ ### Linux / macOS
180
+
181
+ ```bash
182
+ source venv/bin/activate
183
+ ```
184
+
185
+ Install dependencies
186
+
187
+ ```bash
188
+ pip install -r requirements.txt
189
+ ```
190
+
191
+ Create a `.env` file
192
+
193
+ ```env
194
+ OPENAI_API_KEY=your_api_key_here
195
+ ```
196
+
197
+ ---
198
+
199
+ # ๐Ÿš€ Quick Start
200
+
201
+ ### Basic Chain
202
+
203
+ ```python
204
+ from educhain.prompts.prompt import PromptTemplate
205
+ from educhain.models.llm import ChatModel
206
+ from educhain.output_parsers.parser import StringOutputParser
207
+
208
+ prompt = PromptTemplate(
209
+ template="Explain {topic} in simple words.",
210
+ input_variables=["topic"]
211
+ )
212
+
213
+ model = ChatModel()
214
+ parser = StringOutputParser()
215
+
216
+ chain = prompt | model | parser
217
+
218
+ response = chain.invoke({"topic": "Artificial Intelligence"})
219
+ print(response)
220
+ ```
221
+
222
+ ### Agent with Tools + RAG
223
+
224
+ ```python
225
+ from educhain.models.llm import ChatModel
226
+ from educhain.core.tool import Tool
227
+ from educhain.core.agent import Agent
228
+
229
+ def get_weather(city: str) -> str:
230
+ """Get the current weather for a specific named city."""
231
+ return f"It's sunny in {city}"
232
+
233
+ weather_tool = Tool(get_weather)
234
+ model = ChatModel(tools=[weather_tool])
235
+ agent = Agent(model=model)
236
+
237
+ answer = agent.invoke("What's the weather like in Delhi?")
238
+ print(answer)
239
+ ```
240
+
241
+ See `demo_agent.py` for a full example combining Tool Calling, RAG, and Callbacks together.
242
+
243
+ ---
244
+
245
+ # ๐Ÿ”„ Execution Flow
246
+
247
+ **Basic chain:**
248
+
249
+ Dictionary โ†’ PromptTemplate โ†’ Formatted Prompt โ†’ ChatModel โ†’ AIMessage โ†’ OutputParser โ†’ Final Response
250
+
251
+
252
+ **Agent loop:**
253
+
254
+ Question โ†’ ChatModel (with tools) โ†’ tool call requested?
255
+ โ”‚
256
+ yes โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ no
257
+ โ”‚ โ”‚
258
+ run the tool final answer
259
+ โ”‚
260
+ feed result back, repeat
261
+ (up to max_iterations)
262
+
263
+
264
+ ---
265
+
266
+ # ๐Ÿงฉ Core Components
267
+
268
+ ## Runnable
269
+ The base abstraction of EduChain. Every component inherits from `Runnable` and implements `invoke()`, with optional `stream()` and `ainvoke()` overrides.
270
+
271
+ ## PromptTemplate
272
+ Formats prompts by replacing template variables with user input.
273
+
274
+ ## ChatModel
275
+ 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.
276
+
277
+ ## OutputParser
278
+ Transforms raw model outputs into clean Python objects โ€” `StringOutputParser` and `JsonOutputParser`.
279
+
280
+ ## RunnableSequence
281
+ 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.
282
+
283
+ ## RunnableParallel
284
+ Executes multiple independent chains โ€” concurrently via threads (`invoke`) or `asyncio.gather` (`ainvoke`).
285
+
286
+ ## Tool
287
+ 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.
288
+
289
+ ## InMemoryVectorStore
290
+ Stores text + embeddings, retrieves the most relevant chunks for a query using hand-written cosine similarity search.
291
+
292
+ ## RAGChain
293
+ Combines a vector store with an existing chain โ€” retrieves relevant context, injects it into the prompt, generates a grounded answer.
294
+
295
+ ## Agent
296
+ 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).
297
+
298
+ ## CallbackHandler
299
+ 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.
300
+
301
+ ---
302
+
303
+ # ๐Ÿ“š Examples & Demos
304
+
305
+ demo_all_features.py โ†’ core primitives working together
306
+ demo_async.py โ†’ async chains + speed comparison
307
+ demo_callbacks.py โ†’ built-in + custom callback handlers
308
+ demo_tools.py โ†’ tool detection, execution, full round trip
309
+ demo_vectorstore.py โ†’ semantic search across mixed topics
310
+ demo_rag.py โ†’ retrieval-augmented generation, grounded answers
311
+ demo_agent.py โ†’ full agentic loop, including RAG-as-a-tool
312
+
313
+
314
+ Run any demo directly:
315
+
316
+ ```bash
317
+ python demo_agent.py
318
+ ```
319
+
320
+ ---
321
+
322
+ # ๐Ÿงช Tests
323
+
324
+ Run the full test suite (36 tests โ€” happy paths, validation, and full-stack integration):
325
+
326
+ ```bash
327
+ python test_all_features.py
328
+ ```
329
+
330
+ ---
331
+
332
+ # ๐ŸŽฏ Learning Objectives
333
+
334
+ EduChain was built to understand the engineering principles behind modern LLM frameworks โ€” not just use them.
335
+
336
+ Topics covered include:
337
+
338
+ - Object-Oriented Programming & Abstract Base Classes
339
+ - Operator Overloading & Method Chaining
340
+ - Software Architecture & Dependency Injection
341
+ - Sequential and Parallel Pipelines (threads + asyncio)
342
+ - Streaming (source vs. transformer detection)
343
+ - Observer Pattern (Callback System)
344
+ - Function Calling / Tool Use (JSON schema generation from type hints)
345
+ - Vector Embeddings & Cosine Similarity (implemented by hand)
346
+ - Retrieval-Augmented Generation
347
+ - Agentic Reasoning Loops & Safety Limits (`max_iterations`)
348
+
349
+ ---
350
+
351
+ ## ๐Ÿ›ฃ Roadmap
352
+
353
+ ### โœ… v1.0 โ€” Complete
354
+
355
+ - [x] Runnable, PromptTemplate, ChatModel, OutputParser
356
+ - [x] RunnableSequence, RunnableParallel, RunnablePassthrough, RunnableLambda
357
+ - [x] Chat History / Memory
358
+ - [x] Streaming
359
+ - [x] Async Support
360
+ - [x] Callback System
361
+ - [x] Tool Calling
362
+ - [x] Vector Store
363
+ - [x] RAG Pipeline
364
+ - [x] Agents
365
+
366
+ ### ๐Ÿ’ก Future Directions (not committed)
367
+
368
+ - [ ] Persistent storage for memory & vector store (swap in-memory for a real DB)
369
+ - [ ] Pluggable vector store backends (FAISS/Chroma) behind the same interface
370
+ - [ ] Multi-agent coordination
371
+ - [ ] Streaming support inside the Agent loop
372
+ - [ ] Batch processing (`.batch()`)
373
+
374
+ See `ROADMAP.md` for full build history and design decisions.
375
+
376
+ ---
377
+
378
+ # ๐Ÿ“– Documentation
379
+
380
+ Additional documentation is available in the `docs/` directory and `ROADMAP.md`.
381
+
382
+ ---
383
+
384
+ # ๐Ÿค Contributing
385
+
386
+ 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.
387
+
388
+ ---
389
+
390
+ # ๐Ÿ“„ License
391
+
392
+ This project is licensed under the MIT License.
393
+
394
+ ---
395
+
396
+ # ๐Ÿ™ Acknowledgements
397
+
398
+ 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.
399
+
400
+ ---
401
+
402
+ # ๐Ÿ‘จโ€๐Ÿ’ป Author
403
+
404
+ **Deepak Kumar Singh**
405
+ [GitHub](https://github.com/CodeWithDks)
406
+
407
+ If you found this project helpful, consider giving it a โญ on GitHub.