Update agent.py
Browse files
agent.py
CHANGED
|
@@ -12,26 +12,31 @@ from docx import Document
|
|
| 12 |
import fitz # PyMuPDF
|
| 13 |
import requests
|
| 14 |
|
| 15 |
-
class
|
| 16 |
-
def __init__(self, api_key: str):
|
| 17 |
self.api_key = api_key
|
| 18 |
-
self.
|
|
|
|
| 19 |
|
| 20 |
def run(self, query: str) -> str:
|
| 21 |
try:
|
| 22 |
response = requests.get(
|
| 23 |
self.base_url,
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
response.raise_for_status()
|
| 28 |
-
results = response.json().get("
|
| 29 |
if results:
|
| 30 |
-
return results[0].get("title", "") + ": " + results[0].get("
|
| 31 |
else:
|
| 32 |
return "No results found."
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
| 35 |
|
| 36 |
def classify_question_type(question: str) -> str:
|
| 37 |
q = question.lower()
|
|
@@ -47,11 +52,13 @@ def classify_question_type(question: str) -> str:
|
|
| 47 |
class Agent:
|
| 48 |
def __init__(self):
|
| 49 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
if not gemini_key:
|
| 52 |
raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
| 53 |
-
if not
|
| 54 |
-
raise ValueError("
|
| 55 |
|
| 56 |
llm = ChatGoogleGenerativeAI(
|
| 57 |
model="gemini-1.5-pro",
|
|
@@ -71,9 +78,9 @@ class Agent:
|
|
| 71 |
description="Useful for solving math and logical problems through Python."
|
| 72 |
),
|
| 73 |
Tool(
|
| 74 |
-
name="
|
| 75 |
-
func=
|
| 76 |
-
description="Useful for factual
|
| 77 |
)
|
| 78 |
]
|
| 79 |
|
|
@@ -94,7 +101,7 @@ class Agent:
|
|
| 94 |
|
| 95 |
type_prefix = f"[Task Type: {task_type.upper()}]\n\n"
|
| 96 |
system_prompt = (
|
| 97 |
-
"You are a member of a multidisciplinary research institute
|
| 98 |
)
|
| 99 |
|
| 100 |
file_summary = ""
|
|
@@ -125,10 +132,11 @@ class Agent:
|
|
| 125 |
else:
|
| 126 |
summaries.append(f"{fname}: Unsupported file type {ext}")
|
| 127 |
except Exception as fe:
|
| 128 |
-
|
|
|
|
| 129 |
|
| 130 |
file_summary = "\n\n".join(summaries)
|
| 131 |
-
full_prompt = type_prefix + system_prompt + file_summary + f"\n\nTASK:\n{question}"
|
| 132 |
result = self.agent.run(full_prompt)
|
| 133 |
return result.strip()
|
| 134 |
except Exception as e:
|
|
|
|
| 12 |
import fitz # PyMuPDF
|
| 13 |
import requests
|
| 14 |
|
| 15 |
+
class GoogleCustomSearchTool:
|
| 16 |
+
def __init__(self, api_key: str, cse_id: str):
|
| 17 |
self.api_key = api_key
|
| 18 |
+
self.cse_id = cse_id
|
| 19 |
+
self.base_url = "https://www.googleapis.com/customsearch/v1"
|
| 20 |
|
| 21 |
def run(self, query: str) -> str:
|
| 22 |
try:
|
| 23 |
response = requests.get(
|
| 24 |
self.base_url,
|
| 25 |
+
params={
|
| 26 |
+
"key": self.api_key,
|
| 27 |
+
"cx": self.cse_id,
|
| 28 |
+
"q": query,
|
| 29 |
+
},
|
| 30 |
+
timeout=10,
|
| 31 |
)
|
| 32 |
response.raise_for_status()
|
| 33 |
+
results = response.json().get("items", [])
|
| 34 |
if results:
|
| 35 |
+
return results[0].get("title", "") + ": " + results[0].get("link", "")
|
| 36 |
else:
|
| 37 |
return "No results found."
|
| 38 |
except Exception as e:
|
| 39 |
+
return f"GoogleCustomSearchTool ERROR: {str(e)}"
|
| 40 |
|
| 41 |
def classify_question_type(question: str) -> str:
|
| 42 |
q = question.lower()
|
|
|
|
| 52 |
class Agent:
|
| 53 |
def __init__(self):
|
| 54 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 55 |
+
gcs_key = os.getenv("GOOGLE_API_KEY")
|
| 56 |
+
gcs_cx = os.getenv("GOOGLE_CSE_ID")
|
| 57 |
+
|
| 58 |
if not gemini_key:
|
| 59 |
raise ValueError("GEMINI_API_KEY not found in environment variables.")
|
| 60 |
+
if not gcs_key or not gcs_cx:
|
| 61 |
+
raise ValueError("GOOGLE_API_KEY or GOOGLE_CSE_ID not found in environment variables.")
|
| 62 |
|
| 63 |
llm = ChatGoogleGenerativeAI(
|
| 64 |
model="gemini-1.5-pro",
|
|
|
|
| 78 |
description="Useful for solving math and logical problems through Python."
|
| 79 |
),
|
| 80 |
Tool(
|
| 81 |
+
name="Google Custom Search",
|
| 82 |
+
func=GoogleCustomSearchTool(api_key=gcs_key, cse_id=gcs_cx).run,
|
| 83 |
+
description="Useful for factual queries using Google Custom Search."
|
| 84 |
)
|
| 85 |
]
|
| 86 |
|
|
|
|
| 101 |
|
| 102 |
type_prefix = f"[Task Type: {task_type.upper()}]\n\n"
|
| 103 |
system_prompt = (
|
| 104 |
+
"You are a member of a multidisciplinary research institute. If a file cannot be loaded, do not abandon the task. Use your best judgment based on the task and file name. The file may be unavailable by design; this is part of the test. Always attempt to reason based on partial or inferred data."
|
| 105 |
)
|
| 106 |
|
| 107 |
file_summary = ""
|
|
|
|
| 132 |
else:
|
| 133 |
summaries.append(f"{fname}: Unsupported file type {ext}")
|
| 134 |
except Exception as fe:
|
| 135 |
+
guessed_type = "spreadsheet" if ext in [".csv", ".tsv", ".xlsx"] else "document" if ext in [".pdf", ".docx"] else "data file"
|
| 136 |
+
summaries.append(f"{fname}: Could not load, but based on the file extension, we assume it is a {guessed_type}. Please attempt to reason based on the task.")
|
| 137 |
|
| 138 |
file_summary = "\n\n".join(summaries)
|
| 139 |
+
full_prompt = type_prefix + system_prompt + "\n\n" + file_summary + f"\n\nTASK:\n{question}"
|
| 140 |
result = self.agent.run(full_prompt)
|
| 141 |
return result.strip()
|
| 142 |
except Exception as e:
|