Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- agent.py +23 -0
- reference.jsonl +20 -0
- requirements.txt +86 -0
- system_prompt.txt +5 -0
- tools.py +215 -0
agent.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langgraph.prebuilt import create_react_agent
|
| 2 |
+
from langchain_openai import ChatOpenAI
|
| 3 |
+
from langchain_core.messages import HumanMessage
|
| 4 |
+
import os
|
| 5 |
+
import dotenv
|
| 6 |
+
from tools import get_tools
|
| 7 |
+
import json
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
dotenv.load_dotenv()
|
| 12 |
+
|
| 13 |
+
llm = ChatOpenAI(
|
| 14 |
+
api_key=os.getenv("API_KEY"),
|
| 15 |
+
model=os.getenv("MODEL")
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
tools = get_tools()
|
| 19 |
+
|
| 20 |
+
with open("system_prompt.txt") as f:
|
| 21 |
+
system_prompt = f.read()
|
| 22 |
+
|
| 23 |
+
agent = create_react_agent(model=llm, tools=tools, prompt=system_prompt)
|
reference.jsonl
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{"task_id":"8e867cd7-cff9-4e6c-867a-ff5ddc2550be","question":"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.","Level":"1","file_name":"","answer":"3"}
|
| 2 |
+
{"task_id":"a1e91b78-d3d8-4675-bb8d-62741b4b68a6","question":"In the video https:\/\/www.youtube.com\/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?","Level":"1","file_name":"","answer":""}
|
| 3 |
+
{"task_id":"2d83110e-a098-4ebb-9987-066c06fa42d0","question":".rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI","Level":"1","file_name":"","answer":"\"right\""}
|
| 4 |
+
{"task_id":"cca530fc-4052-43b2-b130-b30968d8aa44","question":"Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.","Level":"1","file_name":"cca530fc-4052-43b2-b130-b30968d8aa44.png","answer":""}
|
| 5 |
+
{"task_id":"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8","question":"Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?","Level":"1","file_name":"","answer":"FunkMonk"}
|
| 6 |
+
{"task_id":"6f37996b-2ac7-44b0-8e68-6d28256631b4","question":"Given this table defining * on the set S = {a, b, c, d, e}\n\n|*|a|b|c|d|e|\n|---|---|---|---|---|---|\n|a|a|b|c|b|d|\n|b|b|c|a|e|c|\n|c|c|a|b|b|a|\n|d|b|e|b|e|d|\n|e|d|b|a|d|c|\n\nprovide the subset of S involved in any possible counter-examples that prove * is not commutative. Provide your answer as a comma separated list of the elements in the set in alphabetical order.","Level":"1","file_name":"","answer":"a, b, c, e"}
|
| 7 |
+
{"task_id":"9d191bce-651d-4746-be2d-7ef8ecadb9c2","question":"Examine the video at https:\/\/www.youtube.com\/watch?v=1htKBjuUWec.\n\nWhat does Teal'c say in response to the question \"Isn't that hot?\"","Level":"1","file_name":"","answer":""}
|
| 8 |
+
{"task_id":"cabe07ed-9eca-40ea-8ead-410ef5e83f91","question":"What is the surname of the equine veterinarian mentioned in 1.E Exercises from the chemistry materials licensed by Marisa Alviar-Agnew & Henry Agnew under the CK-12 license in LibreText's Introductory Chemistry materials as compiled 08\/21\/2023?","Level":"1","file_name":"","answer":"Louvrier"}
|
| 9 |
+
{"task_id":"3cef3a44-215e-4aed-8e3b-b1e3f08063b7","question":"I'm making a grocery list for my mom, but she's a professor of botany and she's a real stickler when it comes to categorizing things. I need to add different foods to different categories on the grocery list, but if I make a mistake, she won't buy anything inserted in the wrong category. Here's the list I have so far:\n\nmilk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts\n\nI need to make headings for the fruits and vegetables. Could you please create a list of just the vegetables from my list? If you could do that, then I can figure out how to categorize the rest of the list into the appropriate categories. But remember that my mom is a real stickler, so make sure that no botanical fruits end up on the vegetable list, or she won't get them when she's at the store. Please alphabetize the list of vegetables, and place each item in a comma separated list.","Level":"1","file_name":"","answer":"broccoli, celery, corn, fresh basil, green beans, lettuce, sweet potatoes"}
|
| 10 |
+
{"task_id":"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3","question":"Hi, I'm making a pie but I could use some help with my shopping list. I have everything I need for the crust, but I'm not sure about the filling. I got the recipe from my friend Aditi, but she left it as a voice memo and the speaker on my phone is buzzing so I can't quite make out what she's saying. Could you please listen to the recipe and list all of the ingredients that my friend described? I only want the ingredients for the filling, as I have everything I need to make my favorite pie crust. I've attached the recipe as Strawberry pie.mp3.\n\nIn your response, please only list the ingredients, not any measurements. So if the recipe calls for \"a pinch of salt\" or \"two cups of ripe strawberries\" the ingredients on the list would be \"salt\" and \"ripe strawberries\".\n\nPlease format your response as a comma separated list of ingredients. Also, please alphabetize the ingredients.","Level":"1","file_name":"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3","answer":""}
|
| 11 |
+
{"task_id":"305ac316-eef6-4446-960a-92d80d542f82","question":"Who did the actor who played Ray in the Polish-language version of Everybody Loves Raymond play in Magda M.? Give only the first name.","Level":"1","file_name":"","answer":"Wojciech"}
|
| 12 |
+
{"task_id":"f918266a-b3e0-4914-865d-4faa564f1aef","question":"What is the final numeric output from the attached Python code?","Level":"1","file_name":"f918266a-b3e0-4914-865d-4faa564f1aef.py","answer":""}
|
| 13 |
+
{"task_id":"3f57289b-8c60-48be-bd80-01f8099ca449","question":"How many at bats did the Yankee with the most walks in the 1977 regular season have that same season?","Level":"1","file_name":"","answer":"589"}
|
| 14 |
+
{"task_id":"1f975693-876d-457b-a649-393859e79bf3","question":"Hi, I was out sick from my classes on Friday, so I'm trying to figure out what I need to study for my Calculus mid-term next week. My friend from class sent me an audio recording of Professor Willowbrook giving out the recommended reading for the test, but my headphones are broken :(\n\nCould you please listen to the recording for me and tell me the page numbers I'm supposed to go over? I've attached a file called Homework.mp3 that has the recording. Please provide just the page numbers as a comma-delimited list. And please provide the list in ascending order.","Level":"1","file_name":"1f975693-876d-457b-a649-393859e79bf3.mp3","answer":""}
|
| 15 |
+
{"task_id":"840bfca7-4f7b-481a-8794-c560c340185d","question":"On June 6, 2023, an article by Carolyn Collins Petersen was published in Universe Today. This article mentions a team that produced a paper about their observations, linked at the bottom of the article. Find this paper. Under what NASA award number was the work performed by R. G. Arendt supported by?","Level":"1","file_name":"","answer":"80GSFC21M0002"}
|
| 16 |
+
{"task_id":"bda648d7-d618-4883-88f4-3466eabd860e","question":"Where were the Vietnamese specimens described by Kuznetzov in Nedoshivina's 2010 paper eventually deposited? Just give me the city name without abbreviations.","Level":"1","file_name":"","answer":"Saint Petersburg"}
|
| 17 |
+
{"task_id":"cf106601-ab4f-4af9-b045-5295fe67b37d","question":"What country had the least number of athletes at the 1928 Summer Olympics? If there's a tie for a number of athletes, return the first in alphabetical order. Give the IOC country code as your answer.","Level":"1","file_name":"","answer":"CUB"}
|
| 18 |
+
{"task_id":"a0c07678-e491-4bbc-8f0b-07405144218f","question":"Who are the pitchers with the number before and after Taish\u014d Tamai's number as of July 2023? Give them to me in the form Pitcher Before, Pitcher After, use their last names only, in Roman characters.","Level":"1","file_name":"","answer":"Yamasaki, Uehara"}
|
| 19 |
+
{"task_id":"7bd855d8-463d-4ed5-93ca-5fe35145f733","question":"The attached Excel file contains the sales of menu items for a local fast-food chain. What were the total sales that the chain made from food (not including drinks)? Express your answer in USD with two decimal places.","Level":"1","file_name":"7bd855d8-463d-4ed5-93ca-5fe35145f733.xlsx","answer":""}
|
| 20 |
+
{"task_id":"5a0c1adf-205e-4841-a666-7c3ef95def9d","question":"What is the first name of the only Malko Competition recipient from the 20th Century (after 1977) whose nationality on record is a country that no longer exists?","Level":"1","file_name":"","answer":"Claus"}
|
requirements.txt
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==24.1.0
|
| 2 |
+
annotated-types==0.7.0
|
| 3 |
+
anyio==4.9.0
|
| 4 |
+
audioop-lts==0.2.1
|
| 5 |
+
beautifulsoup4==4.13.4
|
| 6 |
+
bs4==0.0.2
|
| 7 |
+
certifi==2025.4.26
|
| 8 |
+
charset-normalizer==3.4.2
|
| 9 |
+
click==8.2.1
|
| 10 |
+
colorama==0.4.6
|
| 11 |
+
distro==1.9.0
|
| 12 |
+
duckduckgo-search==8.0.2
|
| 13 |
+
fastapi==0.115.12
|
| 14 |
+
ffmpy==0.5.0
|
| 15 |
+
filelock==3.18.0
|
| 16 |
+
fsspec==2025.5.1
|
| 17 |
+
gradio==5.31.0
|
| 18 |
+
gradio-client==1.10.1
|
| 19 |
+
greenlet==3.2.2
|
| 20 |
+
groovy==0.1.2
|
| 21 |
+
h11==0.16.0
|
| 22 |
+
httpcore==1.0.9
|
| 23 |
+
httpx==0.28.1
|
| 24 |
+
huggingface-hub==0.32.0
|
| 25 |
+
idna==3.10
|
| 26 |
+
jinja2==3.1.6
|
| 27 |
+
jiter==0.10.0
|
| 28 |
+
jsonpatch==1.33
|
| 29 |
+
jsonpointer==3.0.0
|
| 30 |
+
langchain==0.3.25
|
| 31 |
+
langchain-core==0.3.61
|
| 32 |
+
langchain-openai==0.3.18
|
| 33 |
+
langchain-text-splitters==0.3.8
|
| 34 |
+
langgraph==0.4.7
|
| 35 |
+
langgraph-checkpoint==2.0.26
|
| 36 |
+
langgraph-prebuilt==0.2.1
|
| 37 |
+
langgraph-sdk==0.1.70
|
| 38 |
+
langsmith==0.3.42
|
| 39 |
+
lxml==5.4.0
|
| 40 |
+
markdown-it-py==3.0.0
|
| 41 |
+
markupsafe==3.0.2
|
| 42 |
+
mdurl==0.1.2
|
| 43 |
+
numpy==2.2.6
|
| 44 |
+
openai==1.82.0
|
| 45 |
+
orjson==3.10.18
|
| 46 |
+
ormsgpack==1.9.1
|
| 47 |
+
packaging==24.2
|
| 48 |
+
pandas==2.2.3
|
| 49 |
+
pillow==11.2.1
|
| 50 |
+
primp==0.15.0
|
| 51 |
+
pydantic==2.11.5
|
| 52 |
+
pydantic-core==2.33.2
|
| 53 |
+
pydub==0.25.1
|
| 54 |
+
pygments==2.19.1
|
| 55 |
+
pytesseract==0.3.13
|
| 56 |
+
python-dateutil==2.9.0.post0
|
| 57 |
+
python-dotenv==1.1.0
|
| 58 |
+
python-multipart==0.0.20
|
| 59 |
+
pytz==2025.2
|
| 60 |
+
pyyaml==6.0.2
|
| 61 |
+
regex==2024.11.6
|
| 62 |
+
requests==2.32.3
|
| 63 |
+
requests-toolbelt==1.0.0
|
| 64 |
+
rich==14.0.0
|
| 65 |
+
ruff==0.11.11
|
| 66 |
+
safehttpx==0.1.6
|
| 67 |
+
semantic-version==2.10.0
|
| 68 |
+
shellingham==1.5.4
|
| 69 |
+
six==1.17.0
|
| 70 |
+
sniffio==1.3.1
|
| 71 |
+
soupsieve==2.7
|
| 72 |
+
sqlalchemy==2.0.41
|
| 73 |
+
starlette==0.46.2
|
| 74 |
+
tenacity==9.1.2
|
| 75 |
+
tiktoken==0.9.0
|
| 76 |
+
tomlkit==0.13.2
|
| 77 |
+
tqdm==4.67.1
|
| 78 |
+
typer==0.15.3
|
| 79 |
+
typing-extensions==4.13.2
|
| 80 |
+
typing-inspection==0.4.1
|
| 81 |
+
tzdata==2025.2
|
| 82 |
+
urllib3==2.4.0
|
| 83 |
+
uvicorn==0.34.2
|
| 84 |
+
websockets==15.0.1
|
| 85 |
+
xxhash==3.5.0
|
| 86 |
+
zstandard==0.23.0
|
system_prompt.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 3 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 4 |
+
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.
|
| 5 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
tools.py
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from langchain.tools import tool
|
| 3 |
+
from duckduckgo_search import DDGS
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
import tempfile
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import os
|
| 8 |
+
from urllib.parse import urlparse
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@tool("search", return_direct=False)
|
| 12 |
+
def search(query: str) -> str:
|
| 13 |
+
"""Searches the internet using DuckDuckGo
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
query (str): Search query
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: Search results
|
| 20 |
+
"""
|
| 21 |
+
with DDGS() as ddgs:
|
| 22 |
+
results = [r for r in ddgs.text(query, max_results=5)]
|
| 23 |
+
return results if results else "No results found."
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@tool("process_content", return_direct=False)
|
| 27 |
+
def process_content(url: str) -> str:
|
| 28 |
+
"""Process content from a webpage
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
url (str): URL to get content
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
str: Content in the webpage
|
| 35 |
+
"""
|
| 36 |
+
response = requests.get(url)
|
| 37 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
| 38 |
+
return soup.get_text()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@tool("save_file")
|
| 42 |
+
def save_file(content: str, filename: Optional[str] = None) -> str:
|
| 43 |
+
"""
|
| 44 |
+
Save content to a temporary file and return the path.
|
| 45 |
+
Useful for processing files from the GAIA API.
|
| 46 |
+
|
| 47 |
+
Args:
|
| 48 |
+
content: The content to save to the file
|
| 49 |
+
filename: Optional filename, will generate a random name if not provided
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
Path to the saved file
|
| 53 |
+
"""
|
| 54 |
+
temp_dir = tempfile.gettempdir()
|
| 55 |
+
if filename is None:
|
| 56 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 57 |
+
filepath = temp_file.name
|
| 58 |
+
else:
|
| 59 |
+
filepath = os.path.join(temp_dir, filename)
|
| 60 |
+
|
| 61 |
+
# Write content to the file
|
| 62 |
+
with open(filepath, "w") as f:
|
| 63 |
+
f.write(content)
|
| 64 |
+
|
| 65 |
+
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@tool("download_file_from_url")
|
| 69 |
+
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 70 |
+
"""
|
| 71 |
+
Download a file from a URL and save it to a temporary location.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
url: The URL to download from
|
| 75 |
+
filename: Optional filename, will generate one based on URL if not provided
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
Path to the downloaded file
|
| 79 |
+
"""
|
| 80 |
+
try:
|
| 81 |
+
# Parse URL to get filename if not provided
|
| 82 |
+
if not filename:
|
| 83 |
+
path = urlparse(url).path
|
| 84 |
+
filename = os.path.basename(path)
|
| 85 |
+
if not filename:
|
| 86 |
+
# Generate a random name if we couldn't extract one
|
| 87 |
+
import uuid
|
| 88 |
+
|
| 89 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 90 |
+
|
| 91 |
+
# Create temporary file
|
| 92 |
+
temp_dir = tempfile.gettempdir()
|
| 93 |
+
filepath = os.path.join(temp_dir, filename)
|
| 94 |
+
|
| 95 |
+
# Download the file
|
| 96 |
+
response = requests.get(url, stream=True)
|
| 97 |
+
response.raise_for_status()
|
| 98 |
+
|
| 99 |
+
# Save the file
|
| 100 |
+
with open(filepath, "wb") as f:
|
| 101 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 102 |
+
f.write(chunk)
|
| 103 |
+
|
| 104 |
+
return f"File downloaded to {filepath}. You can now process this file."
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"Error downloading file: {str(e)}"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@tool("extract_text_from_image")
|
| 110 |
+
def extract_text_from_image(image_path: str) -> str:
|
| 111 |
+
"""
|
| 112 |
+
Extract text from an image using pytesseract (if available).
|
| 113 |
+
|
| 114 |
+
Args:
|
| 115 |
+
image_path: Path to the image file
|
| 116 |
+
|
| 117 |
+
Returns:
|
| 118 |
+
Extracted text or error message
|
| 119 |
+
"""
|
| 120 |
+
try:
|
| 121 |
+
# Try to import pytesseract
|
| 122 |
+
import pytesseract
|
| 123 |
+
from PIL import Image
|
| 124 |
+
|
| 125 |
+
# Open the image
|
| 126 |
+
image = Image.open(image_path)
|
| 127 |
+
|
| 128 |
+
# Extract text
|
| 129 |
+
text = pytesseract.image_to_string(image)
|
| 130 |
+
|
| 131 |
+
return f"Extracted text from image:\n\n{text}"
|
| 132 |
+
except ImportError:
|
| 133 |
+
return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
|
| 134 |
+
except Exception as e:
|
| 135 |
+
return f"Error extracting text from image: {str(e)}"
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
@tool("analyze_csv_file")
|
| 139 |
+
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 140 |
+
"""
|
| 141 |
+
Analyze a CSV file using pandas and answer a question about it.
|
| 142 |
+
|
| 143 |
+
Args:
|
| 144 |
+
file_path: Path to the CSV file
|
| 145 |
+
query: Question about the data
|
| 146 |
+
|
| 147 |
+
Returns:
|
| 148 |
+
Analysis result or error message
|
| 149 |
+
"""
|
| 150 |
+
try:
|
| 151 |
+
import pandas as pd
|
| 152 |
+
|
| 153 |
+
# Read the CSV file
|
| 154 |
+
df = pd.read_csv(file_path)
|
| 155 |
+
|
| 156 |
+
# Run various analyses based on the query
|
| 157 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 158 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 159 |
+
|
| 160 |
+
# Add summary statistics
|
| 161 |
+
result += "Summary statistics:\n"
|
| 162 |
+
result += str(df.describe())
|
| 163 |
+
|
| 164 |
+
return result
|
| 165 |
+
except ImportError:
|
| 166 |
+
return "Error: pandas is not installed. Please install it with 'pip install pandas'."
|
| 167 |
+
except Exception as e:
|
| 168 |
+
return f"Error analyzing CSV file: {str(e)}"
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
@tool("analyze_excel_file")
|
| 172 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 173 |
+
"""
|
| 174 |
+
Analyze an Excel file using pandas and answer a question about it.
|
| 175 |
+
|
| 176 |
+
Args:
|
| 177 |
+
file_path: Path to the Excel file
|
| 178 |
+
query: Question about the data
|
| 179 |
+
|
| 180 |
+
Returns:
|
| 181 |
+
Analysis result or error message
|
| 182 |
+
"""
|
| 183 |
+
try:
|
| 184 |
+
import pandas as pd
|
| 185 |
+
|
| 186 |
+
# Read the Excel file
|
| 187 |
+
df = pd.read_excel(file_path)
|
| 188 |
+
|
| 189 |
+
# Run various analyses based on the query
|
| 190 |
+
result = (
|
| 191 |
+
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 192 |
+
)
|
| 193 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 194 |
+
|
| 195 |
+
# Add summary statistics
|
| 196 |
+
result += "Summary statistics:\n"
|
| 197 |
+
result += str(df.describe())
|
| 198 |
+
|
| 199 |
+
return result
|
| 200 |
+
except ImportError:
|
| 201 |
+
return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
|
| 202 |
+
except Exception as e:
|
| 203 |
+
return f"Error analyzing Excel file: {str(e)}"
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def get_tools():
|
| 207 |
+
return [
|
| 208 |
+
search,
|
| 209 |
+
# process_content,
|
| 210 |
+
# save_file,
|
| 211 |
+
# download_file_from_url,
|
| 212 |
+
# extract_text_from_image,
|
| 213 |
+
# analyze_csv_file,
|
| 214 |
+
# analyze_excel_file
|
| 215 |
+
]
|