forked from princeixr/Multi-Agent-Financial-Complaint-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting_sample.py
More file actions
378 lines (331 loc) · 12.2 KB
/
testing_sample.py
File metadata and controls
378 lines (331 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Batch pipeline test: load N CFPB-style CSV rows and run ``process_complaint`` on each.
from __future__ import annotations
import json
import os
import traceback
from datetime import datetime
try:
from dotenv import load_dotenv
except ModuleNotFoundError:
load_dotenv = None # type: ignore[assignment, misc]
if load_dotenv:
load_dotenv()
try:
from app.observability.logging import setup_logging
from app.observability.tracing import setup_tracing
except ImportError:
setup_logging = None # type: ignore[assignment, misc]
setup_tracing = None # type: ignore[assignment, misc]
try:
import pandas as pd
except ModuleNotFoundError as e:
raise RuntimeError(
"pandas is required. Install in this environment, e.g. "
"'./.venv/bin/python -m pip install pandas'"
) from e
from app.knowledge.mock_company_pack import deployment_label
from app.orchestrator.workflow import process_complaint
CSV_PATH = os.getenv("TEST_CSV_PATH", "complaints.csv")
OUTPUT_CSV = os.getenv(
"TEST_PIPELINE_OUTPUT_CSV",
"testing_sample_pipeline_output.csv",
)
SAMPLE_COUNT = max(1, 5 )#int(os.getenv("TEST_SAMPLE_COUNT", "10")))
def get_first_existing(df: pd.DataFrame, col_candidates: list[str]) -> str | None:
for c in col_candidates:
if c in df.columns:
return c
return None
def _safe_json(obj: object) -> str:
if obj is None:
return ""
if isinstance(obj, str):
return obj
try:
return json.dumps(obj, default=str)
except TypeError:
return str(obj)
def build_row_record(
payload: dict,
case: object,
*,
source_csv: str,
sample_index: int,
pipeline_error: str | None = None,
) -> dict[str, str | int | None]:
"""Flatten CaseRead (or dict) into one CSV row."""
if hasattr(case, "model_dump"):
c = case.model_dump()
elif isinstance(case, dict):
c = case
else:
c = {}
cls = c.get("classification") or {}
risk = c.get("risk_assessment") or {}
res = c.get("proposed_resolution") or {}
rc = c.get("root_cause_hypothesis") or {}
st = c.get("status", "")
if hasattr(st, "value"):
st = st.value
return {
"sample_index": sample_index,
"run_at_utc": datetime.utcnow().isoformat() + "Z",
"source_csv": source_csv,
"deployment": deployment_label(),
"consumer_narrative": (c.get("consumer_narrative") or payload.get("consumer_narrative")),
"routed_to": c.get("routed_to"),
"status": str(st) if st is not None else "",
"classification_product_category": cls.get("product_category"),
"classification_issue_type": cls.get("issue_type"),
"classification_confidence": cls.get("confidence"),
"risk_level": (risk.get("risk_level") if isinstance(risk, dict) else None),
"risk_score": risk.get("risk_score") if isinstance(risk, dict) else None,
"root_cause_summary": rc.get("summary") if isinstance(rc, dict) else _safe_json(rc),
"resolution_action": res.get("recommended_action") if isinstance(res, dict) else None,
"resolution_confidence": res.get("confidence") if isinstance(res, dict) else None,
"compliance_flags_json": _safe_json(c.get("compliance_flags")),
"review_notes": c.get("review_notes"),
"classification_json": _safe_json(cls),
"risk_assessment_json": _safe_json(risk),
"proposed_resolution_json": _safe_json(res),
"root_cause_json": _safe_json(rc),
"evidence_trace_json": _safe_json(c.get("evidence_trace")),
"pipeline_error": pipeline_error or "",
}
def _cell_str(row: pd.Series, col: str | None) -> str | None:
if not col or col not in row.index:
return None
val = row[col]
if pd.isna(val):
return None
s = str(val).strip()
return s or None
def row_to_payload(
row: pd.Series,
*,
col_narrative: str | None,
col_product: str | None,
col_sub_product: str | None,
col_sub_issue: str | None,
col_company: str | None,
col_state: str | None,
col_zip: str | None,
col_channel: str | None,
col_response: str | None,
col_date_received: str | None,
col_issue: str | None,
) -> dict:
channel_raw = (
str(row[col_channel]).strip() if col_channel else "web"
).lower()
channel_map = {
"web": "web",
"online": "web",
"phone": "phone",
"email": "email",
"fax": "fax",
"postal": "postal",
"mail": "postal",
"referral": "referral",
}
channel = channel_map.get(channel_raw, "web")
submitted_at = None
if col_date_received:
val = row[col_date_received]
if pd.notna(val):
try:
submitted_at = pd.to_datetime(val, errors="coerce").to_pydatetime()
except Exception:
submitted_at = None
if col_narrative:
nar_raw = row[col_narrative]
nar_str = "" if pd.isna(nar_raw) else str(nar_raw).strip()
else:
nar_str = ""
product_str = _cell_str(row, col_product)
sub_product_str = _cell_str(row, col_sub_product)
issue_str = _cell_str(row, col_issue)
sub_issue_str = _cell_str(row, col_sub_issue)
has_rich_narrative = len(nar_str) >= 10
has_cfpb_core = bool(product_str and issue_str)
if not has_rich_narrative and not has_cfpb_core:
raise ValueError(
"Row needs either consumer_narrative (>=10 chars) or Product+Issue for CFPB structured path"
)
return {
"consumer_narrative": nar_str if nar_str else None,
"product": product_str,
"sub_product": sub_product_str,
"cfpb_product": product_str,
"cfpb_sub_product": sub_product_str,
"cfpb_issue": issue_str,
"cfpb_sub_issue": sub_issue_str,
"company": _cell_str(row, col_company),
"state": _cell_str(row, col_state),
"zip_code": _cell_str(row, col_zip),
"channel": channel,
"submitted_at": submitted_at.isoformat() if submitted_at else None,
"external_product_category": product_str,
"external_issue_type": issue_str,
"requested_resolution": _cell_str(row, col_response),
}
def main() -> None:
if setup_logging:
setup_logging()
if setup_tracing:
setup_tracing()
llm_provider = os.getenv("LLM_PROVIDER", "openai").lower()
api_key_env = "DEEPSEEK_API_KEY" if llm_provider == "deepseek" else "OPENAI_API_KEY"
api_key = os.getenv(api_key_env)
print(f"LLM_PROVIDER: {llm_provider}")
print(f"{api_key_env} set:", bool(api_key))
print("CSV_PATH:", CSV_PATH)
print("DEFAULT_COMPANY_ID:", DEFAULT_COMPANY_ID)
print("SAMPLE_COUNT:", SAMPLE_COUNT)
print("Pipeline output CSV:", OUTPUT_CSV)
df = pd.read_csv(CSV_PATH)
print("Loaded row columns:")
print(list(df.columns))
col_narrative = get_first_existing(
df,
[
"Consumer complaint narrative",
"consumer_narrative",
"narrative",
],
)
col_product = get_first_existing(df, ["Product", "product"])
col_sub_product = get_first_existing(df, ["Sub-product", "sub_product"])
col_company = get_first_existing(df, ["Company", "company"])
col_state = get_first_existing(df, ["State", "state"])
col_zip = get_first_existing(df, ["ZIP code", "Zip code", "zip_code", "ZIP"])
col_channel = get_first_existing(df, ["Submitted via", "Channel", "channel"])
col_response = get_first_existing(
df, ["Company response to consumer", "Company response"]
)
col_date_received = get_first_existing(df, ["Date received", "date_received"])
col_issue = get_first_existing(df, ["Issue", "issue"])
col_sub_issue = get_first_existing(
df, ["Sub-issue", "Sub-Issue", "sub_issue", "Sub issue"]
)
missing = [name for name, val in [("issue", col_issue)] if val is None]
if missing:
raise RuntimeError(
"CSV is missing required columns for the test: " + ", ".join(missing)
)
assert col_issue is not None
if col_narrative is None and not (col_product and col_issue):
raise RuntimeError(
"CSV needs a narrative column, or both Product and Issue columns, "
"for structured-only CFPB rows."
)
print("Using columns:")
print(
{
"narrative": col_narrative,
"issue": col_issue,
"product": col_product,
"sub_product": col_sub_product,
"company": col_company,
"state": col_state,
"zip_code": col_zip,
"channel": col_channel,
"date_received": col_date_received,
"requested_resolution": col_response,
"sub_issue": col_sub_issue,
}
)
narrative_ok = (
df[col_narrative].notna()
& (df[col_narrative].astype(str).str.len() >= 10)
if col_narrative
else pd.Series(False, index=df.index)
)
structured_ok = pd.Series(False, index=df.index)
if col_product and col_issue:
structured_ok = (
df[col_product].notna()
& df[col_issue].notna()
& (df[col_product].astype(str).str.strip() != "")
& (df[col_issue].astype(str).str.strip() != "")
)
valid_mask = narrative_ok | structured_ok
if not valid_mask.any():
raise RuntimeError(
"No valid rows: need narrative >= 10 characters or non-empty Product+Issue."
)
valid_df = df.loc[valid_mask]
n_available = len(valid_df)
n_run = min(SAMPLE_COUNT, n_available)
sample_df = valid_df.sample(n=n_run)
print(f"\nValid rows in file: {n_available}; running {n_run} random sample(s) (SAMPLE_COUNT={SAMPLE_COUNT}).")
if n_run < SAMPLE_COUNT:
print(f"Note: fewer than {SAMPLE_COUNT} valid rows; only {n_run} executed.")
if not api_key:
print(
f"\n{api_key_env} is not set; skipping the LLM-powered pipeline run."
)
return
out_rows: list[dict] = []
for i, (_, row) in enumerate(sample_df.iterrows(), start=1):
print(f"\n{'='*60}\n--- Complaint {i}/{n_run} ---\n{'='*60}")
try:
payload = row_to_payload(
row,
col_narrative=col_narrative,
col_product=col_product,
col_sub_product=col_sub_product,
col_sub_issue=col_sub_issue,
col_company=col_company,
col_state=col_state,
col_zip=col_zip,
col_channel=col_channel,
col_response=col_response,
col_date_received=col_date_received,
col_issue=col_issue,
)
except ValueError as e:
print(f"Skip row {i}: {e}")
continue
trimmed = {
k: (str(v)[:120] + "..." if isinstance(v, str) and len(v) > 120 else v)
for k, v in payload.items()
}
print("Payload (trimmed):", trimmed)
try:
final_state = process_complaint(payload)
except Exception as exc:
err = f"{type(exc).__name__}: {exc}"
print(f"Pipeline error: {err}")
traceback.print_exc()
out_rows.append(
build_row_record(
payload,
{},
source_csv=CSV_PATH,
sample_index=i,
pipeline_error=err[:2000],
)
)
continue
case = final_state["case"]
routed = getattr(case, "routed_to", None) or (
case.get("routed_to") if isinstance(case, dict) else None
)
print("Routed to:", routed)
if i == 1:
cls = getattr(case, "classification", None) or (
case.get("classification") if isinstance(case, dict) else None
)
print("\nClassification (sample detail for complaint 1):")
print(json.dumps(cls, indent=2, default=str))
out_rows.append(
build_row_record(payload, case, source_csv=CSV_PATH, sample_index=i)
)
if out_rows:
pd.DataFrame(out_rows).to_csv(OUTPUT_CSV, index=False)
print(f"\nWrote {len(out_rows)} row(s) to {OUTPUT_CSV!r}")
else:
print("\nNo output rows to write.")
if __name__ == "__main__":
main()