Engineering · 2026-11-16 · 17 min read
From legal documents to actionable intelligence — exploring the engineering behind an AI-powered legal platform built with Next.js, polyglot microservices, PostgreSQL, pgvector, and OpenAI.

Building an AI-powered product is not simply about connecting an application to an LLM API.
The real engineering challenge lies in transforming complex domain knowledge into reliable, accessible, and actionable intelligence.
Lawise is a LegalTech platform designed to make legal knowledge more accessible and simplify contract-related workflows through artificial intelligence.
The platform focuses on three primary capabilities:
AI Contract Review: Analyzing contracts, identifying potentially risky clauses, explaining legal implications, and suggesting improvements.
AI Legal Assistant: Helping users navigate Vietnamese legal information through natural-language interactions and legal references.
AI Contract Generation: Assisting users in creating structured contracts based on their requirements.
The project explores an interesting intersection of software engineering, artificial intelligence, information retrieval, and domain-specific product design.
Rather than approaching Lawise as another AI chatbot, I approached it as an engineering challenge: building a system that connects legal knowledge, intelligent processing, and a practical user experience.
This article explores the architecture, technical decisions, and engineering considerations behind building Lawise.
Before discussing the architecture, it is important to understand the problems Lawise is designed to solve.
Legal documents are inherently complex. Contracts often contain lengthy provisions, interconnected clauses, formal terminology, and legal references that require contextual understanding.
For individuals and small businesses, reviewing these documents can become a time-consuming process.
Some common challenges include:
Identifying potentially unfavorable contractual terms.
Understanding legal terminology and implications.
Finding relevant legal provisions.
Comparing contractual clauses against applicable regulations.
Drafting and revising documents without introducing inconsistencies.
Lawise aims to reduce this friction by combining AI capabilities with legal-domain workflows.
The distinction between these capabilities is important.
A legal assistant primarily helps users explore and understand legal information. Contract review, on the other hand, requires working with an existing document, identifying specific clauses, and presenting actionable findings.
These are different problems and require different processing workflows.
One of the most important decisions in building Lawise was adopting a distributed backend architecture.
The platform uses:
Next.js for the frontend.
Polyglot Microservices for backend services implemented in multiple programming languages.
PostgreSQL as the primary relational database.
pgvector for vector storage and similarity search.
OpenAI for language-model capabilities.
The architecture separates the presentation layer, application services, data persistence, and AI integration.
High-level architecture illustrating the major technology layers.
Conceptual architecture diagram. Individual service boundaries and communication paths should be aligned with the deployed implementation.
Instead of concentrating all backend responsibilities into a single application, Lawise adopts a multi-language microservices approach.
This architectural direction provides flexibility when different backend capabilities have different technical requirements.
It also encourages clearer separation of responsibilities.
However, microservices introduce additional engineering complexity.
Each service needs well-defined interfaces, communication contracts, error-handling strategies, and clear ownership of its responsibilities.
The important lesson is that service separation should solve an actual architectural problem rather than introduce unnecessary infrastructure.
AI-powered legal applications need to work with different types of information.
Some information is structured:
User accounts.
Contracts and document metadata.
Application records.
Business-related entities.
Other information is semantic:
Legal provisions.
Regulatory content.
Contextual relationships between legal concepts.
Textual information used during AI-assisted retrieval.
PostgreSQL provides the relational foundation, while pgvector extends that foundation with vector storage and similarity search.
The general vector retrieval process consists of several steps:
Extract textual content from legal documents.
Normalize and prepare the text.
Divide content into meaningful segments.
Generate vector embeddings.
Store embeddings alongside relevant document metadata.
Retrieve semantically similar content when processing a query.
A simplified representation:
Legal Documents
|
v
Text Extraction
|
v
Text Processing
|
v
Document Chunking
|
v
Embedding Generation
|
v
PostgreSQL + pgvector
|
v
Semantic RetrievalChoosing pgvector allows vector representations to coexist within the PostgreSQL ecosystem.
This provides several architectural advantages:
Reduced infrastructure fragmentation.
Relational data and vector data can be managed together.
SQL-based data management.
Support for vector similarity operations.
A unified database environment.
However, vector similarity alone does not guarantee legal relevance.
Two passages may be semantically similar while having different legal meanings or applicability.
For legal applications, retrieval quality must be evaluated in the context of the actual legal question.
The AI layer is one of the most interesting parts of Lawise.
Large language models provide powerful language-understanding and generation capabilities, but their outputs are not inherently reliable.
This is especially important in legal applications.
A fluent answer is not necessarily a correct answer. A convincing explanation does not automatically establish legal validity.
Therefore, the engineering challenge is not simply generating responses. It is building workflows that provide useful information while preserving context and making limitations clear.
A retrieval-oriented architecture can help ground model responses in relevant legal information.
A typical RAG workflow looks like this:
The general idea is to retrieve relevant information before asking the language model to generate a response.
This changes the role of the LLM.
Instead of relying exclusively on its internal knowledge, the model can work with additional contextual information supplied by the application.
For a legal assistant, this approach is particularly valuable because users often need explanations grounded in specific legal provisions.
Legal interpretation depends heavily on context.
A clause cannot always be evaluated independently from the rest of a contract or the applicable legal framework.
This creates several engineering considerations:
Retrieval relevance.
Context selection.
Token limitations.
Prompt construction.
Source traceability.
Handling incomplete or conflicting information.
The objective is not to make the model appear more confident.
It is to make its responses more useful, understandable, and appropriately grounded.
Contract review is a particularly interesting workflow because it requires more than natural-language generation.
The system needs to process document content, understand individual provisions, identify potential issues, and communicate findings in a format users can act upon.
A conceptual contract review pipeline can be represented as follows:
The workflow highlights an important distinction between analyzing a document and generating a response.
A useful contract review experience should make findings understandable and actionable.
Rather than returning an unrestricted block of text, a contract review interface benefits from presenting findings in a structured manner.
For example:
The relevant contractual provision.
The identified concern.
An explanation of why it may be problematic.
Relevant legal context.
A suggested improvement.
This structure helps users understand not only what the system has identified, but also why the finding matters.
It also makes the output easier to review and evaluate.
AI-generated content is difficult to integrate into complex document workflows when everything is returned as plain text.
Structured outputs create a more predictable interface between AI processing and application logic.
For example, a conceptual representation of a review finding could look like this:
{
"clause": "Penalty for breach of contract",
"risk_level": "high",
"issue": "Potentially unfavorable penalty provision",
"explanation": "Requires further legal evaluation.",
"recommendation": "Review the penalty provision against applicable regulations."
}This is an illustrative data structure, not a representation of the production API schema.
The broader principle is that AI should produce information that the application can organize, display, and process consistently.
An AI system can be technically sophisticated and still fail as a product if users cannot understand or interact with its outputs.
This is particularly relevant to LegalTech.
The target audience may include individuals and business owners who do not have extensive legal knowledge.
The frontend therefore plays an important role in translating technical capabilities into understandable workflows.
Using Next.js provides the foundation for building the web application and organizing the user-facing experience.
Some important product considerations include:
Making complex legal information easier to navigate.
Presenting AI findings in a structured format.
Distinguishing risk levels visually.
Keeping contract-related workflows understandable.
Providing clear feedback during AI processing.
Making generated suggestions easy to review.
The goal is to make the technology feel approachable without oversimplifying the underlying legal complexity.
Building Lawise involves several architectural and technical trade-offs.
LLMs are flexible, but legal workflows require consistency.
A system must account for incomplete context, unsupported conclusions, and potentially misleading generated content.
The application should communicate uncertainty appropriately and avoid presenting AI-generated information as definitive legal advice.
Polyglot microservices provide flexibility and separation of responsibilities.
However, they also introduce:
More deployment units.
Distributed debugging.
Network communication overhead.
Service availability dependencies.
More complex observability.
The architectural benefits must justify the additional operational cost.
Vector search is useful for discovering semantically related content.
However, legal correctness depends on more than textual similarity.
The challenge is to ensure that retrieved information is relevant to the actual legal context, not simply close in embedding space.
AI-powered workflows can involve multiple processing stages.
Document analysis and retrieval-heavy workflows can increase token consumption and response latency.
Some important optimization directions include:
Avoiding unnecessary model calls.
Retrieving only relevant context.
Controlling prompt size.
Selecting models based on task complexity.
Reusing suitable intermediate results.
These considerations become increasingly important as an AI application moves beyond experimentation.
LayerTechnologyPurposeFrontendNext.jsWeb application and user experienceBackendPolyglot MicroservicesDistributed application servicesPrimary DatabasePostgreSQLRelational data persistenceVector SearchpgvectorVector storage and similarity searchAI ProviderOpenAILanguage-model capabilities
The technology choices reflect an architectural approach that separates application responsibilities while keeping the data and AI layers integrated into the overall product.
Working on Lawise reinforced several important lessons about building AI-powered software.
AI is only one component of an AI product.
The quality of the overall system depends on much more than the model itself. Data organization, retrieval, backend architecture, and user experience all contribute to the final result.
Architecture should reflect the problem domain.
Contract review and legal assistance are related capabilities, but they solve different problems. Treating their workflows as distinct concerns creates opportunities for clearer system design.
Vector search is not a replacement for domain understanding.
Similarity search helps locate potentially relevant information, but relevance is not equivalent to legal correctness.
Microservices introduce both flexibility and responsibility.
Distributed architecture creates opportunities for independent development, but it also requires discipline around interfaces, observability, and service boundaries.
A useful AI product needs to be designed around its users.
The most impressive model output is not necessarily the most useful one. What matters is whether users can understand the result, evaluate it, and take appropriate action.
Lawise represents my exploration of building software at the intersection of artificial intelligence, information retrieval, and domain-specific applications.
The project challenged me to think beyond implementing individual features and consider how different engineering layers work together to deliver a coherent product.
From a Next.js frontend and polyglot microservices backend to PostgreSQL, pgvector, and OpenAI integration, the architecture reflects an attempt to balance flexibility, maintainability, and practical AI capabilities.
More importantly, it reinforced a principle I find increasingly important in software engineering:
Building an AI-powered product is not about making an application that can talk. It is about engineering a system that can turn information into something genuinely useful.
Project: Lawise
Website: https://lawisevn.com/
Domain: LegalTech · AI Engineering · Backend Architecture · Information Retrieval
Core Technologies: Next.js · Polyglot Microservices · PostgreSQL · pgvector · OpenAI
Lawise — AI-powered legal assistance and contract review for the Vietnamese market.
Keep exploring