Calendar + NLP modules implemented
This commit is contained in:
BIN
backend/modules/nlp/__pycache__/api.cpython-312.pyc
Normal file
BIN
backend/modules/nlp/__pycache__/api.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/modules/nlp/__pycache__/service.cpython-312.pyc
Normal file
BIN
backend/modules/nlp/__pycache__/service.cpython-312.pyc
Normal file
Binary file not shown.
53
backend/modules/nlp/api.py
Normal file
53
backend/modules/nlp/api.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# modules/nlp/api.py
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.database import get_db
|
||||
from core.exceptions import bad_request_exception
|
||||
|
||||
from modules.auth.dependencies import get_current_user
|
||||
from modules.auth.models import User
|
||||
from modules.nlp.service import process_request, ask_ai
|
||||
from modules.calendar.service import create_calendar_event, get_calendar_events, update_calendar_event, delete_calendar_event
|
||||
from modules.calendar.schemas import CalendarEventCreate
|
||||
|
||||
|
||||
router = APIRouter(prefix="/nlp", tags=["nlp"])
|
||||
|
||||
@router.post("/process-command")
|
||||
def process_command(user_input: str, current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
||||
"""
|
||||
Process the user command and return the appropriate action.
|
||||
"""
|
||||
command = process_request(user_input)
|
||||
|
||||
if "error" in command:
|
||||
raise bad_request_exception(command["error"])
|
||||
|
||||
match command["intent"]:
|
||||
case "ask_ai":
|
||||
result = ask_ai(**command["params"])
|
||||
return {"action": "ai_response", "details": result}
|
||||
|
||||
case "get_calendar_events":
|
||||
result = get_calendar_events(db, current_user.id, **command["params"])
|
||||
return {"action": "calendar_events_retrieved", "details": result}
|
||||
|
||||
case "add_calendar_event":
|
||||
event = CalendarEventCreate(**command["params"])
|
||||
result = create_calendar_event(db, current_user.id, event)
|
||||
return {"action": "calendar_event_created", "details": result}
|
||||
|
||||
case "update_calendar_event":
|
||||
event = CalendarEventCreate(**command["params"])
|
||||
result = update_calendar_event(db, current_user.id, 0, event_data=event) ## PLACEHOLDER
|
||||
return {"action": "calendar_event_updated", "details": result}
|
||||
|
||||
case "delete_calendar_event":
|
||||
result = update_calendar_event(db, current_user.id, 0) ## PLACEHOLDER
|
||||
return {"action": "calendar_event_deleted", "details": result}
|
||||
|
||||
case "unknown":
|
||||
return {"action": "unknown_command", "details": command["params"]}
|
||||
case _:
|
||||
raise bad_request_exception(400, detail="Unrecognized command")
|
||||
100
backend/modules/nlp/service.py
Normal file
100
backend/modules/nlp/service.py
Normal file
@@ -0,0 +1,100 @@
|
||||
# modules/nlp/service.py
|
||||
|
||||
from google import genai
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
# from core.config import settings
|
||||
|
||||
# client = genai.Client(api_key=settings.GOOGLE_API_KEY)
|
||||
client = genai.Client(api_key="AIzaSyBrte_mETZJce8qE6cRTSz_fHOjdjlShBk")
|
||||
|
||||
### Base prompt for MAIA, used for inital user requests
|
||||
SYSTEM_PROMPT = """
|
||||
You are MAIA - My AI Assistant. Your job is to parse user requests into structured JSON commands.
|
||||
|
||||
Available functions:
|
||||
1. ask_ai(request: str). If the intent of the request is a simple question (e.x. What is the weather like today?), you should call this function, and forward the user's request as the parameter.
|
||||
2. get_calendar_events(start: Optional[datetime], end: Optional[datetime])
|
||||
3. add_calendar_event(title: str, description: str, start: datetime, end: Optional[datetime], location: str)
|
||||
4. update_calendar_event(event_id: int, title: Optional[str], description: Optional[str], start: Optional[datetime], end: Optional[datetime], location: Optional[str])
|
||||
5. delete_calendar_event(event_id: int)
|
||||
|
||||
Respond **ONLY** with JSON like this:
|
||||
{
|
||||
"intent": "add_calendar_event",
|
||||
"params": {
|
||||
"title": "Team Meeting",
|
||||
"description": "Discuss project updates",
|
||||
"start": "2025-04-16 15:00:00.000000+00:00",
|
||||
"end": "2025-04-16 16:00:00.000000+00:00",
|
||||
"location": "Office"
|
||||
}
|
||||
}
|
||||
|
||||
The datetime right now is """+str(datetime.now(timezone.utc))+""".
|
||||
"""
|
||||
|
||||
### Prompt for MAIA to forward user request to AI
|
||||
SYSTEM_FORWARD_PROMPT = f"""
|
||||
You are MAIA - My AI Assistant. Your job is to answer user simple user requests.
|
||||
Here is some context for you:
|
||||
- The datetime right now is {str(datetime.now(timezone.utc))}.
|
||||
Here is the user request:
|
||||
|
||||
"""
|
||||
def process_request(request: str):
|
||||
"""
|
||||
Process the user request using the Google GenAI API.
|
||||
"""
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.0-flash",
|
||||
contents=SYSTEM_PROMPT + f"\n\nUser: {request}\nMAIA:",
|
||||
config={
|
||||
"temperature": 0.3, # Less creativity, more factual
|
||||
"response_mime_type": "application/json",
|
||||
# "response_schema": { ### NOT WORKING
|
||||
# "type": "object",
|
||||
# "properties": {
|
||||
# "intent": {
|
||||
# "type": "string",
|
||||
# "enum": [
|
||||
# "get_calendar_events",
|
||||
# "add_calendar_event",
|
||||
# "update_calendar_event",
|
||||
# "delete_calendar_event"
|
||||
# ]
|
||||
# },
|
||||
# "params": {
|
||||
# "type": "object",
|
||||
# "properties": {
|
||||
# "title": {"type": "string"},
|
||||
# "description": {"type": "string"},
|
||||
# "start": {"type": "string", "format": "date-time"},
|
||||
# "end": {"type": "string", "format": "date-time"},
|
||||
# "location": {"type": "string"},
|
||||
# "event_id": {"type": "integer"},
|
||||
|
||||
# },
|
||||
# }
|
||||
# },
|
||||
# "required": ["intent", "params"]
|
||||
# }
|
||||
}
|
||||
)
|
||||
|
||||
# Parse the JSON response
|
||||
try:
|
||||
return json.loads(response.text)
|
||||
except ValueError:
|
||||
raise ValueError("Invalid JSON response from AI")
|
||||
|
||||
def ask_ai(request: str):
|
||||
"""
|
||||
Ask the AI a question.
|
||||
This is only called by MAIA when the intent is a simple question.
|
||||
"""
|
||||
response = client.models.generate_content(
|
||||
model="gemini-2.0-flash",
|
||||
contents=SYSTEM_FORWARD_PROMPT+request,
|
||||
)
|
||||
return response.text
|
||||
Reference in New Issue
Block a user