|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +OptILLM is an OpenAI API compatible optimizing inference proxy that implements state-of-the-art techniques to improve accuracy and performance of LLMs. It focuses on reasoning improvements for coding, logical, and mathematical queries through inference-time compute optimization. |
| 8 | + |
| 9 | +## Core Architecture |
| 10 | + |
| 11 | +### Main Components |
| 12 | + |
| 13 | +1. **Entry Points**: |
| 14 | + - `optillm.py` - Main Flask server with inference routing |
| 15 | + - `optillm/inference.py` - Local inference engine with transformer models |
| 16 | + - Setup via `setup.py` with console script `optillm=optillm:main` |
| 17 | + |
| 18 | +2. **Optimization Techniques** (`optillm/`): |
| 19 | + - **Reasoning**: `cot_reflection.py`, `plansearch.py`, `leap.py`, `reread.py` |
| 20 | + - **Sampling**: `bon.py` (Best of N), `moa.py` (Mixture of Agents), `self_consistency.py` |
| 21 | + - **Search**: `mcts.py` (Monte Carlo Tree Search), `rstar.py` (R* Algorithm) |
| 22 | + - **Verification**: `pvg.py` (Prover-Verifier Game), `z3_solver.py` |
| 23 | + - **Advanced**: `cepo/` (Cerebras Planning & Optimization), `rto.py` (Round Trip) |
| 24 | + |
| 25 | +3. **Decoding Techniques**: |
| 26 | + - `cot_decoding.py` - Chain-of-thought without explicit prompting |
| 27 | + - `entropy_decoding.py` - Adaptive sampling based on token uncertainty |
| 28 | + - `thinkdeeper.py` - Reasoning effort scaling |
| 29 | + - `autothink/` - Query complexity classification with steering vectors |
| 30 | + |
| 31 | +4. **Plugin System** (`optillm/plugins/`): |
| 32 | + - `spl/` - System Prompt Learning (third paradigm learning) |
| 33 | + - `deepthink/` - Gemini-like deep thinking with inference scaling |
| 34 | + - `longcepo/` - Long-context processing with divide-and-conquer |
| 35 | + - `mcp_plugin.py` - Model Context Protocol client |
| 36 | + - `memory_plugin.py` - Short-term memory for unbounded context |
| 37 | + - `privacy_plugin.py` - PII anonymization/deanonymization |
| 38 | + - `executecode_plugin.py` - Code interpreter integration |
| 39 | + - `json_plugin.py` - Structured outputs with outlines library |
| 40 | + |
| 41 | +## Development Commands |
| 42 | + |
| 43 | +### Installation & Setup |
| 44 | +```bash |
| 45 | +# Development setup |
| 46 | +python3 -m venv .venv |
| 47 | +source .venv/bin/activate |
| 48 | +pip install -r requirements.txt |
| 49 | + |
| 50 | +# Package installation |
| 51 | +pip install optillm |
| 52 | +``` |
| 53 | + |
| 54 | +### Running the Server |
| 55 | +```bash |
| 56 | +# Basic server (auto approach detection) |
| 57 | +python optillm.py |
| 58 | + |
| 59 | +# With specific approach |
| 60 | +python optillm.py --approach moa --model gpt-4o-mini |
| 61 | + |
| 62 | +# With external endpoint |
| 63 | +python optillm.py --base_url http://localhost:8080/v1 |
| 64 | + |
| 65 | +# Docker |
| 66 | +docker compose up -d |
| 67 | +``` |
| 68 | + |
| 69 | +### Testing |
| 70 | +```bash |
| 71 | +# Run all approach tests |
| 72 | +python test.py |
| 73 | + |
| 74 | +# Test specific approaches |
| 75 | +python test.py --approaches moa bon mcts |
| 76 | + |
| 77 | +# Test with specific model/endpoint |
| 78 | +python test.py --model gpt-4o-mini --base-url http://localhost:8080/v1 |
| 79 | + |
| 80 | +# Single test case |
| 81 | +python test.py --single-test "specific_test_name" |
| 82 | +``` |
| 83 | + |
| 84 | +### Evaluation Scripts |
| 85 | +```bash |
| 86 | +# Math benchmark evaluation |
| 87 | +python scripts/eval_math500_benchmark.py |
| 88 | + |
| 89 | +# AIME benchmark |
| 90 | +python scripts/eval_aime_benchmark.py |
| 91 | + |
| 92 | +# Arena Hard Auto evaluation |
| 93 | +python scripts/eval_arena_hard_auto_rtc.py |
| 94 | + |
| 95 | +# FRAMES benchmark |
| 96 | +python scripts/eval_frames_benchmark.py |
| 97 | + |
| 98 | +# OptILLM benchmark generation/evaluation |
| 99 | +python scripts/gen_optillmbench.py |
| 100 | +python scripts/eval_optillmbench.py |
| 101 | +``` |
| 102 | + |
| 103 | +## Usage Patterns |
| 104 | + |
| 105 | +### Approach Selection (Priority Order) |
| 106 | +1. **Model prefix**: `moa-gpt-4o-mini` (approach slug + model name) |
| 107 | +2. **extra_body field**: `{"optillm_approach": "bon|moa|mcts"}` |
| 108 | +3. **Prompt tags**: `<optillm_approach>re2</optillm_approach>` in system/user prompt |
| 109 | + |
| 110 | +### Approach Combinations |
| 111 | +- **Pipeline** (`&`): `cot_reflection&moa` - sequential processing |
| 112 | +- **Parallel** (`|`): `bon|moa|mcts` - multiple responses returned as list |
| 113 | + |
| 114 | +### Local Inference |
| 115 | +- Set `OPTILLM_API_KEY=optillm` to enable built-in transformer inference |
| 116 | +- Supports HuggingFace models with LoRA adapters: `model+lora1+lora2` |
| 117 | +- Advanced decoding: `{"decoding": "cot_decoding", "k": 10}` |
| 118 | + |
| 119 | +### Plugin Configuration |
| 120 | +- MCP: `~/.optillm/mcp_config.json` for Model Context Protocol servers |
| 121 | +- SPL: Built-in system prompt learning for solving strategies |
| 122 | +- Memory: Automatic unbounded context via chunking and retrieval |
| 123 | + |
| 124 | +## Key Concepts |
| 125 | + |
| 126 | +### Inference Optimization |
| 127 | +The proxy intercepts OpenAI API calls and applies optimization techniques before forwarding to LLM providers (OpenAI, Cerebras, Azure, LiteLLM). Each technique implements specific reasoning or sampling improvements. |
| 128 | + |
| 129 | +### Plugin Architecture |
| 130 | +Plugins extend functionality via standardized interfaces. They can modify requests, process responses, add tools, or provide entirely new capabilities like code execution or structured outputs. |
| 131 | + |
| 132 | +### Multi-Provider Support |
| 133 | +Automatically detects and routes to appropriate LLM provider based on environment variables (`OPENAI_API_KEY`, `CEREBRAS_API_KEY`, etc.) with fallback to LiteLLM for broader model support. |
0 commit comments