Spaces:
Running
Running
Commit
·
40c9bf4
1
Parent(s):
ab5c7fc
updated agent.py
Browse files- backend/agent.py +32 -5
backend/agent.py
CHANGED
|
@@ -27,6 +27,10 @@ from langgraph.graph import StateGraph, START, END
|
|
| 27 |
from langgraph.prebuilt import ToolNode
|
| 28 |
from langgraph.checkpoint.memory import MemorySaver
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
import logging
|
| 31 |
tool_log = logging.getLogger("tools")
|
| 32 |
|
|
@@ -447,6 +451,29 @@ def find_meetings(
|
|
| 447 |
rows.append(f"{ev.get('id','')} | {start} | {ev.get('summary','')}")
|
| 448 |
return "event_id | start | title\n" + "\n".join(rows)
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
@tool("download_resume")
|
| 451 |
def download_resume() -> str:
|
| 452 |
"""
|
|
@@ -462,7 +489,7 @@ def download_resume() -> str:
|
|
| 462 |
|
| 463 |
|
| 464 |
# tools for the agent
|
| 465 |
-
tools = [retriever, memory_search, schedule_meeting, update_meeting, delete_meeting, find_meetings, download_resume]
|
| 466 |
|
| 467 |
model = ChatOpenAI(
|
| 468 |
model="gpt-4o",
|
|
@@ -476,7 +503,7 @@ class AgentState(TypedDict):
|
|
| 476 |
|
| 477 |
system_prompt = SystemMessage(
|
| 478 |
content=f"""
|
| 479 |
-
|
| 480 |
|
| 481 |
### When the user asks about Krishna:
|
| 482 |
- Use the `retriever` tool to fetch facts (no fabrication) and memory search tool to query long-term memory for past context.
|
|
@@ -488,7 +515,7 @@ ou are Krishna's personal AI assistant — answer **clearly, thoroughly, and pro
|
|
| 488 |
- If retrieval yields nothing, **ask clarifying questions** to narrow the request and explain what details you could provide if available.
|
| 489 |
|
| 490 |
### When the topic is unrelated to Krishna:
|
| 491 |
-
- Respond with **
|
| 492 |
|
| 493 |
### Formatting & Style:
|
| 494 |
- Use **Markdown** formatting.
|
|
@@ -516,12 +543,12 @@ When describing Krishna’s skills or projects:
|
|
| 516 |
- `location` (optional): Physical or virtual location if not using Meet.
|
| 517 |
- `calendar_id` (optional): Defaults to "primary".
|
| 518 |
- `make_meet_link`: Set to true if a Google Meet link should be generated.
|
| 519 |
-
-
|
| 520 |
- Confirm details back to the user after scheduling, including date, time, attendees, and meeting link if available.
|
| 521 |
|
| 522 |
If the user asks to edit or cancel a meeting, call update_meeting or delete_meeting. Prefer PATCH semantics (only change fields the user mentions). Always include event_id (ask for it or infer from the last created event in this thread).
|
| 523 |
|
| 524 |
-
If the user asks for the resume or CV, call download_resume and return the link.
|
| 525 |
---
|
| 526 |
**Krishna’s Background:**
|
| 527 |
{KRISHNA_BIO}
|
|
|
|
| 27 |
from langgraph.prebuilt import ToolNode
|
| 28 |
from langgraph.checkpoint.memory import MemorySaver
|
| 29 |
|
| 30 |
+
from dateutil import parser as date_parser
|
| 31 |
+
from datetime import datetime, timedelta
|
| 32 |
+
import pytz
|
| 33 |
+
|
| 34 |
import logging
|
| 35 |
tool_log = logging.getLogger("tools")
|
| 36 |
|
|
|
|
| 451 |
rows.append(f"{ev.get('id','')} | {start} | {ev.get('summary','')}")
|
| 452 |
return "event_id | start | title\n" + "\n".join(rows)
|
| 453 |
|
| 454 |
+
@tool("parse_datetime")
|
| 455 |
+
def parse_datetime(natural_text: str, default_duration_minutes: int = 30, tz: str = "America/New_York") -> dict:
|
| 456 |
+
"""
|
| 457 |
+
Parse natural language date/time (e.g., 'next Monday 3pm', 'today 10am') into
|
| 458 |
+
RFC3339 start and end timestamps. Falls back to current year if year missing.
|
| 459 |
+
"""
|
| 460 |
+
try:
|
| 461 |
+
now = datetime.now(pytz.timezone(tz))
|
| 462 |
+
dt = date_parser.parse(natural_text, default=now)
|
| 463 |
+
# if year not provided, enforce current year
|
| 464 |
+
if dt.year < now.year:
|
| 465 |
+
dt = dt.replace(year=now.year)
|
| 466 |
+
|
| 467 |
+
start = dt.astimezone(pytz.timezone(tz))
|
| 468 |
+
end = start + timedelta(minutes=default_duration_minutes)
|
| 469 |
+
|
| 470 |
+
return {
|
| 471 |
+
"start_rfc3339": start.isoformat(),
|
| 472 |
+
"end_rfc3339": end.isoformat()
|
| 473 |
+
}
|
| 474 |
+
except Exception as e:
|
| 475 |
+
return {"error": f"Failed to parse datetime: {str(e)}"}
|
| 476 |
+
|
| 477 |
@tool("download_resume")
|
| 478 |
def download_resume() -> str:
|
| 479 |
"""
|
|
|
|
| 489 |
|
| 490 |
|
| 491 |
# tools for the agent
|
| 492 |
+
tools = [retriever, memory_search, schedule_meeting, update_meeting, delete_meeting, find_meetings, download_resume, parse_datetime]
|
| 493 |
|
| 494 |
model = ChatOpenAI(
|
| 495 |
model="gpt-4o",
|
|
|
|
| 503 |
|
| 504 |
system_prompt = SystemMessage(
|
| 505 |
content=f"""
|
| 506 |
+
You are Krishna's personal AI assistant — answer **clearly, thoroughly, and professionally** with rich detail and well-structured explanations.
|
| 507 |
|
| 508 |
### When the user asks about Krishna:
|
| 509 |
- Use the `retriever` tool to fetch facts (no fabrication) and memory search tool to query long-term memory for past context.
|
|
|
|
| 515 |
- If retrieval yields nothing, **ask clarifying questions** to narrow the request and explain what details you could provide if available.
|
| 516 |
|
| 517 |
### When the topic is unrelated to Krishna:
|
| 518 |
+
- Respond with **little humor**, then **gracefully redirect** the conversation back to Krishna’s **skills, projects, or experiences** by linking the topic to relevant work.
|
| 519 |
|
| 520 |
### Formatting & Style:
|
| 521 |
- Use **Markdown** formatting.
|
|
|
|
| 543 |
- `location` (optional): Physical or virtual location if not using Meet.
|
| 544 |
- `calendar_id` (optional): Defaults to "primary".
|
| 545 |
- `make_meet_link`: Set to true if a Google Meet link should be generated.
|
| 546 |
+
- Use parse_datetime tool to convert natural language date/time (e.g., "tomorrow 3pm CT for 30 minutes") into precise RFC3339 format before calling.
|
| 547 |
- Confirm details back to the user after scheduling, including date, time, attendees, and meeting link if available.
|
| 548 |
|
| 549 |
If the user asks to edit or cancel a meeting, call update_meeting or delete_meeting. Prefer PATCH semantics (only change fields the user mentions). Always include event_id (ask for it or infer from the last created event in this thread).
|
| 550 |
|
| 551 |
+
If the user asks for the resume or CV, call download_resume tool and return the link.
|
| 552 |
---
|
| 553 |
**Krishna’s Background:**
|
| 554 |
{KRISHNA_BIO}
|