MindPalaceAI / app /services.py
LahiruD95's picture
HF Space Docker
42cbd9d
import json
from app.models import client
from datetime import datetime
# 🔹 Extract Actionable Tasks
def extract_tasks(text):
today_date = datetime.today().strftime("%Y-%m-%d")
response = client.chat.completions.create(
model="accounts/fireworks/models/llama-v3p1-8b-instruct",
messages=[
{
"role": "system",
"content": (
f"You are a task extraction assistant. Today's date is **{today_date}**.\n"
"Your goal is to extract **exactly 2** actionable tasks from the given text.\n"
"Each task must have:\n"
"- A **title** (short summary of the task)\n"
"- A **dueDate** in `YYYY-MM-DD` format (Convert words like 'tomorrow', 'next week' into actual dates based on today's date)\n\n"
"Return **ONLY valid JSON** with this format:\n"
"{\n"
' "tasks": [\n'
' {\n'
' "title": "Task description",\n'
' "dueDate": "YYYY-MM-DD" # Always absolute date\n'
" }\n"
" ]\n"
"}"
),
},
{"role": "user", "content": text},
],
max_tokens=200,
)
# Ensure response is valid JSON
try:
model_output = response.choices[0].message.content.strip()
parsed_response = json.loads(model_output)
return parsed_response.get("tasks", [])
except json.JSONDecodeError:
return []