Thursday, 28 August 2025

Efficient Prompting with Text Segmentation in LangChain4j

When working with Large Language Models (LLMs), feeding the entire document or knowledge base into the prompt is not always practical or even possible. That's where text segmentation comes in. LangChain4j offers a powerful abstraction to break down large documents into smaller, manageable chunks using the TextSegment class.

Understanding how and why to segment your documents is important for building high-performance, cost-effective LLM applications, especially for search, retrieval-augmented generation (RAG), or document question-answering use cases.

 

1. What is a TextSegment in LangChain4j?

In LangChain4j, once you’ve loaded your documents using tools like FileSystemDocumentLoader, you’ll typically want to split these documents into smaller parts, known as text segments.

 

LangChain4j provides an API called TextSegment to represent these chunks. A TextSegment encapsulates only textual content (not images or rich media) and is designed to be passed to LLMs more efficiently.

 

TextSegmentDemo.java

package com.sample.app.textsegment;

import java.util.HashMap;
import java.util.Map;

import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.segment.TextSegment;

public class TextSegmentDemo {

	public static void main(String[] args) {
		Map<String, Object> metadata = new HashMap<>();
		metadata.put("title", "Demo of TextSegment");

		TextSegment segment = TextSegment.from("This is a chunk of my document", Metadata.from(metadata));

		System.out.println(segment.text());
		System.out.println(segment.metadata());

	}

}

Output

This is a chunk of my document
Metadata { metadata = {title=Demo of TextSegment} }

Why Segment Documents?

Here are some practical reasons why segmenting documents is essential.

 

·      LLM Context Window Limitations: LLMs can only process a limited number of tokens at once (e.g., 8K, 32K, 128K). Splitting allows you to fit relevant segments within this constraint.

·      Speed and Efficiency: A smaller prompt means faster processing. Why send 50 pages when 3 lines are enough?

·      Cost Control: Most LLM providers charge based on tokens. Segmenting allows you to reduce cost by sending only what’s necessary.

·      Improved Relevance: By filtering out irrelevant segments, you reduce noise and help the LLM focus on what matters, leading to better responses.

·      Traceability and Explainability: Smaller, focused segments make it easier to trace back how a specific answer was generated.

 

Let’s see a simple transformation from a full document to TextSegment objects using a naive splitter (you can later use more advanced strategies like recursive or semantic chunking).

 

Example

List<TextSegment> splitIntoSegments(Document document) {
	String[] paragraphs = document.text().split("\n");
	return Arrays.stream(paragraphs).map(TextSegment::from).collect(Collectors.toList());
}

 

DocumentSegmentation.java

package com.sample.app.textsegment;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.segment.TextSegment;

public class DocumentSegmentation {
	private static final String CONTENT = """
			LangChain4j is a powerful Java framework that helps developers integrate Large Language Models (LLMs) into their applications. It offers components like document loaders, text segmenters, retrievers, and chat interfaces, making it easy to build LLM-powered apps in Java.
			Text segmentation is a crucial step in building scalable and efficient LLM solutions. Rather than sending an entire document to the LLM, which might exceed token limits or introduce irrelevant noise, it's more practical to split the content into meaningful segments.
			For instance, if you’re building a document search engine, you want the LLM to search through relevant pieces instead of parsing a 100-page document at once. Segments can be paragraphs, sentences, or even semantically meaningful sections, depending on your use case.
			Let’s say you are analyzing a user manual. The introduction, setup instructions, troubleshooting steps, and safety guidelines can be treated as separate segments. This makes it easier to retrieve and serve only the part of the document that answers the user's query.
			LangChain4j supports different splitting strategies such as fixed-size chunking, recursive splitting, or custom logic based on punctuation or semantics. Choosing the right strategy impacts the quality of responses generated by the LLM.
			In summary, proper segmentation reduces cost, enhances response relevance, and ensures faster processing times. It also improves traceability, making it easier to understand why the LLM gave a certain response.

						""";

	private static List<TextSegment> splitIntoSegments(Document document) {
		String[] paragraphs = document.text().split("\n");
		return Arrays.stream(paragraphs).map(TextSegment::from).collect(Collectors.toList());
	}

	public static void main(String[] args) {
		Document doc = Document.from(CONTENT);

		List<TextSegment> segments = splitIntoSegments(doc);

		int sNo = 1;
		for (TextSegment textSegment : segments) {
			System.out.println(sNo + " : " + textSegment.text() + "\n");
			sNo++;
		}
	}

}

Output

1 : LangChain4j is a powerful Java framework that helps developers integrate Large Language Models (LLMs) into their applications. It offers components like document loaders, text segmenters, retrievers, and chat interfaces, making it easy to build LLM-powered apps in Java.

2 : Text segmentation is a crucial step in building scalable and efficient LLM solutions. Rather than sending an entire document to the LLM, which might exceed token limits or introduce irrelevant noise, it's more practical to split the content into meaningful segments.

3 : For instance, if you’re building a document search engine, you want the LLM to search through relevant pieces instead of parsing a 100-page document at once. Segments can be paragraphs, sentences, or even semantically meaningful sections, depending on your use case.

4 : Let’s say you are analyzing a user manual. The introduction, setup instructions, troubleshooting steps, and safety guidelines can be treated as separate segments. This makes it easier to retrieve and serve only the part of the document that answers the user's query.

5 : LangChain4j supports different splitting strategies such as fixed-size chunking, recursive splitting, or custom logic based on punctuation or semantics. Choosing the right strategy impacts the quality of responses generated by the LLM.

6 : In summary, proper segmentation reduces cost, enhances response relevance, and ensures faster processing times. It also improves traceability, making it easier to understand why the LLM gave a certain response.

 

Using DocumentSplitters for Smart Chunking

LangChain4j provides ready-made utilities like DocumentSplitters.

List<TextSegment> segments = DocumentSplitters.recursive(200, 30).split(doc);

 

This recursive splitter chunks your document with a target size of 200 characters and an overlap of 30, preserving context and structure.

 

Find the below working application.

 

DocumentSplittersDemo.java

package com.sample.app.textsegment;

import java.util.List;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.splitter.DocumentSplitters;
import dev.langchain4j.data.segment.TextSegment;

public class DocumentSplittersDemo {
  private static final String CONTENT = """
      LangChain4j is a powerful Java framework that helps developers integrate Large Language Models (LLMs) into their applications. It offers components like document loaders, text segmenters, retrievers, and chat interfaces, making it easy to build LLM-powered apps in Java.
      Text segmentation is a crucial step in building scalable and efficient LLM solutions. Rather than sending an entire document to the LLM, which might exceed token limits or introduce irrelevant noise, it's more practical to split the content into meaningful segments.
      For instance, if you’re building a document search engine, you want the LLM to search through relevant pieces instead of parsing a 100-page document at once. Segments can be paragraphs, sentences, or even semantically meaningful sections, depending on your use case.
      Let’s say you are analyzing a user manual. The introduction, setup instructions, troubleshooting steps, and safety guidelines can be treated as separate segments. This makes it easier to retrieve and serve only the part of the document that answers the user's query.
      LangChain4j supports different splitting strategies such as fixed-size chunking, recursive splitting, or custom logic based on punctuation or semantics. Choosing the right strategy impacts the quality of responses generated by the LLM.
      In summary, proper segmentation reduces cost, enhances response relevance, and ensures faster processing times. It also improves traceability, making it easier to understand why the LLM gave a certain response.
            """;

  public static void main(String[] args) {
    Document doc = Document.from(CONTENT);

    List<TextSegment> segments = DocumentSplitters.recursive(200, 30).split(doc);

    int sNo = 1;
    for (TextSegment textSegment : segments) {
      System.out.println(sNo + " : " + textSegment.text() + "\n");
      sNo++;
    }
  }

}

 

Output

1 : LangChain4j is a powerful Java framework that helps developers integrate Large Language Models (LLMs) into their applications.

2 : It offers components like document loaders, text segmenters, retrievers, and chat interfaces, making it easy to build LLM-powered apps in Java.

3 : Text segmentation is a crucial step in building scalable and efficient LLM solutions.

4 : Rather than sending an entire document to the LLM, which might exceed token limits or introduce irrelevant noise, it's more practical to split the content into meaningful segments.

5 : For instance, if you’re building a document search engine, you want the LLM to search through relevant pieces instead of parsing a 100-page document at once.

6 : Segments can be paragraphs, sentences, or even semantically meaningful sections, depending on your use case.

7 : Let’s say you are analyzing a user manual. The introduction, setup instructions, troubleshooting steps, and safety guidelines can be treated as separate segments.

8 : This makes it easier to retrieve and serve only the part of the document that answers the user's query.

9 : LangChain4j supports different splitting strategies such as fixed-size chunking, recursive splitting, or custom logic based on punctuation or semantics.

10 : Choosing the right strategy impacts the quality of responses generated by the LLM.

11 : In summary, proper segmentation reduces cost, enhances response relevance, and ensures faster processing times.

12 : It also improves traceability, making it easier to understand why the LLM gave a certain response.

2. How Big Should Segments Be?

It depends on the retrieval strategy and the downstream use case (The way the retrieved segments will be used later, especially by the Large Language Model (LLM) to generate answers, summaries, or insights).

 

When integrating documents into a Retrieval-Augmented Generation (RAG) pipeline, determining the right segment size is a trade-off between context preservation and retrieval quality. Two main approaches are commonly used.

 

Approach 1: Treating the Entire Document as an Atomic Unit

In this strategy, each document (e.g., a PDF, webpage, etc.) is treated as indivisible. The retrieval system indexes the full document as a single chunk and retrieves the top-N most relevant documents at query time. These complete documents are then passed to the LLM.

 

This approach is best for use cases where complete information is required, such as legal, scientific, or compliance scenarios, where missing a detail could result in incorrect outcomes.

 

Pros

·      No context is lost: The LLM receives the full document, preserving all relationships between sections.

·      Preserves structure: Useful when structure, flow, or interdependent content matters.

 

Cons

·      High token consumption: Long documents can quickly hit token limits, especially with shorter-context models.

·      Reduced relevance granularity: A document might contain multiple unrelated topics. Only a subset may be relevant to the query.

·      Poorer vector embedding quality: Compressing a large and diverse document into a single vector often leads to diluted semantic representation. This reduces the accuracy of similarity-based retrieval.

 

Approach 2: Splitting Documents into Smaller Segments

Here, documents are segmented into smaller parts, typically at the paragraph or sentence level, often with sliding window overlap to preserve partial context. During retrieval, the system fetches the top-N most relevant segments rather than full documents.

 

It is best for broad knowledge base applications, FAQs, chatbots, and scenarios where lower token usage are priorities.

 

Pros

·      Improved vector search quality: Smaller chunks yield more precise embeddings, improving semantic matching.

·      Efficient use of tokens: Smaller inputs mean more results can be packed into the LLM context window.

·      Scalable: Works better with short-context LLMs.

 

Cons

·      Risk of missing context: Isolated segments might lack background needed for correct interpretation.

·      Possible hallucinations: LLMs may invent missing details if a segment is too sparse or ambiguous.

·      Overlapping chunks increase index size and retrieval time.

 

To mitigate the downsides of Approach 2, several strategies have emerged:

 

·      Sliding Window with Overlap: Chunks overlap by a few sentences to preserve context flow.

·      Sentence Window Retrieval: Instead of fixed chunks, retrieval includes adjacent sentences around the matched content.

·      Auto-Merging Retrieval: Merges nearby retrieved segments at runtime to reconstruct richer context dynamically.

·      Parent Document Retrieval: Retrieves small segments for search accuracy but returns the full parent document or section to the LLM for context.

 

In most modern RAG pipelines, Approach 2 with context aware chunking and retrieval enhancement techniques strikes the best balance. However, for highly sensitive or interconnected content, Approach 1 may be necessary.

 

 

Previous                                                    Next                                                    Home

Sunday, 10 August 2025

Introduction to DocumentTransformer in LangChain4j

In the world of LLM applications, how you prepare your documents often determines the quality of your results. Before feeding documents into an embedding model or a retriever, it’s essential to transform them into a more structured, optimized format. That’s where LangChain4j's DocumentTransformer interface comes in.

This interface is designed to help you apply various transformations to documents, making them cleaner, more relevant, and more informative for downstream tasks such as retrieval-augmented generation (RAG).

 

What Can DocumentTransformer Do?

Removes unnecessary or noisy content from the document text, like boilerplate footers, HTML tags, redundant line breaks, etc., It helps to reduce token usage and prevents distractions in the LLM’s attention span.

 

·      Filtering: Allows you to programmatically discard documents that don't meet certain criteria. For example, documents that are too short, outdated, or irrelevant to your domain.

 

·      Enriching: We can add extra metadata or context to documents, such as source labels, topic categories, or timestamps. It improves search relevance and enables better ranking or grouping during retrieval.

 

·      Summarizing: We can generate a concise summary of the document and stores it in the document’s metadata. This summary can later be attached to each TextSegment (explained later) to enhance context during retrieval.

 

In addition to transforming document text, DocumentTransformer implementations can add, modify, or remove metadata entries. This is important for setting up searchable tags, user-defined filters, or dynamic content scoring strategies.

 

Here’s what the actual interface looks like.

public interface DocumentTransformer {
    Document transform(Document document);

    default List<Document> transformAll(List<Document> documents) {
        return documents.stream()
                .map(this::transform)
                .filter(Objects::nonNull)
                .collect(toList());
    }
}

·      transform(Document document): Transforms a single document.

·      transformAll(List<Document> documents): Applies the transformation across a batch of documents.

·      Filters out null results, which is useful for transformers that may exclude some documents (e.g., filters).

 

Find the below working application.

 

DocumentTransformerDemo.java

package com.sample.app.document.transformers;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentTransformer;

public class DocumentTransformerDemo {

	public static void main(String[] args) {

		Document doc = Document.from("""
				This is just a simple     example document.
				It is meant to demonstrate a few really basic transformations.
				There are some     unnecessary spaces and very redundant words.
				""");

		// Print the content before transformation
		System.out.println("Before Transformation:\n" + doc.text());

		// Transformer 1: Trim whitespace in each line
		DocumentTransformer trimWhitespaceTransformer = document -> {
			String transformed = Arrays.stream(document.text().split("\n")).map(String::trim)
					.collect(Collectors.joining("\n"));
			return Document.from(transformed);
		};

		// Transformer 2: Convert to lowercase
		DocumentTransformer lowercaseTransformer = document -> {
			String transformed = document.text().toLowerCase();
			return Document.from(transformed);
		};

		// Transformer 3: Remove filler words
		DocumentTransformer removeFillerWordsTransformer = document -> {
			List<String> fillerWords = List.of("just", "really", "very", "actually", "basically", "simply");
			String transformed = Arrays.stream(document.text().split("\\s+"))
					.filter(word -> !fillerWords.contains(word.toLowerCase())).collect(Collectors.joining(" "));
			return Document.from(transformed);
		};

		// Apply all transformations sequentially
		Document transformedDoc = doc;
		transformedDoc = trimWhitespaceTransformer.transform(transformedDoc);
		transformedDoc = lowercaseTransformer.transform(transformedDoc);
		transformedDoc = removeFillerWordsTransformer.transform(transformedDoc);

		// Print the content after transformation
		System.out.println("\nAfter Transformation:\n" + transformedDoc.text());
	}
}

Output

Before Transformation:
This is just a simple     example document.
It is meant to demonstrate a few really basic transformations.
There are some     unnecessary spaces and very redundant words.


After Transformation:
this is a simple example document. it is meant to demonstrate a few basic transformations. there are some unnecessary spaces and redundant words.

Currently, LangChain4j provides a built-in transformer specifically for HTML documents,

the HtmlToTextDocumentTransformer, available in the langchain4j-document-transformer-jsoup module.

<dependency>
	<groupId>dev.langchain4j</groupId>
	<artifactId>langchain4j-document-transformer-jsoup</artifactId>
</dependency>

This transformer is designed to parse raw HTML, extract meaningful text content, and optionally extract metadata like the title, headings, etc. It uses the Jsoup library internally for parsing and cleaning up the HTML.

 

HtmlTransformerExample.java

package com.sample.app.document.transformers;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.transformer.jsoup.HtmlToTextDocumentTransformer;

public class HtmlTransformerExample {

    public static void main(String[] args) {
        String html = """
                <html>
                    <head><title>Welcome</title></head>
                    <body>
                        <h1>Main Heading</h1>
                        <p>This is a <b>sample</b> HTML document.</p>
                        <p>It contains multiple elements like <a href="#">links</a>, paragraphs, and headings.</p>
                    </body>
                </html>
                """;

        // Create a Document from raw HTML
        Document htmlDocument = Document.from(html);

        // Create an instance of the HTML to Text transformer
        HtmlToTextDocumentTransformer transformer = new HtmlToTextDocumentTransformer();

        // Apply the transformation
        Document transformedDocument = transformer.transform(htmlDocument);

        // Print the plain text result
        System.out.println("Extracted Text:\n" + transformedDocument.text());

        // Optionally, access metadata (if configured in transformer)
        System.out.println("\nMetadata:\n" + transformedDocument.metadata());
    }
}

 

Output

Extracted Text:
Welcome  
Main Heading
 
This is a sample HTML document.
 
It contains multiple elements like links, paragraphs, and headings.

Metadata:
Metadata { metadata = {} }

In summary, the DocumentTransformer interface in LangChain4j provides a modular and powerful way to prepare your documents before they are fed into the LLM pipeline. Whether it’s about cleaning up messy text, enriching content with metadata, or summarizing for better retrieval, these transformers help you get the most out of your documents and ultimately, your AI system.

 

Previous                                                    Next                                                    Home

Parsing Files Made Easy with LangChain4j: A Guide to DocumentParser Implementations

In building LLM (Large Language Model) applications, one of the first and most crucial steps is loading and parsing unstructured data from various document formats. LangChain4j simplifies this step with its powerful DocumentParser interface and a set of pre built parsers for common file types. This blog post helps to introduce Java developers to the different DocumentParser implementations available in LangChain4j and how to use them to ingest data from PDFs, DOCX, TXT, and more.

What is a DocumentParser?

A DocumentParser in LangChain4j is an interface responsible for converting raw files into a standardized Document object that the rest of the LangChain4j ecosystem can work with including embedding, retrieval, and question answering.

 

public interface DocumentParser {

    Document parse(InputStream inputStream);

}

 

Following are the built-in parsers.

·      TextDocumentParser: Parses: .txt, .md, .html, and other plain text files.

·      ApachePdfBoxDocumentParser: Parses: .pdf files. You should add the dependency 'langchain4j-document-parser-apache-pdfbox' to consume this parser.

·      ApachePoiDocumentParser: Parses: .doc, .docx, .ppt, .pptx, .xls, .xlsx. It is good for for handling MS Office documents using Apache POI. You should use the dependency 'langchain4j-document-parser-apache-poi' to consume the parser.

·      ApacheTikaDocumentParser: It parses almose any file format. You should use the dependency langchain4j-document-parser-apache-tika to consume this parse.

 

Find the below working Application.

 

TextDocumentParserDemo.java

package com.sample.app.documentparsers;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;

public class TextDocumentParserDemo {

	public static void main(String[] args) {
		Document document = FileSystemDocumentLoader.loadDocument("/Users/Shared/llm_docs/test.txt",
				new TextDocumentParser());

		System.out.println(document.text());
	}

}

 


Previous                                                    Next                                                    Home