Spaces:
Runtime error
Runtime error
| # File: src/agent.py | |
| # Purpose: LLM agent that extracts booking intent from transcript using Groq | |
| import json | |
| from pathlib import Path | |
| from groq import Groq | |
| import sys | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from config import GROQ_API_KEY, DEPARTMENTS, AVAILABLE_SLOTS, MIN_CONFIDENCE_THRESHOLD | |
| client = Groq(api_key=GROQ_API_KEY) | |
| SYSTEM_PROMPT = """ | |
| You are a hospital appointment booking assistant. Extract booking details from the patient request. | |
| Respond ONLY with a valid JSON object. No explanation, no extra text, no markdown backticks. | |
| Extract these fields: | |
| - patient_name: string (use "Unknown" if not mentioned) | |
| - department: one of {departments} | |
| - date: string in YYYY-MM-DD format (infer from relative terms like "tomorrow") | |
| - slot: one of {slots} | |
| - confidence: float between 0 and 1 | |
| - missing_info: list of fields you could not determine | |
| Today is {today}. | |
| """.strip() | |
| def extract_booking_intent(transcript: str) -> dict: | |
| from datetime import date | |
| system = SYSTEM_PROMPT.format( | |
| departments=DEPARTMENTS, | |
| slots=AVAILABLE_SLOTS, | |
| today=date.today().isoformat(), | |
| ) | |
| response = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| temperature=0.0, | |
| max_tokens=512, | |
| response_format={"type": "json_object"}, | |
| messages=[ | |
| {"role": "system", "content": system}, | |
| {"role": "user", "content": transcript}, | |
| ], | |
| ) | |
| intent = json.loads(response.choices[0].message.content.strip()) | |
| print(f"[Agent] Extracted intent: {json.dumps(intent, indent=2)}") | |
| return intent | |
| def assess_confidence(intent: dict) -> bool: | |
| return intent.get("confidence", 0.0) >= MIN_CONFIDENCE_THRESHOLD | |
| if __name__ == "__main__": | |
| test = "I want to book a cardiology appointment tomorrow at 2 PM. My name is Ranjith Kumar." | |
| result = extract_booking_intent(test) | |
| print(f"Confidence OK: {assess_confidence(result)}") |