Spaces:
Runtime error
Runtime error
File size: 3,757 Bytes
fe64236 | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | """
inference.py
====================================
Entry point for Meta PyTorch OpenEnv Hackathon.
REQUIRED LOG FORMAT:
[START]
[STEP]
[END]
"""
import os
import json
import traceback
from openai import OpenAI
from pydebug_optimizer.env import PyDebugOptimizerEnv
from pydebug_optimizer.models import Action
from pydebug_optimizer.utils import parse_agent_response
# ============================================================
# ๐ง ENV CONFIG
# ============================================================
API_BASE_URL = os.getenv("API_BASE_URL")
MODEL_NAME = os.getenv("MODEL_NAME")
HF_TOKEN = os.getenv("HF_TOKEN")
# ============================================================
# ๐ค LLM CALL
# ============================================================
def query_llm(prompt: str) -> dict:
"""
Sends prompt to LLM and returns parsed JSON response.
"""
try:
client = OpenAI(
base_url=API_BASE_URL,
api_key=HF_TOKEN,
)
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{
"role": "system",
"content": (
"You are a senior Python engineer. "
"Return ONLY valid JSON with fields:\n"
"error_type, error_justification, fixed_code, "
"fix_justification, optimized_code, complexity_justification"
),
},
{"role": "user", "content": prompt},
],
temperature=0.2,
)
raw_output = response.choices[0].message.content
parsed = parse_agent_response(raw_output)
return parsed
except Exception as e:
print("LLM ERROR:", str(e))
return {}
# ============================================================
# ๐ MAIN LOOP
# ============================================================
def main():
print("[START]")
try:
# ----------------------------------------------------
# INIT ENV
# ----------------------------------------------------
env = PyDebugOptimizerEnv()
obs, _ = env.reset()
prompt = f"""
You are given buggy Python code.
TASK:
{obs['task_description']}
CODE:
{obs['code_snippet']}
Respond ONLY in JSON with:
- error_type
- error_justification
- fixed_code
- fix_justification
- optimized_code
- complexity_justification
"""
# ----------------------------------------------------
# QUERY LLM
# ----------------------------------------------------
action_dict = query_llm(prompt)
if not action_dict:
raise ValueError("Empty or invalid LLM response")
# ----------------------------------------------------
# VALIDATE ACTION
# ----------------------------------------------------
action = Action(**action_dict)
# ----------------------------------------------------
# STEP ENV
# ----------------------------------------------------
obs, reward, terminated, truncated, info = env.step(action.model_dump())
print("[STEP]")
print("Reward:", reward)
print("Info:", info)
except Exception as e:
print("[STEP]")
print("ERROR:", str(e))
print(traceback.format_exc())
finally:
print("[END]")
# ============================================================
# ENTRY
# ============================================================
if __name__ == "__main__":
main() |