Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,36 +1,47 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from typing import Dict
|
| 3 |
from langchain.agents import initialize_agent, AgentType
|
| 4 |
from langchain_community.tools import Tool, WikipediaQueryRun
|
| 5 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 6 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
|
|
|
| 7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
class Agent:
|
| 10 |
def __init__(self):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
llm = ChatGoogleGenerativeAI(
|
| 16 |
model="gemini-1.5-pro",
|
| 17 |
-
google_api_key=
|
| 18 |
convert_system_message_to_human=True
|
| 19 |
)
|
| 20 |
|
| 21 |
-
wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 22 |
-
python_tool = PythonREPLTool()
|
| 23 |
-
|
| 24 |
tools = [
|
| 25 |
Tool(
|
| 26 |
name="Wikipedia",
|
| 27 |
-
func=
|
| 28 |
description="Useful for general knowledge and encyclopedic questions."
|
| 29 |
),
|
| 30 |
Tool(
|
| 31 |
name="Calculator",
|
| 32 |
-
func=
|
| 33 |
description="Useful for solving math and logical problems through Python."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
]
|
| 36 |
|
|
@@ -44,11 +55,38 @@ class Agent:
|
|
| 44 |
|
| 45 |
def __call__(self, input_data: Dict) -> str:
|
| 46 |
question = input_data.get("question", "")
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
result = self.agent.run(question)
|
| 51 |
return result.strip()
|
| 52 |
except Exception as e:
|
| 53 |
return f"AGENT ERROR: {str(e)}"
|
| 54 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
from typing import Dict
|
| 4 |
from langchain.agents import initialize_agent, AgentType
|
| 5 |
from langchain_community.tools import Tool, WikipediaQueryRun
|
| 6 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 7 |
from langchain_experimental.tools.python.tool import PythonREPLTool
|
| 8 |
+
from langchain_community.tools.brave_search import BraveSearch
|
| 9 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
+
import pandas as pd
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from docx import Document
|
| 13 |
+
import fitz # PyMuPDF
|
| 14 |
|
| 15 |
class Agent:
|
| 16 |
def __init__(self):
|
| 17 |
+
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 18 |
+
brave_key = os.getenv("BRAVE_SEARCH_API_KEY")
|
| 19 |
+
if not gemini_key:
|
| 20 |
raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
| 21 |
+
if not brave_key:
|
| 22 |
+
raise ValueError("BRAVE_SEARCH_API_KEY not found in environment variables.")
|
| 23 |
|
| 24 |
llm = ChatGoogleGenerativeAI(
|
| 25 |
model="gemini-1.5-pro",
|
| 26 |
+
google_api_key=gemini_key,
|
| 27 |
convert_system_message_to_human=True
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
tools = [
|
| 31 |
Tool(
|
| 32 |
name="Wikipedia",
|
| 33 |
+
func=WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()).run,
|
| 34 |
description="Useful for general knowledge and encyclopedic questions."
|
| 35 |
),
|
| 36 |
Tool(
|
| 37 |
name="Calculator",
|
| 38 |
+
func=PythonREPLTool().run,
|
| 39 |
description="Useful for solving math and logical problems through Python."
|
| 40 |
+
),
|
| 41 |
+
Tool(
|
| 42 |
+
name="Brave Search",
|
| 43 |
+
func=BraveSearch(api_key=brave_key).run,
|
| 44 |
+
description="Useful for factual and current event queries using Brave search engine."
|
| 45 |
)
|
| 46 |
]
|
| 47 |
|
|
|
|
| 55 |
|
| 56 |
def __call__(self, input_data: Dict) -> str:
|
| 57 |
question = input_data.get("question", "")
|
| 58 |
+
file_names = input_data.get("file_names", [])
|
| 59 |
+
task_id = input_data.get("task_id", "")
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
+
if file_names:
|
| 63 |
+
file_path = f"/home/user/app/files/{task_id}/{file_names[0]}"
|
| 64 |
+
ext = Path(file_path).suffix.lower()
|
| 65 |
+
if ext in [".csv", ".tsv"]:
|
| 66 |
+
df = pd.read_csv(file_path)
|
| 67 |
+
return f"Loaded table with {df.shape[0]} rows and {df.shape[1]} columns."
|
| 68 |
+
elif ext in [".xlsx"]:
|
| 69 |
+
df = pd.read_excel(file_path)
|
| 70 |
+
return f"Loaded spreadsheet with {df.shape[0]} rows and {df.shape[1]} columns."
|
| 71 |
+
elif ext in [".json", ".jsonl"]:
|
| 72 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 73 |
+
if ext == ".jsonl":
|
| 74 |
+
data = [json.loads(line) for line in f if line.strip()]
|
| 75 |
+
else:
|
| 76 |
+
data = json.load(f)
|
| 77 |
+
return f"Loaded JSON with {len(data)} items."
|
| 78 |
+
elif ext == ".docx":
|
| 79 |
+
doc = Document(file_path)
|
| 80 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 81 |
+
return f"Loaded DOCX with {len(text)} characters."
|
| 82 |
+
elif ext == ".pdf":
|
| 83 |
+
doc = fitz.open(file_path)
|
| 84 |
+
text = "".join([page.get_text() for page in doc])
|
| 85 |
+
return f"Loaded PDF with {len(doc)} pages and {len(text)} characters."
|
| 86 |
+
else:
|
| 87 |
+
return "Unsupported file type for this task."
|
| 88 |
+
|
| 89 |
result = self.agent.run(question)
|
| 90 |
return result.strip()
|
| 91 |
except Exception as e:
|
| 92 |
return f"AGENT ERROR: {str(e)}"
|
|
|