File size: 3,921 Bytes
a15f756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e9b7d
 
a15f756
 
 
 
 
 
 
 
 
 
 
e80aab9
31243f4
a15f756
31243f4
a15f756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
a15f756
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31243f4
a15f756
 
 
 
 
 
 
 
 
 
31243f4
a15f756
 
 
 
 
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
# ============================================================
# INSTALL
# ============================================================

# Put this at the TOP of app.py if smolagents is not already
# available in your Space requirements.

import subprocess
import sys

subprocess.check_call([
    sys.executable,
    "-m",
    "pip",
    "install",
    "-q",
    "smolagents",
    "ddgs"
])


# ============================================================
# IMPORTS
# ============================================================

import os

from smolagents import (
    CodeAgent,
    InferenceClientModel,
    DuckDuckGoSearchTool,
    PythonInterpreterTool
)


# ============================================================
# YOUR GAIA AGENT
# ============================================================

class BasicAgent:

    def __init__(self):

        print("Starting GAIA Agent...")

        # Get Hugging Face token from Space Secret
        hf_token = os.getenv("HF_TOKEN")

        if not hf_token:
            raise ValueError(
                "HF_TOKEN is missing. "
                "Go to Space Settings → Secrets and add HF_TOKEN."
            )

        # ----------------------------------------------------
        # MODEL
        # ----------------------------------------------------

        model = InferenceClientModel(
            model_id="Qwen/Qwen2.5-72B-Instruct",
            token=hf_token
        )

        # ----------------------------------------------------
        # WEB SEARCH
        # ----------------------------------------------------

        search = DuckDuckGoSearchTool(
            max_results=5
        )

        # ----------------------------------------------------
        # PYTHON / CALCULATOR
        # ----------------------------------------------------

        calculator = PythonInterpreterTool(
            authorized_imports=[
                "math",
                "statistics",
                "datetime",
                "json",
                "re"
            ]
        )

        # ----------------------------------------------------
        # AGENT
        # ----------------------------------------------------

        self.agent = CodeAgent(
            model=model,
            tools=[
                search,
                calculator
            ],
            max_steps=8
        )

        print("GAIA Agent ready!")


    # ========================================================
    # THIS IS CALLED BY THE COURSE EVALUATOR
    # ========================================================

    def __call__(self, question: str) -> str:

        print("\n" + "=" * 60)
        print("QUESTION:")
        print(question)
        print("=" * 60)

        prompt = f"""
You are a highly capable general-purpose AI agent.

Solve the user's question accurately.

IMPORTANT:

1. Understand exactly what the question asks.

2. Use web search when the question requires:
   - current information
   - factual information
   - information not available from your knowledge

3. Use the Python tool whenever calculations are required.

4. Carefully check arithmetic.

5. For questions involving dates, numbers, percentages,
   units, names, or lists, verify your answer carefully.

6. If several steps are required, solve them systematically.

7. Do not invent information.

8. Use the available tools whenever they improve accuracy.

9. The final response must directly answer the question.

10. Follow the requested answer format exactly.

11. Do not include unnecessary discussion in the final answer.

USER QUESTION:
{question}
"""

        try:

            result = self.agent.run(prompt)

            answer = str(result).strip()

            print("\nANSWER:")
            print(answer)

            return answer

        except Exception as e:

            print("AGENT ERROR:")
            print(e)

            return f"Agent error: {e}"