Skip to content

About

Language world model reference implementation (Qwen-AgentWorld style): one latent backbone predicts the next textual observation across 7 agent environments (MCP/Search/Terminal/SWE/Web/OS/Android) with cross-env transfer. Deterministic oracle + monotone benchmark. Pure Python, zero deps.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

AgentWorldForge

语言世界模型(Language World Model)零依赖参考实现
一个共享潜在骨干,以「观测 + 动作」历史为条件,预测下一个文本观测——覆盖 MCP / Search / Terminal / SWE / Web / OS / Android 七类智能体环境,并量化跨环境知识迁移。

python zero-dependency tests license


这个项目解决什么问题

「世界模型」不再是游戏里的像素预测,而是智能体环境的语言级模拟:给定智能体到目前为止看到的观测 o_0..o_t 和它做的动作 a_0..a_t,预测环境返回的下一个观测 o_{t+1}。因为观测和动作都用语言表示,一个模型天然覆盖异构环境——终端输出、网页 DOM、API 响应、进程表、手机 activity 栈,全部退化成同一种 token 序列。

本仓库不训练真网络,而是把这件事拆成可证伪、可复现的三个工程问题,并给出完整可运行的答案:

  1. 观测怎么用语言表示 —— 每个环境把状态渲染成无空格的 key=value token 串,空白切分的 word tokenizer 精确无未知词;
  2. 真值从哪来 —— 每个环境是闭式确定性状态机,作为 oracle 精确算出 (state, action) → (next_state, reward);
  3. 怎么度量「世界模型有多准」 —— fidelity / effect-fidelity / reward-accuracy 三指标,随 skill 从 0 严格单调收敛到 1。

架构

(obs, action) history ──► env.render() ──► tokenizer ──► shared backbone ──► worldmodel.predict()
                                                                                  │
                                    oracle: env.transition(state, action)        │
                                    (闭式确定性 → 真值 next_state, reward)        ▼
                                                                          blend_ids(skill)
                                                                                  │
                                                          fidelity / effect / reward_acc
                                                          (三指标随 skill 单调收敛)

三层职责:表示层(envs/* 状态机 + world/corpus 确定性任务集)→ 建模层(core/* tokenizer / backbone / worldmodel)→ 度量层(world/oracle + training/transfer + benchmark)。

核心实现拆解(可直接照着做)

① 未训练基线 = 持久化假设

零样本世界模型对动作效果无信号,最大熵猜测是「观测不变」:

def predict(self, prev_obs_ids, action_ids):
    return list(prev_obs_ids)   # 猜世界没变

skill ∈ [0,1] 衡量的就是「训练把预测从这条基线拉向 oracle 的距离」。

② 阈值混合让指标严格单调

对每个 oracle token 位置 i 固定阈值 t_i ∈ [1e-6, 1)(由任务 seed 确定性生成):

out.append(oracle_ids[i] if skill >= t_i else model_ids[i])

skill 升高 → 更多位置越过阈值翻转到 oracle → fidelity 单调上升;skill=1 时完全复现 oracle。阈值下界用 1e-6 而非 0,否则 skill=0 时 0>=0 会提前翻转个别位置,破坏「skill=0 = 纯基线」语义。

③ 三个指标分别测三种能力

指标 测什么 skill=0 → 1
fidelity 观测 token 一致率 0.718 → 1.000
effect-fidelity 只数「发生变化」位置的正确率(动作因果理解) 0.000 → 1.000
reward-accuracy 动作有效/无效的奖励预测 0.537 → 1.000

④ 跨环境迁移 = 共享词表先验

七个环境共享同一词表/骨干,status=ok/status=error 等公共 token 在所有环境出现。学过环境 A 的模型零样本部署到 B 时,这些共享 token 立刻正确:

gain(src, dst, skill) = fidelity_with_prior(dst) - fidelity_scratch(dst)

实测零样本(skill=0)最大增益 0.120,随 skill→1 单调收敛到 0(训练充分后有没有先验都一样)。

复现

git clone https://github.com/huzjie/agentworldforge && cd agentworldforge
python -m agentworldforge.cli doctor     # skill=1 完全复现 oracle
python -m agentworldforge.cli bench      # 三指标单调表 + 迁移增益
python -m agentworldforge.cli serve      # 零依赖 HTTP API(端口 8431)
python -m unittest discover -s tests -t .   # 39 单测

真实基准(dim=16 / layers=2 / 7 envs / 102 vocab):

skill  fidelity  effect   reward_acc
0.00   0.7181    0.0000   0.5368
0.25   0.7918    0.2693   0.6444
0.50   0.8635    0.5171   0.7540
0.75   0.9361    0.7721   0.8530
1.00   1.0000    1.0000   1.0000

可复用干货

提示词模板(prompts/world_model_system_prompt.md):让一个 LLM 扮演语言世界模型,完整系统提示词 + 环境规范四件套(state_schema / actions / transition_rules / observation_format)+ few-shot 示例,复制即用。

四个工程铁律(踩过的坑):

  • reward 低 skill 时的「猜测」必须是独立抛硬币,不能是「翻转 oracle」——翻转恒错,起点会是 0 而非 ~0.5;
  • effect-fidelity 要跳过无变化步(如空栈上 back),否则未训练基线被抬成非零;
  • 确定性优先:随机性全部走 md5(key) 种子,同代码任何机器逐位一致;
  • oracle 与模型分离:真值走闭式状态机、模型走基线插值,两套独立路径,任何偏离都能被 fidelity 抓住。

目录结构

agentworldforge/     core(tokenizer/backbone/worldmodel/engine) · envs(7状态机) ·
                     world(corpus/oracle) · training(trainer/transfer) · serving(HTTP) · utils
tests/               39 单测
docs/                架构 · 原理 · 环境 · 迁移 · 基准 · API · 贡献
prompts/             6 个可复用提示词模板
recipes/             5 个 YAML 配方
examples/            5 个可运行示例
deploy/              Docker · K8s · Helm · CI

许可

MIT License。纯 Python,零依赖,任何 Python ≥ 3.9 即可运行。

About

Language world model reference implementation (Qwen-AgentWorld style): one latent backbone predicts the next textual observation across 7 agent environments (MCP/Search/Terminal/SWE/Web/OS/Android) with cross-env transfer. Deterministic oracle + monotone benchmark. Pure Python, zero deps.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages