Add LLM token

This commit is contained in:
wangwei
2026-07-02 22:03:39 +08:00
parent e3afb8a07a
commit 52e67b0e7b
36 changed files with 2392 additions and 394 deletions
+23 -7
View File
@@ -85,9 +85,10 @@ async def analyze_stream(
Events: stage | source | finding | done | error
"""
from app.application.compliance.pipeline import (
detect_cross_clause_conflicts,
extract_text_from_doc_id,
extract_text_from_file,
run_clauses_parallel,
run_clauses_streaming,
split_into_clauses,
synthesize_conclusion,
)
@@ -135,23 +136,27 @@ async def analyze_stream(
await asyncio.sleep(0)
clauses: list[str] = await asyncio.to_thread(split_into_clauses, para_text, client)
# ── Stage 3: retrieve + gap check (parallel across all clauses) ────────────
# ── Stage 3: progressive per-clause retrieve + gap check ──────
findings: list[dict] = []
total_clauses = len(clauses)
yield _sse({
"type": "stage",
"stage": "analyzing",
"label": f"Analyzing {len(clauses)} clauses in parallel",
"label": f"Analyzing {total_clauses} clauses…",
})
# Emit initial progress so the frontend can show the total count
yield _sse({"type": "progress", "done": 0, "total": total_clauses})
await asyncio.sleep(0)
clause_results = await run_clauses_parallel(
done_count = 0
# Stream results as each clause completes (not after all finish)
async for res in run_clauses_streaming(
clauses, retrieval_service, client,
top_k=5,
domains=domains or None,
)
for res in clause_results:
):
done_count += 1
i = res["index"]
chunks = res["chunks"]
finding = res["finding"]
@@ -165,14 +170,25 @@ async def analyze_stream(
"score": round(float(getattr(chunk, "score", 0)), 3),
"status": "retrieved",
"full_content": (getattr(chunk, "text", "") or "")[:300],
"clause_index": i,
})
if finding:
findings.append(finding)
yield _sse({"type": "finding", **finding})
# Real progress update after each clause completes
yield _sse({"type": "progress", "done": done_count, "total": total_clauses})
await asyncio.sleep(0)
# ── Stage 3b: cross-clause conflict detection ─────────────────
if findings:
conflicts = await asyncio.to_thread(
detect_cross_clause_conflicts, findings, client
)
if conflicts:
yield _sse({"type": "conflicts", "items": conflicts})
# ── Stage 4: synthesize conclusion ────────────────────────────
yield _sse({"type": "stage", "stage": "concluding", "label": "Generating conclusion…"})
await asyncio.sleep(0)