Update codex/code_generator.py
Browse files- codex/code_generator.py +16 -10
codex/code_generator.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
HF_API = "https://api-inference.huggingface.co/
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
|
|
|
|
|
|
| 7 |
def generate_code(task):
|
| 8 |
if not HF_TOKEN:
|
| 9 |
raise RuntimeError("HF_TOKEN not set")
|
| 10 |
|
| 11 |
-
headers = {
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
prompt = f"""
|
| 14 |
You are a senior Python engineer.
|
|
@@ -28,20 +33,21 @@ Rules:
|
|
| 28 |
"""
|
| 29 |
|
| 30 |
payload = {
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
|
| 36 |
-
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
| 40 |
r.raise_for_status()
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
# Strip markdown if present
|
| 45 |
if "```" in text:
|
| 46 |
text = text.split("```")[-1]
|
| 47 |
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
HF_API = "https://api-inference.huggingface.co/v1/chat/completions"
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
+
MODEL = "meta-llama/Llama-3.1-8B-Instruct" # Stable + free-tier friendly
|
| 8 |
+
|
| 9 |
def generate_code(task):
|
| 10 |
if not HF_TOKEN:
|
| 11 |
raise RuntimeError("HF_TOKEN not set")
|
| 12 |
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
|
| 18 |
prompt = f"""
|
| 19 |
You are a senior Python engineer.
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
payload = {
|
| 36 |
+
"model": MODEL,
|
| 37 |
+
"messages": [
|
| 38 |
+
{"role": "system", "content": "You generate safe Python code only."},
|
| 39 |
+
{"role": "user", "content": prompt}
|
| 40 |
+
],
|
| 41 |
+
"max_tokens": 300,
|
| 42 |
+
"temperature": 0.2
|
| 43 |
}
|
| 44 |
|
| 45 |
r = requests.post(HF_API, headers=headers, json=payload, timeout=60)
|
| 46 |
r.raise_for_status()
|
| 47 |
|
| 48 |
+
data = r.json()
|
| 49 |
+
text = data["choices"][0]["message"]["content"]
|
| 50 |
|
|
|
|
| 51 |
if "```" in text:
|
| 52 |
text = text.split("```")[-1]
|
| 53 |
|