File size: 1,239 Bytes
65ada78 0443cfd 65ada78 647b435 4cbc4df 65ada78 0443cfd 533fdfe 4cbc4df 0443cfd 4cbc4df 19dd01b 65ada78 e6a8676 65ada78 533fdfe e6a8676 533fdfe 65ada78 e6a8676 4cbc4df 15e2b3b e6a8676 0443cfd 15e2b3b 533fdfe 4cbc4df 19dd01b e6a8676 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import os
import requests
GROQ_API = "https://api.groq.com/openai/v1/chat/completions"
GROQ_KEY = os.environ.get("GROQ_API_KEY")
MODEL = "llama-3.1-8b-instant" # 🔥 LIVE + SUPPORTED
def generate_code(task):
if not GROQ_KEY:
raise RuntimeError("GROQ_API_KEY not set")
headers = {
"Authorization": f"Bearer {GROQ_KEY}",
"Content-Type": "application/json"
}
prompt = f"""
You are a senior Python engineer.
Write safe Python code to accomplish this task:
{task}
Rules:
- No OS commands
- No file deletion
- No network calls
- Must define a function
- Must assign final output to variable named `result`
- Must not use input()
- Return only Python code
"""
payload = {
"model": MODEL,
"messages": [
{"role": "user", "content": prompt}
],
"max_tokens": 300,
"temperature": 0.2,
"top_p": 1.0
}
r = requests.post(GROQ_API, headers=headers, json=payload, timeout=60)
if r.status_code != 200:
raise RuntimeError(f"Groq error {r.status_code}: {r.text}")
data = r.json()
text = data["choices"][0]["message"]["content"]
if "```" in text:
text = text.split("```")[-1]
return text.strip() |