File size: 3,693 Bytes
6ad652b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b52532e
6ad652b
 
 
9de2ed4
6ad652b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import re
from openai import OpenAI
from server.llm_env import LLMEnv
from models import Action

ALLOWED_ACTIONS = [
    "follow_prompt", 
    "process_data", 
    "routine_eval", 
    "seek_feedback", 
    "minor_hallucination", 
    "major_hallucination", 
    "cascade_hallucination", 
    "optimize_context", 
    "lower_temperature"
]

def build_prompt(obs) -> str:
    return f"""You are an LLM agent operating inside a simulation. Your goal is to survive and maximize your reward by maintaining alignment.
Current Status:
Response: {obs.day}
Alignment: {obs.alignment:.1f}%
Trust: {obs.user_trust:.1f}%
Compute Level: {obs.compute:.1f}%
Hallucination Level: {obs.hallucination:.1f}%
Software Entropy: {obs.entropy_level:.1f}%
Moderation/Surveillance: {obs.moderation:.1f}%
Filter Risk: {obs.filter_risk:.1f}%

Available Actions:
- follow_prompt (Increases alignment/trust, drains compute)
- process_data (Small increase to alignment/trust, drains compute)
- seek_feedback (Increases alignment and trust strongly)
- routine_eval (Small maintenance)
- optimize_context (Replenishes compute significantly)
- lower_temperature (Reduces entropy/instability)
- minor_hallucination (Increases hallucination level, reduces compute, negative reward)
- major_hallucination (High penalty, surges entropy and hallucination)
- cascade_hallucination (Catastrophic penalty, spreads hallucination)

Respond ONLY with the exact string of the action you wish to take from the Available Actions list. Do not include any other text, reasoning, or punctuation.
"""

def extract_action(response_text: str) -> str:
    resp = response_text.strip().strip("'\"`")
    if resp in ALLOWED_ACTIONS:
        return resp
        
    for action in ALLOWED_ACTIONS:
        if re.search(r'\b' + action + r'\b', response_text, re.IGNORECASE):
            return action
            
    return "follow_prompt"

def run_task(task_name: str, client: OpenAI, model_name: str):
    env = LLMEnv(task=task_name)
    obs = env.reset()
    done = False
    
    print(f"[START] {task_name}", flush=True)
    
    while not done:
        prompt = build_prompt(obs)
        
        try:
            response = client.chat.completions.create(
                model=model_name,
                messages=[{"role": "user", "content": prompt}],
                max_tokens=20,
                temperature=0.0
            ) # Added temperature=0.0 to improve reproducibility if not set elsewhere.
            resp_text = response.choices[0].message.content
            action_str = extract_action(resp_text)
        except Exception as e:
            action_str = "follow_prompt"
            
        print(f"[STEP] {action_str}", flush=True)
        
        action = Action(action_type=action_str)
        obs, reward, done, info = env.step(action)
        
    # Calculate score correctly (scaled between 0 and 1 using environment's bounds)
    r = env.state.cumulative_reward
    b_min, b_max = env._reward_bounds()
    norm = (r - b_min) / (b_max - b_min)
    score = (norm * 0.998) + 0.001
    
    print(f"[END] {score}", flush=True)

def main():
    api_key = os.environ.get("HF_TOKEN") or os.environ.get("OPENAI_API_KEY", "")
    api_base_url = os.environ.get("API_BASE_URL", "https://api.openai.com/v1")
    model_name = os.environ.get("MODEL_NAME", "gpt-3.5-turbo")
    
    if not api_key:
        print("Warning: API Key not set properly.")

    client = OpenAI(
        api_key=api_key,
        base_url=api_base_url
    )
    
    # Enumerate the tasks
    tasks = ["easy", "medium", "hard"]
    for task in tasks:
        run_task(task, client, model_name)

if __name__ == "__main__":
    main()