|
| 1 | +""" |
| 2 | +Docker REPL example with code execution and LLM queries. |
| 3 | +
|
| 4 | +Setup: |
| 5 | + 1. Ensure Docker is running |
| 6 | + 2. Run: python -m examples.docker_repl_example |
| 7 | +
|
| 8 | +The default image (python:3.11-slim) will be pulled automatically. |
| 9 | +""" |
| 10 | + |
| 11 | +from rlm.clients.base_lm import BaseLM |
| 12 | +from rlm.core.lm_handler import LMHandler |
| 13 | +from rlm.core.types import ModelUsageSummary, UsageSummary |
| 14 | +from rlm.environments.docker_repl import DockerREPL |
| 15 | + |
| 16 | + |
| 17 | +class MockLM(BaseLM): |
| 18 | + def __init__(self): |
| 19 | + super().__init__(model_name="mock") |
| 20 | + |
| 21 | + def completion(self, prompt): |
| 22 | + return f"Mock: {str(prompt)[:50]}" |
| 23 | + |
| 24 | + async def acompletion(self, prompt): |
| 25 | + return self.completion(prompt) |
| 26 | + |
| 27 | + def get_usage_summary(self): |
| 28 | + return UsageSummary({"mock": ModelUsageSummary(1, 10, 10)}) |
| 29 | + |
| 30 | + def get_last_usage(self): |
| 31 | + return self.get_usage_summary() |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + print("=" * 50) |
| 36 | + print("Docker REPL Example") |
| 37 | + print("=" * 50) |
| 38 | + |
| 39 | + # Basic execution (no LLM) |
| 40 | + print("\n[1] Basic code execution") |
| 41 | + with DockerREPL() as repl: |
| 42 | + result = repl.execute_code("x = 1 + 2") |
| 43 | + print(f" x = 1 + 2 → locals: {result.locals}") |
| 44 | + |
| 45 | + result = repl.execute_code("print(x * 2)") |
| 46 | + print(f" print(x * 2) → {result.stdout.strip()}") |
| 47 | + |
| 48 | + # With LLM handler |
| 49 | + print("\n[2] With LLM handler") |
| 50 | + with LMHandler(client=MockLM()) as handler: |
| 51 | + print(f" Handler at {handler.address}") |
| 52 | + |
| 53 | + with DockerREPL(lm_handler_address=handler.address) as repl: |
| 54 | + result = repl.execute_code('r = llm_query("Hello!")') |
| 55 | + print(f" llm_query → stderr: {result.stderr or '(none)'}") |
| 56 | + |
| 57 | + result = repl.execute_code("print(r)") |
| 58 | + print(f" Response: {result.stdout.strip()}") |
| 59 | + |
| 60 | + result = repl.execute_code('rs = llm_query_batched(["Q1", "Q2"])') |
| 61 | + result = repl.execute_code("print(len(rs))") |
| 62 | + print(f" Batched count: {result.stdout.strip()}") |
| 63 | + |
| 64 | + print("\n" + "=" * 50) |
| 65 | + print("Done!") |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + main() |
0 commit comments