mirror of
https://github.com/huggingface/lerobot.git
synced 2026-07-20 16:31:55 +00:00
refactor(pi052): introduce generic language runtime
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
from lerobot.policies.language_conditioned import (
|
||||
LanguageConditionedRuntime,
|
||||
RuntimeState,
|
||||
ToolCall,
|
||||
VQAResult,
|
||||
)
|
||||
|
||||
|
||||
class FakeAdapter:
|
||||
def __init__(self):
|
||||
self.updated = False
|
||||
self.text_calls = []
|
||||
|
||||
def select_action(self, observation, state):
|
||||
assert observation == {"observation.state": 1}
|
||||
assert state.task == "clean"
|
||||
return ["a0", "a1"]
|
||||
|
||||
def select_text(self, kind, observation, state, user_text=None):
|
||||
self.text_calls.append((kind, user_text))
|
||||
return "new plan <say>ok</say>"
|
||||
|
||||
def parse_tool_calls(self, text):
|
||||
assert text == "new plan <say>ok</say>"
|
||||
return [ToolCall("say", {"text": "ok"})]
|
||||
|
||||
def answer_vqa(self, question, camera, observation, state):
|
||||
return VQAResult(answer=f"answer: {question}")
|
||||
|
||||
def update_language_state(self, observation, state):
|
||||
self.updated = True
|
||||
state.set_context("subtask", "pick cup", label="subtask")
|
||||
|
||||
|
||||
class FakeTool:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
|
||||
def call(self, args):
|
||||
self.calls.append(args)
|
||||
|
||||
|
||||
def test_runtime_tick_updates_language_enqueues_and_dispatches_action():
|
||||
adapter = FakeAdapter()
|
||||
executed = []
|
||||
runtime = LanguageConditionedRuntime(
|
||||
policy_adapter=adapter,
|
||||
observation_provider=lambda: {"observation.state": 1},
|
||||
action_executor=executed.append,
|
||||
)
|
||||
runtime.set_task("clean")
|
||||
|
||||
logs = runtime.step_once()
|
||||
|
||||
assert adapter.updated
|
||||
assert runtime.state.language_context["subtask"] == "pick cup"
|
||||
assert executed == ["a0"]
|
||||
assert list(runtime.state.action_queue) == ["a1"]
|
||||
assert " subtask: pick cup" in logs
|
||||
|
||||
|
||||
def test_runtime_handles_user_interjection_and_dispatches_tools():
|
||||
adapter = FakeAdapter()
|
||||
tool = FakeTool()
|
||||
runtime = LanguageConditionedRuntime(
|
||||
policy_adapter=adapter,
|
||||
observation_provider=lambda: {"observation.state": 1},
|
||||
tools={"say": tool},
|
||||
)
|
||||
runtime.set_task("clean")
|
||||
runtime.state.extra["recent_interjection"] = "please say ok"
|
||||
runtime.state.emit("user_interjection")
|
||||
|
||||
logs = runtime.step_once()
|
||||
|
||||
assert ("interjection", "please say ok") in adapter.text_calls
|
||||
assert runtime.state.language_context["plan"] == "new plan <say>ok</say>"
|
||||
assert tool.calls == [{"text": "ok"}]
|
||||
assert " speech: ok" in logs
|
||||
|
||||
|
||||
def test_runtime_state_aliases_legacy_keys_to_language_context():
|
||||
state = RuntimeState()
|
||||
state["current_subtask"] = "open drawer"
|
||||
state["current_memory"] = "drawer open"
|
||||
|
||||
assert state.get("current_subtask") == "open drawer"
|
||||
assert state.language_context == {"subtask": "open drawer", "memory": "drawer open"}
|
||||
@@ -0,0 +1,54 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from lerobot.policies.language_conditioned import RuntimeState
|
||||
from lerobot.policies.pi052.inference.pi052_adapter import PI052PolicyAdapter, split_plan_and_say
|
||||
|
||||
|
||||
def test_pi052_adapter_builds_recipe_prompts_from_runtime_state():
|
||||
adapter = PI052PolicyAdapter(policy=object())
|
||||
state = RuntimeState(
|
||||
task="clean the kitchen",
|
||||
language_context={"memory": "cup moved", "plan": "pick then place"},
|
||||
extra={"prior_subtask": "pick the cup"},
|
||||
)
|
||||
|
||||
assert adapter.messages_for("subtask", state) == [{"role": "user", "content": "clean the kitchen"}]
|
||||
assert adapter.messages_for("memory", state) == [
|
||||
{"role": "user", "content": "clean the kitchen"},
|
||||
{"role": "assistant", "content": "Previous memory: cup moved"},
|
||||
{"role": "user", "content": "Completed subtask: pick the cup"},
|
||||
]
|
||||
assert adapter.messages_for("interjection", state, user_text="wait") == [
|
||||
{"role": "user", "content": "clean the kitchen"},
|
||||
{"role": "assistant", "content": "Previous plan:\npick then place"},
|
||||
{"role": "user", "content": "wait"},
|
||||
]
|
||||
assert adapter.messages_for("vqa", state, user_text="where is the cup?") == [
|
||||
{"role": "user", "content": "where is the cup?"}
|
||||
]
|
||||
|
||||
|
||||
def test_pi052_adapter_parses_say_tool_calls_and_plan_text():
|
||||
adapter = PI052PolicyAdapter(policy=object())
|
||||
text = "Move to the sink. <say>heading to the sink</say>"
|
||||
|
||||
assert split_plan_and_say(text) == ("Move to the sink.", "heading to the sink")
|
||||
assert adapter.parse_tool_calls(text)[0].name == "say"
|
||||
assert adapter.parse_tool_calls(text)[0].arguments == {"text": "heading to the sink"}
|
||||
assert adapter.plan_from_text(text) == "Move to the sink."
|
||||
|
||||
|
||||
def test_pi052_runtime_cli_smoke_does_not_load_model(monkeypatch):
|
||||
from lerobot.policies.pi052.inference import runtime_cli
|
||||
|
||||
fake_policy = SimpleNamespace(config=SimpleNamespace(device="cpu"))
|
||||
|
||||
monkeypatch.setattr(
|
||||
runtime_cli,
|
||||
"_load_policy_and_preprocessor",
|
||||
lambda policy_path, dataset_repo_id: (fake_policy, None, None, None),
|
||||
)
|
||||
monkeypatch.setattr(runtime_cli, "_build_tools", lambda no_tts, tts_voice: {})
|
||||
monkeypatch.setattr(runtime_cli, "_run_repl", lambda runtime, initial_task, max_ticks: 0)
|
||||
|
||||
assert runtime_cli.main(["--policy.path=fake", "--no_robot", "--task=clean", "--max_ticks=0"]) == 0
|
||||
Reference in New Issue
Block a user