File size: 13,551 Bytes
4aea6f7 3cd45c9 4aea6f7 d888f9f 4aea6f7 d888f9f 4aea6f7 d888f9f | 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | import os
from dotenv import load_dotenv
from logger import logger
import base64
from PIL import Image
from io import BytesIO
import pytesseract
from typing import Dict, Any, Optional
from langchain_core.tools import tool
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.document_loaders import WikipediaLoader
from langchain_community.document_loaders import ArxivLoader
load_dotenv()
tavily_api_key = os.getenv("TAVILY_API_KEY")
if not tavily_api_key:
logger.warning("TAVILY_API_KEY not found in environment variables.")
# 1. Web search tool (using the @tool decorator)
@tool
def web_search(query: str) -> str:
"""Search the web for information.
Args:
query: The search query.
Returns:
str: Search results.
"""
search_tool = TavilySearchResults(api_key=tavily_api_key)
return search_tool.invoke(query)
# 2. Wiki search tool
@tool
def wiki_search(query: str) -> str:
"""Search Wikipedia for information.
Args:
query: The search topic.
Returns:
str: Wikipedia article content.
"""
try:
loader = WikipediaLoader(query=query, load_max_docs=2)
docs = loader.load()
return "\n\n".join([doc.page_content for doc in docs])
except Exception as e:
return f"Error searching Wikipedia: {str(e)}"
@tool
def arxiv_search(query: str) -> str:
"""Search Arxiv for a query and return maximum 3 result.
Args:
query: The search query.
Returns:
An array documents
"""
print("arxiv_search")
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
formatted_search_docs = "\n\n---\n\n".join(
[
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
for doc in search_docs
])
return {"arvix_results": formatted_search_docs}
# 3. Basic Math Operations
@tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers.
Args:
a: The first number.
b: The second number.
Returns:
float: The product of the two numbers.
"""
return a * b
@tool
def add(a: float, b: float) -> float:
"""Add two numbers.
Args:
a: The first number.
b: The second number.
Returns:
float: The sum of the two numbers.
"""
return a + b
@tool
def subtract(a: float, b: float) -> float:
"""Subtract two numbers.
Args:
a: The first number.
b: The second number.
Returns:
float: The difference between the two numbers.
"""
return a - b
@tool
def divide(a: float, b: float) -> float:
"""Divide two numbers.
Args:
a: The first number.
b: The second number.
Returns:
float: The quotient of the division.
"""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
@tool
def square_root(a: float) -> float | complex:
"""
Get the square root of a number.
Args:
a (float): the number to get the square root of
"""
if a >= 0:
return a**0.5
return cmath.sqrt(a)
# 4. File Operations
@tool
def save_file(content: str, file_path: str) -> str:
"""Save content to a file.
Args:
content: Content to save.
file_path: Path to save the file.
Returns:
str: Status message.
"""
try:
with open(file_path, 'w') as f:
f.write(content)
return f"Successfully saved content to {file_path}"
except Exception as e:
return f"Error saving file: {str(e)}"
@tool
def read_file(file_path: str) -> str:
"""Read content from a file.
Args:
file_path: Path to the file.
Returns:
str: File content.
"""
try:
with open(file_path, 'r') as f:
content = f.read()
return content
except Exception as e:
return f"Error reading file: {str(e)}"
# 5. OCR for images
@tool
def extract_text_from_image(image_path_or_base64: str) -> str:
"""Extract text from an image using OCR.
Args:
image_path_or_base64: Path to image file or base64 encoded image.
Returns:
str: Extracted text.
"""
try:
# Check if input is a file path or base64
if os.path.exists(image_path_or_base64):
image = Image.open(image_path_or_base64)
else:
# Assume it's base64
try:
image_data = base64.b64decode(image_path_or_base64)
image = Image.open(BytesIO(image_data))
except:
return "Error: Invalid image path or base64 string"
# Extract text
text = pytesseract.image_to_string(image)
return text
except Exception as e:
return f"Error extracting text from image: {str(e)}"
@tool
def get_youtube_transcript(page_url: str) -> str:
"""Get the transcript of a YouTube video
Args:
page_url (str): YouTube URL of the video
"""
try:
# get video ID from URL
video_id = extract.video_id(page_url)
# get transcript
ytt_api = YouTubeTranscriptApi()
transcript = ytt_api.fetch(video_id)
# keep only text
txt = '\n'.join([s.text for s in transcript.snippets])
return txt
except Exception as e:
return f"get_youtube_transcript failed: {e}"
@tool
def transcribe_audio(file_path: str):
"""
Transcribes an audio file to text using local Whisper model.
Args:
file_path: Path to the audio file
Returns:
A dictionary containing the transcription and metadata
"""
try:
print(f"Transcribing audio file: {file_path}")
# Validate file exists
if not os.path.exists(file_path):
return {
"status": "error",
"message": f"File not found: {file_path}"
}
# Load a Whisper model - we'll use the small model for better performance
# Options include: tiny, base, small, medium, large
model = whisper.load_model("small")
# Transcribe the audio
result = model.transcribe(file_path)
print({
"status": "success",
"transcription": result["text"],
"language": result.get("language", "unknown"),
"file_path": file_path
})
# Return the transcription and metadata
return {
"status": "success",
"transcription": result["text"],
"language": result.get("language", "unknown"),
"file_path": file_path
}
except Exception as e:
print({
"status": "error",
"message": f"Error transcribing audio: {str(e)}"
})
return {
"status": "error",
"message": f"Error transcribing audio: {str(e)}"
}
@tool
def execute_code_multilang(code: str, language: str = "python") -> str:
"""Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results.
Args:
code (str): The source code to execute.
language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java".
Returns:
A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any).
"""
supported_languages = ["python", "bash", "sql", "c", "java"]
language = language.lower()
if language not in supported_languages:
return f"❌ Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}"
result = interpreter_instance.execute_code(code, language=language)
response = []
if result["status"] == "success":
response.append(f"✅ Code executed successfully in **{language.upper()}**")
if result.get("stdout"):
response.append(
"\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```"
)
if result.get("stderr"):
response.append(
"\n**Standard Error (if any):**\n```\n"
+ result["stderr"].strip()
+ "\n```"
)
if result.get("result") is not None:
response.append(
"\n**Execution Result:**\n```\n"
+ str(result["result"]).strip()
+ "\n```"
)
if result.get("dataframes"):
for df_info in result["dataframes"]:
response.append(
f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**"
)
df_preview = pd.DataFrame(df_info["head"])
response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```")
if result.get("plots"):
response.append(
f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)"
)
else:
response.append(f"❌ Code execution failed in **{language.upper()}**")
if result.get("stderr"):
response.append(
"\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```"
)
return "\n".join(response)
@tool
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
"""
Download a file from a URL and save it to a temporary location.
Args:
url (str): the URL of the file to download.
filename (str, optional): the name of the file. If not provided, a random name file will be created.
"""
try:
# Parse URL to get filename if not provided
if not filename:
path = urlparse(url).path
filename = os.path.basename(path)
if not filename:
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
# Create temporary file
temp_dir = tempfile.gettempdir()
filepath = os.path.join(temp_dir, filename)
# Download the file
response = requests.get(url, stream=True)
response.raise_for_status()
# Save the file
with open(filepath, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return f"File downloaded to {filepath}. You can read this file to process its contents."
except Exception as e:
return f"Error downloading file: {str(e)}"
@tool
def analyze_csv_file(file_path: str, query: str) -> str:
"""
Analyze a CSV file using pandas and answer a question about it.
Args:
file_path (str): the path to the CSV file.
query (str): Question about the data
"""
try:
# Read the CSV file
df = pd.read_csv(file_path)
# Run various analyses based on the query
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
result += f"Columns: {', '.join(df.columns)}\n\n"
# Add summary statistics
result += "Summary statistics:\n"
result += str(df.describe())
return result
except Exception as e:
return f"Error analyzing CSV file: {str(e)}"
@tool
def analyze_excel_file(file_path: str, query: str) -> str:
"""
Analyze an Excel file using pandas and answer a question about it.
Args:
file_path (str): the path to the Excel file.
query (str): Question about the data
"""
try:
# Read the Excel file
df = pd.read_excel(file_path)
# Run various analyses based on the query
result = (
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
)
result += f"Columns: {', '.join(df.columns)}\n\n"
# Add summary statistics
result += "Summary statistics:\n"
result += str(df.describe())
return result
except Exception as e:
return f"Error analyzing Excel file: {str(e)}"
@tool
def analyze_image(image_base64: str) -> Dict[str, Any]:
"""
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview).
Args:
image_base64 (str): Base64 encoded image string
Returns:
Dictionary with analysis result
"""
try:
img = decode_image(image_base64)
width, height = img.size
mode = img.mode
if mode in ("RGB", "RGBA"):
arr = np.array(img)
avg_colors = arr.mean(axis=(0, 1))
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])]
brightness = avg_colors.mean()
color_analysis = {
"average_rgb": avg_colors.tolist(),
"brightness": brightness,
"dominant_color": dominant,
}
else:
color_analysis = {"note": f"No color analysis for mode {mode}"}
thumbnail = img.copy()
thumbnail.thumbnail((100, 100))
thumb_path = save_image(thumbnail, "thumbnails")
thumbnail_base64 = encode_image(thumb_path)
return {
"dimensions": (width, height),
"mode": mode,
"color_analysis": color_analysis,
"thumbnail": thumbnail_base64,
}
except Exception as e:
return {"error": str(e)}
|