Spaces:
Runtime error
Runtime error
Added doc strings and updates @tool
Browse files
agent.py
CHANGED
|
@@ -24,6 +24,15 @@ load_dotenv()
|
|
| 24 |
# Custom tools
|
| 25 |
@tool
|
| 26 |
def arxiv_search(query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 28 |
return "\n\n---\n\n".join([
|
| 29 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
|
@@ -32,6 +41,15 @@ def arxiv_search(query: str) -> str:
|
|
| 32 |
|
| 33 |
@tool
|
| 34 |
def extract_text_from_image(image_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
import pytesseract
|
| 37 |
from PIL import Image
|
|
@@ -42,6 +60,36 @@ def extract_text_from_image(image_path: str) -> str:
|
|
| 42 |
return "Error: pytesseract is not installed."
|
| 43 |
except Exception as e:
|
| 44 |
return f"Error extracting text: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Model and agent setup
|
| 47 |
API_KEY = os.getenv("OPENAI_API_KEY_AG")
|
|
|
|
| 24 |
# Custom tools
|
| 25 |
@tool
|
| 26 |
def arxiv_search(query: str) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Search Arxiv for a query and return up to 3 documents.
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
query (str): The search query to run on Arxiv.
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
str: Formatted Arxiv document summaries.
|
| 35 |
+
"""
|
| 36 |
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 37 |
return "\n\n---\n\n".join([
|
| 38 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
|
|
|
| 41 |
|
| 42 |
@tool
|
| 43 |
def extract_text_from_image(image_path: str) -> str:
|
| 44 |
+
"""
|
| 45 |
+
Extract text from an image using pytesseract.
|
| 46 |
+
|
| 47 |
+
Args:
|
| 48 |
+
image_path (str): Path to the image file.
|
| 49 |
+
|
| 50 |
+
Returns:
|
| 51 |
+
str: Extracted text or error message.
|
| 52 |
+
"""
|
| 53 |
try:
|
| 54 |
import pytesseract
|
| 55 |
from PIL import Image
|
|
|
|
| 60 |
return "Error: pytesseract is not installed."
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error extracting text: {str(e)}"
|
| 63 |
+
@tool
|
| 64 |
+
def read_python_file(file_name: str) -> str:
|
| 65 |
+
"""
|
| 66 |
+
Read a Python (.py) file and return its content.
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
file_name (str): The file name of the Python script to read.
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
str: The contents of the file as a string.
|
| 73 |
+
"""
|
| 74 |
+
base_path = "data/question_files"
|
| 75 |
+
with open(os.path.join(base_path, file_name), "r") as f:
|
| 76 |
+
return f.read()
|
| 77 |
+
|
| 78 |
+
@tool
|
| 79 |
+
def read_excel_file(file_name: str) -> str:
|
| 80 |
+
"""
|
| 81 |
+
Read an Excel (.xlsx) file and return its tabular content as a string.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
file_name (str): The Excel file to read.
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
str: The content of the Excel file in plain text format.
|
| 88 |
+
"""
|
| 89 |
+
base_path = "data/question_files"
|
| 90 |
+
df = pd.read_excel(os.path.join(base_path, file_name))
|
| 91 |
+
return df.to_string()
|
| 92 |
+
|
| 93 |
|
| 94 |
# Model and agent setup
|
| 95 |
API_KEY = os.getenv("OPENAI_API_KEY_AG")
|