|
| 1 | +""" |
| 2 | +Example usage of Daytona REPL with code execution and LLM queries. |
| 3 | +
|
| 4 | +Run with: python -m examples.daytona_repl_example |
| 5 | +""" |
| 6 | + |
| 7 | +from rlm.clients.base_lm import BaseLM |
| 8 | +from rlm.core.lm_handler import LMHandler |
| 9 | +from rlm.core.types import ModelUsageSummary, UsageSummary |
| 10 | +from rlm.environments.daytona_repl import DaytonaREPL |
| 11 | + |
| 12 | + |
| 13 | +class MockLM(BaseLM): |
| 14 | + """Simple mock LM that echoes prompts.""" |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + super().__init__(model_name="mock-model") |
| 18 | + |
| 19 | + def completion(self, prompt): |
| 20 | + return f"Mock response to: {prompt[:50]}" |
| 21 | + |
| 22 | + async def acompletion(self, prompt): |
| 23 | + return self.completion(prompt) |
| 24 | + |
| 25 | + def get_usage_summary(self): |
| 26 | + return UsageSummary( |
| 27 | + model_usage_summaries={ |
| 28 | + "mock-model": ModelUsageSummary( |
| 29 | + total_calls=1, total_input_tokens=10, total_output_tokens=10 |
| 30 | + ) |
| 31 | + } |
| 32 | + ) |
| 33 | + |
| 34 | + def get_last_usage(self): |
| 35 | + return self.get_usage_summary() |
| 36 | + |
| 37 | + |
| 38 | +def main(): |
| 39 | + print("=" * 60) |
| 40 | + print("Daytona REPL Example") |
| 41 | + print("=" * 60) |
| 42 | + |
| 43 | + # Note: Requires DAYTONA_API_KEY environment variable to be set |
| 44 | + # or passed explicitly to DaytonaREPL(api_key="...") |
| 45 | + |
| 46 | + # Example 1: Basic code execution |
| 47 | + print("\n[1] Basic code execution (no LLM handler)") |
| 48 | + print("-" * 40) |
| 49 | + |
| 50 | + try: |
| 51 | + with DaytonaREPL(name="rlm-example") as repl: |
| 52 | + print(f"Daytona sandbox started with ID: {repl.sandbox.id}") |
| 53 | + result = repl.execute_code("x = 1 + 2") |
| 54 | + print("Executed: x = 1 + 2") |
| 55 | + print(f"Locals: {result.locals}") |
| 56 | + |
| 57 | + result = repl.execute_code("print(x * 2)") |
| 58 | + print("Executed: print(x * 2)") |
| 59 | + print(f"Stdout: {result.stdout.strip()}") |
| 60 | + |
| 61 | + result = repl.execute_code("answer = 42") |
| 62 | + result = repl.execute_code('print(FINAL_VAR("answer"))') |
| 63 | + print(f"FINAL_VAR('answer'): {result.stdout.strip()}") |
| 64 | + |
| 65 | + # Example 2: With LLM handler |
| 66 | + print("\n[2] Code execution with LLM handler") |
| 67 | + print("-" * 40) |
| 68 | + |
| 69 | + mock_client = MockLM() |
| 70 | + |
| 71 | + with LMHandler(client=mock_client) as handler: |
| 72 | + print(f"LM Handler started at {handler.address}") |
| 73 | + |
| 74 | + with DaytonaREPL( |
| 75 | + name="rlm-example-handler", |
| 76 | + lm_handler_address=handler.address, |
| 77 | + ) as repl: |
| 78 | + print(f"Daytona sandbox started with ID: {repl.sandbox.id}") |
| 79 | + print(f"Broker URL: {repl.broker_url}") |
| 80 | + |
| 81 | + # Single LLM query |
| 82 | + result = repl.execute_code('response = llm_query("What is 2+2?")') |
| 83 | + print("Executed: response = llm_query('What is 2+2?')") |
| 84 | + if result.stderr: |
| 85 | + print(f"Stderr: {result.stderr}") |
| 86 | + |
| 87 | + result = repl.execute_code("print(response)") |
| 88 | + print(f"Response: {result.stdout.strip()}") |
| 89 | + |
| 90 | + # Batched LLM query |
| 91 | + result = repl.execute_code( |
| 92 | + 'responses = llm_query_batched(["Question 1", "Question 2", "Question 3"])' |
| 93 | + ) |
| 94 | + print("\nExecuted: responses = llm_query_batched([...])") |
| 95 | + |
| 96 | + result = repl.execute_code("print(f'Got {len(responses)} responses')") |
| 97 | + print(f"Result: {result.stdout.strip()}") |
| 98 | + |
| 99 | + result = repl.execute_code("print(responses[0])") |
| 100 | + print(f"First response: {result.stdout.strip()}") |
| 101 | + |
| 102 | + except Exception as e: |
| 103 | + print(f"Error: {e}") |
| 104 | + print("\nMake sure Daytona is configured correctly and DAYTONA_API_KEY is set.") |
| 105 | + |
| 106 | + print("\n" + "=" * 60) |
| 107 | + print("Done!") |
| 108 | + print("=" * 60) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
0 commit comments