Skip to content

mjdevaccount/llm-extraction-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LLM Design Patterns Repository - Foundation

A modular, learning-first repository for implementing and understanding five core LLM agent design patterns. Built with Python, LangGraph, and MCP integration.

🎯 Implementation Status (December 2025)

Pattern Status Learning Value Production Ready
IEV (Intelligence-Extraction-Validation) βœ… COMPLETE ⭐⭐⭐ Best starting point ⭐⭐⭐ High
Evaluator-Optimizer (Draft-Critique-Refine) 🚧 Basic ⭐⭐⭐ Quality feedback loop ⭐⭐ Medium
Orchestrator (Task Delegation) 🚧 Stub ⭐⭐⭐ Complex workflows ⭐ Foundation
Agentic RAG (Retrieval-Verification) 🚧 Stub ⭐⭐ Research & fact-checking ⭐ Foundation
System 2 (Thinking-Before-Acting) 🚧 Stub ⭐⭐⭐ Complex reasoning ⭐ Foundation

Legend: βœ… = Fully implemented | 🚧 = Basic/stub implementation | ⭐ = Learning/production score

Repository Structure

aistack-patterns/
β”‚
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ pyproject.toml              # Project metadata & dependencies
β”œβ”€β”€ .env.example                # Configuration template
β”‚
β”œβ”€β”€ src/
β”‚   └── patterns/
β”‚       β”‚
β”‚       β”œβ”€β”€ __init__.py         # Package exports
β”‚       β”‚
β”‚       β”œβ”€β”€ core/               # βœ… SHARED INFRASTRUCTURE
β”‚       β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”œβ”€β”€ llm_client.py   # LLM wrapper (OpenAI, Anthropic, local Ollama)
β”‚       β”‚   β”œβ”€β”€ mcp_tools.py    # Your aistack-mcp connector
β”‚       β”‚   β”œβ”€β”€ memory.py       # Vector DB + context management
β”‚       β”‚   β”œβ”€β”€ types.py        # Shared type definitions
β”‚       β”‚   └── logger.py       # Logging utilities
β”‚       β”‚
β”‚       β”œβ”€β”€ patterns/           # βœ… THE 5 PATTERN IMPLEMENTATIONS
β”‚       β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”‚
β”‚       β”‚   β”œβ”€β”€ iev/            # Pattern 1: Intelligence-Extraction-Validation βœ…
β”‚       β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”‚   β”œβ”€β”€ graph.py    # State machine definition
β”‚       β”‚   β”‚   β”œβ”€β”€ prompts.py  # Verification-specific prompts
β”‚       β”‚   β”‚   β”œβ”€β”€ example.py  # Minimal working example
β”‚       β”‚   β”‚   β”œβ”€β”€ abstractions.py
β”‚       β”‚   β”‚   β”œβ”€β”€ nodes/      # Individual node implementations
β”‚       β”‚   β”‚   β”œβ”€β”€ strategies/ # JSON repair & validation strategies
β”‚       β”‚   β”‚   └── workflows/  # SOLID workflow orchestration
β”‚       β”‚   β”‚
β”‚       β”‚   β”œβ”€β”€ evaluator_optimizer/  # Pattern 2: Draft-Critique-Refine 🚧
β”‚       β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”‚   β”œβ”€β”€ graph.py
β”‚       β”‚   β”‚   β”œβ”€β”€ grader.py   # Scoring & feedback logic
β”‚       β”‚   β”‚   β”œβ”€β”€ prompts.py
β”‚       β”‚   β”‚   └── example.py
β”‚       β”‚   β”‚
β”‚       β”‚   β”œβ”€β”€ orchestrator/        # Pattern 3: Orchestrator-Workers 🚧
β”‚       β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”‚   β”œβ”€β”€ orchestrator.py  # Router logic
β”‚       β”‚   β”‚   β”œβ”€β”€ workers.py       # Worker definitions
β”‚       β”‚   β”‚   β”œβ”€β”€ prompts.py
β”‚       β”‚   β”‚   └── example.py
β”‚       β”‚   β”‚
β”‚       β”‚   β”œβ”€β”€ agentic_rag/         # Pattern 4: Iterative Retrieval-Verification 🚧
β”‚       β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”‚   β”œβ”€β”€ retriever.py     # Search logic
β”‚       β”‚   β”‚   β”œβ”€β”€ verifier.py      # Sufficiency checker
β”‚       β”‚   β”‚   β”œβ”€β”€ prompts.py
β”‚       β”‚   β”‚   └── example.py
β”‚       β”‚   β”‚
β”‚       β”‚   └── system2/             # Pattern 5: Thinking-Before-Acting 🚧
β”‚       β”‚       β”œβ”€β”€ __init__.py
β”‚       β”‚       β”œβ”€β”€ flow.py          # <thought> generation pipeline
β”‚       β”‚       β”œβ”€β”€ parser.py        # Extract reasoning
β”‚       β”‚       β”œβ”€β”€ prompts.py
β”‚       β”‚       └── example.py
β”‚       β”‚
β”‚       └── utils/             # βœ… LEARNING & DEBUGGING
β”‚           β”œβ”€β”€ __init__.py
β”‚           β”œβ”€β”€ test_helpers.py     # Mock LLMs for testing
β”‚           β”œβ”€β”€ visualizer.py       # Graph state visualization
β”‚           └── prompt_tester.py    # Quick prompt iteration
β”‚
β”œβ”€β”€ tests/                      # Unit tests (one per pattern)
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_iev.py
β”‚   β”œβ”€β”€ test_evaluator_opt.py
β”‚   β”œβ”€β”€ test_orchestrator.py
β”‚   β”œβ”€β”€ test_agentic_rag.py
β”‚   └── test_system2.py
β”‚
β”œβ”€β”€ docs/                       # Learning materials
β”‚   β”œβ”€β”€ PATTERNS_OVERVIEW.md    # Detailed pattern explanations
β”‚   β”œβ”€β”€ GETTING_STARTED.md      # Step-by-step setup
β”‚   β”œβ”€β”€ ARCHITECTURE.md         # Design decisions
β”‚   └── examples/               # Real-world use cases
β”‚
β”œβ”€β”€ examples/                   # Runnable demonstrations
β”‚   β”œβ”€β”€ simple_iev.py           # "Delete this file safely"
β”‚   β”œβ”€β”€ code_review.py          # Evaluator-Optimizer for code
β”‚   β”œβ”€β”€ research_project.py     # Orchestrator for multi-step project
β”‚   β”œβ”€β”€ fact_checker.py         # Agentic RAG example
β”‚   └── math_problem.py         # System 2 for complex logic
β”‚
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .env.example                # Copy to .env for local config
└── Makefile                    # Common commands (run, test, docs)

Core Philosophy: "Learn One Pattern at a Time"

Each pattern is independent and minimal. You can understand Pattern 1 (IEV) completely before touching Pattern 2.

Getting Started (3 Steps)

Step 1: Choose Your First Pattern

Start with IEV (Intelligence-Extraction-Validation) because:

βœ… Simplest mental model: Explore β†’ Verify β†’ Act

βœ… Direct connection to safety (most intuitive)

βœ… Only 3 nodes in the graph

βœ… Most complete implementation

Step 2: Run the Minimal Example

# Install dependencies
pip install -e ".[openai]"

# Copy environment template
cp .env.example .env
# Edit .env with your OPENAI_API_KEY

# Run the IEV example
python -m patterns.patterns.iev.example

This executes a simple scenario like: "Extract deal information from text and verify it."

Step 3: Modify & Learn

Open src/patterns/patterns/iev/example.py and:

  • Change the verification prompt
  • Add a new tool call
  • See how the graph behaves
  • Experiment with different validation strategies

Pattern Quick Reference

Pattern File Purpose Complexity When to Use Status
IEV patterns/iev/ Safety & Precision ⭐ Easy High-stakes actions βœ… Ready
Evaluator-Optimizer patterns/evaluator_optimizer/ Quality Control ⭐⭐ Medium Content refinement 🚧 Basic
Orchestrator patterns/orchestrator/ Complex Tasks ⭐⭐⭐ Medium Multi-step projects 🚧 Stub
Agentic RAG patterns/agentic_rag/ Research & Lookup ⭐⭐ Medium Fact-heavy queries 🚧 Stub
System 2 patterns/system2/ Hard Logic ⭐⭐⭐ Medium Math/reasoning 🚧 Stub

Core Module Descriptions

core/llm_client.py

Unified interface to swap LLM providers:

from patterns.core import create_llm_client

# Use OpenAI
llm = create_llm_client(provider="openai", model="gpt-4")

# Or local Ollama
llm = create_llm_client(provider="ollama", model="mistral:7b")

# Or Anthropic
llm = create_llm_client(provider="anthropic", model="claude-3-5-sonnet-20241022")

# Call the same way regardless
response = llm.generate(system="You are helpful", user="Hello")

core/mcp_tools.py

Your aistack-mcp integration point:

from patterns.core import get_mcp_toolkit

tools = get_mcp_toolkit()  # Returns your aistack-mcp client

# Use in any pattern
result = tools.call("file_read", path="/path/to/file")

core/memory.py

Short-term context management:

from patterns.core import ConversationMemory

memory = ConversationMemory(max_history=10)
memory.add_message(role="user", content="...")
memory.add_message(role="assistant", content="...")

# Used by any pattern to maintain state
context = memory.get_context()

Dependency Stack

[project]
dependencies = [
    "pydantic>=2.0.0",
    "python-dotenv>=1.0.0",
    "httpx>=0.24.0",
]

[project.optional-dependencies]
openai = ["openai>=1.3.0"]
anthropic = ["anthropic>=0.25.0"]
ollama = ["ollama>=0.1.0"]
langgraph = ["langgraph>=0.2.0", "langchain>=0.1.0"]
dev = ["pytest>=7.4.0", "black>=23.0.0", "ruff>=0.1.0"]

Learning Path (Recommended)

Week 1: Foundation (IEV Mastery)

  • Read docs/PATTERNS_OVERVIEW.md
  • Run python -m patterns.patterns.iev.example
  • Modify the verification prompt
  • Add your own validation rule
  • Write a test in tests/test_iev.py

Week 2: IEV in Production

  • Build a custom scenario (e.g., "Verify before trading")
  • Understand state transitions and error handling
  • Experiment with different LLM providers
  • Learn when to use different validation strategies

Week 3: Layer Evaluator-Optimizer

  • Run examples/code_review.py
  • Understand the critique loop
  • Experiment with grading criteria
  • Combine with IEV for enhanced workflows

Week 4+: Advanced Patterns

  • Use Orchestrator to delegate tasks
  • Integrate Agentic RAG for research
  • Implement System 2 for logic puzzles
  • Build hybrid patterns (e.g., IEV β†’ Evaluator-Optimizer β†’ Action)

Running Locally

Prerequisites

  • Python 3.10+
  • Your aistack-mcp server running (optional, for MCP tests)

Setup

# Clone the repo
git clone https://github.com/mjdevaccount/llm-extraction-patterns.git
cd llm-extraction-patterns

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode
pip install -e ".[openai]"

# Copy environment template
cp .env.example .env

# Edit .env with your API keys (OpenAI, Anthropic, etc.)
# OPENAI_API_KEY=sk-...
# ANTHROPIC_API_KEY=sk-ant-...

# Run the first example
python -m patterns.patterns.iev.example

Run All Tests

pytest tests/ -v

Run with Different LLM Providers

# Using Anthropic Claude
PROVIDER=anthropic python -m patterns.patterns.iev.example

# Using local Ollama
PROVIDER=ollama MODEL=mistral:7b python -m patterns.patterns.iev.example

# Using OpenAI (default)
PROVIDER=openai python -m patterns.patterns.iev.example

What's NOT in This Repo

❌ Pre-built production agents (this is for learning, not deployment)

❌ Complex multi-repo orchestration (keep it simple)

❌ Web UI or API (focus on patterns, not plumbing)

❌ Financial-specific logic (patterns are domain-agnostic)

Next Steps

  1. Clone & setup this repo (5 min)
  2. Run the IEV example (5 min)
  3. Read the IEV explanation in docs/PATTERNS_OVERVIEW.md (20 min)
  4. Modify the prompt in patterns/iev/prompts.py (10 min)
  5. Write a test for your scenario (15 min)
  6. Move to Evaluator-Optimizer when ready

Once you've mastered IEV, the others follow naturallyβ€”they're all variations on the same feedback loop.

Contributing & Extending

To add your own pattern or example:

  1. Create a new folder in src/patterns/patterns/{pattern_name}/
  2. Include: __init__.py, graph.py, example.py, prompts.py
  3. Add a test in tests/test_{pattern_name}.py
  4. Update docs/PATTERNS_OVERVIEW.md with an explanation
  5. Document the learning path

License

MIT

Questions & Support

  • GitHub Issues: For bugs and feature requests
  • Documentation: Check docs/ for detailed explanations

Start with IEV. Master it. Then layer on the others.

Last updated: December 2025

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages