Skip to content

Latest commit

 

History

History
200 lines (152 loc) · 4.9 KB

File metadata and controls

200 lines (152 loc) · 4.9 KB

LLM Function Calling Patterns - Python

Production-ready Python implementation of LLM function calling patterns (ReAct, ReWOO).

Requirements

  • Python 3.12+
  • uv (recommended) or pip

Quick Start

Using uv (Recommended)

# Install uv if you haven't
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create virtual environment and install dependencies
uv venv
source .venv/bin/activate  # or `.venv\Scripts\activate` on Windows
uv pip install -e ".[dev]"

# Run tests
uv run pytest

# Run ReAct CLI
uv run react-cli

# Run ReWOO CLI
uv run rewoo-cli

Using pip

# Create virtual environment
python -m venv .venv
source .venv/bin/activate

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

# Run tests
pytest

# Run CLIs
react-cli
rewoo-cli

Configuration

The library uses environment variables for configuration. Copy .env.example from the root directory:

cp ../.env.example .env

Required Environment Variables

Variable Description
LLM_PROVIDER Provider name (openai, anthropic, groq, etc.)
LLM_MODEL Model identifier
OPENAI_API_KEY OpenAI API key (if using OpenAI)
ANTHROPIC_API_KEY Anthropic API key (if using Anthropic)

Optional Environment Variables

Variable Default Description
LLM_BASE_URL Provider default Custom API endpoint
LLM_TIMEOUT_MS 30000 Request timeout
LLM_MAX_RETRIES 3 Retry attempts
TEMPERATURE 0.7 Sampling temperature
MAX_TOKENS Provider default Max completion tokens
TAVILY_API_KEY - For web search functionality

Usage

ReAct Pattern

import asyncio
from llm_patterns import ReActAgent, load_react_config
from llm_patterns.react.tools import CalculatorTool, ThoughtTool

async def main():
    config = load_react_config()
    resolved = config.ai.resolve()

    agent = ReActAgent(
        config=resolved,
        tools=[CalculatorTool(), ThoughtTool()],
        max_iterations=10,
    )

    answer = await agent.answer("What is 15% of 250?")
    print(answer)

asyncio.run(main())

ReWOO Pattern

import asyncio
from llm_patterns import ReWOO, load_rewoo_config
from llm_patterns.rewoo.tools import CalculatorTool, LLMTool

async def main():
    config = load_rewoo_config()
    resolved = config.planner_ai.resolve()

    ai_config = {
        "model": resolved.model,
        "api_key": resolved.api_key,
        "base_url": resolved.base_url,
    }

    rewoo = ReWOO(
        config=ai_config,
        tools=[CalculatorTool(), LLMTool()],
    )

    state = await rewoo.process("Calculate the compound interest on $1000 at 5% for 3 years")
    print(state.result)

asyncio.run(main())

Supported Providers

Provider Models Function Calling
OpenAI GPT-4o, GPT-4o-mini, GPT-4-Turbo Yes
Anthropic Claude 3.5 Sonnet, Claude 3 Opus Yes
Groq Llama 3.3 70B, Mixtral 8x7B Yes
Together AI Llama 3.1, Qwen 2.5 Yes
DeepSeek DeepSeek Chat/Coder Yes
Cerebras Llama 3.1 70B/8B Yes
Ollama Local models Yes

Development

# Install dev dependencies
uv pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=src/llm_patterns --cov-report=html

# Type checking
mypy src/llm_patterns

# Linting
ruff check src/llm_patterns
ruff format src/llm_patterns

Project Structure

python/
├── pyproject.toml          # Project configuration
├── README.md               # This file
└── src/
    └── llm_patterns/
        ├── __init__.py     # Main exports
        ├── config/         # Configuration system
        │   ├── providers.py    # LLM provider definitions
        │   ├── schema.py       # Pydantic schemas
        │   └── loader.py       # Config loading
        ├── core/           # Core functionality
        │   ├── ai/         # AI generation
        │   ├── types/      # Type definitions
        │   ├── services/   # Business logic
        │   └── db/         # Database integration
        ├── react/          # ReAct pattern
        │   ├── agent.py    # Main agent
        │   ├── types.py    # Type definitions
        │   ├── errors.py   # Error classes
        │   ├── cli.py      # CLI interface
        │   └── tools/      # Built-in tools
        └── rewoo/          # ReWOO pattern
            ├── rewoo.py    # Main orchestrator
            ├── planner.py  # Plan generation
            ├── solver.py   # Solution synthesis
            ├── worker.py   # Step execution
            ├── events.py   # Event system
            ├── cli.py      # CLI interface
            └── tools/      # Built-in tools

License

MIT