File size: 3,178 Bytes
625b444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, sys, requests, json, time
from openai import OpenAI

ENV_BASE_URL = "http://localhost:8000"

def play_round(round_number):
    print(f"\n{'='*50}\n🏁 ROUND {round_number} STARTS!\n{'='*50}")
    
    # 1. Get Task safely
    try:
        resp = requests.post(f"{ENV_BASE_URL}/reset", timeout=120).json()
    except Exception as e:
        sys.exit(f"🚨 Error: Cannot connect to OpenEnv Server. {e}")

    task = ""
    if isinstance(resp, dict):
        if "observation" in resp and isinstance(resp["observation"], dict) and "echoed_message" in resp["observation"]:
            task = resp["observation"]["echoed_message"]
        elif "observation" in resp and isinstance(resp["observation"], dict) and "task_prompt" in resp["observation"]:
             task = resp["observation"]["task_prompt"]
        elif "echoed_message" in resp:
            task = resp["echoed_message"]
        else:
            task = json.dumps(resp)
    else:
        task = str(resp)
        
    print(f"🔥 JUDGE ASKS:\n{task}\n")

    # 2. Qwen Agent API Call (Working on HF Router)
    print("🤖 Agent is thinking (Using Qwen 2.5)...")
    hf_token = os.environ.get("HF_TOKEN", "")
    if not hf_token:
        print("🚨 WARNING: HF_TOKEN is missing. Please set it in your environment variables.")
        
    client = OpenAI(
        base_url="https://router.huggingface.co/v1",
        api_key=hf_token
    )
    
    try:
        completion = client.chat.completions.create(
            model="Qwen/Qwen2.5-72B-Instruct",
            messages=[
                {"role": "system", "content": "You are a Python expert. Output ONLY valid Python code. No explanations, no markdown blocks like ```python."},
                {"role": "user", "content": task}
            ],
        )
        agent_answer = completion.choices[0].message.content.replace("```python", "").replace("```", "").strip()
    except Exception as e:
        print(f"🚨 HF API Error: {e}")
        agent_answer = "def generic_answer(): pass"

    print(f"🗣️ AGENT'S ANSWER (Snippet):\n{agent_answer[:150]}...\n")
    
    # 3. Submit to Server (Direct Payload)
    print("⚖️ Submitting to Judge...")
    payload = {"action": {"answer": agent_answer}}
    try:
        step_resp = requests.post(f"{ENV_BASE_URL}/step", json=payload, timeout=120)
        
        if step_resp.status_code == 200:
            result = step_resp.json()
            score = result.get("observation", {}).get("reward", result.get("reward", 0.0))
        else:
            print(f"🚨 Server Error! Status: {step_resp.status_code}")
            print(f"🚨 Details: {step_resp.text}")
            score = 0.0
             
    except Exception as e:
        print(f"🚨 Server Communication Error: {e}")
        score = 0.0
        
    print(f"🏆 ROUND {round_number} SCORE : {score} / 1.0")
    return score

def main():
    print("🚀 [START] GEMMA AGENT vs OPENAI JUDGE")
    total_score = 0
    for i in range(1, 4):
        total_score += play_round(i)
        time.sleep(2) 
    print(f"\n🎉🎉 MATCH FINISHED! FINAL TOTAL SCORE: {total_score} / 3.0 🎉🎉")

if __name__ == "__main__":
    main()