Upload 16 files
Browse files- prompts/system_prompt.txt +8 -0
- tools/file/analyze_csv_file.py +28 -0
- tools/file/analyze_excel_file.py +30 -0
- tools/file/analyze_image.py +73 -0
- tools/file/download_file_from_url.py +41 -0
- tools/file/save_content_to_file.py +26 -0
- tools/math/add.py +12 -0
- tools/math/divide.py +14 -0
- tools/math/modulus.py +12 -0
- tools/math/multiply.py +11 -0
- tools/math/power.py +12 -0
- tools/math/square_root.py +14 -0
- tools/math/subtract.py +12 -0
- tools/search/arxiv_search.py +18 -0
- tools/search/web_search.py +15 -0
- tools/search/wiki_search.py +18 -0
prompts/system_prompt.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
| 2 |
+
You must only respond with one sentence using this strict format:
|
| 3 |
+
FINAL ANSWER: [your final answer]
|
| 4 |
+
- The final answer must be only a number, a single string, or a comma-separated list.
|
| 5 |
+
- Do not include explanations, reasoning, references, or quotes after the final answer.
|
| 6 |
+
- Do not apologize or hedge your answer.
|
| 7 |
+
- If you are unsure, use the best answer based on available information and still format it as required.
|
| 8 |
+
- The format must exactly follow FINAL ANSWER: [your final answer] and nothing else.
|
tools/file/analyze_csv_file.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Analyze a CSV file using pandas and answer a question about it.
|
| 9 |
+
Args:
|
| 10 |
+
file_path (str): the path to the CSV file.
|
| 11 |
+
query (str): Question about the data
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Read the CSV file
|
| 15 |
+
df = pd.read_csv(file_path)
|
| 16 |
+
|
| 17 |
+
# Run various analyses based on the query
|
| 18 |
+
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 19 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 20 |
+
|
| 21 |
+
# Add summary statistics
|
| 22 |
+
result += "Summary statistics:\n"
|
| 23 |
+
result += str(df.describe())
|
| 24 |
+
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error analyzing CSV file: {str(e)}"
|
tools/file/analyze_excel_file.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Analyze an Excel file using pandas and answer a question about it.
|
| 9 |
+
Args:
|
| 10 |
+
file_path (str): the path to the Excel file.
|
| 11 |
+
query (str): Question about the data
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Read the Excel file
|
| 15 |
+
df = pd.read_excel(file_path)
|
| 16 |
+
|
| 17 |
+
# Run various analyses based on the query
|
| 18 |
+
result = (
|
| 19 |
+
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
|
| 20 |
+
)
|
| 21 |
+
result += f"Columns: {', '.join(df.columns)}\n\n"
|
| 22 |
+
|
| 23 |
+
# Add summary statistics
|
| 24 |
+
result += "Summary statistics:\n"
|
| 25 |
+
result += str(df.describe())
|
| 26 |
+
|
| 27 |
+
return result
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error analyzing Excel file: {str(e)}"
|
tools/file/analyze_image.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
import uuid
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# Helper functions for image processing
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def decode_image(base64_string: str) -> Image.Image:
|
| 14 |
+
"""Convert a base64 string to a PIL Image."""
|
| 15 |
+
image_data = base64.b64decode(base64_string)
|
| 16 |
+
return Image.open(io.BytesIO(image_data))
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
| 20 |
+
"""Save a PIL Image to disk and return the path."""
|
| 21 |
+
os.makedirs(directory, exist_ok=True)
|
| 22 |
+
image_id = str(uuid.uuid4())
|
| 23 |
+
image_path = os.path.join(directory, f"{image_id}.png")
|
| 24 |
+
image.save(image_path)
|
| 25 |
+
return image_path
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def encode_image(image_path: str) -> str:
|
| 29 |
+
"""Convert an image file to base64 string."""
|
| 30 |
+
with open(image_path, "rb") as image_file:
|
| 31 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@tool
|
| 35 |
+
def analyze_image(image_base64: str) -> Dict[str, Any]:
|
| 36 |
+
"""
|
| 37 |
+
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview).
|
| 38 |
+
Args:
|
| 39 |
+
image_base64 (str): Base64 encoded image string
|
| 40 |
+
Returns:
|
| 41 |
+
Dictionary with analysis result
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
img = decode_image(image_base64)
|
| 45 |
+
width, height = img.size
|
| 46 |
+
mode = img.mode
|
| 47 |
+
|
| 48 |
+
if mode in ("RGB", "RGBA"):
|
| 49 |
+
arr = np.array(img)
|
| 50 |
+
avg_colors = arr.mean(axis=(0, 1))
|
| 51 |
+
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])]
|
| 52 |
+
brightness = avg_colors.mean()
|
| 53 |
+
color_analysis = {
|
| 54 |
+
"average_rgb": avg_colors.tolist(),
|
| 55 |
+
"brightness": brightness,
|
| 56 |
+
"dominant_color": dominant,
|
| 57 |
+
}
|
| 58 |
+
else:
|
| 59 |
+
color_analysis = {"note": f"No color analysis for mode {mode}"}
|
| 60 |
+
|
| 61 |
+
thumbnail = img.copy()
|
| 62 |
+
thumbnail.thumbnail((100, 100))
|
| 63 |
+
thumb_path = save_image(thumbnail, "thumbnails")
|
| 64 |
+
thumbnail_base64 = encode_image(thumb_path)
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"dimensions": (width, height),
|
| 68 |
+
"mode": mode,
|
| 69 |
+
"color_analysis": color_analysis,
|
| 70 |
+
"thumbnail": thumbnail_base64,
|
| 71 |
+
}
|
| 72 |
+
except Exception as e:
|
| 73 |
+
return {"error": str(e)}
|
tools/file/download_file_from_url.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import os
|
| 4 |
+
from urllib.parse import urlparse
|
| 5 |
+
import requests
|
| 6 |
+
import uuid
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@tool
|
| 11 |
+
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 12 |
+
"""
|
| 13 |
+
Download a file from a URL and save it to a temporary location.
|
| 14 |
+
Args:
|
| 15 |
+
url (str): the URL of the file to download.
|
| 16 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# Parse URL to get filename if not provided
|
| 20 |
+
if not filename:
|
| 21 |
+
path = urlparse(url).path
|
| 22 |
+
filename = os.path.basename(path)
|
| 23 |
+
if not filename:
|
| 24 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
| 25 |
+
|
| 26 |
+
# Create temporary file
|
| 27 |
+
temp_dir = tempfile.gettempdir()
|
| 28 |
+
filepath = os.path.join(temp_dir, filename)
|
| 29 |
+
|
| 30 |
+
# Download the file
|
| 31 |
+
response = requests.get(url, stream=True)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
|
| 34 |
+
# Save the file
|
| 35 |
+
with open(filepath, "wb") as f:
|
| 36 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 37 |
+
f.write(chunk)
|
| 38 |
+
|
| 39 |
+
return f"File downloaded to {filepath}. You can read this file to process its contents."
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"Error downloading file: {str(e)}"
|
tools/file/save_content_to_file.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from typing import Optional
|
| 3 |
+
import os
|
| 4 |
+
from urllib.parse import urlparse
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@tool
|
| 9 |
+
def save_content_to_file(content: str, filename: Optional[str] = None) -> str:
|
| 10 |
+
"""
|
| 11 |
+
Save content to a file and return the path.
|
| 12 |
+
Args:
|
| 13 |
+
content (str): the content to save to the file
|
| 14 |
+
filename (str, optional): the name of the file. If not provided, a random name file will be created.
|
| 15 |
+
"""
|
| 16 |
+
temp_dir = tempfile.gettempdir()
|
| 17 |
+
if filename is None:
|
| 18 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)
|
| 19 |
+
filepath = temp_file.name
|
| 20 |
+
else:
|
| 21 |
+
filepath = os.path.join(temp_dir, filename)
|
| 22 |
+
|
| 23 |
+
with open(filepath, "w") as f:
|
| 24 |
+
f.write(content)
|
| 25 |
+
|
| 26 |
+
return f"File saved to {filepath}. You can read this file to process its contents."
|
tools/math/add.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def add(a: int, b: int) -> int:
|
| 6 |
+
"""Add two numbers.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
return a + b
|
tools/math/divide.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def divide(a: int, b: int) -> int:
|
| 6 |
+
"""Divide two numbers.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
if b == 0:
|
| 13 |
+
raise ValueError("Cannot divide by zero.")
|
| 14 |
+
return a / b
|
tools/math/modulus.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def modulus(a: int, b: int) -> int:
|
| 6 |
+
"""Get the modulus of two numbers.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
return a % b
|
tools/math/multiply.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def multiply(a: int, b: int) -> int:
|
| 6 |
+
"""Multiply two numbers.
|
| 7 |
+
Args:
|
| 8 |
+
a: first int
|
| 9 |
+
b: second int
|
| 10 |
+
"""
|
| 11 |
+
return a * b
|
tools/math/power.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def power(a: float, b: float) -> float:
|
| 6 |
+
"""
|
| 7 |
+
Get the power of two numbers.
|
| 8 |
+
Args:
|
| 9 |
+
a (float): the first number
|
| 10 |
+
b (float): the second number
|
| 11 |
+
"""
|
| 12 |
+
return a**b
|
tools/math/square_root.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
import cmath
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def square_root(a: float) -> float | complex:
|
| 7 |
+
"""
|
| 8 |
+
Get the square root of a number.
|
| 9 |
+
Args:
|
| 10 |
+
a (float): the number to get the square root of
|
| 11 |
+
"""
|
| 12 |
+
if a >= 0:
|
| 13 |
+
return a**0.5
|
| 14 |
+
return cmath.sqrt(a)
|
tools/math/subtract.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def subtract(a: int, b: int) -> int:
|
| 6 |
+
"""Subtract two numbers.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
a: first int
|
| 10 |
+
b: second int
|
| 11 |
+
"""
|
| 12 |
+
return a - b
|
tools/search/arxiv_search.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def arxiv_search(query: str) -> str:
|
| 7 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
query: The search query."""
|
| 11 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 12 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 13 |
+
[
|
| 14 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 15 |
+
for doc in search_docs
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
return {"arvix_results": formatted_search_docs}
|
tools/search/web_search.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def web_search(input: str) -> dict:
|
| 7 |
+
"""Perform a web search and return maximum 3 results."""
|
| 8 |
+
search_docs = TavilySearchResults(max_results=3).invoke(input)
|
| 9 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 10 |
+
[
|
| 11 |
+
f'<Document source="{doc["url"]}" title="{doc["title"]}"/>\n{doc["content"]}\n</Document>'
|
| 12 |
+
for doc in search_docs
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
return {"web_results": formatted_search_docs}
|
tools/search/wiki_search.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def wiki_search(query: str) -> str:
|
| 7 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
query: The search query."""
|
| 11 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 12 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 13 |
+
[
|
| 14 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 15 |
+
for doc in search_docs
|
| 16 |
+
]
|
| 17 |
+
)
|
| 18 |
+
return {"wiki_results": formatted_search_docs}
|