File size: 10,419 Bytes
489ed9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import concurrent
import os
import time
from concurrent import futures
from pathlib import Path
from typing import Any, List, Dict, Tuple

import pandas as pd
import requests
from dotenv import dotenv_values, load_dotenv
from openai import AzureOpenAI, RateLimitError
from smolagents import tool
from tqdm.auto import tqdm
from smolagents import GoogleSearchTool
import requests
import urllib.request
from markdownify import markdownify as md
from bs4 import BeautifulSoup
import json

test_api_base = "https://agents-course-unit4-scoring.hf.space"

# Configuration
load_dotenv()

client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    azure_endpoint=os.getenv("AZURE_OPENAI_API_BASE"),
    api_version=os.getenv("AZURE_OPENAI_API_VERSION")
)
openai_chatmodel = os.getenv("AZURE_OPENAI_CHAT_MODEL")

GRAY = "\033[90m"
BOLD = "\033[1m"
RESET = "\033[0m"

# Load questions
response = requests.get(f"{test_api_base}/questions", timeout=15)
response.raise_for_status()

questions_data = response.json()
df = pd.DataFrame(questions_data)

# Define tools & agent

@tool
def read_file(file_path_str: str) -> str:
    """
    A tool that reads the contents of a file and returns them as text.

    Args:
        file_path_str: The path to the file that should be read.
    """

    file_path = Path(file_path_str)
    file_path = file_path.resolve()
    if not file_path.exists() or not file_path.is_file():
        raise ValueError(f"File {file_path} does not exist or is not a file.")

    switcher = {
        ".txt": lambda: file_path.read_text(encoding="utf-8"),
        ".csv": lambda: file_path.read_text(encoding="utf-8"),
        ".py": lambda: file_path.read_text(encoding="utf-8"),
        ".xlsx": lambda: pd.read_excel(file_path).to_string(),
    }

    return switcher.get(file_path.suffix, lambda: "Unsupported file type")()





def get_search_results_for(query):
    encoded_query = urllib.parse.urlencode({'q': query})
    url = f'https://html.duckduckgo.com/html?q={encoded_query}'

    request = urllib.request.Request(url)
    request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')

    raw_response = urllib.request.urlopen(request).read()
    html = raw_response.decode("utf-8")

    soup = BeautifulSoup(html, 'html.parser')
    a_results = soup.select("a.result__a")

    links = []
    for a_result in a_results:
        # print(a_result)
        url = a_result.attrs['href']
        title = a_result.text
        links.append({"title": title,  "url": url} )

    return links

search_tool = GoogleSearchTool("serper")

def get_google_search_results_for(query: str):
    return search_tool.forward(query)


def load_page_content(url) -> str:
    response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'})
    page_content = response.content.decode('utf-8')
    page_content_md = md(page_content)

    return page_content_md

tools = [{
        "type": "function",
        "function": {
            "name": "get_search_results_for",
            "description": "Returns the top 10 results for a DuckDuckGo query.",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "query to search for on DuckDuckGo"
                    }
                },
                "required": [
                    "query"
                ],
                "additionalProperties": False
            },
            "strict": True
        }
    },
    {
        "type": "function",
        "function": {
            "name": "load_page_content",
            "description": "Returns the content of a particular webpage.",
            "parameters": {
                "type": "object",
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "Url of the webpage for which to retrieve the content"
                    }
                },
                "required": [
                    "url"
                ],
                "additionalProperties": False
            },
            "strict": True
        }
    }
]

def call_function(name, args):
    if name == "get_search_results_for":
        return get_google_search_results_for(**args)
    if name == "load_page_content":
        return load_page_content(**args)

    return None


def run_agent(task: str):

    messages = [
        {
            "role": "system",
            "content": "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
        },
        {"role": "user", "content": task}
    ]

    while True:
        for i in range(10):
            try:
                completion = client.chat.completions.create(
                    model=openai_chatmodel,
                    messages=messages,
                    tools=tools
                )
                break
            except RateLimitError:
                print(f"{GRAY}Rate limit exceeded, waiting for 10 seconds...{RESET}")
                time.sleep(i*10)
                continue

        if completion.choices[0].finish_reason == "stop":
            print(f"{BOLD}Final answer: {completion.choices[0].message.content}{RESET}")
            return completion.choices[0].message.content.split("FINAL ANSWER:")[-1].strip()
        elif completion.choices[0].finish_reason == "tool_calls":
            messages.append(completion.choices[0].message)
            for tool_call in completion.choices[0].message.tool_calls:
                name = tool_call.function.name
                args = json.loads(tool_call.function.arguments)

                try:
                    result = call_function(name, args)
                except Exception as e:
                    result = "Error calling function: " + str(e)
                print(f"Called {BOLD}{name}({args}){RESET} and it returned {GRAY}{str(result)[:300]}{RESET}")

                messages.append({
                    "role": "tool",
                    "tool_call_id": tool_call.id,
                    "content": str(result)
                })
        else:
            raise Exception("We're not supposed to be here")



def process_question(question_data: dict[str, Any]) -> dict[str, str]:
    task_id = question_data.get("task_id")
    question_text = question_data.get("question")

    # file_path = None
    # if question_data.get("file_name"):
    #     task_id = question_data["task_id"]
    #     file_url = f"{test_api_base}/files/{task_id}"
    #
    #     download_dir = Path("downloaded_files")
    #     download_dir.mkdir(exist_ok=True)
    #
    #     file_response = requests.get(file_url, timeout=30)
    #     file_response.raise_for_status()
    #
    #     file_path = download_dir / question_data.get("file_name")
    #
    #     with open(file_path, 'wb') as f:
    #         f.write(file_response.content)

    answer = run_agent(question_text)

    # if file_path and file_path.suffix in ['.png', '.jpg', '.jpeg']:   # I know, it's inconsistent
    #     answer = agent.run(task=adjusted_question_text, images=[Image.open(file_path)])
    # else:
    #     answer = agent.run(task=f"{adjusted_question_text}{f' File: |{file_path}|' if question_data.get('file_name') else ''}", )

    # print(f"Task ID: {task_id}, Question: {question_text}, Answer: {answer}")

    return {
        "task_id": task_id,
        "submitted_answer": answer,
        "question": question_text
    }


def run_agents_parallel(questions_data: List[Dict[str, Any]], max_workers: int = 4) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:
    start = time.time()

    answers = []
    results_log = []

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
        future_to_question = {executor.submit(process_question, q): q for q in questions_data}

        for future in tqdm(concurrent.futures.as_completed(future_to_question)):
            try:
                answer = future.result()
                results_log.append(answer)
                answers.append(answer)

            except Exception as e:
                print(f"Question processing failed: {e}")

    submission_data = {
        "username": "vladi",
        "agent_code": "https://huggingface.co/spaces/vladi/AgentsGAIAFun",
        "answers": answers
    }
    end = time.time()
    print(f"Processing time (parallel): {end - start:.2f} seconds")

    return submission_data, results_log

def run_agents(questions_data: list[{}]):
    start = time.time()

    answers = []
    results_log = []
    for question_data in tqdm(questions_data):

        answer = process_question(question_data)

        results_log.append(answer)
        answers.append(answer)

    submission_data = {
        "username": "vladi",
        "agent_code": "https://huggingface.co/spaces/vladi/AgentsGAIAFun",
        "answers": answers
    }

    end = time.time()
    print(f"Processing time (sequential): {end - start:.2f} seconds")

    return submission_data, results_log

def submit_answers(submission_data: dict):
    print(f"Submitting {len(submission_data['answers'])} answers")

    response = requests.post(f"{test_api_base}/submit", json=submission_data, timeout=60)
    response.raise_for_status()
    result_data = response.json()

    return result_data


submission_data, results_log = run_agents(questions_data)#[:20])
# submission_data, results_log = run_agents_parallel(questions_data)
results_df = pd.DataFrame(results_log)

# Last but not least...

submit_answers(submission_data)