Khoa Nguyen

All rights reserved © yunkhngn · v1.6.6

Journal

Engineering · 2026-11-16 · 17 min read

Engineering Lawise: Building an AI-Powered LegalTech Platform

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.

Engineering Lawise: Building an AI-Powered LegalTech Platform

On this page

  1. 01. Introduction
  2. 02. Understanding the Product
  3. The Core Product Experience
  4. 03. System Architecture
  5. High-Level Architecture
  6. Why Polyglot Microservices?
  7. 04. Building the Data Layer: PostgreSQL and pgvector
  8. From Text to Vector Representations
  9. Why pgvector?
  10. 05. AI Engineering: Integrating OpenAI
  11. Retrieval-Augmented Generation
  12. The Importance of Context
  13. 06. AI Contract Review: From Documents to Actionable Insights
  14. The Review Workflow
  15. Risk Identification
  16. Why Structured Outputs Matter
  17. 07. Frontend Engineering: Turning AI Capabilities into a Product
  18. 08. Engineering Challenges and Trade-offs
  19. 8.1. AI Reliability vs. Model Flexibility
  20. 8.2. Microservices vs. Operational Complexity
  21. 8.3. Semantic Retrieval vs. Legal Precision
  22. 8.4. AI Cost and Latency
  23. 09. Technology Stack
  24. 10. Lessons Learned
  25. 11. Conclusion
On this page
  1. 01. Introduction
  2. 02. Understanding the Product
  3. The Core Product Experience
  4. 03. System Architecture
  5. High-Level Architecture
  6. Why Polyglot Microservices?
  7. 04. Building the Data Layer: PostgreSQL and pgvector
  8. From Text to Vector Representations
  9. Why pgvector?
  10. 05. AI Engineering: Integrating OpenAI
  11. Retrieval-Augmented Generation
  12. The Importance of Context
  13. 06. AI Contract Review: From Documents to Actionable Insights
  14. The Review Workflow
  15. Risk Identification
  16. Why Structured Outputs Matter
  17. 07. Frontend Engineering: Turning AI Capabilities into a Product
  18. 08. Engineering Challenges and Trade-offs
  19. 8.1. AI Reliability vs. Model Flexibility
  20. 8.2. Microservices vs. Operational Complexity
  21. 8.3. Semantic Retrieval vs. Legal Precision
  22. 8.4. AI Cost and Latency
  23. 09. Technology Stack
  24. 10. Lessons Learned
  25. 11. Conclusion

01. Introduction

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.

02. Understanding the Product

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 Core Product Experience

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.

03. System Architecture

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

High-level architecture illustrating the major technology layers.

Flowchart
Loading diagram…

Conceptual architecture diagram. Individual service boundaries and communication paths should be aligned with the deployed implementation.

Why Polyglot Microservices?

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.

04. Building the Data Layer: PostgreSQL and pgvector

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.

From Text to Vector Representations

The general vector retrieval process consists of several steps:

  1. Extract textual content from legal documents.

  2. Normalize and prepare the text.

  3. Divide content into meaningful segments.

  4. Generate vector embeddings.

  5. Store embeddings alongside relevant document metadata.

  6. 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 Retrieval

Why pgvector?

Choosing 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.

05. AI Engineering: Integrating OpenAI

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.

Retrieval-Augmented Generation

A retrieval-oriented architecture can help ground model responses in relevant legal information.

A typical RAG workflow looks like this:

Flowchart
Loading diagram…

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.

The Importance of Context

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.

06. AI Contract Review: From Documents to Actionable Insights

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.

The Review Workflow

A conceptual contract review pipeline can be represented as follows:

Flowchart
Loading diagram…

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.

Risk Identification

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.

Why Structured Outputs Matter

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.

07. Frontend Engineering: Turning AI Capabilities into a Product

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.

08. Engineering Challenges and Trade-offs

Building Lawise involves several architectural and technical trade-offs.

8.1. AI Reliability vs. Model Flexibility

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.

8.2. Microservices vs. Operational Complexity

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.

8.3. Semantic Retrieval vs. Legal Precision

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.

8.4. AI Cost and Latency

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.

09. Technology Stack

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.

10. Lessons Learned

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.

11. Conclusion

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

From the journal

2026-11-22

Building a Vector Search and Embeddings System with MongoDB

Explore how to integrate embeddings and Vector Search features directly into MongoDB to build intelligent semantic search applications.

2026-11-15

AI Kitchen

Explore the building process of 'AI Kitchen' - a space where I turn raw ideas into complete products by optimizing my AI workflow.

2026-11-15

Beautiful on the Surface, Engineered Underneath - My Legacy Website

Building a Personal Website into a Microservice Platform. An engineering retrospective on building a design-driven personal website backed by a microservice architecture, computer vision experiments, automated Docker builds, and production infrastructure."

Related work

2025

Yunkhngn Microservice Platform

A sophisticated microservice ecosystem powering a personal portfolio and interactive digital experiences, utilizing AI-driven image processing and a robust, containerized architecture.

2026

Dinner AI

An intelligent culinary web application powered by Google Gemini AI that automatically generates tailored dinner menus, recipes, and consolidated shopping lists.