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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
|
|
|
| 20 |
@tool
|
| 21 |
def add(a: int, b: int) -> int:
|
| 22 |
""" Add a and b """
|
|
@@ -95,7 +257,8 @@ If you are asked for a comma separated list, apply the above rules depending of
|
|
| 95 |
system_message = SystemMessage(content=system_prompt)
|
| 96 |
|
| 97 |
tools = [
|
| 98 |
-
add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search
|
|
|
|
| 99 |
]
|
| 100 |
|
| 101 |
|
|
|
|
| 16 |
|
| 17 |
#load_dotenv()
|
| 18 |
google_api_key = os.environ["GOOGLE_API_KEY"]
|
| 19 |
+
#@@@@@@@@@@@@@@@@@/////////////////////////////////////////////////
|
| 20 |
+
@tool
|
| 21 |
+
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Save content to a temporary file and return the path.
|
| 24 |
+
Useful for processing files from the GAIA API.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
content: The content to save to the file
|
| 28 |
+
filename: Optional filename, will generate a random name if not provided
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
Path to the saved file
|
| 32 |
+
"""
|
| 33 |
+
temp_dir = tempfile.gettempdir()
|
| 34 |
+
if filename is None:
|
| 35 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
| 36 |
+
filepath = temp_file.name
|
| 37 |
+
else:
|
| 38 |
+
filepath = os.path.join(temp_dir, filename)
|
| 39 |
+
|
| 40 |
+
# Write content to the file
|
| 41 |
+
with open(filepath, 'w') as f:
|
| 42 |
+
f.write(content)
|
| 43 |
+
|
| 44 |
+
return f"File saved to {filepath}. You can read this file to process its contents."
|
| 45 |
+
|
| 46 |
+
@tool
|
| 47 |
+
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 48 |
+
"""
|
| 49 |
+
Download a file from a URL and save it to a temporary location.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
url: The URL to download from
|
| 53 |
+
filename: Optional filename, will generate one based on URL if not provided
|
| 54 |
+
|
| 55 |
+
Returns:
|
| 56 |
+
Path to the downloaded file
|
| 57 |
+
"""
|
| 58 |
+
try:
|
| 59 |
+
# Parse URL to get filename if not provided
|
| 60 |
+
if not filename:
|
| 61 |
+
path = urlparse(url).path
|
| 62 |
+
filename = os.path.basename(path)
|
| 63 |
+
if not filename:
|
| 64 |
+
# Generate a random name if we couldn't extract one
|
| 65 |
+
import uuid
|
| 66 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 67 |
+
|
| 68 |
+
# Create temporary file
|
| 69 |
+
temp_dir = tempfile.gettempdir()
|
| 70 |
+
filepath = os.path.join(temp_dir, filename)
|
| 71 |
+
|
| 72 |
+
# Download the file
|
| 73 |
+
response = requests.get(url, stream=True)
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
|
| 76 |
+
# Save the file
|
| 77 |
+
with open(filepath, 'wb') as f:
|
| 78 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 79 |
+
f.write(chunk)
|
| 80 |
+
|
| 81 |
+
return f"File downloaded to {filepath}. You can now process this file."
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"Error downloading file: {str(e)}"
|
| 84 |
+
|
| 85 |
+
@tool
|
| 86 |
+
def extract_text_from_image(image_path: str) -> str:
|
| 87 |
+
"""
|
| 88 |
+
Extract text from an image using pytesseract (if available).
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
image_path: Path to the image file
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Extracted text or error message
|
| 95 |
+
"""
|
| 96 |
+
try:
|
| 97 |
+
# Try to import pytesseract
|
| 98 |
+
import pytesseract
|
| 99 |
+
from PIL import Image
|
| 100 |
+
|
| 101 |
+
# Open the image
|
| 102 |
+
image = Image.open(image_path)
|
| 103 |
+
|
| 104 |
+
# Extract text
|
| 105 |
+
text = pytesseract.image_to_string(image)
|
| 106 |
+
|
| 107 |
+
return f"Extracted text from image:\n\n{text}"
|
| 108 |
+
except ImportError:
|
| 109 |
+
return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
|
| 110 |
+
except Exception as e:
|
| 111 |
+
return f"Error extracting text from image: {str(e)}"
|
| 112 |
+
|
| 113 |
+
@tool
|
| 114 |
+
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 115 |
+
"""
|
| 116 |
+
Analyze a CSV file using pandas and answer a question about it.
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
file_path: Path to the CSV file
|
| 120 |
+
query: Question about the data
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
Analysis result or error message
|
| 124 |
+
"""
|
| 125 |
+
try:
|
| 126 |
+
import pandas as pd
|
| 127 |
+
|
| 128 |
+
# Read the CSV file
|
| 129 |
+
df = pd.read_csv(file_path)
|
| 130 |
+
|
| 131 |
+
# Run various analyses based on the query
|
| 132 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 133 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 134 |
+
|
| 135 |
+
# Add summary statistics
|
| 136 |
+
result += "Summary statistics:\n"
|
| 137 |
+
result += str(df.describe())
|
| 138 |
+
|
| 139 |
+
return result
|
| 140 |
+
except ImportError:
|
| 141 |
+
return "Error: pandas is not installed. Please install it with 'pip install pandas'."
|
| 142 |
+
except Exception as e:
|
| 143 |
+
return f"Error analyzing CSV file: {str(e)}"
|
| 144 |
+
|
| 145 |
+
@tool
|
| 146 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 147 |
+
"""
|
| 148 |
+
Analyze an Excel file using pandas and answer a question about it.
|
| 149 |
+
|
| 150 |
+
Args:
|
| 151 |
+
file_path: Path to the Excel file
|
| 152 |
+
query: Question about the data
|
| 153 |
+
|
| 154 |
+
Returns:
|
| 155 |
+
Analysis result or error message
|
| 156 |
+
"""
|
| 157 |
+
try:
|
| 158 |
+
import pandas as pd
|
| 159 |
+
|
| 160 |
+
# Read the Excel file
|
| 161 |
+
df = pd.read_excel(file_path)
|
| 162 |
+
|
| 163 |
+
# Run various analyses based on the query
|
| 164 |
+
result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 165 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 166 |
+
|
| 167 |
+
# Add summary statistics
|
| 168 |
+
result += "Summary statistics:\n"
|
| 169 |
+
result += str(df.describe())
|
| 170 |
+
|
| 171 |
+
return result
|
| 172 |
+
except ImportError:
|
| 173 |
+
return "Error: pandas and openpyxl are not installed. Please install them with 'pip install pandas openpyxl'."
|
| 174 |
+
except Exception as e:
|
| 175 |
+
return f"Error analyzing Excel file: {str(e)}"
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
|
| 180 |
|
| 181 |
+
#@@@@@@@@@@@@@@@@@@///////////////////////////////////////////////
|
| 182 |
@tool
|
| 183 |
def add(a: int, b: int) -> int:
|
| 184 |
""" Add a and b """
|
|
|
|
| 257 |
system_message = SystemMessage(content=system_prompt)
|
| 258 |
|
| 259 |
tools = [
|
| 260 |
+
add,subtract,multiply,divide,web_search,wikipedia_search,arxiv_search,
|
| 261 |
+
save_and_read_file,download_file_from_url,extract_text_from_image,analyze_csv_file,analyze_excel_file
|
| 262 |
]
|
| 263 |
|
| 264 |
|