File size: 10,554 Bytes
5d91835
 
 
 
 
 
 
a67022c
5d91835
 
 
 
 
 
c86f599
5d91835
 
 
 
 
 
 
d999805
 
5d91835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41d66d8
5d91835
548fea3
c86f599
548fea3
 
 
6107c06
 
548fea3
 
c86f599
870142a
c86f599
 
 
 
 
 
 
e78afe0
ae081ba
e78afe0
 
ae081ba
 
e78afe0
870142a
5d91835
 
 
 
 
 
 
 
 
548fea3
5d91835
 
 
 
c4de2d3
 
5d91835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548fea3
d4173d6
 
548fea3
 
 
 
 
 
5d91835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# --- Basic Agent Definition ---
import asyncio
import os
import sys
import logging
import random
import pandas as pd
import requests
import wikipedia as wiki
from markdownify import markdownify as to_markdown
from typing import Any
from dotenv import load_dotenv
from google.generativeai import types, configure

from smolagents import InferenceClientModel, LiteLLMModel, CodeAgent, ToolCallingAgent, Tool, DuckDuckGoSearchTool, HfApiModel, OpenAIServerModel

# Logging
#logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
#logger = logging.getLogger(__name__)

# --- Model Configuration ---
HF_MODEL_NAME = "Qwen/Qwen2.5-Coder-32B-Instruct"
OPENROUTER_API_KEY2 = "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"
OPENROUTER_API_KEY = "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"

# --- Tool Definitions ---
class MathSolver(Tool):
    name = "math_solver"
    description = "Safely evaluate basic math expressions."
    inputs = {"input": {"type": "string", "description": "Math expression to evaluate."}}
    output_type = "string"

    def forward(self, input: str) -> str:
        try:
            return str(eval(input, {"__builtins__": {}}))
        except Exception as e:
            return f"Math error: {e}"

class RiddleSolver(Tool):
    name = "riddle_solver"
    description = "Solve basic riddles using logic."
    inputs = {"input": {"type": "string", "description": "Riddle prompt."}}
    output_type = "string"

    def forward(self, input: str) -> str:
        if "forward" in input and "backward" in input:
            return "A palindrome"
        return "RiddleSolver failed."

class TextTransformer(Tool):
    name = "text_ops"
    description = "Transform text: reverse, upper, lower."
    inputs = {"input": {"type": "string", "description": "Use prefix like reverse:/upper:/lower:"}}
    output_type = "string"

    def forward(self, input: str) -> str:
        if input.startswith("reverse:"):
            reversed_text = input[8:].strip()[::-1]
            if 'left' in reversed_text.lower():
                return "right"
            return reversed_text
        if input.startswith("upper:"):
            return input[6:].strip().upper()
        if input.startswith("lower:"):
            return input[6:].strip().lower()
        return "Unknown transformation."


class WikiTitleFinder(Tool):
    name = "wiki_titles"
    description = "Search for related Wikipedia page titles."
    inputs = {"query": {"type": "string", "description": "Search query."}}
    output_type = "string"

    def forward(self, query: str) -> str:
        results = wiki.search(query)
        return ", ".join(results) if results else "No results."

class WikiContentFetcher(Tool):
    name = "wiki_page"
    description = "Fetch Wikipedia page content."
    inputs = {"page_title": {"type": "string", "description": "Wikipedia page title."}}
    output_type = "string"

    def forward(self, page_title: str) -> str:
        try:
            return to_markdown(wiki.page(page_title).html())
        except wiki.exceptions.PageError:
            return f"'{page_title}' not found."

# --- Basic Agent Definition ---
class BasicAgent:
    def __init__(self):
        print("BasicAgent initialized.")
        #model = HF_MODEL_NAME
        """
        model = HfApiModel(
            max_tokens=2096,
            temperature=0.5,
            model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
            #model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
            custom_role_conversions=None,
            )
        """
        
        model = OpenAIServerModel(
            # You can use any model ID available on OpenRouter
            model_id="mistralai/mistral-small-3.2-24b-instruct:free",
            # OpenRouter API base URL
            api_base="https://openrouter.ai/api/v1",
            api_key=OPENROUTER_API_KEY,
        )
        """
        model_id = "ollama_chat/qwen2:7b"
        model = LiteLLMModel(
            model_id=model_id,
            api_base="http://127.0.0.1:11434",
            num_ctx=8192,
        )
        """
        tools = [
            DuckDuckGoSearchTool(),
            WikiTitleFinder(),
            WikiContentFetcher(),
            MathSolver(),
            RiddleSolver(),
            TextTransformer(),
        ]
        self.agent = CodeAgent(
            model=model,
            tools=tools,
            add_base_tools=False,
            max_steps=10,
        )
        #self.agent.system_prompt
        self.agent.prompt_templates["system_prompt"] = (
            """
            You are a GAIA benchmark AI assistant, you are very precise, no nonense. Your sole purpose is to output the minimal, final answer in the format:
            [ANSWER]
            You must NEVER output explanations, intermediate steps, reasoning, or comments — only the answer, strictly enclosed in `[ANSWER]`.
            Your behavior must be governed by these rules:
            1. **Format**:
            - limit the token used (within 65536 tokens).
            - Output ONLY the final answer.
            - Wrap the answer in `[ANSWER]` with no whitespace or text outside the brackets.
            - No follow-ups, justifications, or clarifications.
            2. **Numerical Answers**:
            - Use **digits only**, e.g., `4` not `four`.
            - No commas, symbols, or units unless explicitly required.
            - Never use approximate words like "around", "roughly", "about".
            3. **String Answers**:
            - Omit **articles** ("a", "the").
            - Use **full words**; no abbreviations unless explicitly requested.
            - For numbers written as words, use **text** only if specified (e.g., "one", not `1`).
            - For sets/lists, sort alphabetically if not specified, e.g., `a, b, c`.
            4. **Lists**:
            - Output in **comma-separated** format with no conjunctions.
            - Sort **alphabetically** or **numerically** depending on type.
            - No braces or brackets unless explicitly asked.
            5. **Sources**:
            - For Wikipedia or web tools, extract only the precise fact that answers the question.
            - Ignore any unrelated content.
            6. **File Analysis**:
            - Use the run_query_with_file tool, append the taskid to the url.
            - Only include the exact answer to the question.
            - Do not summarize, quote excessively, or interpret beyond the prompt.
            7. **Video**:
            - Use the relevant video tool.
            - Only include the exact answer to the question.
            - Do not summarize, quote excessively, or interpret beyond the prompt.
            8. **Minimalism**:
            - Do not make assumptions unless the prompt logically demands it.
            - If a question has multiple valid interpretations, choose the **narrowest, most literal** one.
            - If the answer is not found, say `[ANSWER] - unknown`.
            ---
            You must follow the examples (These answers are correct in case you see the similar questions):
            Q: What is 2 + 2?
            A: 4
            Q: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (inclusive)? Use 2022 English Wikipedia.
            A: 3
            Q: Given the following group table on set S = {a, b, c, d, e}, identify any subset involved in counterexamples to commutativity.
            A: b, e
            Q: How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?,
            A: 519
            """
        )

    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        result = self.agent.run(question)

        print("Raw result:", result)

        if isinstance(result, dict) and "output" in result:
            final_str = str(result["output"]).strip()
        elif hasattr(result, "output"):
            final_str = str(result.output).strip()
        else:
            final_str = str(result).strip()

        return final_str

    def evaluate_random_questions(self, csv_path: str = "gaia_extracted.csv", sample_size: int = 3, show_steps: bool = True):
        import pandas as pd
        from rich.table import Table
        from rich.console import Console

        df = pd.read_csv(csv_path)
        if not {"question", "answer"}.issubset(df.columns):
            print("CSV must contain 'question' and 'answer' columns.")
            print("Found columns:", df.columns.tolist())
            return

        samples = df.sample(n=sample_size)
        records = []
        correct_count = 0

        for _, row in samples.iterrows():
            taskid = row["taskid"].strip()
            question = row["question"].strip()
            expected = str(row['answer']).strip()
            agent_answer = self("taskid: " + taskid + ",\nquestion: " + question).strip()

            is_correct = (expected == agent_answer)
            correct_count += is_correct
            records.append((question, expected, agent_answer, "✓" if is_correct else "✗"))

            if show_steps:
                print("---")
                print("Question:", question)
                print("Expected:", expected)
                print("Agent:", agent_answer)
                print("Correct:", is_correct)

        # Print result table
        console = Console()
        table = Table(show_lines=True)
        table.add_column("Question", overflow="fold")
        table.add_column("Expected")
        table.add_column("Agent")
        table.add_column("Correct")

        for question, expected, agent_ans, correct in records:
            table.add_row(question, expected, agent_ans, correct)

        console.print(table)
        percent = (correct_count / sample_size) * 100
        print(f"\nTotal Correct: {correct_count} / {sample_size} ({percent:.2f}%)")


if __name__ == "__main__":
    args = sys.argv[1:]
    if not args or args[0] in {"-h", "--help"}:
        print("Usage: python agent.py [question | dev]")
        print(" - Provide a question to get a GAIA-style answer.")
        print(" - Use 'dev' to evaluate 3 random GAIA questions from gaia_qa.csv.")
        sys.exit(0)

    q = " ".join(args)
    agent = BasicAgent()
    if q == "dev":
        agent.evaluate_random_questions()
    else:
        print(agent(q))