{ "cells": [ { "cell_type": "code", "id": "initial_id", "metadata": { "collapsed": true }, "source": [ "import time\n", "import concurrent\n", "from concurrent import futures\n", "from typing import Any, List, Dict, Tuple\n", "\n", "from PIL import Image\n", "from openai import AzureOpenAI, RateLimitError\n", "from tqdm.auto import tqdm\n", "\n", "import json\n", "from pathlib import Path\n", "\n", "import pandas as pd\n", "import requests\n", "from dotenv import dotenv_values\n", "from smolagents import tool, DuckDuckGoSearchTool\n", "import wikipediaapi\n", "\n", "\n", "test_api_base = \"https://agents-course-unit4-scoring.hf.space\"\n" ], "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "config = dotenv_values()\n", "\n", "client = AzureOpenAI(\n", " api_key=config[\"AZURE_OPENAI_API_KEY\"],\n", " azure_endpoint=config[\"AZURE_OPENAI_API_BASE\"],\n", " api_version=config[\"AZURE_OPENAI_API_VERSION\"]\n", ")\n", "openai_chatmodel = config[\"AZURE_OPENAI_CHAT_MODEL\"]\n", "\n", "GRAY = \"\\033[90m\"\n", "BOLD = \"\\033[1m\"\n", "RESET = \"\\033[0m\"" ], "id": "99393f634f21563f", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "markdown", "source": "# Question Processing", "id": "1290570b730fda4" }, { "metadata": {}, "cell_type": "code", "source": [ "response = requests.get(f\"{test_api_base}/questions\", timeout=15)\n", "response.raise_for_status()\n", "\n", "questions_data = response.json()" ], "id": "9f6fe414bc8fb090", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "df = pd.DataFrame(questions_data)\n", "df" ], "id": "3d0ab116de02315e", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "@tool\n", "def read_file(file_path_str: str) -> str:\n", " \"\"\"\n", " A tool that reads the contents of a file and returns them as text.\n", "\n", " Args:\n", " file_path_str: The path to the file that should be read.\n", " \"\"\"\n", "\n", " file_path = Path(file_path_str)\n", " file_path = file_path.resolve()\n", " if not file_path.exists() or not file_path.is_file():\n", " raise ValueError(f\"File {file_path} does not exist or is not a file.\")\n", "\n", " switcher = {\n", " \".txt\": lambda: file_path.read_text(encoding=\"utf-8\"),\n", " \".csv\": lambda: file_path.read_text(encoding=\"utf-8\"),\n", " \".py\": lambda: file_path.read_text(encoding=\"utf-8\"),\n", " \".xlsx\": lambda: pd.read_excel(file_path).to_string(),\n", " }\n", "\n", " return switcher.get(file_path.suffix, lambda: \"Unsupported file type\")()\n", "\n", "from smolagents import GoogleSearchTool\n", "import requests\n", "import urllib.request\n", "from markdownify import markdownify as md\n", "from bs4 import BeautifulSoup\n", "from dotenv import dotenv_values\n", "from openai import AzureOpenAI\n", "import json\n", "\n", "\n", "\n", "def get_search_results_for(query):\n", " encoded_query = urllib.parse.urlencode({'q': query})\n", " url = f'https://html.duckduckgo.com/html?q={encoded_query}'\n", "\n", " request = urllib.request.Request(url)\n", " 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')\n", "\n", " raw_response = urllib.request.urlopen(request).read()\n", " html = raw_response.decode(\"utf-8\")\n", "\n", " soup = BeautifulSoup(html, 'html.parser')\n", " a_results = soup.select(\"a.result__a\")\n", "\n", " links = []\n", " for a_result in a_results:\n", " # print(a_result)\n", " url = a_result.attrs['href']\n", " title = a_result.text\n", " links.append({\"title\": title, \"url\": url} )\n", "\n", " return links\n", "\n", "search_tool = GoogleSearchTool(\"serper\")\n", "\n", "def get_google_search_results_for(query: str):\n", " return search_tool.forward(query)\n", "\n", "\n", "def load_page_content(url) -> str:\n", " 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'})\n", " page_content = response.content.decode('utf-8')\n", " page_content_md = md(page_content)\n", "\n", " return page_content_md\n", "\n", "tools = [{\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"get_search_results_for\",\n", " \"description\": \"Returns the top 10 results for a DuckDuckGo query.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"query\": {\n", " \"type\": \"string\",\n", " \"description\": \"query to search for on DuckDuckGo\"\n", " }\n", " },\n", " \"required\": [\n", " \"query\"\n", " ],\n", " \"additionalProperties\": False\n", " },\n", " \"strict\": True\n", " }\n", " },\n", " {\n", " \"type\": \"function\",\n", " \"function\": {\n", " \"name\": \"load_page_content\",\n", " \"description\": \"Returns the content of a particular webpage.\",\n", " \"parameters\": {\n", " \"type\": \"object\",\n", " \"properties\": {\n", " \"url\": {\n", " \"type\": \"string\",\n", " \"description\": \"Url of the webpage for which to retrieve the content\"\n", " }\n", " },\n", " \"required\": [\n", " \"url\"\n", " ],\n", " \"additionalProperties\": False\n", " },\n", " \"strict\": True\n", " }\n", " }\n", "]\n", "\n", "def call_function(name, args):\n", " if name == \"get_search_results_for\":\n", " return get_google_search_results_for(**args)\n", " if name == \"load_page_content\":\n", " return load_page_content(**args)\n", "\n", " return None\n", "\n", "\n", "def run_agent(task: str):\n", "\n", " messages = [\n", " {\n", " \"role\": \"system\",\n", " \"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.\"\n", " },\n", " {\"role\": \"user\", \"content\": task}\n", " ]\n", "\n", " while True:\n", " for i in range(10):\n", " try:\n", " completion = client.chat.completions.create(\n", " model=openai_chatmodel,\n", " messages=messages,\n", " tools=tools\n", " )\n", " break\n", " except RateLimitError:\n", " print(f\"{GRAY}Rate limit exceeded, waiting for 10 seconds...{RESET}\")\n", " time.sleep(i*10)\n", " continue\n", "\n", " if completion.choices[0].finish_reason == \"stop\":\n", " print(f\"{BOLD}Final answer: {completion.choices[0].message.content}{RESET}\")\n", " return completion.choices[0].message.content.split(\"FINAL ANSWER:\")[-1].strip()\n", " elif completion.choices[0].finish_reason == \"tool_calls\":\n", " messages.append(completion.choices[0].message)\n", " for tool_call in completion.choices[0].message.tool_calls:\n", " name = tool_call.function.name\n", " args = json.loads(tool_call.function.arguments)\n", "\n", " try:\n", " result = call_function(name, args)\n", " except Exception as e:\n", " result = \"Error calling function: \" + str(e)\n", " print(f\"Called {BOLD}{name}({args}){RESET} and it returned {GRAY}{str(result)[:300]}{RESET}\")\n", "\n", " messages.append({\n", " \"role\": \"tool\",\n", " \"tool_call_id\": tool_call.id,\n", " \"content\": str(result)\n", " })\n", " else:\n", " raise Exception(\"We're not supposed to be here\")\n", "\n", "\n", "\n", "def process_question(question_data: dict[str, Any]) -> dict[str, str]:\n", " task_id = question_data.get(\"task_id\")\n", " question_text = question_data.get(\"question\")\n", "\n", " # file_path = None\n", " # if question_data.get(\"file_name\"):\n", " # task_id = question_data[\"task_id\"]\n", " # file_url = f\"{test_api_base}/files/{task_id}\"\n", " #\n", " # download_dir = Path(\"downloaded_files\")\n", " # download_dir.mkdir(exist_ok=True)\n", " #\n", " # file_response = requests.get(file_url, timeout=30)\n", " # file_response.raise_for_status()\n", " #\n", " # file_path = download_dir / question_data.get(\"file_name\")\n", " #\n", " # with open(file_path, 'wb') as f:\n", " # f.write(file_response.content)\n", "\n", " answer = run_agent(question_text)\n", "\n", " # if file_path and file_path.suffix in ['.png', '.jpg', '.jpeg']: # I know, it's inconsistent\n", " # answer = agent.run(task=adjusted_question_text, images=[Image.open(file_path)])\n", " # else:\n", " # answer = agent.run(task=f\"{adjusted_question_text}{f' File: |{file_path}|' if question_data.get('file_name') else ''}\", )\n", "\n", " # print(f\"Task ID: {task_id}, Question: {question_text}, Answer: {answer}\")\n", "\n", " return {\n", " \"task_id\": task_id,\n", " \"submitted_answer\": answer,\n", " \"question\": question_text\n", " }\n", "\n", "\n", "def run_agents_parallel(questions_data: List[Dict[str, Any]], max_workers: int = 4) -> Tuple[Dict[str, Any], List[Dict[str, Any]]]:\n", " start = time.time()\n", "\n", " answers = []\n", " results_log = []\n", "\n", " with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:\n", " future_to_question = {executor.submit(process_question, q): q for q in questions_data}\n", "\n", " for future in tqdm(concurrent.futures.as_completed(future_to_question)):\n", " try:\n", " answer = future.result()\n", " results_log.append(answer)\n", " answers.append(answer)\n", "\n", " except Exception as e:\n", " print(f\"Question processing failed: {e}\")\n", "\n", " submission_data = {\n", " \"username\": \"vladi\",\n", " \"agent_code\": \"https://huggingface.co/spaces/vladi/AgentsGAIAFun\",\n", " \"answers\": answers\n", " }\n", " end = time.time()\n", " print(f\"Processing time (parallel): {end - start:.2f} seconds\")\n", "\n", " return submission_data, results_log\n", "\n", "def run_agents(questions_data: list[{}]):\n", " start = time.time()\n", "\n", " answers = []\n", " results_log = []\n", " for question_data in tqdm(questions_data):\n", "\n", " answer = process_question(question_data)\n", "\n", " results_log.append(answer)\n", " answers.append(answer)\n", "\n", " submission_data = {\n", " \"username\": \"vladi\",\n", " \"agent_code\": \"https://huggingface.co/spaces/vladi/AgentsGAIAFun\",\n", " \"answers\": answers\n", " }\n", "\n", " end = time.time()\n", " print(f\"Processing time (sequential): {end - start:.2f} seconds\")\n", "\n", " return submission_data, results_log\n", "\n", "def submit_answers(submission_data: dict):\n", " print(f\"Submitting {len(submission_data['answers'])} answers\")\n", "\n", " response = requests.post(f\"{test_api_base}/submit\", json=submission_data, timeout=60)\n", " response.raise_for_status()\n", " result_data = response.json()\n", "\n", " return result_data\n" ], "id": "74bce95503481798", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "submission_data, results_log = run_agents(questions_data)#[:20])\n", "# submission_data, results_log = run_agents_parallel(questions_data)\n", "results_df = pd.DataFrame(results_log)\n", "\n", "results_log" ], "id": "57e1c5515e9bf8a1", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": "submission_data", "id": "d66d9affd58d3428", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": [ "import copy\n", "\n", "s2 = copy.deepcopy(submission_data)\n", "s2[\"answers\"][0][\"submitted_answer\"] = 'FINAL ANSWER: 3'" ], "id": "3206c60aa68333bc", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": "submit_answers(submission_data)", "id": "ea6c404b932e9922", "outputs": [], "execution_count": null }, { "metadata": {}, "cell_type": "code", "source": "", "id": "6129f916b20f27cb", "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }