|
1 | 1 | """ |
2 | | -Docker REPL example with code execution and LLM queries. |
| 2 | +Docker REPL example with code execution, LLM queries, recursive sub-RLM calls, |
| 3 | +and custom tools. |
3 | 4 |
|
4 | 5 | Setup: |
5 | 6 | 1. Ensure Docker is running |
6 | 7 | 2. Run: python -m examples.docker_repl_example |
7 | 8 |
|
8 | 9 | The default image (python:3.11-slim) will be pulled automatically. |
| 10 | +
|
| 11 | +Persistence and compaction: |
| 12 | + DockerREPL also supports multi-turn persistence and compaction. Drive them |
| 13 | + through the RLM rather than the env directly, e.g.: |
| 14 | +
|
| 15 | + rlm = RLM(backend=..., environment="docker", persistent=True) # multi-turn |
| 16 | + rlm = RLM(backend=..., environment="docker", compaction=True) # auto-summarize |
9 | 17 | """ |
10 | 18 |
|
11 | 19 | from rlm.clients.base_lm import BaseLM |
12 | 20 | from rlm.core.lm_handler import LMHandler |
13 | | -from rlm.core.types import ModelUsageSummary, UsageSummary |
| 21 | +from rlm.core.types import ModelUsageSummary, RLMChatCompletion, UsageSummary |
14 | 22 | from rlm.environments.docker_repl import DockerREPL |
15 | 23 |
|
16 | 24 |
|
@@ -61,6 +69,32 @@ def main(): |
61 | 69 | result = repl.execute_code("print(len(rs))") |
62 | 70 | print(f" Batched count: {result.stdout.strip()}") |
63 | 71 |
|
| 72 | + # Recursive sub-RLM calls + custom tools |
| 73 | + print("\n[3] Recursive sub-RLM (rlm_query) + custom tools") |
| 74 | + |
| 75 | + def mock_subcall(prompt, model=None): |
| 76 | + # In real use this is RLM._subcall, which spawns a child RLM. |
| 77 | + return RLMChatCompletion( |
| 78 | + root_model=model or "child", |
| 79 | + prompt=prompt, |
| 80 | + response=f"child-answer({prompt})", |
| 81 | + usage_summary=UsageSummary(model_usage_summaries={}), |
| 82 | + execution_time=0.0, |
| 83 | + ) |
| 84 | + |
| 85 | + with DockerREPL( |
| 86 | + subcall_fn=mock_subcall, |
| 87 | + custom_tools={"triple": "def triple(x):\n return x * 3", "CONST": 7}, |
| 88 | + ) as repl: |
| 89 | + result = repl.execute_code("print(triple(CONST))") |
| 90 | + print(f" custom tools triple(CONST) → {result.stdout.strip()}") |
| 91 | + |
| 92 | + result = repl.execute_code('print(rlm_query("solve subtask"))') |
| 93 | + print(f" rlm_query → {result.stdout.strip()}") |
| 94 | + |
| 95 | + result = repl.execute_code('print(rlm_query_batched(["a", "b", "c"]))') |
| 96 | + print(f" rlm_query_batched → {result.stdout.strip()}") |
| 97 | + |
64 | 98 | print("\n" + "=" * 50) |
65 | 99 | print("Done!") |
66 | 100 |
|
|
0 commit comments