File size: 7,356 Bytes
40f02cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from smolagents import (
    CodeAgent,
    VisitWebpageTool,
    WebSearchTool,
    WikipediaSearchTool,
    PythonInterpreterTool,
    FinalAnswerTool,
)
from groq import Groq
from vision_tool import image_reasoning_tool
import os
import time


# ---- TOOLS ----


# ---- GROQ MODEL WRAPPER ----
class GroqModel:
    def __init__(self, model_name=""):
        self.model_name = model_name
        self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

    def __call__(self, prompt, max_tokens=8096):
        if isinstance(prompt, str):
            messages = [{"role": "user", "content": prompt}]
        else:
            messages = prompt

        response = None
        for attempt in range(3):
            try:
                response = self.client.chat.completions.create(
                    messages=messages,
                    model=self.model_name,
                    stream=False,
                    max_tokens=max_tokens,
                )
                break
            except Exception as e:
                msg = str(e).lower()
                if "rate limit" in msg and attempt < 2:
                    wait = 10 * (attempt + 1)
                    time.sleep(wait)
                    continue
                raise

        if response is None:
            response = self.client.chat.completions.create(
                messages=messages,
                model=self.model_name,
                stream=False,
                max_tokens=max_tokens,
            )

        choice = response.choices[0]
        if hasattr(choice, "message"):
            content = choice.message.content
        else:
            # Fallback for text-only completions
            if hasattr(choice, "text"):
                content = choice.text
            elif isinstance(choice, str):
                content = choice
            else:
                content = str(choice)
        # token usage is calculated but currently unused
        if hasattr(response, "usage") and response.usage is not None:
            _ = response.usage.total_tokens

        return content

    def generate(self, prompt, max_tokens=8096, **kwargs):
        # For compatibility with agent frameworks
        return self.__call__(prompt, max_tokens=max_tokens)


# ---- MULTI-AGENT SYSTEM ----
class MultyAgentSystem:
    def __init__(self):
        self.primary_model_name = "deepseek-r1-distill-llama-70b"
        self.fallback_model_name = "llama3-70b-8k"

        self.deepseek_model = GroqModel(self.primary_model_name)
        qwen_model = GroqModel("qwen-qwq-32b")
        self.verification_limit = int(os.getenv("VERIFY_WORD_LIMIT", "75"))

        # --- Web agent definition ---
        self.web_agent = CodeAgent(
            model=qwen_model,
            tools=[WebSearchTool(), VisitWebpageTool(), WikipediaSearchTool()],
            name="web_agent",
            description=(
                "You are a web browsing agent. Whenever the given {task} involves browsing "
                "the web or a specific website such as Wikipedia or YouTube, you will use "
                "the provided tools. For web-based factual and retrieval tasks, be as precise and source-reliable as possible."
            ),
            additional_authorized_imports=[
                "markdownify",
                "json",
                "requests",
                "urllib.request",
                "urllib.parse",
                "wikipedia-api",
            ],
            verbosity_level=0,
            max_steps=10,
        )

        # --- Info agent definition ---
        self.info_agent = CodeAgent(
            model=qwen_model,
            tools=[PythonInterpreterTool(), image_reasoning_tool],
            name="info_agent",
            description=(
                "You are an agent tasked with cleaning, parsing, calculating information, and performing OCR if images are provided in the {task}. "
                "You can also analyze images using a vision model. You handle all math, code, and data manipulation. Use numpy, math, and available libraries. "
                "For image or chess tasks, use pytesseract, PIL, chess, or the image_reasoning_tool as required."
            ),
            additional_authorized_imports=[
                "numpy",
                "math",
                "pytesseract",
                "PIL",
                "chess",
            ],
        )

        # --- Manager agent definition ---
        manager_planning_interval = int(os.getenv("MANAGER_PLANNING_INTERVAL", "3"))
        manager_max_steps = int(os.getenv("MANAGER_MAX_STEPS", "8"))

        self.manager_agent = CodeAgent(
            model=qwen_model,
            tools=[FinalAnswerTool()],
            managed_agents=[self.web_agent, self.info_agent],
            name="manager_agent",
            description=(
                "You are the manager. Given a {task}, plan which agent to use: "
                "If web data is needed, delegate to web_agent. If math, parsing, image reasoning, or code is needed, use info_agent. "
                "After collecting outputs, optionally cross-validate and check correctness, then finalize and submit the best answer using FinalAnswerTool. "
                "For each task, explicitly explain your planning steps and reasons for choosing which agent, and always prefer the most accurate and complete answer possible."
            ),
            additional_authorized_imports=[
                "json",
                "pandas",
                "numpy",
            ],
            planning_interval=manager_planning_interval,
            verbosity_level=2,
            max_steps=manager_max_steps,
        )

        # runtime tracking for fallback switching
        self.total_runtime = 0.0
        self.first_call_duration = None
        self.model_switched = False

    def _switch_to_fallback(self):
        if self.model_switched:
            return
        self.manager_agent.model = GroqModel(self.fallback_model_name)
        self.model_switched = True

    def run(self, question, high_stakes: bool = False, **kwargs):
        start_time = time.time()
        print("Generating initial answer with Qwen-32B")
        initial_answer = self.manager_agent(question, **kwargs)
        call_duration = time.time() - start_time

        answer = initial_answer
        if high_stakes or len(initial_answer.split()) > self.verification_limit:
            print("Verifying answer using DeepSeek-70B")
            verification_prompt = (
                "Review the following answer for accuracy and rewrite if needed:"
                f"\n\n{initial_answer}"
            )
            try:
                answer = self.deepseek_model(verification_prompt)
            except Exception as e:
                print(f"Verification failed: {e}. Using initial answer.")
                answer = initial_answer

        if self.first_call_duration is None:
            self.first_call_duration = call_duration
            if self.first_call_duration > 30:
                self._switch_to_fallback()

        self.total_runtime += call_duration
        if self.total_runtime > 300 and not self.model_switched:
            self._switch_to_fallback()

        return answer

    def __call__(self, question, high_stakes: bool = False, **kwargs):

        return self.run(question, high_stakes=high_stakes, **kwargs)