Calendar + NLP modules implemented
This commit is contained in:
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