Spaces:
Sleeping
Sleeping
EtienneB
commited on
Commit
·
30b4543
1
Parent(s):
374dd02
updates
Browse files
agent.py
CHANGED
|
@@ -5,14 +5,13 @@ import re
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from langchain_core.messages import (AIMessage, HumanMessage, SystemMessage,
|
| 7 |
ToolMessage)
|
| 8 |
-
from langchain_huggingface import
|
| 9 |
-
HuggingFaceEndpoint)
|
| 10 |
from langgraph.graph import START, MessagesState, StateGraph
|
| 11 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 12 |
|
| 13 |
from tools import (absolute, add, analyze_csv_file, analyze_excel_file,
|
| 14 |
arvix_search, audio_transcription, compound_interest,
|
| 15 |
-
convert_temperature, divide, exponential,
|
| 16 |
extract_text_from_image, factorial, floor_divide,
|
| 17 |
get_current_time_in_timezone, greatest_common_divisor,
|
| 18 |
is_prime, least_common_multiple, logarithm, modulus,
|
|
@@ -35,6 +34,7 @@ tools = [
|
|
| 35 |
wiki_search, analyze_excel_file, arvix_search,
|
| 36 |
audio_transcription, python_code_parser, analyze_csv_file,
|
| 37 |
extract_text_from_image, reverse_sentence, web_content_extract,
|
|
|
|
| 38 |
]
|
| 39 |
|
| 40 |
# Updated system prompt for cleaner output
|
|
@@ -42,7 +42,9 @@ system_prompt = """
|
|
| 42 |
You are a helpful AI assistant. When asked a question, think through it step by step and provide only the final answer.
|
| 43 |
|
| 44 |
CRITICAL INSTRUCTIONS:
|
|
|
|
| 45 |
- Use available tools when needed to gather information or perform calculations
|
|
|
|
| 46 |
- After using tools and analyzing the information, provide ONLY the final answer
|
| 47 |
- Do not include explanations, reasoning, or extra text in your final response
|
| 48 |
- If the answer is a number, provide just the number (no units unless specifically requested)
|
|
@@ -190,5 +192,4 @@ if __name__ == "__main__":
|
|
| 190 |
else:
|
| 191 |
print("❌ Output is NOT in the correct format!")
|
| 192 |
except Exception as e:
|
| 193 |
-
print("❌ Output is NOT in the correct format!", e)
|
| 194 |
-
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from langchain_core.messages import (AIMessage, HumanMessage, SystemMessage,
|
| 7 |
ToolMessage)
|
| 8 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
|
|
|
| 9 |
from langgraph.graph import START, MessagesState, StateGraph
|
| 10 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 11 |
|
| 12 |
from tools import (absolute, add, analyze_csv_file, analyze_excel_file,
|
| 13 |
arvix_search, audio_transcription, compound_interest,
|
| 14 |
+
convert_temperature, divide, download_file, exponential,
|
| 15 |
extract_text_from_image, factorial, floor_divide,
|
| 16 |
get_current_time_in_timezone, greatest_common_divisor,
|
| 17 |
is_prime, least_common_multiple, logarithm, modulus,
|
|
|
|
| 34 |
wiki_search, analyze_excel_file, arvix_search,
|
| 35 |
audio_transcription, python_code_parser, analyze_csv_file,
|
| 36 |
extract_text_from_image, reverse_sentence, web_content_extract,
|
| 37 |
+
download_file,
|
| 38 |
]
|
| 39 |
|
| 40 |
# Updated system prompt for cleaner output
|
|
|
|
| 42 |
You are a helpful AI assistant. When asked a question, think through it step by step and provide only the final answer.
|
| 43 |
|
| 44 |
CRITICAL INSTRUCTIONS:
|
| 45 |
+
- If the question mentions attachments, files, images, documents, or URLs, use the download_file tool FIRST to download them
|
| 46 |
- Use available tools when needed to gather information or perform calculations
|
| 47 |
+
- For file analysis, use appropriate tools (analyze_csv_file, analyze_excel_file, extract_text_from_image, etc.)
|
| 48 |
- After using tools and analyzing the information, provide ONLY the final answer
|
| 49 |
- Do not include explanations, reasoning, or extra text in your final response
|
| 50 |
- If the answer is a number, provide just the number (no units unless specifically requested)
|
|
|
|
| 192 |
else:
|
| 193 |
print("❌ Output is NOT in the correct format!")
|
| 194 |
except Exception as e:
|
| 195 |
+
print("❌ Output is NOT in the correct format!", e)
|
|
|
tools.py
CHANGED
|
@@ -2,10 +2,13 @@ import base64
|
|
| 2 |
import datetime
|
| 3 |
import math
|
| 4 |
import os
|
|
|
|
|
|
|
| 5 |
from typing import Dict, Union
|
| 6 |
|
| 7 |
import pandas
|
| 8 |
import pytz
|
|
|
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
from langchain_community.document_loaders import (
|
| 11 |
ArxivLoader, AssemblyAIAudioTranscriptLoader, WikipediaLoader)
|
|
@@ -13,11 +16,83 @@ from langchain_community.document_loaders.generic import GenericLoader
|
|
| 13 |
from langchain_community.document_loaders.parsers import LanguageParser
|
| 14 |
from langchain_core.messages import HumanMessage
|
| 15 |
# from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 16 |
from langchain_core.tools import tool
|
| 17 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 18 |
from langchain_tavily import TavilySearch
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def multiply(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
|
| 23 |
"""Multiplies two numbers and returns the product.
|
|
|
|
| 2 |
import datetime
|
| 3 |
import math
|
| 4 |
import os
|
| 5 |
+
import urllib.parse
|
| 6 |
+
from pathlib import Path
|
| 7 |
from typing import Dict, Union
|
| 8 |
|
| 9 |
import pandas
|
| 10 |
import pytz
|
| 11 |
+
import requests
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
from langchain_community.document_loaders import (
|
| 14 |
ArxivLoader, AssemblyAIAudioTranscriptLoader, WikipediaLoader)
|
|
|
|
| 16 |
from langchain_community.document_loaders.parsers import LanguageParser
|
| 17 |
from langchain_core.messages import HumanMessage
|
| 18 |
# from langchain_community.tools import DuckDuckGoSearchRun
|
| 19 |
+
# from langchain_community.tools import DuckDuckGoSearchRun
|
| 20 |
from langchain_core.tools import tool
|
| 21 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 22 |
from langchain_tavily import TavilySearch
|
| 23 |
|
| 24 |
|
| 25 |
+
@tool
|
| 26 |
+
def download_file(url: str, filename: str = None) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Download a file from a URL and save it locally for analysis.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
url: The URL of the file to download
|
| 32 |
+
filename: Optional custom filename. If not provided, will extract from URL
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
The local file path where the file was saved
|
| 36 |
+
"""
|
| 37 |
+
try:
|
| 38 |
+
# Clean and validate URL
|
| 39 |
+
url = url.strip()
|
| 40 |
+
if not url.startswith(('http://', 'https://')):
|
| 41 |
+
return f"Error: Invalid URL format: {url}"
|
| 42 |
+
|
| 43 |
+
# Create downloads directory if it doesn't exist
|
| 44 |
+
download_dir = Path("downloads")
|
| 45 |
+
download_dir.mkdir(exist_ok=True)
|
| 46 |
+
|
| 47 |
+
# Determine filename
|
| 48 |
+
if not filename:
|
| 49 |
+
# Extract filename from URL
|
| 50 |
+
parsed_url = urllib.parse.urlparse(url)
|
| 51 |
+
filename = os.path.basename(parsed_url.path)
|
| 52 |
+
|
| 53 |
+
# If no filename in URL, create one based on URL hash
|
| 54 |
+
if not filename or '.' not in filename:
|
| 55 |
+
url_hash = str(hash(url))[-8:] # Last 8 characters of hash
|
| 56 |
+
filename = f"downloaded_file_{url_hash}"
|
| 57 |
+
|
| 58 |
+
# Ensure filename has proper extension if we can detect content type
|
| 59 |
+
filepath = download_dir / filename
|
| 60 |
+
|
| 61 |
+
# Download the file
|
| 62 |
+
headers = {
|
| 63 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
print(f"Downloading file from: {url}")
|
| 67 |
+
response = requests.get(url, headers=headers, timeout=30, stream=True)
|
| 68 |
+
response.raise_for_status()
|
| 69 |
+
|
| 70 |
+
# Try to get better filename from Content-Disposition header
|
| 71 |
+
if 'content-disposition' in response.headers:
|
| 72 |
+
import re
|
| 73 |
+
cd = response.headers['content-disposition']
|
| 74 |
+
filename_match = re.findall('filename=(.+)', cd)
|
| 75 |
+
if filename_match:
|
| 76 |
+
suggested_filename = filename_match[0].strip('"')
|
| 77 |
+
if suggested_filename:
|
| 78 |
+
filepath = download_dir / suggested_filename
|
| 79 |
+
|
| 80 |
+
# Write file to disk
|
| 81 |
+
with open(filepath, 'wb') as f:
|
| 82 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 83 |
+
f.write(chunk)
|
| 84 |
+
|
| 85 |
+
file_size = os.path.getsize(filepath)
|
| 86 |
+
print(f"Successfully downloaded {filename} ({file_size} bytes)")
|
| 87 |
+
|
| 88 |
+
return str(filepath)
|
| 89 |
+
|
| 90 |
+
except requests.exceptions.RequestException as e:
|
| 91 |
+
return f"Error downloading file: {str(e)}"
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return f"Error saving file: {str(e)}"
|
| 94 |
+
|
| 95 |
+
|
| 96 |
@tool
|
| 97 |
def multiply(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
|
| 98 |
"""Multiplies two numbers and returns the product.
|