ABVM commited on
Commit
262515c
·
verified ·
1 Parent(s): 6b0e1b2

Upload 2 files

Browse files
Files changed (2) hide show
  1. multi_agent.py +154 -0
  2. throttle.py +33 -0
multi_agent.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ initial_answer = self.manager_agent(question, **kwargs)
121
+ call_duration = time.time() - start_time
122
+
123
+ answer = initial_answer
124
+ if high_stakes or len(initial_answer.split()) > self.verification_limit:
125
+ print("Verifying answer using DeepSeek-70B")
126
+ verification_prompt = (
127
+ "Review the following answer for accuracy and rewrite if needed:"
128
+ f"\n\n{initial_answer}"
129
+ )
130
+ try:
131
+ max_completion_tokens = kwargs.get("max_completion_tokens", 512)
132
+ prompt_tokens = len(verification_prompt.split())
133
+ consume(prompt_tokens + max_completion_tokens)
134
+ answer = self.deepseek_model(
135
+ verification_prompt, max_completion_tokens=max_completion_tokens
136
+ )
137
+ except Exception as e:
138
+ print(f"Verification failed: {e}. Using initial answer.")
139
+ answer = initial_answer
140
+
141
+ if self.first_call_duration is None:
142
+ self.first_call_duration = call_duration
143
+ if self.first_call_duration > 30:
144
+ self._switch_to_fallback()
145
+
146
+ self.total_runtime += call_duration
147
+ if self.total_runtime > 300 and not self.model_switched:
148
+ self._switch_to_fallback()
149
+
150
+ return answer
151
+
152
+ def __call__(self, question, high_stakes: bool = False, **kwargs):
153
+
154
+ return self.run(question, high_stakes=high_stakes, **kwargs)
throttle.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ import time
3
+
4
+ TOKENS_PER_MINUTE = 5500
5
+ _tokens_per_second = TOKENS_PER_MINUTE / 60.0
6
+ _capacity = TOKENS_PER_MINUTE
7
+
8
+ _lock = threading.Lock()
9
+ _tokens = _capacity
10
+ _last_timestamp = time.monotonic()
11
+
12
+ def consume(n: int) -> None:
13
+ """Consume *n* tokens, waiting if necessary.
14
+
15
+ This function implements a simple thread-safe token bucket to keep
16
+ requests under the configured tokens-per-minute rate.
17
+ """
18
+ global _tokens, _last_timestamp
19
+ if n <= 0:
20
+ return
21
+ while True:
22
+ with _lock:
23
+ now = time.monotonic()
24
+ elapsed = now - _last_timestamp
25
+ _tokens = min(_capacity, _tokens + elapsed * _tokens_per_second)
26
+ _last_timestamp = now
27
+ if n <= _tokens:
28
+ _tokens -= n
29
+ return
30
+ needed = n - _tokens
31
+ wait_time = needed / _tokens_per_second
32
+ _tokens = 0
33
+ time.sleep(wait_time)