zhangzaibin/spagent

SPAgent, a foundation agent for understanding, reasoning over, and operating within the physical and spatial world.

219

stars

435

commits

Python

primary language

Sep 3, 2026

updated

README

SPAgent Logo

🌍 SPAgent: Agent in the Physical & Spatial World

Think3D: Thinking with Space for Spatial Reasoning

Project Page arXiv Hugging Face Hugging Face License Python 3.11


πŸ“Œ Introduction

SPAgent is a foundation agent for perception, reasoning, and action in the physical and spatial world. It provides a modular, open-ended ecosystem of expert tools spanning 2D vision, 3D reconstruction, world modeling, video/image generation, and beyond β€” enabling grounded spatial reasoning and flexible interaction in complex real-world environments.

πŸ“š Documentation

DocumentDescription
Tool ReferenceExternal expert tools API and deployment guide
External ExpertsFull list of supported expert models and default ports
Advanced Examplesstep() API, AgentMemory, video/image gen, RL training, testing
RL TrainingGRPO training pipeline: dataset prep, offline Pi3X cache, rewards, launch
Reproduce ResultsEnd-to-end recipe to reproduce benchmark numbers
Quick Evalquick_eval.py reference and shell-script shortcuts
Dataset PreparationPer-benchmark dataset download and JSONL conversion
Adding New ToolsGuide for extending SPAgent with new expert tools
Contributing ToolsTool contract checklist and CI commands for contributors

βœ… Features

  • Modular Tool System β€” Mix and match any combination of expert tools
  • Dynamic Tool Management β€” Add/remove tools at runtime
  • Parallel Tool Execution β€” Automatic concurrent processing when possible
  • Multi-Image Analysis β€” Handle single or multiple images seamlessly
  • Multiple Model Support β€” GPT, Qwen, and local VLLM models
  • Customizable System Prompt β€” Per-agent templates; built-in 3D spatial and general vision presets
  • Multimodal Agent Memory β€” AgentMemory records every turn: text, images, tool calls, and results
  • Multi-turn Stateful Conversations β€” Pass AgentMemory across step() calls; save/load sessions
  • Reinforcement Learning β€” GRPO training via ms-swift with multi-turn tool calling and an offline Pi3X cache (no expert servers needed in the training loop) β†’ see RL Training

πŸ› οΈ Installation & Setup

1. Environment

conda create -n spagent python=3.11
conda activate spagent

pip install -r requirements.txt
pip install "httpx[socks]"

2. API Keys

# OpenAI (also used by SoraTool)
export OPENAI_API_KEY="your_api_key"
export OPENAI_BASE_URL="your_base_url"

# Qwen / DashScope (apply at: https://bailian.console.aliyun.com)
export DASHSCOPE_API_KEY="your_api_key"

# Moondream (apply at: https://moondream.ai)
export MOONDREAM_API_KEY="your_api_key"

# Google Gemini (used by VeoTool)
export GOOGLE_API_KEY="your_google_api_key"

3. Deploy Expert Servers

See Tool Reference for per-tool deployment instructions (Depth, SAM2, GroundingDINO, Pi3, Molmo2, OrientAnythingV2, WildDet3D, FlowSeek, PaddleOCR-VL, OneFormer, Sana, Qwen Image Edit, VACE, …).

πŸš€ Quick Start

Basic Usage

from spagent import SPAgent
from spagent.models import GPTModel
from spagent.tools import DepthEstimationTool, SegmentationTool

model = GPTModel(model_name="gpt-4o-mini")
tools = [
    DepthEstimationTool(use_mock=True),
    SegmentationTool(use_mock=True),
]

agent = SPAgent(model=model, tools=tools)
result = agent.solve_problem("image.jpg", "Analyze depth and main objects in this image")
print(result['answer'])

Multi-Tool Agent

from spagent.tools import (
    DepthEstimationTool, SegmentationTool,
    ZoomObjectTool,      # GroundingDINO: crop close-up for attribute inspection
    LocalizeObjectTool,  # GroundingDINO: bbox annotation for spatial/counting
    SupervisionTool, YOLOETool,
)

tools = [
    DepthEstimationTool(use_mock=True),
    SegmentationTool(use_mock=True),
    ZoomObjectTool(use_mock=True),
    LocalizeObjectTool(use_mock=True),
    SupervisionTool(use_mock=True),
    YOLOETool(use_mock=True),
]

agent = SPAgent(model=GPTModel(model_name="gpt-4o-mini"), tools=tools, max_workers=4)
result = agent.solve_problem("image.jpg", "Comprehensively analyze this image")
print(result['answer'])
print(result['used_tools'])

Real Expert Services

tools = [
    DepthEstimationTool(use_mock=False, server_url="http://localhost:20019"),
    SegmentationTool(use_mock=False,    server_url="http://localhost:20020"),
    ZoomObjectTool(use_mock=False,      server_url="http://localhost:20022"),
    LocalizeObjectTool(use_mock=False,  server_url="http://localhost:20022"),
]

For video generation, image generation, multi-turn AgentMemory, custom system prompts, RL training, and detailed testing, see Advanced Examples.

πŸ“Š Evaluation

scripts/quick_eval.py is the unified entry point. It runs SPAgent over VLMEvalKit benchmarks and local datasets with automatic resuming and per-sample traces.

60-second smoke test (no servers needed):

python scripts/quick_eval.py --model gpt-4.1-mini --datasets MMStar --limit 5

With tools:

python scripts/quick_eval.py \
    --model gpt-4.1-mini --tools zoom localize \
    --datasets MMStar VStarBench --limit 50 \
    --detection-url http://localhost:20022

Full recipes β†’ REPRODUCE.md Β· QUICK_EVAL.md Β· EVALUATION.md

🎯 Reinforcement Learning

SPAgent now supports GRPO reinforcement learning of the policy VLM with multi-turn tool calling, powered by ms-swift. The Pi3X expert runs offline during rollouts: point clouds are pre-computed once and cached to disk, so training needs no live expert servers or network calls.

# 1. Fix dataset image paths
python scripts/build_rl_dataset.py \
    --input "dataset/crossviewQA_train_rl (1).jsonl" \
    --output dataset/crossviewQA_train_rl_fixed.jsonl

# 2. Pre-compute the Pi3X point-cloud cache (run once)
python train/precompute_pi3x_cache.py \
    --dataset dataset/crossviewQA_train_rl_fixed.jsonl \
    --cache-dir dataset/pi3x_cache \
    --checkpoint checkpoints/pi3x/model.safetensors

# 3. Launch GRPO training
cd train && bash train_grpo.sh

Full guide (dataset prep, caching, reward functions, scheduler, post-training) β†’ RL Training

⚠️ Important Notes

  • Python Version: Python 3.11 recommended; other versions may have compatibility issues
  • GPU Memory: Real mode requires β‰₯ 24 GB GPU memory
  • Concurrency: Control parallel tool execution via max_workers

πŸ“ Citation

@article{zhang2026think3d,
  title={Think3D: Thinking with Space for Spatial Reasoning},
  author={Zhang, Zaibin and Wu, Yuhan and Jia, Lianjie and Wang, Yifan and Zhang, Zhongbo and Li, Yijiang and Ran, Binghao and Zhang, Fuxi and Sun, Zhuohan and Yin, Zhenfei and others},
  journal={arXiv preprint arXiv:2601.13029},
  year={2026}
}

⭐ Star History

🌟 Thank you for your support! 🌟

Star History Chart

Contributors

zhangzaibin

241 commits

YuhanWu27

105 commits

Irisicy4

39 commits

jialianjie

13 commits

zhangzaibin/spagent

SPAgent, a foundation agent for understanding, reasoning over, and operating within the physical and spatial world.

219

stars

435

commits

Python

primary language

Sep 3, 2026

updated

README

SPAgent Logo

🌍 SPAgent: Agent in the Physical & Spatial World

Think3D: Thinking with Space for Spatial Reasoning

Project Page arXiv Hugging Face Hugging Face License Python 3.11


πŸ“Œ Introduction

SPAgent is a foundation agent for perception, reasoning, and action in the physical and spatial world. It provides a modular, open-ended ecosystem of expert tools spanning 2D vision, 3D reconstruction, world modeling, video/image generation, and beyond β€” enabling grounded spatial reasoning and flexible interaction in complex real-world environments.

πŸ“š Documentation

DocumentDescription
Tool ReferenceExternal expert tools API and deployment guide
External ExpertsFull list of supported expert models and default ports
Advanced Examplesstep() API, AgentMemory, video/image gen, RL training, testing
RL TrainingGRPO training pipeline: dataset prep, offline Pi3X cache, rewards, launch
Reproduce ResultsEnd-to-end recipe to reproduce benchmark numbers
Quick Evalquick_eval.py reference and shell-script shortcuts
Dataset PreparationPer-benchmark dataset download and JSONL conversion
Adding New ToolsGuide for extending SPAgent with new expert tools
Contributing ToolsTool contract checklist and CI commands for contributors

βœ… Features

  • Modular Tool System β€” Mix and match any combination of expert tools
  • Dynamic Tool Management β€” Add/remove tools at runtime
  • Parallel Tool Execution β€” Automatic concurrent processing when possible
  • Multi-Image Analysis β€” Handle single or multiple images seamlessly
  • Multiple Model Support β€” GPT, Qwen, and local VLLM models
  • Customizable System Prompt β€” Per-agent templates; built-in 3D spatial and general vision presets
  • Multimodal Agent Memory β€” AgentMemory records every turn: text, images, tool calls, and results
  • Multi-turn Stateful Conversations β€” Pass AgentMemory across step() calls; save/load sessions
  • Reinforcement Learning β€” GRPO training via ms-swift with multi-turn tool calling and an offline Pi3X cache (no expert servers needed in the training loop) β†’ see RL Training

πŸ› οΈ Installation & Setup

1. Environment

conda create -n spagent python=3.11
conda activate spagent

pip install -r requirements.txt
pip install "httpx[socks]"

2. API Keys

# OpenAI (also used by SoraTool)
export OPENAI_API_KEY="your_api_key"
export OPENAI_BASE_URL="your_base_url"

# Qwen / DashScope (apply at: https://bailian.console.aliyun.com)
export DASHSCOPE_API_KEY="your_api_key"

# Moondream (apply at: https://moondream.ai)
export MOONDREAM_API_KEY="your_api_key"

# Google Gemini (used by VeoTool)
export GOOGLE_API_KEY="your_google_api_key"

3. Deploy Expert Servers

See Tool Reference for per-tool deployment instructions (Depth, SAM2, GroundingDINO, Pi3, Molmo2, OrientAnythingV2, WildDet3D, FlowSeek, PaddleOCR-VL, OneFormer, Sana, Qwen Image Edit, VACE, …).

πŸš€ Quick Start

Basic Usage

from spagent import SPAgent
from spagent.models import GPTModel
from spagent.tools import DepthEstimationTool, SegmentationTool

model = GPTModel(model_name="gpt-4o-mini")
tools = [
    DepthEstimationTool(use_mock=True),
    SegmentationTool(use_mock=True),
]

agent = SPAgent(model=model, tools=tools)
result = agent.solve_problem("image.jpg", "Analyze depth and main objects in this image")
print(result['answer'])

Multi-Tool Agent

from spagent.tools import (
    DepthEstimationTool, SegmentationTool,
    ZoomObjectTool,      # GroundingDINO: crop close-up for attribute inspection
    LocalizeObjectTool,  # GroundingDINO: bbox annotation for spatial/counting
    SupervisionTool, YOLOETool,
)

tools = [
    DepthEstimationTool(use_mock=True),
    SegmentationTool(use_mock=True),
    ZoomObjectTool(use_mock=True),
    LocalizeObjectTool(use_mock=True),
    SupervisionTool(use_mock=True),
    YOLOETool(use_mock=True),
]

agent = SPAgent(model=GPTModel(model_name="gpt-4o-mini"), tools=tools, max_workers=4)
result = agent.solve_problem("image.jpg", "Comprehensively analyze this image")
print(result['answer'])
print(result['used_tools'])

Real Expert Services

tools = [
    DepthEstimationTool(use_mock=False, server_url="http://localhost:20019"),
    SegmentationTool(use_mock=False,    server_url="http://localhost:20020"),
    ZoomObjectTool(use_mock=False,      server_url="http://localhost:20022"),
    LocalizeObjectTool(use_mock=False,  server_url="http://localhost:20022"),
]

For video generation, image generation, multi-turn AgentMemory, custom system prompts, RL training, and detailed testing, see Advanced Examples.

πŸ“Š Evaluation

scripts/quick_eval.py is the unified entry point. It runs SPAgent over VLMEvalKit benchmarks and local datasets with automatic resuming and per-sample traces.

60-second smoke test (no servers needed):

python scripts/quick_eval.py --model gpt-4.1-mini --datasets MMStar --limit 5

With tools:

python scripts/quick_eval.py \
    --model gpt-4.1-mini --tools zoom localize \
    --datasets MMStar VStarBench --limit 50 \
    --detection-url http://localhost:20022

Full recipes β†’ REPRODUCE.md Β· QUICK_EVAL.md Β· EVALUATION.md

🎯 Reinforcement Learning

SPAgent now supports GRPO reinforcement learning of the policy VLM with multi-turn tool calling, powered by ms-swift. The Pi3X expert runs offline during rollouts: point clouds are pre-computed once and cached to disk, so training needs no live expert servers or network calls.

# 1. Fix dataset image paths
python scripts/build_rl_dataset.py \
    --input "dataset/crossviewQA_train_rl (1).jsonl" \
    --output dataset/crossviewQA_train_rl_fixed.jsonl

# 2. Pre-compute the Pi3X point-cloud cache (run once)
python train/precompute_pi3x_cache.py \
    --dataset dataset/crossviewQA_train_rl_fixed.jsonl \
    --cache-dir dataset/pi3x_cache \
    --checkpoint checkpoints/pi3x/model.safetensors

# 3. Launch GRPO training
cd train && bash train_grpo.sh

Full guide (dataset prep, caching, reward functions, scheduler, post-training) β†’ RL Training

⚠️ Important Notes

  • Python Version: Python 3.11 recommended; other versions may have compatibility issues
  • GPU Memory: Real mode requires β‰₯ 24 GB GPU memory
  • Concurrency: Control parallel tool execution via max_workers

πŸ“ Citation

@article{zhang2026think3d,
  title={Think3D: Thinking with Space for Spatial Reasoning},
  author={Zhang, Zaibin and Wu, Yuhan and Jia, Lianjie and Wang, Yifan and Zhang, Zhongbo and Li, Yijiang and Ran, Binghao and Zhang, Fuxi and Sun, Zhuohan and Yin, Zhenfei and others},
  journal={arXiv preprint arXiv:2601.13029},
  year={2026}
}

⭐ Star History

🌟 Thank you for your support! 🌟

Star History Chart

Contributors

zhangzaibin

241 commits

YuhanWu27

105 commits

Irisicy4

39 commits

jialianjie

13 commits

Languages

Python

98.7%

Shell

1.3%