Update app.py
Browse files
app.py
CHANGED
|
@@ -127,10 +127,11 @@ def generate_policy_queries(claim_info_json: str) -> str:
|
|
| 127 |
"""
|
| 128 |
global llm_model
|
| 129 |
prompt = f"""
|
| 130 |
-
Analyze the following auto insurance claim
|
| 131 |
-
-
|
|
|
|
| 132 |
- Claim Data: {claim_info_json}
|
| 133 |
-
- Return a JSON object
|
| 134 |
"""
|
| 135 |
try:
|
| 136 |
messages = [{"role": "user", "content": prompt}]
|
|
@@ -154,7 +155,7 @@ def retrieve_policy_text(queries_json: str) -> str:
|
|
| 154 |
policy_texts = []
|
| 155 |
for q in query_strings:
|
| 156 |
query_embedding = embedder.encode([q])[0].tolist()
|
| 157 |
-
results = collection.query(query_embeddings=[query_embedding], n_results=
|
| 158 |
if results['documents']:
|
| 159 |
policy_texts.extend(results['documents'][0])
|
| 160 |
return "\n\n".join(set(policy_texts)) # Set removes duplicate chunks
|
|
@@ -194,7 +195,7 @@ def generate_recommendation(claim_info_json: str, policy_text: str) -> str:
|
|
| 194 |
return f"Error generating recommendation: {str(e)}"
|
| 195 |
|
| 196 |
@tool
|
| 197 |
-
def finalize_decision(claim_info_json: str, recommendation_json: str) -> str:
|
| 198 |
"""Finalize the claim decision based on the recommendation and format output.
|
| 199 |
|
| 200 |
Args:
|
|
@@ -202,8 +203,16 @@ def finalize_decision(claim_info_json: str, recommendation_json: str) -> str:
|
|
| 202 |
recommendation_json: The AI-generated recommendation output.
|
| 203 |
"""
|
| 204 |
try:
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Safe defaults if the model missed a key
|
| 209 |
covered = "covered" in rec_data.get("recommendation_summary", "").lower() or (rec_data.get("settlement_amount", 0) or 0) > 0
|
|
|
|
| 127 |
"""
|
| 128 |
global llm_model
|
| 129 |
prompt = f"""
|
| 130 |
+
Analyze the following auto insurance claim and generate exactly 2 short, keyword-based search queries to find the right policy sections.
|
| 131 |
+
- Example good queries: "collision coverage", "deductible limits", "exclusions".
|
| 132 |
+
- DO NOT write full sentences.
|
| 133 |
- Claim Data: {claim_info_json}
|
| 134 |
+
- Return a JSON object strictly matching this schema: {{"queries": ["keyword 1", "keyword 2"]}}
|
| 135 |
"""
|
| 136 |
try:
|
| 137 |
messages = [{"role": "user", "content": prompt}]
|
|
|
|
| 155 |
policy_texts = []
|
| 156 |
for q in query_strings:
|
| 157 |
query_embedding = embedder.encode([q])[0].tolist()
|
| 158 |
+
results = collection.query(query_embeddings=[query_embedding], n_results=1)
|
| 159 |
if results['documents']:
|
| 160 |
policy_texts.extend(results['documents'][0])
|
| 161 |
return "\n\n".join(set(policy_texts)) # Set removes duplicate chunks
|
|
|
|
| 195 |
return f"Error generating recommendation: {str(e)}"
|
| 196 |
|
| 197 |
@tool
|
| 198 |
+
def finalize_decision(claim_info_json: str | dict, recommendation_json: str | dict) -> str:
|
| 199 |
"""Finalize the claim decision based on the recommendation and format output.
|
| 200 |
|
| 201 |
Args:
|
|
|
|
| 203 |
recommendation_json: The AI-generated recommendation output.
|
| 204 |
"""
|
| 205 |
try:
|
| 206 |
+
# Gracefully handle if the LLM passes a dict OR a string
|
| 207 |
+
if isinstance(claim_info_json, dict):
|
| 208 |
+
claim_info = ClaimInfo.model_validate(claim_info_json)
|
| 209 |
+
else:
|
| 210 |
+
claim_info = ClaimInfo.model_validate_json(claim_info_json)
|
| 211 |
+
|
| 212 |
+
if isinstance(recommendation_json, dict):
|
| 213 |
+
rec_data = recommendation_json
|
| 214 |
+
else:
|
| 215 |
+
rec_data = json.loads(recommendation_json)
|
| 216 |
|
| 217 |
# Safe defaults if the model missed a key
|
| 218 |
covered = "covered" in rec_data.get("recommendation_summary", "").lower() or (rec_data.get("settlement_amount", 0) or 0) > 0
|