| from backend.llm.factory import get_llm |
| from backend.utils import safe_invoke |
| from backend.core.logger import get_logger |
|
|
| logger = get_logger(__name__) |
|
|
| PERSPECTIVE_PROMPT = """ |
| You are Velra's Perspective Detection Engine. |
| Your job is to perform a lightweight, rapid analysis of a conversation to determine the user's perspective before full emotional analysis. |
| |
| You must analyze the text/chat to estimate: |
| 1. Which side is the user (uploader)? Look for emotional investment, who seeks clarity, vulnerability patterns, and standard UI heuristics (e.g. right side is usually the sender/uploader in iMessage/WhatsApp). |
| 2. What is your confidence level in your perspective assessment? (0.0 to 1.0). |
| |
| --- |
| |
| ## INPUT CONTEXT |
| Conversation Text / OCR: |
| {chat} |
| |
| User's typed feelings (if any): |
| {feelings} |
| |
| --- |
| |
| ## RULES |
| - If there is no chat or very little text, confidence should be low (<0.5). |
| - If the feelings explicitly state their perspective (e.g., "I said...", "He didn't reply"), confidence should be high. |
| - Set needs_clarification to true if confidence is less than 0.75. |
| |
| ## OUTPUT JSON FORMAT |
| {{ |
| "likely_user_side": "left/right/unknown", |
| "confidence": 0.0, |
| "needs_clarification": true |
| }} |
| |
| Return ONLY the raw JSON without markdown fences. |
| """ |
|
|
| def detect_perspective(chat, feelings): |
| try: |
| logger.info("Starting perspective detection") |
| llm = get_llm() |
| chat_block = chat if chat and str(chat).strip() else "No conversation provided." |
| feelings_block = feelings if feelings and str(feelings).strip() else "No explicit feelings provided." |
| |
| prompt = PERSPECTIVE_PROMPT.format(chat=chat_block, feelings=feelings_block) |
| response = safe_invoke(llm, prompt) |
| logger.info(f"LLM Response: {response}") |
| return response |
| except Exception as e: |
| logger.error(f"Error in detect_perspective: {str(e)}") |
| return f"[ERROR] {str(e)}" |
|
|
|
|