Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- src/agent_v2.py +18 -11
src/agent_v2.py
CHANGED
|
@@ -54,6 +54,7 @@ def empty_case_state() -> Dict:
|
|
| 54 |
"turn_count": 0,
|
| 55 |
"facts_missing": [],
|
| 56 |
"context_interpreted": False,
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
|
|
@@ -159,6 +160,7 @@ NEW USER MESSAGE:
|
|
| 159 |
|
| 160 |
Rules:
|
| 161 |
- If last_response_type was "question", action_needed CANNOT be "question"
|
|
|
|
| 162 |
- Extract ALL facts from user message even if implied
|
| 163 |
- Update hypothesis confidence based on new evidence
|
| 164 |
- search_queries must be specific legal questions for vector search"""
|
|
@@ -232,6 +234,7 @@ def retrieve_parallel(search_queries: List[str], top_k: int = 5) -> List[Dict]:
|
|
| 232 |
def respond(user_message: str, analysis: Dict, chunks: List[Dict], session: Dict) -> str:
|
| 233 |
system_prompt = build_prompt(analysis)
|
| 234 |
cs = session["case_state"]
|
|
|
|
| 235 |
|
| 236 |
context_parts = []
|
| 237 |
for chunk in chunks[:5]:
|
|
@@ -262,7 +265,7 @@ def respond(user_message: str, analysis: Dict, chunks: List[Dict], session: Dict
|
|
| 262 |
) or " none established"
|
| 263 |
|
| 264 |
case_summary = f"""
|
| 265 |
-
CASE STATE (built across {
|
| 266 |
Parties: {', '.join(cs.get('parties', [])) or 'unspecified'}
|
| 267 |
Events: {', '.join(cs.get('events', [])) or 'unspecified'}
|
| 268 |
Evidence: {', '.join(cs.get('documents', [])) or 'none mentioned'}
|
|
@@ -272,18 +275,22 @@ Active hypotheses:
|
|
| 272 |
Missing facts: {', '.join(cs.get('facts_missing', [])) or 'none critical'}
|
| 273 |
Stage: {cs.get('stage', 'intake')}"""
|
| 274 |
|
|
|
|
| 275 |
interpret_instruction = ""
|
| 276 |
should_interpret = analysis.get("should_interpret_context", False)
|
| 277 |
-
if should_interpret and not cs.get("context_interpreted"):
|
| 278 |
-
interpret_instruction = ""
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
summary = session.get("summary", "")
|
| 289 |
last_msgs = session.get("last_3_messages", [])
|
|
|
|
| 54 |
"turn_count": 0,
|
| 55 |
"facts_missing": [],
|
| 56 |
"context_interpreted": False,
|
| 57 |
+
"last_radar_turn": -3, # track when radar last fired
|
| 58 |
}
|
| 59 |
|
| 60 |
|
|
|
|
| 160 |
|
| 161 |
Rules:
|
| 162 |
- If last_response_type was "question", action_needed CANNOT be "question"
|
| 163 |
+
- action_needed SHOULD differ from last_response_type for variety
|
| 164 |
- Extract ALL facts from user message even if implied
|
| 165 |
- Update hypothesis confidence based on new evidence
|
| 166 |
- search_queries must be specific legal questions for vector search"""
|
|
|
|
| 234 |
def respond(user_message: str, analysis: Dict, chunks: List[Dict], session: Dict) -> str:
|
| 235 |
system_prompt = build_prompt(analysis)
|
| 236 |
cs = session["case_state"]
|
| 237 |
+
turn_count = cs.get("turn_count", 0)
|
| 238 |
|
| 239 |
context_parts = []
|
| 240 |
for chunk in chunks[:5]:
|
|
|
|
| 265 |
) or " none established"
|
| 266 |
|
| 267 |
case_summary = f"""
|
| 268 |
+
CASE STATE (built across {turn_count} turns):
|
| 269 |
Parties: {', '.join(cs.get('parties', [])) or 'unspecified'}
|
| 270 |
Events: {', '.join(cs.get('events', [])) or 'unspecified'}
|
| 271 |
Evidence: {', '.join(cs.get('documents', [])) or 'none mentioned'}
|
|
|
|
| 275 |
Missing facts: {', '.join(cs.get('facts_missing', [])) or 'none critical'}
|
| 276 |
Stage: {cs.get('stage', 'intake')}"""
|
| 277 |
|
| 278 |
+
# Context interpretation — only once per conversation at turn 2
|
| 279 |
interpret_instruction = ""
|
| 280 |
should_interpret = analysis.get("should_interpret_context", False)
|
| 281 |
+
if should_interpret and not cs.get("context_interpreted") and turn_count == 2:
|
| 282 |
+
interpret_instruction = "\nIn one sentence only, reflect back your understanding of the situation before responding."
|
| 283 |
+
|
| 284 |
+
# Radar — only fires every 3 turns, not every turn
|
| 285 |
+
last_radar_turn = cs.get("last_radar_turn", -3)
|
| 286 |
+
if (turn_count - last_radar_turn) >= 3:
|
| 287 |
+
cs["last_radar_turn"] = turn_count
|
| 288 |
+
radar_instruction = """
|
| 289 |
+
PROACTIVE RADAR — only if a genuinely non-obvious legal angle exists that hasn't been mentioned yet:
|
| 290 |
+
Add a single "⚡ You Should Also Know:" line (1-2 sentences max).
|
| 291 |
+
Skip entirely if the response already covers all relevant angles or if this is a question/understanding turn."""
|
| 292 |
+
else:
|
| 293 |
+
radar_instruction = "Do NOT add a 'You Should Also Know' section this turn."
|
| 294 |
|
| 295 |
summary = session.get("summary", "")
|
| 296 |
last_msgs = session.get("last_3_messages", [])
|