The full technical description of DZ Legal Assistant: how the analysis, drafting, checks and exports work, how to run the local model, the benches and the repository layout. The short overview for visitors is the README.
Supported input: formal Arabic (الفصحى). The facts, grounds and requests should be written in formal Arabic, the language the requête itself must be in (CPCA art. 8). The quality targets and benchmark scores are measured on formal Arabic only.
- Proper names, company names and an occasional French legal term are kept exactly as written.
- Notes in Darija or mostly in French are still accepted, never refused or cut.
- The field shows a quiet note suggesting formal Arabic: «للحصول على أفضل نتيجة، اكتب هذا النص بالعربية الفصحى…».
- Test cases written that way form a separate robustness group, reported apart and not counted in the scores.

Version 0.9.1. Screens in this README are rendered by the automated UI tests, with fictitious data; where a model is shown, it is a scripted test engine.
Law texts: 1,361 articles are loaded:
- the complete Family Code and the complete CPCA (including the 2022 amendment, Law 22-13). They come from the law corpus, where every text was read against the official gazette page that published it;
- 33 articles of the Civil and Commercial Codes, proved letter by letter against their official PDFs;
- 17 labour-law articles, transcribed from the official page images.
None of these checks is a lawyer’s review (LAW-LIBRARY.md). A quotation is checked only against a loaded text. The complete Civil Code follows when the corpus delivers it.
Principles
| Principle | How it is enforced |
|---|---|
| Offline, no data leaves the machine | The desktop app uses no cloud APIs and no telemetry (the public web demo is separate, see above). The only model endpoint allowed is a loopback address (127.0.0.1, ::1, localhost), checked by LoopbackGuard, with the system proxy bypassed. See OFFLINE.md. |
| Assistive, never autonomous | A permanent “draft, review required” banner. The app only produces text for the lawyer to edit and sign. Nothing AI-written leaves the app unreviewed: each section the model wrote carries «راجعت هذا القسم وأتحمل مضمونه»; PDF, Word and copy are refused, with the reasons listed, until every AI section is ticked and no «(…)» placeholder remains. Editing a section does not count as reviewing it, and a redraft asks for a new review. Word exports carry a «مسودة» line in the page header while the draft option is on, and an “AI-assisted draft” note in the file properties. |
| Grounded, not free-generating | Identification, court, facts, dates and the signature block come from templates, deterministically, with grammatical gender taken from the form. Each facts line becomes a «حيث إن / وحيث إن» paragraph exactly as the lawyer wrote it. Small local models swapped lender and borrower when rewriting facts, so they no longer touch them. The requests are the lawyer’s own lines too, numbered under the fixed formula «في الشكل… في الموضوع:»: model-written requests changed the relief in 9 of 15 audited drafts. The model writes only the legal grounds. If a section has no notes, the model is not called. |
| Model output is checked | Deterministic checks remove paragraphs copied from the prompt’s worked example. They also flag foreign-script characters, stray Latin words, invented numbers, dropped numbers and legal qualifications the lawyer never used (for example «عقد المقاولة» in a loan case). Unusable answers are retried once, and otherwise replaced by the lawyer’s notes. |
| Verifiable | The Form Engine is pure and deterministic. A 24-case corpus measures its accuracy per element, and CI requires 100%. |
| Legal rules are data | Elements, articles, templates and prompts live in data/. Changing a rule means editing JSON, not code. |
Legal grounds: analysis of the facts
The legal grounds are often the hardest part for the lawyer. The form has three separate steps for them: Facts → Legal grounds → Requests.
In the Legal grounds step, «حلّل الوقائع واقترح الأسانيد» runs a three-stage analysis with the local model. It takes about 2–3 minutes on a CPU-only laptop (measured: 117–181 s on an Intel i5-1335U) and can be cancelled. While it runs, a panel shows each step, a timer and the model’s words as they are written, so the lawyer never waits in silence. It also says how much time is left, from this computer’s own earlier runs, and warns when the laptop is on battery or in power-saver mode. The desktop starts analysis only after the lawyer clicks Analyse the facts; moving between steps does not start it, even if an older settings file enables analyseInBackground. After starting it, the lawyer can continue filling other steps; nothing is relied on until the lawyer confirms the claim type. The same panel appears while the draft is written (the section being written is marked) and in the section assistant. To shorten the first wait, the app loads the model into memory in the background when it starts (local only; ollama.warmUp).
- Reading the facts. The model states the legal relationship, the obligation breached and what is sought. It recognises claim types from the catalog, by id only, or answers
otherwhen none applies. It names no article. - Gathering the candidates. No model is involved. The candidates are the catalog articles of the recognised claim types, nothing else. When no catalog claim type applies, there is no candidate and nothing is proposed: the app abstains rather than guess. (A library-wide text search used to add candidates; an audit found it added only noise, so it is now only the lawyer’s manual search.)
- Choosing articles. The model chooses among those candidates only, by key. For each it gives a role (basis, proof, damages, procedure) and a short reason. Anything else it writes is discarded.
What the lawyer sees:
- the model’s reading of the facts, labelled as the model’s;
- the exact articles it proposed, each with its role, «تعليل النموذج», the official text (or “not loaded”), the official reference and source link;
- nothing is ticked until the lawyer confirms the claim type. «نعم، هذا نوع الدعوى» ticks the articles the catalog marks as required for that type; «ليس هذا» withdraws the proposals. Ticked articles appear under «الأسانيد المعتمدة»;
- an optional box for the lawyer’s own arguments, and a search to add any article by number or words;
- if the facts change after the analysis, a banner asks to analyse again, and the ticks are kept.
Requests and Documents steps: they offer the usual requests and documents of the recognised claim types as one-click lines, plus any prerequisites (for example labour conciliation). In the Requests step, «اقترح صياغة رسمية للطلبات» asks the AI for a formal Arabic wording of the lawyer’s lines (a help for notes that slipped into Darija or French; formal Arabic remains the supported input). The proposal is shown only if code confirms it kept the number of requests, every amount and number, and every alternative («أو»), with no amount spelled out in words; it replaces the lawyer’s lines only when the lawyer accepts it.
How the draft stays grounded:
- The grounds are drafted from the ticked articles, plus the lawyer’s own arguments.
- Argued grounds, as Algerian statements of claim write them, with the law written by the code. The code plans one paragraph per part the ticked articles play in the claim (the obligation and its breach, its proof, the compensation) and opens each with the articles and their one-line rule from the catalog («حيث إن المادتين 179 و186 من القانون المدني تقضيان بأنه…»); a “limit” article such as no interest on a private loan stays a warning, not a ground. The AI writes only how the facts meet the rule. An application that cites or quotes an article, or has a number found nowhere in the notes, is asked once more, then left as «(…)» for the lawyer, which blocks export.
- When no cited text is loaded, the model writes the grounds with the ticked articles’ official texts as the only law it may cite or quote.
- Any other article in the result is flagged under the section, and so is any «…» quotation that doesn’t match the official text. Either one triggers one automatic retry.
- Nothing is scraped or searched online; the texts come from the local law library (LAW-LIBRARY.md).
⚠️ Status: the claim catalog (its claim types, articles and required flags) is not yet verified by a lawyer. An independent audit (October 2026) found errors in it and in the test expectations; corrections are under way.


Section assistant
Each narrative section (grounds, requests) has a ✦ Assistant button. It opens a panel where the lawyer asks for an edit of that section only: quick chips («أكثر رسمية», «أوجز», «صحح اللغة», «اربط كل مادة بطلبها», «لا تكرر الخاتمة») or a free instruction.
- The model’s answer streams in as it is written, then appears as a word-level diff (added in green, removed struck out) with Accept / Reject / Try again.
- Every proposal goes through the same checks as a first draft: invented or dropped numbers, unexpected Latin words, legal terms the lawyer never used, non-Arabic or foreign-script text. Their hints show above the diff. A hard failure is retried once.
- It is not a chatbot: a legal question gets a fixed reply («المساعد يعدل صياغة هذا القسم فقط»), never an answer.
- Accepting keeps the old text in the section’s history, so Undo restores it.
Regeneration never destroys edits. «صياغة الأسانيد» only fills sections that are empty or unchanged since the model wrote them. A section the lawyer edited keeps its text; its own ↻ Redraft button asks for confirmation first, and the old text can be undone.
Model status is shown by a dot next to the provider. The app asks the local Ollama (/api/tags, loopback only) at startup and every 30 s, and says plainly when Ollama is not running or the model is not installed (ollama pull gemma3:4b).

Output: preview, PDF and Word
The Preview tab shows the requête exactly as it prints: A4 pages rendered from the real PDF, right-to-left, in the bundled Noto Naskh Arabic font, with an optional diagonal «مسودة» watermark.
- Export PDF is for filing and printing.
- Export Word (.docx) produces right-to-left paragraphs set in Simplified Arabic, a font that ships with Windows, so the lawyer can keep editing in Word.
Each section’s print layout (alignment, bold, size, spacing) is data, under layout in definition.json.

PDFs are generated with QuestPDF under its Community license. It is free while the company’s annual revenue stays under USD 1M; above that a commercial license is required. Word files are generated with the MIT-licensed Open XML SDK.
Quick start
Prerequisite: .NET 10 SDK (the version is pinned in global.json).
dotnet build # warnings are errors
dotnet test # engine, drafting, providers, storage, UI (headless)
dotnet run --project src/DzLegal.Harness # Form Engine accuracy report (exit 1 on any mismatch)
dotnet run --project src/DzLegal.Harness -- grounds --models gemma3:4b # grounds analysis quality with a local model
dotnet run --project src/DzLegal.App # the desktop app
The app starts in Arabic and uses the local model by default. With no model installed, choose «دون ذكاء اصطناعي (قوالب)» / “Without AI (fill-in templates)” in the draft panel. The draft is then built from the templates plus your own notes, and everything else works the same.
Using the local model (optional, recommended)
- Install Ollama. It runs as a local process on
127.0.0.1:11434. ollama pull gemma3:4b(about 3.3 GB, one-time download). After that, no network is needed. With 16 GB RAM or a GPU,qwen2.5:7b-instructwrites better Arabic.- Optional, on Windows, recommended on 8 GB RAM:
powershell -ExecutionPolicy Bypass -File tools\model\New-TextOnlyModel.ps1. It createsdz-gemma3-4b-textfrom the installedgemma3:4b(same language weights, nothing downloaded) without the image part the app never uses: about 0.8 GB less memory, identical answers. The app uses it automatically when it is installed. - Start the app and keep the default engine, “Local model (gemma3:4b)”.
Recommended hardware: 16 GB RAM for 7B models (CPU-only works, at about a minute or more per section), or a GPU with 8 GB or more of VRAM. On 8 GB RAM without a GPU, use gemma3:4b. It was the best of the three models benchmarked on such a machine (about 4 minutes of drafting per requête on an Intel i5-1335U, plus 2–3 minutes for the grounds analysis). qwen2.5:3b-instruct is weaker and only worth using on machines too small for gemma. qwen3:4b is not supported there: it writes its English reasoning into the document and takes over 10 minutes per requête. To change a setting, don’t edit appsettings.json. Create appsettings.local.json next to it (in the repo: src/DzLegal.App/appsettings.local.json; git ignores it) containing only the keys you want to change. For example:
{ "ollama": { "model": "qwen2.5:7b-instruct" } }
Shipped defaults: defaultProvider "ollama" (or "stub" for template mode), language "ar" (or "en"), and ollama.endpoint "http://127.0.0.1:11434", ollama.model "gemma3:4b", ollama.timeoutMinutes 10.
A non-loopback endpoint is refused. Further optional keys: temperature (0.2), maxTokens (1000), repeatPenalty (1.1), and "think": false for reasoning models such as qwen3.
Gemma 4 E4B, on 16 GB of RAM. After ollama pull gemma4:e4b (about 6.6 GB, with a recent Ollama that knows Gemma 4), choose it with:
{ "ollama": { "model": "gemma4:e4b" } }
The app switches its thinking off by itself ("think": false unless the setting says otherwise): with thinking on, Gemma 4 spent the whole answer allowance thinking on every fact reading. Measured so far, on the owner’s laptop on mains power: on the second hidden test set, the fact reader (Alpha-01, in the test bench) got 58 of 90 cases fully right with gemma4:e4b against 39 with gemma3:4b, at 21 s against 28 s per case, with a model memory peak of about 7.2 GB (docs/HIDDEN-SET.md). The app’s own steps were then measured on the 46 fictitious development cases, unchanged app, one model at a time (9 Nov 2026, docs/baselines/2026-11-09-alpha-03-app-grounds-stage-1/ and …-app-draft-stage-2/):
| App step, development cases | gemma3:4b |
gemma4:e4b, thinking off |
|---|---|---|
| Analysis: claim type exact | 41 of 46 | 46 of 46 |
| Analysis: core articles found (model’s own choice) | 41 of 68 | 49 of 68 |
| Analysis: core articles found (with the articles the claim type always adds) | 48 of 68 | 51 of 68 |
| Analysis: out-of-scope cases declined | 3 of 4 | 4 of 4 |
| Analysis: time per case | 70 s | 56 s |
| Drafting (12 cases): formally complete | 11 of 12 | 11 of 12 |
| Drafting: automatic issues (wrong law, numbers, Latin words, ungrounded terms) | 0 | 0 |
| Drafting: sections retried | 1 | 0 |
| Drafting: time per case | 39 s | 25 s |
| Highest memory, model + app, either step | 9.5 GB | 8.3 GB |
Both models had no failure, time-out, empty or cut-off answer. The 24 drafted grounds are kept side by side in docs/baselines/2026-11-09-alpha-03-app-draft-stage-2/outputs/; read against the case notes, neither model swapped roles or invented a fact, and Gemma 4’s grounds were closer to the facts in seven of the twelve cases (Gemma 3 missed the decisive fact in two of them). These are development cases the app was tuned on, so both results are optimistic, and the automatic checks do not judge legal correctness. On 8 GB of RAM, keep gemma3:4b.
Speed on a CPU-only laptop (8 GB)
What to expect from a 4B model on CPU: the first request after start-up loads the model (10–40 s). The app warms it up in the background and keeps it in memory for 15 minutes after the last request (ollama.keepAlive), then Ollama frees the memory; closing the app frees it at once. After that, most of the wait is the model reading the prompt, and then it writes a few words per second.
The app keeps prompts short and caps every answer. To see where the time goes on your machine, run either bench (below): its summary has a “Where the time goes” table with load time, prompt tokens and reading speed, answer tokens and writing speed, and time to the first word.
Things that help on the laptop:
- keep it plugged in, with the “best performance” power mode;
- close heavy applications (browsers with many tabs);
- in Ollama’s environment, try
OLLAMA_FLASH_ATTENTION=1andOLLAMA_KV_CACHE_TYPE=q8_0(less memory for the same model).
Models to compare against gemma3:4b on such a machine: gemma3n:e2b and gemma3n:e4b, which are built for on-device speed. Earlier runs ruled out qwen2.5:3b-instruct (weaker) and qwen3:4b (too slow, writes English reasoning).
Comparing local models (drafting bench)
ollama pull gemma3:4b # once, online
dotnet run --project src/DzLegal.Harness -- draft --models gemma3:4b --out bench
This runs the fictitious cases in tests/drafting-cases/ through each model. For every model and case it writes the full document, the time per section, any retries or fallbacks, and the automatic issues. It also writes bench/summary.md. On a CPU-only machine, expect several minutes per case. Automatic checks cannot detect swapped roles or invented reasoning, so read the outputs too; each file ends with a checklist. Use --models stub to try it without any model. To measure the hosted model’s correctness and stability over many runs (--runs, --case, --models openrouter), see ONLINE-TESTING.md.
Packaging for a lawyer’s Windows PC
dotnet publish src/DzLegal.App -c Release -r win-x64 --self-contained -o out/win-x64
The output folder has everything it needs: runtime, data/ rules, fonts and appsettings.json. Copy it to the machine and run DzLegalAssistant.exe.
Never add -p:OnlineTesting=true here. That flag builds the temporary online testing version (ONLINE-TESTING.md), which can send case data to OpenRouter.
Demo walkthrough (for reviewers)
dotnet run --project src/DzLegal.App. On first launch: a welcome screen, then a three-step setup (the lawyer, the office, preferences). The profile fills the signature block of every new statement of claim; Later skips it. Then the start screen: a new statement of claim, open a case, resume the last session. Everything is stored on the computer only (%LOCALAPPDATA%\DzLegalAssistant).- Click English (top bar) if you don’t read Arabic. The interface switches to left-to-right, while the document stays Arabic and right-to-left. The moon icon switches between the light and dark themes.
- In the draft panel, choose the engine Without AI (fill-in templates) unless Ollama is installed; with it, keep Local model (gemma3:4b).
- Left, Case details: nine steps (court, plaintiff, defendant, claim, facts, legal grounds, requests, supporting documents, date and signature), each ✓ when complete. A missing mandatory field gets a red border and the article it comes from, for example «مطلوب — CPCA art. 15-2».
- Legal grounds step: Analyse the facts proposes a claim type and articles, each with the model’s reason and the official text. Nothing is ticked until you confirm the claim type (Yes, this is the claim type); the Requests and Supporting documents steps then offer the usual lines for it, one click each.
- Centre, the statement of claim: Draft legal grounds (Ctrl+G) writes the grounds from the ticked articles; the code writes each rule and its article, the AI only how the facts meet it. Every AI-written section carries «راجعت هذا القسم وأتحمل مضمونه», and PDF, Word and copy stay blocked, with the reasons listed, until each one is ticked. Edit by hand takes a section over and Restore from form gives it back; Preview shows the exact A4 pages, with or without the «مسودة» draft mark.
- Right, Formal check: «9 من 9» counts the mandatory elements of form only, then the legal points to check. Click a missing element to jump to the step that fixes it.
- Work is saved automatically and on close; the next start offers to restore it. Keyboard: Ctrl+N new, Ctrl+O open, Ctrl+S save, Ctrl+G draft, Ctrl+P export PDF.




Repository layout
src/
DzLegal.Domain/ models + validating JSON loader (no dependencies)
DzLegal.FormEngine/ deterministic compliance checker (no AI, no network)
DzLegal.Drafting/ template rendering, grounded prompts, IModelProvider
DzLegal.ModelProviders.Stub/ template mode (no model)
DzLegal.ModelProviders.Ollama/ local model over loopback only
DzLegal.ModelProviders.Hosted/ hosted models, for the web demo and the online testing build only
DzLegal.Storage/ local JSON drafts, profile, preferences, recent cases, evaluation records
DzLegal.Documents/ print layout → PDF (QuestPDF) and Word (Open XML), preview images
DzLegal.Law/ offline law library (official texts), full-text index, claim catalog, official-text parser
DzLegal.Harness/ Form Engine accuracy report, drafting bench, `law import` maintainer tool
DzLegal.Presentation/ screen logic shared by the desktop and the web demo (view models, Arabic/English strings)
DzLegal.App/ Avalonia desktop app: welcome, setup, start screen, workbench
DzLegal.Web/ Blazor Server web demo (the public live demo)
data/law/ official texts to import (sources.json), imported codes, import reports — see docs/LAW-LIBRARY.md
data/document-types/requete-introductive/
definition.json fields, sections, required elements (+ articles)
templates/*.scriban deterministic sections
prompts/*.ar.txt grounded prompts for narrative sections
tests/
corpus/requete-introductive/ documents with known-correct outcomes
DzLegal.*.Tests/ unit, integration and headless UI tests
tools/law/ import and check the official law texts
docs/ architecture, offline guarantees, rule updates, legal verification, briefs
Further documentation
- ARCHITECTURE.md: modules, data flow, design decisions.
- DEVELOPER-GUIDE.md: for working on the code: which file does what, where every prompt lives, the runtime flows and recipes.
- UPDATING-LEGAL-RULES.md: how to change rules, templates and prompts, add corpus cases, add a document type.
- OFFLINE.md: what “offline” means here and how to verify it.
- LEGAL-VERIFICATION.md: the checklist for the practicing lawyer.