Skip to main content

What You’ll Learn

  • Sending prompts to Groq’s OpenAI-compatible API (llama-3.1-8b-instant) for ultra-fast code generation
  • Stripping markdown code fences from LLM responses before execution
  • Writing generated code into a Declaw sandbox filesystem
  • Executing the code securely with sbx.commands.run()
  • Graceful demo mode when no API key is configured

Prerequisites

  • Declaw instance running and DECLAW_API_KEY / DECLAW_DOMAIN set
  • GROQ_API_KEY (optional — the example runs in demo mode without it)
pip install declaw python-dotenv groq

Code Walkthrough

This example is available in Python. TypeScript support coming soon.

1. Ask Groq to generate Python code

The Groq SDK follows the OpenAI client interface exactly — swap in Groq() and choose a model:
from groq import Groq
from declaw import Sandbox

client = Groq()

response = client.chat.completions.create(
    model="llama-3.1-8b-instant",
    messages=[
        {
            "role": "system",
            "content": (
                "You are a Python code interpreter. When asked a question, "
                "respond ONLY with Python code that computes the answer and "
                "prints it. No markdown, no explanation, just code."
            ),
        },
        {
            "role": "user",
            "content": "Compute basic statistics for the list [23, 45, 12, 67, 34, 89, 11, 56, 78, 90]",
        },
    ],
    temperature=0,
)
Groq’s hosted inference is significantly faster than most cloud LLM providers. llama-3.1-8b-instant typically responds in under 200 ms, making it a good choice for high-throughput code-generation pipelines.

2. Strip code fences and execute in a sandbox

def strip_code_fences(code: str) -> str:
    code = code.strip()
    if code.startswith("```"):
        code = "\n".join(code.split("\n")[1:])
    if code.endswith("```"):
        code = "\n".join(code.split("\n")[:-1])
    return code.strip()

code = strip_code_fences(response.choices[0].message.content or "")

sbx = Sandbox.create(template="python", timeout=300)
try:
    sbx.files.write("/tmp/solution.py", code)
    result = sbx.commands.run("python3 /tmp/solution.py", timeout=30)
    print(result.stdout)
    print(result.exit_code)
finally:
    sbx.kill()

3. Demo mode (no API key needed)

code = """\
import statistics

data = [23, 45, 12, 67, 34, 89, 11, 56, 78, 90]
print("Data:", data)
print("Mean:", statistics.mean(data))
print("Median:", statistics.median(data))
print("Std Dev:", round(statistics.stdev(data), 2))
print("Sorted:", sorted(data))
"""

sbx = Sandbox.create(template="python", timeout=300)
try:
    sbx.files.write("/tmp/demo.py", code)
    result = sbx.commands.run("python3 /tmp/demo.py", timeout=30)
    print(result.stdout)
finally:
    sbx.kill()

Expected Output

============================================================
Groq Code Interpreter with Declaw Sandbox
============================================================

--- Demo: Running pre-written code in Declaw sandbox ---

Sandbox created: sbx_abc123
  Output:
Data: [23, 45, 12, 67, 34, 89, 11, 56, 78, 90]
Mean: 50.5
Median: 50.5
Std Dev: 28.14
Sorted: [11, 12, 23, 34, 45, 56, 67, 78, 89, 90]

Sandbox killed.