-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathorchestrator.py
More file actions
217 lines (177 loc) · 8.49 KB
/
Copy pathorchestrator.py
File metadata and controls
217 lines (177 loc) · 8.49 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
import json
import yaml
import time
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import List, Dict, Any
from agent import OpenRouterAgent
class TaskOrchestrator:
def __init__(self, config_path="config.yaml", silent=False):
# Load configuration
with open(config_path, 'r') as f:
self.config = yaml.safe_load(f)
self.num_agents = self.config['orchestrator']['parallel_agents']
self.task_timeout = self.config['orchestrator']['task_timeout']
self.aggregation_strategy = self.config['orchestrator']['aggregation_strategy']
self.silent = silent
# Track agent progress
self.agent_progress = {}
self.agent_results = {}
self.progress_lock = threading.Lock()
def decompose_task(self, user_input: str, num_agents: int) -> List[str]:
"""Use AI to dynamically generate different questions based on user input"""
# Create question generation agent
question_agent = OpenRouterAgent(silent=True)
# Get question generation prompt from config
prompt_template = self.config['orchestrator']['question_generation_prompt']
generation_prompt = prompt_template.format(
user_input=user_input,
num_agents=num_agents
)
# Remove task completion tool to avoid issues
question_agent.tools = [tool for tool in question_agent.tools if tool.get('function', {}).get('name') != 'mark_task_complete']
question_agent.tool_mapping = {name: func for name, func in question_agent.tool_mapping.items() if name != 'mark_task_complete'}
try:
# Get AI-generated questions
response = question_agent.run(generation_prompt)
# Parse JSON response
questions = json.loads(response.strip())
# Validate we got the right number of questions
if len(questions) != num_agents:
raise ValueError(f"Expected {num_agents} questions, got {len(questions)}")
return questions
except (json.JSONDecodeError, ValueError) as e:
# Fallback: create simple variations if AI fails
return [
f"Research comprehensive information about: {user_input}",
f"Analyze and provide insights about: {user_input}",
f"Find alternative perspectives on: {user_input}",
f"Verify and cross-check facts about: {user_input}"
][:num_agents]
def update_agent_progress(self, agent_id: int, status: str, result: str = None):
"""Thread-safe progress tracking"""
with self.progress_lock:
self.agent_progress[agent_id] = status
if result is not None:
self.agent_results[agent_id] = result
def run_agent_parallel(self, agent_id: int, subtask: str) -> Dict[str, Any]:
"""
Run a single agent with the given subtask.
Returns result dictionary with agent_id, status, and response.
"""
try:
self.update_agent_progress(agent_id, "PROCESSING...")
# Use simple agent like in main.py
agent = OpenRouterAgent(silent=True)
start_time = time.time()
response = agent.run(subtask)
execution_time = time.time() - start_time
self.update_agent_progress(agent_id, "COMPLETED", response)
return {
"agent_id": agent_id,
"status": "success",
"response": response,
"execution_time": execution_time
}
except Exception as e:
# Simple error handling
return {
"agent_id": agent_id,
"status": "error",
"response": f"Error: {str(e)}",
"execution_time": 0
}
def aggregate_results(self, agent_results: List[Dict[str, Any]]) -> str:
"""
Combine results from all agents into a comprehensive final answer.
Uses the configured aggregation strategy.
"""
successful_results = [r for r in agent_results if r["status"] == "success"]
if not successful_results:
return "All agents failed to provide results. Please try again."
# Extract responses for aggregation
responses = [r["response"] for r in successful_results]
if self.aggregation_strategy == "consensus":
return self._aggregate_consensus(responses, successful_results)
else:
# Default to consensus
return self._aggregate_consensus(responses, successful_results)
def _aggregate_consensus(self, responses: List[str], _results: List[Dict[str, Any]]) -> str:
"""
Use one final AI call to synthesize all agent responses into a coherent answer.
"""
if len(responses) == 1:
return responses[0]
# Create synthesis agent to combine all responses
synthesis_agent = OpenRouterAgent(silent=True)
# Build agent responses section
agent_responses_text = ""
for i, response in enumerate(responses, 1):
agent_responses_text += f"=== AGENT {i} RESPONSE ===\n{response}\n\n"
# Get synthesis prompt from config and format it
synthesis_prompt_template = self.config['orchestrator']['synthesis_prompt']
synthesis_prompt = synthesis_prompt_template.format(
num_responses=len(responses),
agent_responses=agent_responses_text
)
# Completely remove all tools from synthesis agent to force direct response
synthesis_agent.tools = []
synthesis_agent.tool_mapping = {}
# Get the synthesized response
try:
final_answer = synthesis_agent.run(synthesis_prompt)
return final_answer
except Exception as e:
# Log the error for debugging
print(f"\n🚨 SYNTHESIS FAILED: {str(e)}")
print("📋 Falling back to concatenated responses\n")
# Fallback: if synthesis fails, concatenate responses
combined = []
for i, response in enumerate(responses, 1):
combined.append(f"=== Agent {i} Response ===")
combined.append(response)
combined.append("")
return "\n".join(combined)
def get_progress_status(self) -> Dict[int, str]:
"""Get current progress status for all agents"""
with self.progress_lock:
return self.agent_progress.copy()
def orchestrate(self, user_input: str):
"""
Main orchestration method.
Takes user input, delegates to parallel agents, and returns aggregated result.
"""
# Reset progress tracking
self.agent_progress = {}
self.agent_results = {}
# Decompose task into subtasks
subtasks = self.decompose_task(user_input, self.num_agents)
# Initialize progress tracking
for i in range(self.num_agents):
self.agent_progress[i] = "QUEUED"
# Execute agents in parallel
agent_results = []
with ThreadPoolExecutor(max_workers=self.num_agents) as executor:
# Submit all agent tasks
future_to_agent = {
executor.submit(self.run_agent_parallel, i, subtasks[i]): i
for i in range(self.num_agents)
}
# Collect results as they complete
for future in as_completed(future_to_agent, timeout=self.task_timeout):
try:
result = future.result()
agent_results.append(result)
except Exception as e:
agent_id = future_to_agent[future]
agent_results.append({
"agent_id": agent_id,
"status": "timeout",
"response": f"Agent {agent_id + 1} timed out or failed: {str(e)}",
"execution_time": self.task_timeout
})
# Sort results by agent_id for consistent output
agent_results.sort(key=lambda x: x["agent_id"])
# Aggregate results
final_result = self.aggregate_results(agent_results)
return final_result