ABVM commited on
Commit
c4a1609
·
verified ·
1 Parent(s): 0f639ce

Delete multi_agent.py

Browse files
Files changed (1) hide show
  1. multi_agent.py +0 -187
multi_agent.py DELETED
@@ -1,187 +0,0 @@
1
- from smolagents import (
2
- CodeAgent,
3
- VisitWebpageTool,
4
- WebSearchTool,
5
- WikipediaSearchTool,
6
- PythonInterpreterTool,
7
- FinalAnswerTool,
8
- )
9
- from groq import Groq
10
- from vision_tool import image_reasoning_tool
11
- import os
12
- import time
13
-
14
-
15
- # ---- TOOLS ----
16
-
17
-
18
- # ---- GROQ MODEL WRAPPER ----
19
- class GroqModel:
20
- def __init__(self, model_name=""):
21
- self.model_name = model_name
22
- self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
23
-
24
- def __call__(self, prompt, max_tokens=8096):
25
- if isinstance(prompt, str):
26
- messages = [{"role": "user", "content": prompt}]
27
- else:
28
- messages = prompt
29
-
30
- response = None
31
- for attempt in range(3):
32
- try:
33
- response = self.client.chat.completions.create(
34
- messages=messages,
35
- model=self.model_name,
36
- stream=False,
37
- max_tokens=max_tokens,
38
- )
39
- break
40
- except Exception as e:
41
- msg = str(e).lower()
42
- if "rate limit" in msg and attempt < 2:
43
- wait = 10 * (attempt + 1)
44
- time.sleep(wait)
45
- continue
46
- raise
47
-
48
- if response is None:
49
- response = self.client.chat.completions.create(
50
- messages=messages,
51
- model=self.model_name,
52
- stream=False,
53
- max_tokens=max_tokens,
54
- )
55
-
56
- content = response.choices[0].message.content
57
- # token usage is calculated but currently unused
58
- if hasattr(response, "usage") and response.usage is not None:
59
- _ = response.usage.total_tokens
60
-
61
- return content
62
-
63
- def generate(self, prompt, max_tokens=8096, **kwargs):
64
- # For compatibility with agent frameworks
65
- return self.__call__(prompt, max_tokens=max_tokens)
66
-
67
-
68
- # ---- MULTI-AGENT SYSTEM ----
69
- class MultyAgentSystem:
70
- def __init__(self):
71
- self.primary_model_name = "deepseek-r1-distill-llama-70b"
72
- self.fallback_model_name = "llama3-70b-8k"
73
-
74
- self.deepseek_model = GroqModel(self.primary_model_name)
75
- qwen_model = GroqModel("qwen-qwq-32b")
76
- self.verification_limit = int(os.getenv("VERIFY_WORD_LIMIT", "75"))
77
-
78
- # --- Web agent definition ---
79
- self.web_agent = CodeAgent(
80
- model=qwen_model,
81
- tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
82
- name="web_agent",
83
- description=(
84
- "You are a web browsing agent. Whenever the given {task} involves browsing "
85
- "the web or a specific website such as Wikipedia or YouTube, you will use "
86
- "the provided tools. For web-based factual and retrieval tasks, be as precise and source-reliable as possible."
87
- ),
88
- additional_authorized_imports=[
89
- "markdownify",
90
- "json",
91
- "requests",
92
- "urllib.request",
93
- "urllib.parse",
94
- "wikipedia-api",
95
- ],
96
- verbosity_level=0,
97
- max_steps=10,
98
- )
99
-
100
- # --- Info agent definition ---
101
- self.info_agent = CodeAgent(
102
- model=qwen_model,
103
- tools=[PythonInterpreterTool(), image_reasoning_tool],
104
- name="info_agent",
105
- description=(
106
- "You are an agent tasked with cleaning, parsing, calculating information, and performing OCR if images are provided in the {task}. "
107
- "You can also analyze images using a vision model. You handle all math, code, and data manipulation. Use numpy, math, and available libraries. "
108
- "For image or chess tasks, use pytesseract, PIL, chess, or the image_reasoning_tool as required."
109
- ),
110
- additional_authorized_imports=[
111
- "numpy",
112
- "math",
113
- "pytesseract",
114
- "PIL",
115
- "chess",
116
- ],
117
- )
118
-
119
- # --- Manager agent definition ---
120
- manager_planning_interval = int(os.getenv("MANAGER_PLANNING_INTERVAL", "3"))
121
- manager_max_steps = int(os.getenv("MANAGER_MAX_STEPS", "8"))
122
-
123
- self.manager_agent = CodeAgent(
124
- model=qwen_model,
125
- tools=[FinalAnswerTool()],
126
- managed_agents=[self.web_agent, self.info_agent],
127
- name="manager_agent",
128
- description=(
129
- "You are the manager. Given a {task}, plan which agent to use: "
130
- "If web data is needed, delegate to web_agent. If math, parsing, image reasoning, or code is needed, use info_agent. "
131
- "After collecting outputs, optionally cross-validate and check correctness, then finalize and submit the best answer using FinalAnswerTool. "
132
- "For each task, explicitly explain your planning steps and reasons for choosing which agent, and always prefer the most accurate and complete answer possible."
133
- ),
134
- additional_authorized_imports=[
135
- "json",
136
- "pandas",
137
- "numpy",
138
- ],
139
- planning_interval=manager_planning_interval,
140
- verbosity_level=2,
141
- max_steps=manager_max_steps,
142
- )
143
-
144
- # runtime tracking for fallback switching
145
- self.total_runtime = 0.0
146
- self.first_call_duration = None
147
- self.model_switched = False
148
-
149
- def _switch_to_fallback(self):
150
- if self.model_switched:
151
- return
152
- self.manager_agent.model = GroqModel(self.fallback_model_name)
153
- self.model_switched = True
154
-
155
- def run(self, question, high_stakes: bool = False, **kwargs):
156
- start_time = time.time()
157
- print("Generating initial answer with Qwen-32B")
158
- initial_answer = self.manager_agent(question, **kwargs)
159
- call_duration = time.time() - start_time
160
-
161
- answer = initial_answer
162
- if high_stakes or len(initial_answer.split()) > self.verification_limit:
163
- print("Verifying answer using DeepSeek-70B")
164
- verification_prompt = (
165
- "Review the following answer for accuracy and rewrite if needed:"
166
- f"\n\n{initial_answer}"
167
- )
168
- try:
169
- answer = self.deepseek_model(verification_prompt)
170
- except Exception as e:
171
- print(f"Verification failed: {e}. Using initial answer.")
172
- answer = initial_answer
173
-
174
- if self.first_call_duration is None:
175
- self.first_call_duration = call_duration
176
- if self.first_call_duration > 30:
177
- self._switch_to_fallback()
178
-
179
- self.total_runtime += call_duration
180
- if self.total_runtime > 300 and not self.model_switched:
181
- self._switch_to_fallback()
182
-
183
- return answer
184
-
185
- def __call__(self, question, high_stakes: bool = False, **kwargs):
186
-
187
- return self.run(question, high_stakes=high_stakes, **kwargs)