Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| from openai import OpenAI | |
| from email_triage_env.env import EmailTriageEnv | |
| from email_triage_env.models import Action | |
| # --- 1. MANDATORY ENVIRONMENT VARIABLES --- | |
| API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1") | |
| MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4-turbo-preview") | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| LOCAL_IMAGE_NAME = os.getenv("LOCAL_IMAGE_NAME") | |
| # --- 2. CONFIGURE CLIENT --- | |
| client = OpenAI( | |
| base_url=API_BASE_URL, | |
| api_key=HF_TOKEN or os.getenv("OPENAI_API_KEY", "dummy-key") | |
| ) | |
| def run_baseline(): | |
| env = EmailTriageEnv() | |
| for task_idx in range(len(env.tasks)): | |
| # Safely get the task name (e.g., 'easy', 'medium') | |
| task_name = env.tasks[task_idx].get('id', f'task_{task_idx}') | |
| # --- REQUIRED LOG: START --- | |
| print(f"[START] task={task_name}", flush=True) | |
| obs = env.reset(task_idx) | |
| done = False | |
| step_counter = 0 | |
| current_reward = 0.0 | |
| messages = [ | |
| {"role": "system", "content": "You are a customer support agent. Goal: " + env.tasks[task_idx]['goal']}, | |
| {"role": "user", "content": f"Initial state: {obs.model_dump_json()}"} | |
| ] | |
| while not done: | |
| step_counter += 1 | |
| try: | |
| response = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| messages=messages, | |
| tools=[{ | |
| "type": "function", | |
| "function": { | |
| "name": "take_action", | |
| "description": "Perform an action on an email.", | |
| "parameters": Action.model_json_schema() | |
| } | |
| }], | |
| tool_choice={"type": "function", "function": {"name": "take_action"}} | |
| ) | |
| tool_call = response.choices[0].message.tool_calls[0] | |
| action_args = json.loads(tool_call.function.arguments) | |
| action = Action(**action_args) | |
| obs, reward, done, info = env.step(action) | |
| current_reward = reward.value | |
| # --- REQUIRED LOG: STEP --- | |
| print(f"[STEP] step={step_counter} reward={current_reward}", flush=True) | |
| messages.append({"role": "assistant", "tool_calls": [tool_call]}) | |
| messages.append({ | |
| "role": "tool", | |
| "tool_call_id": tool_call.id, | |
| "name": "take_action", | |
| "content": f"Observation: {obs.model_dump_json()} | Reward: {current_reward} | Done: {done}" | |
| }) | |
| except Exception as e: | |
| print(f"Caught an unhandled API or parsing exception: {e}", flush=True) | |
| # If the grader forces an error, break the loop safely | |
| break | |
| # Failsafe to prevent infinite loops locally | |
| if step_counter >= 10: | |
| break | |
| # --- THE FIX: Clamp the score strictly between 0 and 1 --- | |
| # If it's 0.0, it becomes 0.01. If it's 1.0, it becomes 0.99. | |
| final_score = max(0.01, min(0.99, float(current_reward))) | |
| # --- REQUIRED LOG: END --- | |
| print(f"[END] task={task_name} score={final_score} steps={step_counter}", flush=True) | |
| if __name__ == "__main__": | |
| run_baseline() |