Spaces:
Build error
Build error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -10,8 +10,9 @@ from langchain_groq import ChatGroq
|
|
| 10 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
from langchain_community.document_loaders import WikipediaLoader
|
|
|
|
| 13 |
from langchain_community.document_loaders import ArxivLoader
|
| 14 |
-
from langchain_community.vectorstores import SupabaseVectorStore
|
| 15 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 16 |
from langchain_core.tools import tool
|
| 17 |
from langchain.tools.retriever import create_retriever_tool
|
|
@@ -98,6 +99,29 @@ def modulus(a: int, b: int) -> int:
|
|
| 98 |
"""
|
| 99 |
return a % b
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
@tool
|
| 103 |
def wiki_search(query: str) -> str:
|
|
@@ -159,10 +183,11 @@ def arvix_search(query: str) -> str:
|
|
| 159 |
|
| 160 |
|
| 161 |
|
|
|
|
| 162 |
@tool
|
| 163 |
def analyze_attachment(file_path: str) -> str:
|
| 164 |
"""
|
| 165 |
-
Analyzes attachments including PDF, TXT, DOCX, and XLSX files and returns text content.
|
| 166 |
|
| 167 |
Args:
|
| 168 |
file_path: Local path to the attachment.
|
|
@@ -170,32 +195,38 @@ def analyze_attachment(file_path: str) -> str:
|
|
| 170 |
if not os.path.exists(file_path):
|
| 171 |
return f"File not found: {file_path}"
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
content = ""
|
| 190 |
-
for sheet in wb:
|
| 191 |
-
content += f"Sheet: {sheet.title}\n"
|
| 192 |
-
for row in sheet.iter_rows(values_only=True):
|
| 193 |
-
content += "\t".join([str(cell) if cell is not None else "" for cell in row]) + "\n"
|
| 194 |
|
| 195 |
-
|
| 196 |
-
return "Unsupported file format. Please use PDF, TXT, DOCX, or XLSX."
|
| 197 |
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
@tool
|
|
@@ -425,15 +456,57 @@ calc_tool = Tool(
|
|
| 425 |
|
| 426 |
file_tool = Tool(
|
| 427 |
name="File_Analysis",
|
| 428 |
-
func=
|
| 429 |
description="Analyze and extract data from attachments."
|
| 430 |
)
|
| 431 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
# -------------------------------
|
| 433 |
# Step 7: Create the Planner-Agent Logic
|
| 434 |
# -------------------------------
|
| 435 |
# Define the agent tool set
|
| 436 |
-
tools = [wiki_tool, calc_tool, file_tool]
|
| 437 |
|
| 438 |
# Create an agent using the planner, task classifier, and decision logic
|
| 439 |
agent = initialize_agent(
|
|
|
|
| 10 |
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 11 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 12 |
from langchain_community.document_loaders import WikipediaLoader
|
| 13 |
+
from langchain.utilities import WikipediaAPIWrapper
|
| 14 |
from langchain_community.document_loaders import ArxivLoader
|
| 15 |
+
#from langchain_community.vectorstores import SupabaseVectorStore
|
| 16 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 17 |
from langchain_core.tools import tool
|
| 18 |
from langchain.tools.retriever import create_retriever_tool
|
|
|
|
| 99 |
"""
|
| 100 |
return a % b
|
| 101 |
|
| 102 |
+
@tool
|
| 103 |
+
def calc_tool(a: int, b: int, operation: str) -> float:
|
| 104 |
+
"""
|
| 105 |
+
Perform a calculation between two numbers.
|
| 106 |
+
|
| 107 |
+
Args:
|
| 108 |
+
a: First number.
|
| 109 |
+
b: Second number.
|
| 110 |
+
operation: One of 'add', 'subtract', 'multiply', 'divide', 'modulus'.
|
| 111 |
+
"""
|
| 112 |
+
operation = operation.lower()
|
| 113 |
+
if operation == "add":
|
| 114 |
+
return add(a, b)
|
| 115 |
+
elif operation == "subtract":
|
| 116 |
+
return subtract(a, b)
|
| 117 |
+
elif operation == "multiply":
|
| 118 |
+
return multiply(a, b)
|
| 119 |
+
elif operation == "divide":
|
| 120 |
+
return divide(a, b)
|
| 121 |
+
elif operation == "modulus":
|
| 122 |
+
return modulus(a, b)
|
| 123 |
+
else:
|
| 124 |
+
raise ValueError(f"Unsupported operation: {operation}")
|
| 125 |
|
| 126 |
@tool
|
| 127 |
def wiki_search(query: str) -> str:
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
+
|
| 187 |
@tool
|
| 188 |
def analyze_attachment(file_path: str) -> str:
|
| 189 |
"""
|
| 190 |
+
Analyzes attachments including PY, PDF, TXT, DOCX, and XLSX files and returns text content.
|
| 191 |
|
| 192 |
Args:
|
| 193 |
file_path: Local path to the attachment.
|
|
|
|
| 195 |
if not os.path.exists(file_path):
|
| 196 |
return f"File not found: {file_path}"
|
| 197 |
|
| 198 |
+
try:
|
| 199 |
+
ext = file_path.lower()
|
| 200 |
+
|
| 201 |
+
if ext.endswith(".pdf"):
|
| 202 |
+
loader = PyMuPDFLoader(file_path)
|
| 203 |
+
documents = loader.load()
|
| 204 |
+
content = "\n\n".join([doc.page_content for doc in documents])
|
| 205 |
+
|
| 206 |
+
elif ext.endswith(".txt") or ext.endswith(".py"):
|
| 207 |
+
# Both .txt and .py are plain text files
|
| 208 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
| 209 |
+
content = file.read()
|
| 210 |
+
|
| 211 |
+
elif ext.endswith(".docx"):
|
| 212 |
+
doc = DocxDocument(file_path)
|
| 213 |
+
content = "\n".join([para.text for para in doc.paragraphs])
|
| 214 |
+
|
| 215 |
+
elif ext.endswith(".xlsx"):
|
| 216 |
+
wb = openpyxl.load_workbook(file_path, data_only=True)
|
| 217 |
+
content = ""
|
| 218 |
+
for sheet in wb:
|
| 219 |
+
content += f"Sheet: {sheet.title}\n"
|
| 220 |
+
for row in sheet.iter_rows(values_only=True):
|
| 221 |
+
content += "\t".join([str(cell) if cell is not None else "" for cell in row]) + "\n"
|
| 222 |
|
| 223 |
+
else:
|
| 224 |
+
return "Unsupported file format. Please use PY, PDF, TXT, DOCX, or XLSX."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
+
return content[:3000] # Limit output size for readability
|
|
|
|
| 227 |
|
| 228 |
+
except Exception as e:
|
| 229 |
+
return f"An error occurred while processing the file: {str(e)}"
|
| 230 |
|
| 231 |
|
| 232 |
@tool
|
|
|
|
| 456 |
|
| 457 |
file_tool = Tool(
|
| 458 |
name="File_Analysis",
|
| 459 |
+
func=analyze_attachment().run, # Replace with a file analysis function
|
| 460 |
description="Analyze and extract data from attachments."
|
| 461 |
)
|
| 462 |
|
| 463 |
+
|
| 464 |
+
web_tool = Tool(
|
| 465 |
+
name="web_search",
|
| 466 |
+
func=web_search().run,
|
| 467 |
+
description="Perform web search."
|
| 468 |
+
)
|
| 469 |
+
|
| 470 |
+
|
| 471 |
+
arvix_tool = Tool(
|
| 472 |
+
name="arvix_search",
|
| 473 |
+
func=arvix_search().run,
|
| 474 |
+
description="Perform arvix search."
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
youtube_tool = Tool(
|
| 478 |
+
name="get_youtube_transcript",
|
| 479 |
+
func=get_youtube_transcript().run,
|
| 480 |
+
description="get youtube transcript."
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
video_tool = Tool(
|
| 485 |
+
name="extract_video_id",
|
| 486 |
+
func=extract_video_id().run,
|
| 487 |
+
description="extract_video_id."
|
| 488 |
+
)
|
| 489 |
+
|
| 490 |
+
|
| 491 |
+
analyze_tool = Tool(
|
| 492 |
+
name="analyze_attachment",
|
| 493 |
+
func=analyze_attachment().run,
|
| 494 |
+
description="analyze attachment."
|
| 495 |
+
)
|
| 496 |
+
|
| 497 |
+
|
| 498 |
+
wikiq_tool = Tool(
|
| 499 |
+
name="wikidata_query",
|
| 500 |
+
func=wikidata_query().run,
|
| 501 |
+
description="wikidata query."
|
| 502 |
+
)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
# -------------------------------
|
| 506 |
# Step 7: Create the Planner-Agent Logic
|
| 507 |
# -------------------------------
|
| 508 |
# Define the agent tool set
|
| 509 |
+
tools = [wiki_tool, calc_tool, file_tool, web_tool, arvix_tool, youtube_tool, video_tool, analyze_tool, wikiq_tool]
|
| 510 |
|
| 511 |
# Create an agent using the planner, task classifier, and decision logic
|
| 512 |
agent = initialize_agent(
|