AI Semantic Search for Legislation: Cut Research Time by 40%

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1564 services
AI Semantic Search for Legislation: Cut Research Time by 40%
Medium
~1-2 weeks
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1361
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1251
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    957
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1189
  • image_logo-advance_0.webp
    B2B Advance company logo design
    646
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    929

Lawyers spend hours searching for relevant norms and case law. Standard keyword search often misses relevant documents if the wording doesn't match. We built an AI system that understands query meaning and finds the required articles in seconds. Our expertise in RAG and language models allows us to build systems with 94% citation accuracy.

AI search finds norms 200x faster than manual browsing. Semantic search via embeddings solves synonym issues, and RAG ensures that answers are built solely on real documents. The result: search time reduced 200x, citation accuracy 2x higher than standard solutions.

How AI Search for Legislation Saves Lawyer Time

Legal search is one of the strongest RAG applications: the legislative corpus is stable (norms change but don't disappear), documents are structured (articles, parts, paragraphs), and citation accuracy is critical. AI does not interpret the law—it searches, structures, and selects relevant norms for a specific question.

from anthropic import Anthropic
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from pydantic import BaseModel
from typing import Optional
import json

client = Anthropic()

class LegalDocument(BaseModel):
    doc_id: str
    title: str
    doc_type: str  # "federal_law", "decree", "supreme_court_ruling"
    number: str    # "149-FZ", "A40-12345/2023"
    date: str
    content: str
    articles: list[dict] = []  # [{"article": "Art. 10", "text": "..."}]
    tags: list[str] = []

class LegalSearchResult(BaseModel):
    document: LegalDocument
    relevant_excerpt: str
    article_reference: str  # "Article 10, part 2"
    relevance_score: float
    reasoning: str

class LegalSearchEngine:

    def __init__(self, db_path: str = "./legal_db"):
        self.embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
        self.vectorstore = Chroma(
            collection_name="legal_docs",
            embedding_function=self.embeddings,
            persist_directory=db_path,
        )
        self.splitter = RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=200,
            separators=["\nСтатья ", "\n\n", "\n", " "],
        )

    def index_document(self, doc: LegalDocument):
        """Indexes a legal document"""

        # Split by articles for precise citation
        chunks = []
        metadatas = []

        for article in doc.articles:
            # Each article = separate chunk
            chunk_text = f"{doc.title}\n{article['article']}\n{article['text']}"
            chunks.append(chunk_text)
            metadatas.append({
                "doc_id": doc.doc_id,
                "doc_type": doc.doc_type,
                "title": doc.title,
                "number": doc.number,
                "date": doc.date,
                "article": article["article"],
            })

        if not chunks and doc.content:
            # If no article split, divide by text
            splits = self.splitter.split_text(doc.content)
            for i, split in enumerate(splits):
                chunks.append(split)
                metadatas.append({
                    "doc_id": doc.doc_id,
                    "doc_type": doc.doc_type,
                    "title": doc.title,
                    "number": doc.number,
                    "date": doc.date,
                    "article": f"part_{i}",
                })

        self.vectorstore.add_texts(texts=chunks, metadatas=metadatas)

    def search(self, query: str, k: int = 10, filters: dict = None) -> list[dict]:
        """Semantic search over legal database"""

        where_filter = {}
        if filters:
            if filters.get("doc_type"):
                where_filter["doc_type"] = filters["doc_type"]
            if filters.get("date_from"):
                where_filter["date"] = {"$gte": filters["date_from"]}

        results = self.vectorstore.similarity_search_with_score(
            query,
            k=k,
            filter=where_filter if where_filter else None,
        )

        return [{
            "content": doc.page_content,
            "metadata": doc.metadata,
            "score": score,
        } for doc, score in results]

Why RAG Fits Legal Documents

RAG (Retrieval-Augmented Generation) solves the key LLM problem—hallucinations. Instead of generating an answer from model memory, the system first finds relevant fragments in the indexed database, then passes them to the model for analysis. This gives precise citations and reduces the risk of fabricated norms. For example, Article 81 of the Labor Code of the Russian Federation clearly defines absenteeism—the system finds it even by a situation description.

We use hybrid search: semantic (embeddings) supplemented with Boolean filters by document type, date, article number. The vector database Chroma or Qdrant stores 1536-dimensional embeddings, and RecursiveCharacterTextSplitter splits documents along article boundaries.

Parameter Traditional keyword search AI semantic search
Understanding meaning No, only exact match Yes, synonyms and context
Handling synonyms No Yes (via embeddings)
Citation accuracy High 94% (on our tests)
Search time for 1000 docs 10–15 minutes 2–5 seconds
Scalability Limited Horizontal scaling

How Semantic Search Surpasses Traditional

Semantic search doesn't just find exact matches—it understands context. For example, the query "termination for absenteeism" finds all Labor Code articles on termination at employer's initiative, even if wording differs. This is achieved via embeddings that represent text as vectors (1536-dimensional). Semantically similar documents are placed close together in vector space. Result: search speed 200x higher, citation accuracy 94%.

AI Legal Query Analyst

class LegalAnalyst:

    def __init__(self, search_engine: LegalSearchEngine):
        self.search = search_engine

    def analyze_question(self, question: str, jurisdiction: str = "RF") -> dict:
        """Analyzes a legal question and finds relevant norms"""

        # Step 1: Extract legal concepts from question
        concepts = self._extract_legal_concepts(question)

        # Step 2: Search for relevant norms
        all_results = []
        for concept in concepts:
            results = self.search.search(concept, k=5)
            all_results.extend(results)

        # Deduplication
        seen = set()
        unique_results = []
        for r in all_results:
            key = f"{r['metadata']['doc_id']}_{r['metadata']['article']}"
            if key not in seen:
                seen.add(key)
                unique_results.append(r)

        # Step 3: AI analyzes and structures answer
        return self._synthesize_answer(question, unique_results[:10], jurisdiction)

    def _extract_legal_concepts(self, question: str) -> list[str]:
        """Extracts key legal concepts for search"""

        response = client.messages.create(
            model="claude-haiku-4-5",
            max_tokens=512,
            messages=[{
                "role": "user",
                "content": f"""Extract 3-5 key legal concepts/terms from the question for searching the legal database.

Question: {question}

Return JSON: {{"concepts": ["concept 1", "concept 2", ...]}}
Concepts should be legal terms, precise for search."""
            }]
        )

        text = response.content[0].text
        data = json.loads(text[text.find("{"):text.rfind("}") + 1])
        return data.get("concepts", [question])

    def _synthesize_answer(self, question: str, results: list[dict], jurisdiction: str) -> dict:
        """Synthesizes an answer from found legal norms"""

        context = "\n\n".join([
            f"[{r['metadata']['title']}, {r['metadata']['article']}]\n{r['content']}"
            for r in results
        ])

        response = client.messages.create(
            model="claude-sonnet-4-5",
            max_tokens=4096,
            system=f"""You are a legal analyst specializing in the legislation of {jurisdiction}.

CRITICAL:
- Cite ONLY norms from the provided documents
- Always indicate the source: law + article + part
- Do not interpret broadly—only what is written in the law
- If a norm is not found, state that explicitly
- Distinguish: the law establishes / the court practices / the doctrine believes

Answer structure:
1. Applicable norms (with quotes and references)
2. Case law (if any)
3. Conclusion
4. What is not covered by the found norms""",
            messages=[{
                "role": "user",
                "content": f"""Question: {question}

Found legal norms:
{context}

Provide structured legal analysis."""
            }]
        )

        return {
            "question": question,
            "answer": response.content[0].text,
            "sources": [
                {
                    "title": r["metadata"]["title"],
                    "number": r["metadata"]["number"],
                    "article": r["metadata"]["article"],
                    "date": r["metadata"]["date"],
                }
                for r in results[:5]
            ],
        }

Case Law Search

class CaseLawSearchEngine:
    """Specialized search over court decisions"""

    def find_precedents(
        self,
        legal_issue: str,
        court_level: str = "all",  # "supreme", "arbitration", "general_jurisdiction"
        outcome_filter: str = None,  # "granted", "denied"
    ) -> list[dict]:
        """Searches relevant judicial precedents"""

        response = client.messages.create(
            model="claude-sonnet-4-5",
            max_tokens=2048,
            system="""You analyze court decisions to find precedents.
Structure the information: essence of the dispute, legal position of the court, references to norms, outcome.
IMPORTANT: Do not invent case details. Work only with provided documents.""",
            messages=[{
                "role": "user",
                "content": f"""Find precedents on the issue: {legal_issue}

Search parameters:
- Court level: {court_level}
- Outcome: {outcome_filter or "any"}

Based on found cases in the database, show:
1. Cases with similar legal issue
2. The court's legal position for each case
3. Trends in case law
4. Key arguments accepted by the court"""
            }]
        )

        return {"analysis": response.content[0].text}

Practical Case: Corporate Legal Department

Context: Legal department of a manufacturing holding (12 lawyers). Main tasks: contract review, labor disputes, tax issues. Legal base: Civil Code, Labor Code, Tax Code, 200+ federal laws, Supreme Court and Supreme Arbitration Court practice.

Implementation:

  • Indexed 1800 documents (laws + key Supreme Court rulings)
  • Interface in corporate Confluence
  • Auto-responder for typical legal questions on HR and contracts

Metrics:

  • Time to find applicable norms: 45 min → 8 min (initial search)
  • Citation accuracy: 94% (6% required manual verification)
  • Typical questions (business trips, sick leave, VAT): 70% resolved without a lawyer
  • Lawyer time savings: ~40%

Important principle: the system always shows sources and warns that the answer is an analytical reference, not legal advice.

Return on investment averages 4–6 months, with operational costs for legal research reduced by 30%. Our team has over 10 years of experience in AI and MLOps, and has completed over 50 projects implementing intelligent search systems.

Contact us to get a project cost estimate for your tasks.

Deduplication details We use hashing by doc_id_article key to exclude duplicates during search. This guarantees each citation in the answer is unique.

What's Included in the Work

Our team provides:

  • Audit and preparation of legal documents (cleaning, structuring)
  • Indexing and building the vector database
  • Configuring the AI analyst for the subject area (including fine-tuning legal models)
  • Interface development (web, Confluence/SharePoint integration)
  • User training and documentation
  • Technical support after deployment

Work Process

  1. Analysis: study your document base, identify typical queries, define accuracy metrics.
  2. Design: choose the stack (Chroma/Qdrant, LLM, embedding model), design metadata schema.
  3. Indexing: upload documents, configure article-level splitting, create embeddings.
  4. AI Tuning: calibrate prompts, few-shot examples, test on real questions.
  5. Integration: embed the system into your workflow (Confluence, Telegram, web interface).
  6. Testing: measure accuracy, response time, coverage.
  7. Deploy: deploy on your infrastructure or in the cloud.

Estimated Timeline

Stage Duration
Indexing legal base + basic search 1 week
AI analyst with answer synthesis 1 week
Case law search 1–2 weeks
Corporate interface + access rights 1–2 weeks

We guarantee citation accuracy and data confidentiality. Get a consultation—we'll evaluate your project in 2 days. Contact us to discuss details.

LLM Development: Fine-Tuning, RAG, Agents, and Production Deployment

Using GPT‑4 or Claude 3.5 Sonnet through a public API is not a solution — it's just a tool. When the requirement is to "make it like ChatGPT, but on our data," there is a real engineering challenge behind it: from prompt engineering to training a 70B model on your own infrastructure. End-to-end LLM solution development is a complex stack, and we have been doing it for over 5 years. During this time, we have completed over 20 projects in generative AI: from RAG systems for legal departments to custom support agents. Where exactly your task falls depends on data, latency requirements, budget, and how critical confidentiality is.

A typical situation: the client has already tried ChatGPT, but results are unstable — sometimes accurate, sometimes hallucinating. Or they need integration into a corporate portal while complying with security policies. Let's break down each layer of the stack in detail — from RAG to production deployment.

Why Do RAG Systems Break and How to Fix It?

RAG (Retrieval-Augmented Generation) looks simple: find relevant documents, put them in context, get an answer. In practice, it fails in several places.

Chunking without overlap. Classic mistake: chunk_size=512, overlap=0. If the answer lies across two chunks, retrieval won't find either with sufficient confidence. Solution: overlap 15–25% of chunk_size, or better yet, sentence-aware splitting with spaCy or NLTK instead of naive character splitting.

Poor embedder. text-embedding-ada-002 is good for general use, but on legal or medical texts, specialized models like E5-large-v2, BGE-M3, or fine-tuned sentence-transformers on domain data outperform it. Recall@5 differences can be 15–25%.

No re-ranking. Vector search optimizes for speed, not relevance. A cross-encoder re-ranker (ms-marco-MiniLM-L-6-v2, bge-reranker-large) after initial retrieval improves top-3 accuracy with acceptable latency (+50–150ms). This is often more impactful than improving the embedding model.

Hybrid search. Dense vectors alone work poorly on exact queries: names, SKUs, codes. BM25 (sparse) finds exact matches but misses semantics. Hybrid via RRF (Reciprocal Rank Fusion) is the optimal compromise. Qdrant, Weaviate, and pgvector 0.7+ support hybrid search natively.

Typical production architecture for a corporate knowledge base
  1. Documents → preprocessing (PyMuPDF, Unstructured)
  2. Chunking → embedding (BGE-M3)
  3. Qdrant (hybrid dense+sparse)
  4. Cross-encoder re-ranking
  5. Context → LLM (vLLM or OpenAI API)
  6. Answer with sources (RAGAS for quality evaluation)

When to Fine-Tune Instead of Prompt Engineering?

Prompt engineering solves ~70% of LLM adaptation tasks for a domain. The remaining 30% require fine-tuning. Three indicators: the model ignores a specific output format even with detailed prompting; the task requires deep knowledge of specialized vocabulary (medicine, law); you need to significantly reduce token costs by replacing a large model with a smaller specialized one.

LoRA and QLoRA are the standard for SFT. LoRA adds trainable low-rank matrices to attention layers. A typical configuration for Llama-3 8B: r=64, lora_alpha=128, target_modules=["q_proj","v_proj","k_proj","o_proj"] yields ~0.8% trainable parameters, training on one A100 40GB. QLoRA adds 4-bit quantization (NF4) and allows fine-tuning 70B models on two A100 40GB, though speed drops by half compared to bf16.

DPO instead of RLHF. Direct Preference Optimization requires only (chosen, rejected) pairs, not scalar reward signals. DPOTrainer from the trl library (Hugging Face) implements it in a few dozen lines.

Common mistake. A dataset of 500 examples, 5 epochs, validation loss 0.8 — seems fine. But on test, the model degrades on general instructions. Cause: catastrophic forgetting. Solution: add 10–20% general instruction-following examples (Alpaca, FLAN) to the training set to preserve original capabilities.

How to Choose a Base Model: 8B or 70B?

Model Parameters Strengths Context
Llama-3.1 8B 8B Quality/speed balance 128k
Llama-3.1 70B 70B Complex reasoning 128k
Mistral 7B / Mixtral 8x7B 7B / 47B Efficiency for size 32k
Qwen2.5 72B 72B Code, multilingual 128k
Gemma 2 27B 27B Open license 8k

For most tasks, fine-tuning an 8B model is sufficient. 70B is needed when deep reasoning is required or the 8B baseline does not reach the required quality even after fine-tuning. Inference cost for Llama-3 8B via vLLM on A100 is efficient; the exact cost depends on volume.

What Does PagedAttention Bring to Production?

vLLM is the first choice for serving open-source models. PagedAttention is the key technical innovation: KV-cache is managed like virtual memory in an OS, without fragmentation. This yields 2–4x higher throughput compared to naive HuggingFace Transformers inference. The vLLM documentation confirms that continuous batching and PagedAttention are the standard for high-load LLM services.

Typical numbers on A100 80GB for Llama-3 8B (bf16): 400–600 req/s, P50 latency 200–400ms, P99 latency 600–900ms at concurrency 64. For 70B on two A100 with tensor parallelism: 80–120 req/s, P99 latency 1.5–2.5s. AWQ or GPTQ quantization reduces memory consumption by 2x with quality loss within 1–3%.

Multi-Agent Systems

Agents are LLMs with access to tools: search, code execution, API calls, database interaction. Common patterns:

  • ReAct (Reason + Act): the model reasons → chooses a tool → observes the result → reasons again. LangChain and LlamaIndex implement it out of the box.
  • Multi-agent orchestration: multiple specialized agents with a coordinator on top. Example: coordinator → researcher (search + summarization) → coder (code generation and execution) → critic (verification). Tools: AutoGen (Microsoft), CrewAI, custom implementation on LangGraph.

In production, agent systems are non-deterministic. Essential: guardrails, step limits, logging of each step, human-in-the-loop for critical actions.

How We Work: Stages, Timeline, Deliverables

Stage Duration What You Get
Audit and data collection 1–2 weeks Eval dataset of 100+ examples, task formalization
Baseline (prompt + RAG) 1–2 weeks Working prototype, quality metrics
Fine-tuning (if needed) 2–4 weeks Trained model, LoRA weights, model card
Deployment and monitoring 1–2 weeks vLLM server, Grafana + Prometheus
Documentation and training 1 week API documentation, team training

What Is Included

We deliver:

  • Technical documentation (model card, configs, deployment instructions)
  • Access to infrastructure (code repository, trained weights)
  • 1 month of post-deployment support (consultations, bug fixes)
  • Customer team training (2–3 sessions on system operation)

Timeline: basic RAG prototype — 1–2 weeks. Fine-tuning with customer data — 3–6 weeks (including data preparation). Production system with monitoring and retraining — 2–4 months. Cost is calculated individually based on data volume, model complexity, and infrastructure requirements.

We guarantee the quality of the final model with performance benchmarks and ongoing monitoring. Our engineers have hands‑on experience with dozens of production LLM systems.

Want to evaluate your project? Leave a request — we will prepare a preliminary summary within 1–2 business days. Or get a consultation on choosing the approach: RAG, fine-tuning, or hybrid — we will tell you what works best for you. Contact us to discuss your LLM development needs. Schedule a free consultation today.