Skip to content

HrxuAlbert/LLM-API-demos

Repository files navigation

LLM API Demos

This repository contains lightweight demos for structured LLM API usage, agent-oriented workflows, and research prototyping.

The goal of this repository is to collect small, reusable examples for experimenting with modern LLM APIs, including structured outputs, tool/function calling, prompt-based extraction, and early-stage agent workflow design.

Research Context

This repository supports my broader research on trustworthy multi-agent AI systems and LLM-agent collaboration. In particular, these demos provide practical building blocks for:

  • structured proposal generation;
  • evidence-aware LLM outputs;
  • agent response formatting;
  • lightweight evaluation pipelines;
  • API-based research prototyping.

It is not intended to be a full framework. Instead, it serves as a compact experimental space for testing API behaviors and implementation patterns that may later be integrated into larger research systems.

Scope

Current and future examples may include:

  • structured JSON output generation;
  • function/tool calling demos;
  • multi-agent interaction prototypes;
  • claim verification or evidence-grounded response examples;
  • prompt templates for research experiments;
  • basic visualization or analysis utilities.

πŸ“‹ Table of Contents


🎯 Overview

This project demonstrates three core capabilities of modern LLM APIs:

  1. Structured Extraction: Parse unstructured text into validated JSON schemas
  2. Function Calling: Enable LLMs to interact with external tools and APIs
  3. Multi-Agent Systems: Use LLMs for high-level coordination in MARL environments

Each demo is self-contained, well-documented, and includes practical examples.


πŸš€ Demos

1. Structured Data Extraction

Directory: 01_structured_extract/

Extract structured metadata from academic papers using three major LLM providers:

  • OpenAI GPT-4: Strict JSON schema enforcement
  • Anthropic Claude: Tool-based extraction
  • Google Gemini: JSON mode extraction

Key Features:

  • JSON Schema validation with jsonschema
  • API rate limiting handling
  • Unified schema across providers
  • Batch processing support

Example:

cd 01_structured_extract
python openai_demo.py samples/paper.txt

Output: Validated JSON with title, authors, venue, year, keywords, etc.

πŸ“– Detailed Documentation


2. Function Calling & Tool Use

Directory: 02_tool_calling/

An AI day planner that demonstrates LLM function calling with real-world APIs:

  • Weather API: Fetch forecasts from Open-Meteo
  • Calendar API: Find free slots and book meetings
  • Routing API: Estimate travel time with OSRM

Key Features:

  • OpenAI function calling with multiple tools
  • File-based input/output for easy testing
  • Batch processing of multiple requests
  • Human-readable Markdown output

Example:

cd 02_tool_calling
python planner.py --request requests/example1_basic.txt

Output: Comprehensive daily plan with weather, meetings, and travel times.

πŸ“– Detailed Documentation


3. MARL Visualization

Directory: 03_MARL_ViZ_Demo/

API-first multi-agent reinforcement learning visualization without training:

  • High-level Director: Greedy or LLM-based agent-landmark assignment
  • Low-level Actor: HTTP policy server or local heuristics
  • Environments: PettingZoo cooperative (spread) and adversarial (tag)

Key Features:

  • Real-time GIF generation with English captions
  • Dual-axis metrics plots (reward + coverage)
  • Modular architecture (director + actor)
  • No training required (demonstration only)

Example:

cd 03_MARL_ViZ_Demo
# Terminal 1: Start policy server (optional)
python src/policy_server.py

# Terminal 2: Run visualization
python src/marl_viz.py --env spread --steps 200 --director greedy --actor local

Output:

  • outputs/rollout_*.gif: 20-second animated visualization
  • outputs/metrics_*.png: Performance charts

πŸ“– Detailed Documentation


⚑ Quick Start

Prerequisites

Installation

# Clone the repository
git clone https://github.com/yourusername/API_Demo.git
cd API_Demo

# Install all dependencies
pip install -r requirements.txt

# Set up API keys
echo "OPENAI_API_KEY=your_key_here" > .env
echo "ANTHROPIC_API_KEY=your_key_here" >> .env
echo "GEMINI_API_KEY=your_key_here" >> .env

Run a Demo

# Demo 1: Structured extraction
cd 01_structured_extract
python run_all.py

# Demo 2: Function calling
cd ../02_tool_calling
python run_examples.py

# Demo 3: MARL visualization
cd ../03_MARL_ViZ_Demo
python src/marl_viz.py --env spread --steps 100 --director greedy

πŸ“ Project Structure

API_Demo/
β”œβ”€β”€ README.md                          # This file
β”œβ”€β”€ requirements.txt                   # Unified dependencies
β”œβ”€β”€ LICENSE                            # MIT License
β”œβ”€β”€ .gitignore                        # Git ignore rules
β”‚
β”œβ”€β”€ 01_structured_extract/            # Demo 1: Structured Extraction
β”‚   β”œβ”€β”€ README.md                     # Detailed documentation
β”‚   β”œβ”€β”€ requirements.txt              # Local dependencies
β”‚   β”œβ”€β”€ openai_demo.py               # OpenAI implementation
β”‚   β”œβ”€β”€ anthropic_demo.py            # Anthropic implementation
β”‚   β”œβ”€β”€ gemini_demo.py               # Google Gemini implementation
β”‚   β”œβ”€β”€ run_all.py                   # Batch runner
β”‚   β”œβ”€β”€ common/                       # Shared modules
β”‚   β”‚   β”œβ”€β”€ schema.py                # JSON schema definition
β”‚   β”‚   └── validate.py              # Validation utilities
β”‚   └── samples/                      # Example inputs
β”‚       β”œβ”€β”€ paper.txt
β”‚       └── paper_noise.txt
β”‚
β”œβ”€β”€ 02_tool_calling/                  # Demo 2: Function Calling
β”‚   β”œβ”€β”€ README.md                     # Detailed documentation
β”‚   β”œβ”€β”€ DEMO_GUIDE.md                # Presentation guide
β”‚   β”œβ”€β”€ requirements.txt              # Local dependencies
β”‚   β”œβ”€β”€ planner.py                   # Main planner script
β”‚   β”œβ”€β”€ run_examples.py              # Batch processor
β”‚   β”œβ”€β”€ requests/                     # Example requests
β”‚   β”‚   β”œβ”€β”€ README.md
β”‚   β”‚   β”œβ”€β”€ example1_basic.txt
β”‚   β”‚   β”œβ”€β”€ example2_complex.txt
β”‚   β”‚   └── example3_weather_focused.txt
β”‚   └── plans/                        # Output directory (generated)
β”‚
└── 03_MARL_ViZ_Demo/                # Demo 3: MARL Visualization
    β”œβ”€β”€ README.md                     # Detailed documentation
    β”œβ”€β”€ DEMO_GUIDE.md                # Presentation guide
    β”œβ”€β”€ requirements.txt              # Local dependencies
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ marl_viz.py              # Main visualization client
    β”‚   β”œβ”€β”€ policy_server.py         # FastAPI policy server
    β”‚   └── llm_director.py          # Assignment coordinator
    └── outputs/                      # Output directory (generated)

πŸ“¦ Requirements

Core Dependencies

All demos share these core dependencies:

  • openai>=1.0.0 - OpenAI API client
  • anthropic>=0.34.0 - Anthropic API client
  • google-generativeai>=0.3.0 - Google Gemini API client
  • python-dotenv>=1.0.0 - Environment variable management
  • requests>=2.32.0 - HTTP requests

Demo-Specific Dependencies

Demo 1 (Structured Extract):

  • jsonschema>=4.22.0 - JSON schema validation

Demo 2 (Tool Calling):

  • python-dateutil>=2.8.0 - Date parsing

Demo 3 (MARL Viz):

  • pettingzoo[mpe]>=1.24 - Multi-agent environments
  • gymnasium>=0.29 - RL environment framework
  • numpy>=1.26 - Numerical computing
  • imageio>=2.34 - GIF generation
  • pillow>=10.3 - Image processing
  • matplotlib>=3.8 - Plotting
  • fastapi>=0.115 - API server
  • uvicorn>=0.30 - ASGI server

Installation Options

# Install all dependencies (recommended)
pip install -r requirements.txt

# Or install per demo
pip install -r 01_structured_extract/requirements.txt
pip install -r 02_tool_calling/requirements.txt
pip install -r 03_MARL_ViZ_Demo/requirements.txt

πŸ”§ Configuration

API Keys

Create a .env file in the project root or in each demo directory:

# OpenAI
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-4o-mini  # Optional: override default model

# Anthropic
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_MODEL=claude-sonnet-4-5  # Optional

# Google Gemini
GEMINI_API_KEY=AI...

Rate Limiting

  • Google Gemini Free Tier: 5 RPM (handled automatically with delays)
  • OpenAI: Depends on your tier
  • Anthropic: Depends on your tier

πŸ§ͺ Testing

Each demo includes test scripts:

# Demo 1: Test all three providers
cd 01_structured_extract
python run_all.py

# Demo 2: Test all example requests
cd 02_tool_calling
python run_examples.py

# Demo 3: Test both environments
cd 03_MARL_ViZ_Demo
python src/marl_viz.py --env spread --steps 50
python src/marl_viz.py --env tag --steps 50

πŸŽ“ Use Cases

Academic Research

  • Rapid prototyping of LLM-powered systems
  • Benchmarking different API providers
  • Teaching LLM application development

Industry Applications

  • Data extraction pipelines
  • Intelligent assistants with tool use
  • Multi-agent system coordination

Education

  • Hands-on LLM API tutorials
  • Best practices for API integration
  • Prompt engineering examples

πŸ› Troubleshooting

Common Issues

Issue: ModuleNotFoundError

  • Solution: Install dependencies: pip install -r requirements.txt

Issue: API key not found

  • Solution: Create .env file with your API keys

Issue: Rate limit errors (Google Gemini)

  • Solution: Use free tier with built-in delays or upgrade plan

Issue: Import errors from pettingzoo

  • Solution: Update imports (handled automatically in latest version)

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments


πŸ“§ Contact

For questions or suggestions, please open an issue or contact 2614067X@student.gla.ac.uk.


⭐ If you find this project useful, please consider giving it a star!

About

Minimal demos for structured LLM API usage, agent workflows, and research prototyping.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages