Spaces:
Build error
Build error
Update common/ResponseValidation.py
Browse files- common/ResponseValidation.py +16 -21
common/ResponseValidation.py
CHANGED
|
@@ -31,31 +31,19 @@ def matches_user_speaking_style(answer, processor_llm, user_profile, agent_quest
|
|
| 31 |
# --- Step 2: Context-sensitive first-person check ---
|
| 32 |
fp_prompt = f"""
|
| 33 |
You are an expert in analysing writing style and narrative perspective.
|
| 34 |
-
|
| 35 |
Determine whether the following response is *stylistically appropriate* and matches a first-person perspective *when contextually expected*.
|
| 36 |
-
|
| 37 |
- A first-person response typically includes pronouns like "I", "me", "my", "mine", "we", "our", or "us".
|
| 38 |
- However, for **short factual responses** (e.g. "What's your name?" → "Alex") or answers that clearly imply personal ownership or involvement (e.g. "Our team led the project"), the absence of explicit first-person pronouns can still be acceptable.
|
| 39 |
- The key question is: **Given the question and expected tone, is the response appropriately personal and aligned with a first-person speaking style?**
|
| 40 |
-
|
| 41 |
Evaluate the response below.
|
| 42 |
-
|
| 43 |
### Question:
|
| 44 |
{agent_question}
|
| 45 |
-
|
| 46 |
### Response:
|
| 47 |
{answer}
|
| 48 |
-
|
| 49 |
Return:
|
| 50 |
First Person: Yes
|
| 51 |
or
|
| 52 |
First Person: No
|
| 53 |
-
and briefly explain if "No".
|
| 54 |
-
|
| 55 |
-
Output format:
|
| 56 |
-
First Person: Yes
|
| 57 |
-
or
|
| 58 |
-
First Person: No
|
| 59 |
Reason: <short explanation>
|
| 60 |
"""
|
| 61 |
fp_response = processor_llm.invoke(fp_prompt)
|
|
@@ -77,7 +65,7 @@ Reason: <short explanation>
|
|
| 77 |
# --- Step 4: Style validation prompt ---
|
| 78 |
style_check_prompt = f"""
|
| 79 |
You are a communication coach and writing style analyst.
|
| 80 |
-
Evaluate
|
| 81 |
---
|
| 82 |
### Response:
|
| 83 |
{answer}
|
|
@@ -88,23 +76,29 @@ Evaluate whether the following response aligns with the given communication prof
|
|
| 88 |
- Common Topics: {topics}
|
| 89 |
---
|
| 90 |
### Instructions:
|
| 91 |
-
|
| 92 |
-
-
|
| 93 |
-
-
|
| 94 |
-
Style Match:
|
| 95 |
-
|
| 96 |
-
Style Match:
|
|
|
|
|
|
|
| 97 |
"""
|
| 98 |
logging.info("[Style Match Check] Invoking LLM for profile-based style check")
|
| 99 |
style_response = processor_llm.invoke(style_check_prompt)
|
| 100 |
style_result = style_response.content.strip().lower()
|
| 101 |
|
| 102 |
-
if "style match: yes" in style_result:
|
| 103 |
-
logging.info("[Style Match Check] Match confirmed")
|
| 104 |
if return_explanation:
|
| 105 |
return True, None
|
| 106 |
return True
|
|
|
|
| 107 |
elif "style match: no" in style_result:
|
|
|
|
|
|
|
|
|
|
| 108 |
# --- Ask LLM for explanation on mismatch ---
|
| 109 |
explanation_prompt = f"""
|
| 110 |
You are a communication coach and writing style analyst.
|
|
@@ -127,6 +121,7 @@ Please provide a concise reason why the style does not match.
|
|
| 127 |
if return_explanation:
|
| 128 |
return False, explanation
|
| 129 |
return False
|
|
|
|
| 130 |
else:
|
| 131 |
logging.warning(f"[Style Match Check] Unexpected output format: {style_result}")
|
| 132 |
if return_explanation:
|
|
|
|
| 31 |
# --- Step 2: Context-sensitive first-person check ---
|
| 32 |
fp_prompt = f"""
|
| 33 |
You are an expert in analysing writing style and narrative perspective.
|
|
|
|
| 34 |
Determine whether the following response is *stylistically appropriate* and matches a first-person perspective *when contextually expected*.
|
|
|
|
| 35 |
- A first-person response typically includes pronouns like "I", "me", "my", "mine", "we", "our", or "us".
|
| 36 |
- However, for **short factual responses** (e.g. "What's your name?" → "Alex") or answers that clearly imply personal ownership or involvement (e.g. "Our team led the project"), the absence of explicit first-person pronouns can still be acceptable.
|
| 37 |
- The key question is: **Given the question and expected tone, is the response appropriately personal and aligned with a first-person speaking style?**
|
|
|
|
| 38 |
Evaluate the response below.
|
|
|
|
| 39 |
### Question:
|
| 40 |
{agent_question}
|
|
|
|
| 41 |
### Response:
|
| 42 |
{answer}
|
|
|
|
| 43 |
Return:
|
| 44 |
First Person: Yes
|
| 45 |
or
|
| 46 |
First Person: No
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
Reason: <short explanation>
|
| 48 |
"""
|
| 49 |
fp_response = processor_llm.invoke(fp_prompt)
|
|
|
|
| 65 |
# --- Step 4: Style validation prompt ---
|
| 66 |
style_check_prompt = f"""
|
| 67 |
You are a communication coach and writing style analyst.
|
| 68 |
+
Evaluate how well the following response aligns with the given communication profile.
|
| 69 |
---
|
| 70 |
### Response:
|
| 71 |
{answer}
|
|
|
|
| 76 |
- Common Topics: {topics}
|
| 77 |
---
|
| 78 |
### Instructions:
|
| 79 |
+
Assess how well the response aligns with the communication profile.
|
| 80 |
+
- Allow for natural variation and expressive differences.
|
| 81 |
+
- If the tone and structure mostly match, even if not perfect, that’s acceptable.
|
| 82 |
+
- Only return “Style Match: No” if the response clearly *conflicts* with the profile (e.g., too formal, too short, too robotic).
|
| 83 |
+
Respond only with one of:
|
| 84 |
+
- Style Match: Yes
|
| 85 |
+
- Style Match: Mostly
|
| 86 |
+
- Style Match: No
|
| 87 |
"""
|
| 88 |
logging.info("[Style Match Check] Invoking LLM for profile-based style check")
|
| 89 |
style_response = processor_llm.invoke(style_check_prompt)
|
| 90 |
style_result = style_response.content.strip().lower()
|
| 91 |
|
| 92 |
+
if "style match: yes" in style_result or "style match: mostly" in style_result:
|
| 93 |
+
logging.info("[Style Match Check] Match confirmed (or mostly matched)")
|
| 94 |
if return_explanation:
|
| 95 |
return True, None
|
| 96 |
return True
|
| 97 |
+
|
| 98 |
elif "style match: no" in style_result:
|
| 99 |
+
if "first person: yes" in fp_result:
|
| 100 |
+
logging.info("[Style Match Check] Potential false negative: First-person check passed but style rejected")
|
| 101 |
+
|
| 102 |
# --- Ask LLM for explanation on mismatch ---
|
| 103 |
explanation_prompt = f"""
|
| 104 |
You are a communication coach and writing style analyst.
|
|
|
|
| 121 |
if return_explanation:
|
| 122 |
return False, explanation
|
| 123 |
return False
|
| 124 |
+
|
| 125 |
else:
|
| 126 |
logging.warning(f"[Style Match Check] Unexpected output format: {style_result}")
|
| 127 |
if return_explanation:
|