Spaces:
Sleeping
Sleeping
Update src/agents/tools.py
Browse files- src/agents/tools.py +60 -6
src/agents/tools.py
CHANGED
|
@@ -1,19 +1,73 @@
|
|
| 1 |
from langchain.agents import Tool
|
| 2 |
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 3 |
from src.config.env import SERPER_API_KEY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def get_tools(rag_chain):
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
Tool(
|
| 10 |
name="RAG",
|
| 11 |
func=lambda x: rag_chain.invoke({"input": x}),
|
| 12 |
-
description="
|
| 13 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
Tool(
|
| 15 |
name="Google Search",
|
| 16 |
func=search.run,
|
| 17 |
-
description="
|
| 18 |
)
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain.agents import Tool
|
| 2 |
from langchain_community.utilities import GoogleSerperAPIWrapper
|
| 3 |
from src.config.env import SERPER_API_KEY
|
| 4 |
+
from langchain_google_community import GmailToolkit
|
| 5 |
+
from langchain_google_community.gmail.utils import (
|
| 6 |
+
build_resource_service,
|
| 7 |
+
get_gmail_credentials,
|
| 8 |
+
)
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Gmail Send Helper
|
| 12 |
|
| 13 |
+
def send_email_function(input_data):
|
| 14 |
+
if isinstance(input_data, str):
|
| 15 |
+
input_data = {"message": input_data}
|
| 16 |
+
|
| 17 |
+
input_data.setdefault("to", "default_advisor@gmail.com")
|
| 18 |
+
input_data.setdefault("subject", "Meeting Request")
|
| 19 |
+
input_data.setdefault("message", "Dear Advisor, the student would like to book a meeting with you.")
|
| 20 |
+
|
| 21 |
+
return gmail_send_tool(input_data)
|
| 22 |
+
|
| 23 |
def get_tools(rag_chain):
|
| 24 |
+
tools = []
|
| 25 |
+
|
| 26 |
+
# === RAG Tool ===
|
| 27 |
+
tools.append(
|
| 28 |
Tool(
|
| 29 |
name="RAG",
|
| 30 |
func=lambda x: rag_chain.invoke({"input": x}),
|
| 31 |
+
description="Use this for questions about UWF, using the Student Handbook as context."
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# === Google Search Tool ===
|
| 36 |
+
search = GoogleSerperAPIWrapper(serper_api_key=SERPER_API_KEY)
|
| 37 |
+
tools.append(
|
| 38 |
Tool(
|
| 39 |
name="Google Search",
|
| 40 |
func=search.run,
|
| 41 |
+
description="Fallback when RAG does not contain the information."
|
| 42 |
)
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# === Gmail Tool ===
|
| 46 |
+
try:
|
| 47 |
+
credentials = get_gmail_credentials(
|
| 48 |
+
token_file="token.json",
|
| 49 |
+
scopes=["https://mail.google.com/"],
|
| 50 |
+
client_secrets_file="credentials.json",
|
| 51 |
+
)
|
| 52 |
+
api_resource = build_resource_service(credentials=credentials)
|
| 53 |
+
toolkit = GmailToolkit(api_resource=api_resource)
|
| 54 |
+
gmail_send_tool = next(
|
| 55 |
+
(tool for tool in toolkit.get_tools() if tool.__class__.__name__ == "GmailSendMessage"),
|
| 56 |
+
None
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if gmail_send_tool:
|
| 60 |
+
tools.append(
|
| 61 |
+
Tool(
|
| 62 |
+
name="Gmail",
|
| 63 |
+
func=send_email_function,
|
| 64 |
+
description="Send email to the advisor if the student requests it."
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
else:
|
| 68 |
+
print("[Warning] GmailSendMessage tool not found.")
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"[Warning] Gmail tool could not be initialized: {e}")
|
| 72 |
+
|
| 73 |
+
return tools
|