A Python implementation of the Hierarchical Reasoning Model (HRM) with function calling capabilities and an OpenAI-compatible API.
- Hierarchical Reasoning Model: Implementation based on the paper "Hierarchical Reasoning Model"
- OpenAI-Compatible API: Drop-in replacement for OpenAI API endpoints
- Function Calling: Built-in support for Python function execution
- Extensible: Easy to register custom functions
- Production Ready: Built with FastAPI and proper error handling
# Clone the repository
cd human-agent
# Install with Poetry
poetry install
# Or install with pip
pip install -e .
# Using Poetry
poetry run human-agent-server
# Or directly
python -m human_agent.server
# Run the example client
python examples/client_example.py
import openai
# Point to your local server
openai.api_base = "http://localhost:8000/v1"
openai.api_key = "dummy" # Not used but required
response = openai.ChatCompletion.create(
model="hrm-27m",
messages=[
{"role": "user", "content": "What's 15 + 25 * 3?"}
]
)
print(response.choices[0].message.content)
GET /
- Server infoGET /v1/models
- List available modelsPOST /v1/chat/completions
- Chat completions (OpenAI-compatible)GET /v1/functions
- List available functionsPOST /v1/functions/register
- Register new functions
The model supports automatic function calling. Built-in functions include:
calculate(expression)
- Safe mathematical expression evaluationget_weather(location)
- Mock weather informationsearch_web(query)
- Mock web searchget_current_time()
- Current date and time
# Via API
import requests
function_code = """
def greet(name: str) -> str:
return f"Hello, {name}!"
"""
response = requests.post("http://localhost:8000/v1/functions/register", json={
"name": "greet",
"code": function_code,
"description": "Greet a person by name"
})
The HRM model features:
- Hierarchical Processing: Two-level recurrent architecture
- Temporal Separation: Different timescales for planning vs. execution
- Adaptive Computation Time: Dynamic resource allocation
- Deep Supervision: Multiple training signals
- One-step Gradients: Memory-efficient training
# Install development dependencies
poetry install --with dev
# Run tests
poetry run pytest
See examples/training_example.py
for a basic training loop:
from hrm_api.core.model import create_hrm_model
model = create_hrm_model(
vocab_size=1000,
dim=256,
N=2, # High-level cycles
T=4, # Low-level steps per cycle
)
# Train on your reasoning tasks...
@article{arXiv:2506.21734,
title={Hierarchical Reasoning Model},
author={Guan Wang, Jin Li, Yuhao Sun, Xing Chen, Changling Liu, Yue Wu, Meng Lu, Sen Song, Yasin Abbasi Yadkori},
journal={arXiv preprint arXiv:2506.21734v2},
year={2025}
}
MIT License - see LICENSE file for details.