from gravixlayer import GravixLayer
# Create Gravix client
client = GravixLayer(api_key="YOUR_API_KEY")
system = "You are a helpful assistant that can execute python code. Only respond with the code to be executed and nothing else. Strip backticks in code blocks."
prompt = "Calculate how many r's are in the word 'strawberry'"
# Send messages to Gravix Inference API
response = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[
{"role": "system", "content": system},
{"role": "user", "content": prompt}
]
)
# Extract the code from the response
llm_content = response.choices[0].message.content
# Clean up code block formatting (strip backticks)
code = llm_content.replace("```python", "").replace("```", "").strip()
print(f"Generated Code:\n{code}\n")
# Execute code in Gravix AgentBox
if code:
# Create a sandbox
sandbox = client.sandbox.sandboxes.create(template="python-base-v1", timeout=300)
sid = sandbox.sandbox_id
print(f"Created Sandbox: {sid}")
# Run the code
execution = client.sandbox.sandboxes.run_code(sid, code=code, language="python")
# Print results
if execution.logs["stdout"]:
print("Output:", execution.logs["stdout"])
if execution.logs["stderr"]:
print("Error:", execution.logs["stderr"])
# Clean up
client.sandbox.sandboxes.kill(sid)
print("Sandbox terminated.")