Skip to content

monks1975/function-calling-llm-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM Function Calling Patterns

Production-ready implementations of advanced LLM function calling patterns in TypeScript and Python. This library provides battle-tested implementations of ReAct (Reasoning and Acting) and ReWOO (Reasoning Without Observation) patterns for building sophisticated AI agents.

Features

  • Dual Language Support: Full implementations in both TypeScript (Node.js) and Python
  • Multiple LLM Providers: OpenAI, Anthropic, Groq, Together AI, DeepSeek, Cerebras, Ollama
  • Shared Configuration: Common environment variables work across both implementations
  • Modern Stack:
    • Node.js 22+, TypeScript 5, Vite, ESM modules
    • Python 3.12+, Pydantic, uv package manager
  • Production Ready: Error handling, retries, content moderation, logging

Project Structure

.
├── .env.example          # Shared environment configuration
├── README.md             # This file
├── node/                 # TypeScript/Node.js implementation
│   ├── package.json
│   ├── README.md
│   └── src/
└── python/               # Python implementation
    ├── pyproject.toml
    ├── README.md
    └── src/

Quick Start

Node.js (TypeScript)

cd node
npm install
cp ../.env.example .env
# Edit .env with your API keys
npm run react-cli    # Run ReAct CLI
npm run rewoo-cli    # Run ReWOO CLI

Python

cd python
# Using uv (recommended)
uv venv && source .venv/bin/activate
uv pip install -e ".[dev]"
cp ../.env.example .env
# Edit .env with your API keys
uv run react-cli    # Run ReAct CLI
uv run rewoo-cli    # Run ReWOO CLI

Shared Configuration

Both implementations use the same environment variables. Copy .env.example to .env in either the node/ or python/ directory.

Required Environment Variables

Variable Description Example
LLM_PROVIDER Provider name openai, anthropic, groq
LLM_MODEL Model identifier gpt-4o, claude-3-5-sonnet-20241022
OPENAI_API_KEY OpenAI API key sk-...
ANTHROPIC_API_KEY Anthropic API key sk-ant-...
GROQ_API_KEY Groq API key gsk_...

Optional Environment Variables

Variable Default Description
LLM_BASE_URL Provider default Custom API endpoint
LLM_TIMEOUT_MS 30000 Request timeout in milliseconds
LLM_MAX_RETRIES 3 Maximum retry attempts
TEMPERATURE 0.7 Sampling temperature (0-2)
MAX_TOKENS Provider default Maximum completion tokens
TAVILY_API_KEY - For web search functionality

External Services

Variable Description
TAVILY_API_KEY Web search API key
DOJO_API_KEY Document library API key
DOJO_API_BASE_URL Document library base URL
DOJO_API_LIBRARY_UUID Document library UUID
DATABASE_URL PostgreSQL connection string
POSTGRES_HOST PostgreSQL host
POSTGRES_PORT PostgreSQL port
POSTGRES_DB PostgreSQL database name
POSTGRES_USER PostgreSQL username
POSTGRES_PASSWORD PostgreSQL password

Architecture

ReAct Pattern

The ReAct (Reasoning and Acting) pattern interleaves reasoning traces with actions:

User Question → Thought → Action → Observation → Thought → ... → Final Answer

Key Components:

  • Agent orchestrates the reasoning loop
  • Tools execute actions and return observations
  • Message handler manages conversation history
  • Iteration control prevents infinite loops

ReWOO Pattern

The ReWOO (Reasoning Without Observation) pattern separates planning from execution:

Task → Plan (all steps) → Execute Steps → Synthesize Solution

Key Components:

  • Planner creates execution plans with evidence variables (#E1, #E2, etc.)
  • Worker executes each step using available tools
  • Solver synthesizes final solution from collected evidence
  • Event bus manages reactive event flow

Supported Providers

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

Usage Examples

Node.js (TypeScript)

import { ReActAgent, loadReActConfig } from 'llm-function-calling-patterns';

const config = loadReActConfig();
const agent = new ReActAgent(config.ai, config.tools, config.maxIterations);

const answer = await agent.answer('What is 15% of 250?');
console.log(answer);

Python

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

async def main():
    config = load_react_config()
    agent = ReActAgent(
        config=config.ai.resolve(),
        tools=[CalculatorTool()],
    )
    answer = await agent.answer("What is 15% of 250?")
    print(answer)

asyncio.run(main())

Development

Node.js

cd node
npm install
npm run dev           # Development server
npm test              # Run tests
npm run test:coverage # Coverage report
npm run build         # Production build

Python

cd python
uv pip install -e ".[dev]"
pytest                # Run tests
pytest --cov          # Coverage report
ruff check .          # Linting
mypy src              # Type checking

Documentation

References

License

MIT License - see LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •