ABVM commited on
Commit
878cdf9
·
verified ·
1 Parent(s): 2f4f5b5

Upload multi_agent.py

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