Inspiration

Vietnamese SMEs sign international contracts drafted by the other side's lawyers — and routinely accept clauses that are unfavorable or even void under Vietnamese law (e.g. a 15% penalty when the Commercial Law caps it at 8%). Big-law AI tools (Harvey, Luminance, Spellbook) assume an in-house legal team, an English/common-law playbook, and enterprise budgets. Nobody serves the SME that just needs to know "what's risky here, and how do I push back from my actual bargaining position?" — in Vietnamese, on the chat apps they already use.

What it does

Legal Guard is an autopilot legal agent that:

  • Reviews a contract (paste, upload, or scanned image via OCR) and flags each risky clause, separating ⚖️ illegal (voidable) from merely unfavorable, with a deterministic citation to the in-force article.
  • Negotiates across rounds, not one-shot: you declare leverage / urgency / BATNA and the agent runs a stateful negotiation — a concession ledger remembers what's already secured (never gives back a won point), a deterministic walk-away guardrail protects red-lines, and it proposes trade-based next moves, learning from real deal outcomes.
  • Keeps a human in the loop: the message to the counterparty is locked until a reviewer approves; rejecting escalates to a lawyer.
  • Works proactively (autopilot): a daily cron scans newly-issued laws and tells you which past contracts are now affected — the agent works while you sleep — and self-tunes on dismissed false alarms.
  • Grounded, never fabricated: in-force filtering (won't cite repealed law), NLI verification, and it abstains when the knowledge base doesn't cover a question. Measured 54/54 on a lawyer-style golden set (published at /trust).
  • Channels: Web UI, Slack and Zalo bots, and an MCP tool.

How we built it

  • Hexagonal (Ports & Adapters) FastAPI core — the domain never imports a vendor SDK; swapping a provider is one line. Runs fully offline in a stub mode, so the whole flow is testable without keys (400+ tests).
  • ReAct agent loop with tools (search_legal_knowledge, flag_risk, propose_fallback, request_human_review); every step recorded in a trace + execution_summary.
  • Qwen models on Qwen Cloud / DashScope (Alibaba Model Studio), right-sized per task: qwen3.7-max (reasoning agent) · qwen-flash (NLI verify / self-critique) · qwen-plus (legal lookup) · text-embedding-v4 (retrieval) · qwen3-rerank (cross-encoder) · qwen3.7-plus (OCR).
  • Deployed on Alibaba Cloud ECS: Docker (Caddy HTTPS + app + Postgres + Redis), Alembic migrations, embeddings persisted in Postgres.
  • RAG quality: hybrid retrieval (BM25 + embeddings, RRF), grounding + citation, 2-layer verification (LLM-judge + NLI entailment), in-force / point-in-time filtering, citation closure.

Challenges we ran into

  • Not citing dead law: an in-force filter that returns only law valid at the relevant point in time — a safety feature that must abstain rather than answer when unsure.
  • Grounding vs hallucination: NLI entailment to reject "citation exists but doesn't support the claim".
  • Multi-round memory: keeping a structured concession ledger so the agent never re-negotiates a won point or forgets a red-line across rounds.
  • Latency: model right-sizing (a fast model for yes/no checks) cut post-agent verification from ~24s to ~3s.

What we learned

Real domain expertise (actual in-force legal knowledge + grounding) matters more than model size when building a vertical AI agent. The moat isn't the RAG — that's commodity — it's position-aware negotiation, a data flywheel, and trust by design (human-in-the-loop + audit trail, aligned with Vietnam's new AI Law 134/2025).

Built With

Share this project:

Updates