Spaces:
Paused
Paused
Make the models work in parallel 3
Browse files
crew.py
CHANGED
|
@@ -146,6 +146,8 @@ class GAIACrew():
|
|
| 146 |
|
| 147 |
import concurrent.futures
|
| 148 |
|
|
|
|
|
|
|
| 149 |
def run_parallel_crew(question: str, file_path: str):
|
| 150 |
"""
|
| 151 |
1) Prepares the prompt (including file data if any).
|
|
@@ -164,19 +166,23 @@ def run_parallel_crew(question: str, file_path: str):
|
|
| 164 |
else:
|
| 165 |
final_question = f"{question} File path: {file_path}."
|
| 166 |
|
| 167 |
-
# 2) Instantiate your crew and split manager vs workers
|
| 168 |
crew_instance = GAIACrew()
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# 3) Run workers in parallel
|
| 174 |
inputs = {"question": final_question}
|
| 175 |
results = {}
|
| 176 |
with concurrent.futures.ThreadPoolExecutor(max_workers=len(workers)) as pool:
|
| 177 |
futures = {
|
| 178 |
-
pool.submit(lambda ag: ag.kickoff(inputs), ag):
|
| 179 |
-
for ag in
|
| 180 |
}
|
| 181 |
for fut in concurrent.futures.as_completed(futures):
|
| 182 |
name = futures[fut]
|
|
@@ -186,8 +192,8 @@ def run_parallel_crew(question: str, file_path: str):
|
|
| 186 |
results[name] = f"<error: {e}>"
|
| 187 |
|
| 188 |
# 4) Compose a manager prompt that includes all worker outputs
|
| 189 |
-
combined = "\n\n".join(f"--- {
|
| 190 |
-
for
|
| 191 |
manager_prompt = (
|
| 192 |
f"You have received these reports from your coworkers:\n\n"
|
| 193 |
f"{combined}\n\n"
|
|
@@ -195,11 +201,12 @@ def run_parallel_crew(question: str, file_path: str):
|
|
| 195 |
f"Original question: {question}"
|
| 196 |
)
|
| 197 |
|
| 198 |
-
# 5) Run the manager
|
| 199 |
final = manager.kickoff(inputs={"question": manager_prompt})
|
| 200 |
return get_final_answer(FINAL_ANSWER_MODEL, question, str(final))
|
| 201 |
|
| 202 |
|
|
|
|
| 203 |
def get_final_answer(model, question, answer):
|
| 204 |
prompt_template = """
|
| 205 |
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|
|
|
|
| 146 |
|
| 147 |
import concurrent.futures
|
| 148 |
|
| 149 |
+
import concurrent.futures
|
| 150 |
+
|
| 151 |
def run_parallel_crew(question: str, file_path: str):
|
| 152 |
"""
|
| 153 |
1) Prepares the prompt (including file data if any).
|
|
|
|
| 166 |
else:
|
| 167 |
final_question = f"{question} File path: {file_path}."
|
| 168 |
|
| 169 |
+
# 2) Instantiate your crew and split manager vs workers by zipping names → agents
|
| 170 |
crew_instance = GAIACrew()
|
| 171 |
+
names = list(crew_instance.agents_config.keys())
|
| 172 |
+
agents = crew_instance.agents
|
| 173 |
+
|
| 174 |
+
# Build (name, agent) pairs
|
| 175 |
+
pairs = list(zip(names, agents))
|
| 176 |
+
workers = [agent for name, agent in pairs if name != "manager_agent"]
|
| 177 |
+
manager = next(agent for name, agent in pairs if name == "manager_agent")
|
| 178 |
|
| 179 |
# 3) Run workers in parallel
|
| 180 |
inputs = {"question": final_question}
|
| 181 |
results = {}
|
| 182 |
with concurrent.futures.ThreadPoolExecutor(max_workers=len(workers)) as pool:
|
| 183 |
futures = {
|
| 184 |
+
pool.submit(lambda ag: ag.kickoff(inputs), ag): name
|
| 185 |
+
for name, ag in pairs if name != "manager_agent"
|
| 186 |
}
|
| 187 |
for fut in concurrent.futures.as_completed(futures):
|
| 188 |
name = futures[fut]
|
|
|
|
| 192 |
results[name] = f"<error: {e}>"
|
| 193 |
|
| 194 |
# 4) Compose a manager prompt that includes all worker outputs
|
| 195 |
+
combined = "\n\n".join(f"--- {n} output ---\n{out}"
|
| 196 |
+
for n, out in results.items())
|
| 197 |
manager_prompt = (
|
| 198 |
f"You have received these reports from your coworkers:\n\n"
|
| 199 |
f"{combined}\n\n"
|
|
|
|
| 201 |
f"Original question: {question}"
|
| 202 |
)
|
| 203 |
|
| 204 |
+
# 5) Run the manager for the final answer
|
| 205 |
final = manager.kickoff(inputs={"question": manager_prompt})
|
| 206 |
return get_final_answer(FINAL_ANSWER_MODEL, question, str(final))
|
| 207 |
|
| 208 |
|
| 209 |
+
|
| 210 |
def get_final_answer(model, question, answer):
|
| 211 |
prompt_template = """
|
| 212 |
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|