Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files53 unmodified lines545556575859606199 unmodified lines16116216316416516616721 unmodified lines1891901911921931941951961 unmodified line19819920020120220320420520620720820921021121221321421521621721872 unmodified lines2912922932942952962972982993003013023033043053063073083093103113123133143153163173183192 unmodified lines3223233243253263273283293303313323333343353363377 unmodified lines3453463473483493503513523533543553563573583593603613623633643651 unmodified line36736836937037137237337453 unmodified lines"turn_count": 0,"facts_missing": [],"context_interpreted": False,"radar_count": 0, # track how often radar has fired"last_had_disclaimer": False,}99 unmodified linesRules:- If last_response_type was "question", action_needed CANNOT be "question"- action_needed CANNOT be the same as last_response_type- Extract ALL facts from user message even if implied- Update hypothesis confidence based on new evidence- search_queries must be specific legal questions for vector search"""21 unmodified lines"urgency": "medium","hypotheses": [{"claim": user_message[:80], "confidence": "low", "evidence": []}],"facts_extracted": {}, "facts_missing": [],"legal_issues": [],"clarifying_question": {},"stage": "understanding", "last_response_type": last_response_type,"updated_summary": f"{summary} | {user_message[:100]}","search_queries": [user_message[:200]],1 unmodified line"format_decision": "none"}# Hard enforce variety at code levelif analysis.get("action_needed") == last_response_type and last_response_type != "none":fallback_map = {"question": "partial_finding","advice": "observation","reflection": "partial_finding","partial_finding": "advice","observation": "advice","reassurance": "partial_finding","strategy": "observation","explanation": "advice",}analysis["action_needed"] = fallback_map.get(last_response_type, "advice")logger.info(f"Variety enforcement: changed action from {last_response_type} to {analysis['action_needed']}")return analysis72 unmodified linesMissing facts: {', '.join(cs.get('facts_missing', [])) or 'none critical'}Stage: {cs.get('stage', 'intake')}"""# Context interpretation — only when explicitly flagged, not every turninterpret_instruction = ""should_interpret = analysis.get("should_interpret_context", False)if should_interpret and not cs.get("context_interpreted"):interpret_instruction = "\nBefore your main response, briefly (2 lines max) reflect your understanding back. Start with 'Based on what you've told me...' — only once, never again."# Radar — suppress if fired recentlyradar_count = cs.get("radar_count", 0)turn_count = cs.get("turn_count", 0)show_radar = (turn_count - radar_count) >= 3 # only every 3 turnsif show_radar:cs["radar_count"] = turn_countradar_instruction = """PROACTIVE RADAR — add ONE brief "⚡ You Should Also Know" line (1-2 sentences only).Surface one related legal angle the user hasn't asked about but which is directly relevant.Skip if the response is already long or if this is a purely academic question."""else:radar_instruction = ""# Disclaimer — suppress on short follow-up turnsstage = analysis.get("stage", "understanding")show_disclaimer = stage not in ["understanding", "followup"] and turn_count % 2 == 0disclaimer_instruction = '\nEnd with: "Note: This is not legal advice. Consult a qualified advocate for your specific situation."' if show_disclaimer else ""summary = session.get("summary", "")last_msgs = session.get("last_3_messages", [])2 unmodified linesfor m in last_msgs[-4:]) if last_msgs else ""action = analysis.get("action_needed", "advice")# For question turns — force brevityif action == "question":length_instruction = "\nKEEP THIS RESPONSE TO 3 SENTENCES MAXIMUM. Ask the question. Nothing else."elif stage in ["understanding", "intake"]:length_instruction = "\nKEEP THIS RESPONSE UNDER 150 WORDS."else:length_instruction = ""user_content = f"""CONVERSATION SUMMARY:{summary if summary else "First message."}7 unmodified linesUSER MESSAGE: {user_message}THIS TURN:- Response type: {action} — execute this type ONLY, do not mix with other types- Legal hypotheses: {', '.join(h['claim'] for h in analysis.get('hypotheses', [])[:3]) or 'analysing'}- Stage: {stage}- Urgency: {analysis.get('urgency', 'medium')}- Format: {analysis.get('format_decision', 'appropriate for content')}{interpret_instruction}{length_instruction}Instructions:- Execute the response type "{action}" and ONLY that type this turn- Cite specific sources only when directly relevant — not in every response- Use your legal knowledge for reasoning and context- If giving strategy: include what the other side will argue{radar_instruction}{disclaimer_instruction}"""response = _client.chat.completions.create(model="llama-3.3-70b-versatile",1 unmodified line{"role": "system", "content": system_prompt},{"role": "user", "content": user_content}],temperature=0.7, # higher = more varied, less templatedmax_tokens=800 # shorter responses break the multi-section pattern)return response.choices[0].message.content53 unmodified lines54555657585999 unmodified lines15916016116216316421 unmodified lines1861871881891901911921 unmodified line19419519619719819972 unmodified lines2722732742752762772782792802812822832842852862872882892 unmodified lines2922932942952962977 unmodified lines3053063073083093103113123133143153163173183193203213223231 unmodified line32532632732832933033133253 unmodified lines"turn_count": 0,"facts_missing": [],"context_interpreted": False,}99 unmodified linesRules:- If last_response_type was "question", action_needed CANNOT be "question"- Extract ALL facts from user message even if implied- Update hypothesis confidence based on new evidence- search_queries must be specific legal questions for vector search"""21 unmodified lines"urgency": "medium","hypotheses": [{"claim": user_message[:80], "confidence": "low", "evidence": []}],"facts_extracted": {}, "facts_missing": [],"legal_issues": [], "clarifying_question": {},"stage": "understanding", "last_response_type": last_response_type,"updated_summary": f"{summary} | {user_message[:100]}","search_queries": [user_message[:200]],1 unmodified line"format_decision": "none"}return analysis72 unmodified linesMissing facts: {', '.join(cs.get('facts_missing', [])) or 'none critical'}Stage: {cs.get('stage', 'intake')}"""interpret_instruction = ""should_interpret = analysis.get("should_interpret_context", False)if should_interpret and not cs.get("context_interpreted"):interpret_instruction = """CONTEXT REFLECTION: Before your main response, briefly (2-3 lines) reflect your understanding back to the user. Start with "Based on what you've told me..." This builds trust and confirms you've been tracking the situation."""radar_instruction = """PROACTIVE RADAR — add after your main answer when user has described a real situation:Add a brief "⚡ You Should Also Know" section (3-4 lines max).Surface 1-2 related legal issues or remedies the user hasn't asked about but which are directly relevant.Example: User asked about wrongful termination → proactively mention injunction under Specific Relief Act as faster remedy.Skip this section for purely academic questions with no personal situation described."""summary = session.get("summary", "")last_msgs = session.get("last_3_messages", [])2 unmodified linesfor m in last_msgs[-4:]) if last_msgs else ""user_content = f"""CONVERSATION SUMMARY:{summary if summary else "First message."}7 unmodified linesUSER MESSAGE: {user_message}THIS TURN:- Legal hypotheses: {', '.join(h['claim'] for h in analysis.get('hypotheses', [])[:3]) or 'analysing'}- Stage: {analysis.get('stage', 'understanding')}- Urgency: {analysis.get('urgency', 'medium')}- Response type: {analysis.get('action_needed', 'advice')}- Format: {analysis.get('format_decision', 'appropriate for content')}{interpret_instruction}Instructions:- Cite specific sources when making legal claims- Use your legal knowledge for reasoning and context- Format: {analysis.get('format_decision', 'use the most appropriate format for the content type')}- Opposition war-gaming: if giving strategy, include what the other side will argue{radar_instruction}"""response = _client.chat.completions.create(model="llama-3.3-70b-versatile",1 unmodified line{"role": "system", "content": system_prompt},{"role": "user", "content": user_content}],temperature=0.3,max_tokens=1500)return response.choices[0].message.content
src/agent_v2.py+17-59