forked from microsoft/AIOpsLab
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenrouter.py
More file actions
222 lines (176 loc) · 8.02 KB
/
openrouter.py
File metadata and controls
222 lines (176 loc) · 8.02 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""OpenRouter client (with shell access) for AIOpsLab.
OpenRouter provides access to multiple AI models through a unified API.
More info: https://openrouter.ai/
"""
import os
import asyncio
import tiktoken
import wandb
import argparse
import json
from pathlib import Path
from aiopslab.orchestrator import Orchestrator
from aiopslab.orchestrator.problems.registry import ProblemRegistry
from clients.utils.llm import OpenRouterClient
from clients.utils.templates import DOCS_SHELL_ONLY
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
def count_message_tokens(message, enc):
# Each message format adds ~4 tokens of overhead
tokens = 4 # <|start|>role/name + content + <|end|>
tokens += len(enc.encode(message.get("content", "")))
return tokens
def trim_history_to_token_limit(history, max_tokens=120000, model="gpt-4"):
enc = tiktoken.encoding_for_model(model)
trimmed = []
total_tokens = 0
# Always include the last message
last_msg = history[-1]
last_msg_tokens = count_message_tokens(last_msg, enc)
if last_msg_tokens > max_tokens:
# If even the last message is too big, truncate its content
truncated_content = enc.decode(enc.encode(last_msg["content"])[:max_tokens - 4])
return [{"role": last_msg["role"], "content": truncated_content}]
trimmed.insert(0, last_msg)
total_tokens += last_msg_tokens
# Add earlier messages in reverse until limit is reached
for message in reversed(history[:-1]):
message_tokens = count_message_tokens(message, enc)
if total_tokens + message_tokens > max_tokens:
break
trimmed.insert(0, message)
total_tokens += message_tokens
return trimmed
class OpenRouterAgent:
def __init__(self, model="anthropic/claude-3.5-sonnet"):
self.history = []
self.llm = OpenRouterClient(model=model)
self.model = model
def test(self):
return self.llm.run([{"role": "system", "content": "hello"}])
def init_context(self, problem_desc: str, instructions: str, apis: str):
"""Initialize the context for the agent."""
self.shell_api = self._filter_dict(apis, lambda k, _: "exec_shell" in k)
self.submit_api = self._filter_dict(apis, lambda k, _: "submit" in k)
stringify_apis = lambda apis: "\n\n".join(
[f"{k}\n{v}" for k, v in apis.items()]
)
self.system_message = DOCS_SHELL_ONLY.format(
prob_desc=problem_desc,
shell_api=stringify_apis(self.shell_api),
submit_api=stringify_apis(self.submit_api),
)
self.task_message = instructions
self.history.append({"role": "system", "content": self.system_message})
self.history.append({"role": "user", "content": self.task_message})
async def get_action(self, input) -> str:
"""Wrapper to interface the agent with AIOpsLab.
Args:
input (str): The input from the orchestrator/environment.
Returns:
str: The response from the agent.
"""
self.history.append({"role": "user", "content": input})
try:
trimmed_history = trim_history_to_token_limit(self.history)
response = self.llm.run(trimmed_history)
print(f"===== Agent (OpenRouter - {self.model}) ====\n{response[0]}")
self.history.append({"role": "assistant", "content": response[0]})
return response[0]
except Exception as e:
print(f"OpenRouter API error: {e}")
# Return a fallback response
fallback_response = f"Error occurred while calling OpenRouter API: {e}"
self.history.append({"role": "assistant", "content": fallback_response})
return fallback_response
def _filter_dict(self, dictionary, filter_func):
return {k: v for k, v in dictionary.items() if filter_func(k, v)}
def get_completed_problems(results_dir: Path, agent_name: str, model: str) -> set:
"""Get set of completed problem IDs from existing result files."""
completed = set()
# Look in organized directory structure first
organized_dir = results_dir / agent_name / model.replace("/", "_")
if organized_dir.exists():
for result_file in organized_dir.glob("*.json"):
try:
with open(result_file, 'r') as f:
data = json.load(f)
if 'problem_id' in data:
completed.add(data['problem_id'])
except (json.JSONDecodeError, IOError):
continue
# Also check legacy flat structure
for result_file in results_dir.glob("*.json"):
try:
with open(result_file, 'r') as f:
data = json.load(f)
if ('problem_id' in data and
data.get('agent') == agent_name and
model.split('/')[-1] in str(result_file)):
completed.add(data['problem_id'])
except (json.JSONDecodeError, IOError):
continue
return completed
def setup_results_directory(model: str, agent_name: str = "openrouter") -> Path:
"""Setup organized results directory structure."""
results_base = Path("aiopslab/data/results")
# Create organized structure: results/{agent}/{model_safe}/
model_safe = model.replace("/", "_")
results_dir = results_base / agent_name / model_safe
results_dir.mkdir(parents=True, exist_ok=True)
return results_dir
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run OpenRouter agent on AIOpsLab problems')
parser.add_argument('--skip-completed', action='store_true',
help='Skip problems that have already been completed')
parser.add_argument('--problem-ids', nargs='+',
help='Run only specific problem IDs')
parser.add_argument('--max-steps', type=int, default=30,
help='Maximum steps per problem (default: 30)')
parser.add_argument('--model', type=str,
default=os.getenv("OPENROUTER_MODEL", "openai/gpt-4o-mini"),
help='OpenRouter model to use')
args = parser.parse_args()
# Load use_wandb from environment variable with a default of False
use_wandb = os.getenv("USE_WANDB", "false").lower() == "true"
if use_wandb:
# Initialize wandb running
wandb.init(project="AIOpsLab", entity="AIOpsLab")
model = args.model
agent_name = "openrouter"
# Setup organized results directory
results_dir = setup_results_directory(model, agent_name)
print(f"Results will be saved to: {results_dir}")
# Get all problems
problems = ProblemRegistry().PROBLEM_REGISTRY
# Filter problems if specific IDs requested
if args.problem_ids:
problems = {pid: problems[pid] for pid in args.problem_ids if pid in problems}
if not problems:
print("No valid problem IDs found")
exit(1)
# Skip completed problems if requested
if args.skip_completed:
completed_problems = get_completed_problems(
Path("aiopslab/data/results"), agent_name, model
)
problems = {pid: prob for pid, prob in problems.items()
if pid not in completed_problems}
print(f"Found {len(completed_problems)} completed problems")
print(f"Running {len(problems)} remaining problems")
if not problems:
print("All problems have been completed!")
exit(0)
print(f"Running {len(problems)} problems with model: {model}")
for pid in problems:
print(f"\n=== Starting problem: {pid} ===")
agent = OpenRouterAgent(model=model)
orchestrator = Orchestrator(results_dir=results_dir)
orchestrator.register_agent(agent, name=agent_name)
problem_desc, instructs, apis = orchestrator.init_problem(pid)
agent.init_context(problem_desc, instructs, apis)
asyncio.run(orchestrator.start_problem(max_steps=args.max_steps))
if use_wandb:
# Finish the wandb run
wandb.finish()