|
| 1 | +""" |
| 2 | +Example: Using llm_query() from within a Prime Intellect Sandbox. |
| 3 | +
|
| 4 | +This demonstrates the LM Handler + PrimeREPL integration where code |
| 5 | +running in a cloud sandbox can query the LLM via the HTTP broker pattern. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +from dotenv import load_dotenv |
| 11 | + |
| 12 | +from rlm.clients.portkey import PortkeyClient |
| 13 | +from rlm.core.lm_handler import LMHandler |
| 14 | +from rlm.environments.prime_repl import PrimeREPL |
| 15 | + |
| 16 | +load_dotenv() |
| 17 | + |
| 18 | +setup_code = """ |
| 19 | +secret = "1424424" |
| 20 | +""" |
| 21 | + |
| 22 | +context_payload = """ |
| 23 | +This is a test context. It should print out, revealing the magic number to be 4. |
| 24 | +""" |
| 25 | + |
| 26 | +code = """ |
| 27 | +response = llm_query("What is 2 + 2? Reply with just the number.") |
| 28 | +print(response) |
| 29 | +print(type(response)) |
| 30 | +print(context) |
| 31 | +print("Secret from setup code: ", secret) |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + api_key = os.environ.get("PORTKEY_API_KEY") |
| 37 | + if not api_key: |
| 38 | + print("Error: PORTKEY_API_KEY not set") |
| 39 | + return |
| 40 | + print(f"PORTKEY_API_KEY: {api_key[:8]}...") |
| 41 | + |
| 42 | + client = PortkeyClient(api_key=api_key, model_name="@openai/gpt-5-nano") |
| 43 | + print("Created Portkey client with model: @openai/gpt-5-nano") |
| 44 | + |
| 45 | + # Start LM Handler |
| 46 | + with LMHandler(client=client) as handler: |
| 47 | + print(f"LM Handler started at {handler.address}") |
| 48 | + |
| 49 | + # Create Prime REPL with handler connection |
| 50 | + print("\nCreating Prime sandbox...") |
| 51 | + with PrimeREPL( |
| 52 | + name="rlm-lm-demo", |
| 53 | + docker_image="python:3.11-slim", |
| 54 | + timeout_minutes=30, |
| 55 | + lm_handler_address=handler.address, |
| 56 | + context_payload=context_payload, |
| 57 | + setup_code=setup_code, |
| 58 | + ) as repl: |
| 59 | + print(f"PrimeREPL created, sandbox ID: {repl.sandbox_id}") |
| 60 | + print(f"Broker URL: {repl.broker_url}\n") |
| 61 | + |
| 62 | + # Run code that uses llm_query |
| 63 | + print(f"Executing: {code}") |
| 64 | + |
| 65 | + result = repl.execute_code(code) |
| 66 | + |
| 67 | + print(f"stdout: {result.stdout!r}") |
| 68 | + print(f"stderr: {result.stderr!r}") |
| 69 | + print(f"response variable: {result.locals.get('response')!r}") |
| 70 | + print(f"locals: {result.locals!r}") |
| 71 | + print(f"execution time: {result.execution_time:.3f}s") |
| 72 | + print(f"rlm_calls made: {len(result.rlm_calls)}") |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + main() |
0 commit comments