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|
|
| 1 |
+
from smolagents import (
|
| 2 |
+
CodeAgent,
|
| 3 |
+
VisitWebpageTool,
|
| 4 |
+
WebSearchTool,
|
| 5 |
+
WikipediaSearchTool,
|
| 6 |
+
PythonInterpreterTool,
|
| 7 |
+
FinalAnswerTool,
|
| 8 |
+
LiteLLMModel,
|
| 9 |
+
)
|
| 10 |
+
from vision_tool import image_reasoning_tool
|
| 11 |
+
from throttle import consume
|
| 12 |
+
import os
|
| 13 |
+
import time
|
| 14 |
+
|
| 15 |
+
# ---- TOOLS ----
|
| 16 |
+
|
| 17 |
+
common = dict(
|
| 18 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 19 |
+
api_base="https://api.groq.com/openai/v1",
|
| 20 |
+
flatten_messages_as_text=True,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# ---- MULTI-AGENT SYSTEM ----
|
| 26 |
+
class MultyAgentSystem:
|
| 27 |
+
def __init__(self):
|
| 28 |
+
self.deepseek_model = LiteLLMModel(
|
| 29 |
+
"groq/deepseek-r1-distill-llama-70b",
|
| 30 |
+
max_tokens=512,
|
| 31 |
+
**common,
|
| 32 |
+
)
|
| 33 |
+
self.qwen_model = LiteLLMModel("groq/qwen-qwq-32b", **common)
|
| 34 |
+
self.fallback_model = LiteLLMModel("groq/llama3-70b-8k", **common)
|
| 35 |
+
|
| 36 |
+
self.verification_limit = int(os.getenv("VERIFY_WORD_LIMIT", "75"))
|
| 37 |
+
|
| 38 |
+
# --- Web agent definition ---
|
| 39 |
+
self.web_agent = CodeAgent(
|
| 40 |
+
model=self.qwen_model,
|
| 41 |
+
tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
|
| 42 |
+
name="web_agent",
|
| 43 |
+
description=(
|
| 44 |
+
"You are a web browsing agent. Whenever the given {task} involves browsing "
|
| 45 |
+
"the web or a specific website such as Wikipedia or YouTube, you will use "
|
| 46 |
+
"the provided tools. For web-based factual and retrieval tasks, be as precise and source-reliable as possible."
|
| 47 |
+
),
|
| 48 |
+
additional_authorized_imports=[
|
| 49 |
+
"markdownify",
|
| 50 |
+
"json",
|
| 51 |
+
"requests",
|
| 52 |
+
"urllib.request",
|
| 53 |
+
"urllib.parse",
|
| 54 |
+
"wikipedia-api",
|
| 55 |
+
],
|
| 56 |
+
verbosity_level=0,
|
| 57 |
+
max_steps=10,
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# --- Info agent definition ---
|
| 61 |
+
self.info_agent = CodeAgent(
|
| 62 |
+
model=self.qwen_model,
|
| 63 |
+
tools=[PythonInterpreterTool(), image_reasoning_tool],
|
| 64 |
+
name="info_agent",
|
| 65 |
+
description=(
|
| 66 |
+
"You are an agent tasked with cleaning, parsing, calculating information, and performing OCR if images are provided in the {task}. "
|
| 67 |
+
"You can also analyze images using a vision model. You handle all math, code, and data manipulation. Use numpy, math, and available libraries. "
|
| 68 |
+
"For image or chess tasks, use pytesseract, PIL, chess, or the image_reasoning_tool as required."
|
| 69 |
+
),
|
| 70 |
+
additional_authorized_imports=[
|
| 71 |
+
"numpy",
|
| 72 |
+
"math",
|
| 73 |
+
"pytesseract",
|
| 74 |
+
"PIL",
|
| 75 |
+
"chess",
|
| 76 |
+
],
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# --- Manager agent definition ---
|
| 80 |
+
manager_planning_interval = int(os.getenv("MANAGER_PLANNING_INTERVAL", "3"))
|
| 81 |
+
manager_max_steps = int(os.getenv("MANAGER_MAX_STEPS", "8"))
|
| 82 |
+
|
| 83 |
+
# The manager starts with the smaller Qwen model to minimize token usage
|
| 84 |
+
# and only relies on DeepSeek when verifying critical answers.
|
| 85 |
+
self.manager_agent = CodeAgent(
|
| 86 |
+
model=self.qwen_model,
|
| 87 |
+
tools=[FinalAnswerTool()],
|
| 88 |
+
managed_agents=[self.web_agent, self.info_agent],
|
| 89 |
+
name="manager_agent",
|
| 90 |
+
description=(
|
| 91 |
+
"You are the manager. Given a {task}, plan which agent to use: "
|
| 92 |
+
"If web data is needed, delegate to web_agent. If math, parsing, image reasoning, or code is needed, use info_agent. "
|
| 93 |
+
"After collecting outputs, optionally cross-validate and check correctness, then finalize and submit the best answer using FinalAnswerTool. "
|
| 94 |
+
"For each task, explicitly explain your planning steps and reasons for choosing which agent, and always prefer the most accurate and complete answer possible."
|
| 95 |
+
),
|
| 96 |
+
additional_authorized_imports=[
|
| 97 |
+
"json",
|
| 98 |
+
"pandas",
|
| 99 |
+
"numpy",
|
| 100 |
+
],
|
| 101 |
+
planning_interval=manager_planning_interval,
|
| 102 |
+
verbosity_level=2,
|
| 103 |
+
max_steps=manager_max_steps,
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# runtime tracking for fallback switching
|
| 107 |
+
self.total_runtime = 0.0
|
| 108 |
+
self.first_call_duration = None
|
| 109 |
+
self.model_switched = False
|
| 110 |
+
|
| 111 |
+
def _switch_to_fallback(self):
|
| 112 |
+
if self.model_switched:
|
| 113 |
+
return
|
| 114 |
+
self.manager_agent.model = self.fallback_model
|
| 115 |
+
self.model_switched = True
|
| 116 |
+
|
| 117 |
+
def run(self, question, high_stakes: bool = False, **kwargs):
|
| 118 |
+
start_time = time.time()
|
| 119 |
+
print("Generating initial answer with Qwen-32B")
|
| 120 |
+
max_completion_tokens = kwargs.get("max_completion_tokens", 512)
|
| 121 |
+
prompt_tokens = len(question.split())
|
| 122 |
+
consume(prompt_tokens + max_completion_tokens)
|
| 123 |
+
initial_answer = self.manager_agent(question, **kwargs)
|
| 124 |
+
call_duration = time.time() - start_time
|
| 125 |
+
|
| 126 |
+
answer = initial_answer
|
| 127 |
+
if high_stakes or len(initial_answer.split()) > self.verification_limit:
|
| 128 |
+
print("Verifying answer using DeepSeek-70B")
|
| 129 |
+
verification_prompt = (
|
| 130 |
+
"Review the following answer for accuracy and rewrite if needed:"
|
| 131 |
+
f"\n\n{initial_answer}"
|
| 132 |
+
)
|
| 133 |
+
try:
|
| 134 |
+
max_completion_tokens = kwargs.get("max_completion_tokens", 512)
|
| 135 |
+
prompt_tokens = len(verification_prompt.split())
|
| 136 |
+
consume(prompt_tokens + max_completion_tokens)
|
| 137 |
+
answer = self.deepseek_model(
|
| 138 |
+
verification_prompt, max_completion_tokens=max_completion_tokens
|
| 139 |
+
)
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"Verification failed: {e}. Using initial answer.")
|
| 142 |
+
answer = initial_answer
|
| 143 |
+
|
| 144 |
+
if self.first_call_duration is None:
|
| 145 |
+
self.first_call_duration = call_duration
|
| 146 |
+
if self.first_call_duration > 30:
|
| 147 |
+
self._switch_to_fallback()
|
| 148 |
+
|
| 149 |
+
self.total_runtime += call_duration
|
| 150 |
+
if self.total_runtime > 300 and not self.model_switched:
|
| 151 |
+
self._switch_to_fallback()
|
| 152 |
+
|
| 153 |
+
return answer
|
| 154 |
+
|
| 155 |
+
def __call__(self, question, high_stakes: bool = False, **kwargs):
|
| 156 |
+
|
| 157 |
+
return self.run(question, high_stakes=high_stakes, **kwargs)
|