DEV Community

Adnene HAMDOUNI
Adnene HAMDOUNI

Posted on

RAG with Spring Boot: Give Your AI a Private Contextual Memory

RAG with Spring Boot: Give Your AI a Private Contextual Memory


📌 Metadata

  • Subject: RAG / Vector Databases / Spring AI / Java
  • Target: Java Developers, Data/AI Architects
  • Estimated Reading Time: 10 minutes
  • Tags: #SpringBoot #SpringAI #RAG #VectorDatabase #JavaAI

🎯 Introduction

One of the biggest challenges of generative AI in the enterprise is the phenomenon of hallucinations. An LLM, however powerful, can invent facts with disconcerting confidence, especially when it comes to recent data or confidential internal documents it has never "seen" during its training.

Imagine asking your AI: "What is the reimbursement procedure for customer X?". If the AI doesn't have access to your contracts, it will make a guess based on statistical probabilities. This is where RAG (Retrieval Augmented Generation) changes the game.

RAG doesn't seek to retrain the model (which would be costly and slow). Instead, it turns the AI into an ultra-fast researcher: before answering, the AI searches your private documents, extracts the relevant passages, and uses this information as a basis to formulate its response. This is called grounded generation.

By the end of this article, you will understand how to orchestrate this flow with Spring AI and implement a robust contextual memory architecture.


🛠️ Technical Core

1. The RAG Pipeline Deconstructed: Understanding the Flow

To implement RAG, you need to design two distinct pipelines: one for ingestion and one for retrieval.

A. The Ingestion Pipeline (Storing Knowledge)

This is where your documents become "readable" for the AI:

  1. Document Reading: Load files (PDF, Markdown, HTML) via DocumentReader.
  2. Chunking: Split the text into pieces (chunks). Why? Because LLMs have a limited context window and precise pieces allow for finer search.
  3. Embedding: Each piece is converted into a numerical vector by an embedding model. This vector represents the semantic meaning of the text.
  4. Vector Store: These vectors are stored in a vector database (like PGVector).

B. The Retrieval Pipeline (The Response)

This is what happens when the user asks a question:

  1. Query Vectorization: The question is converted into a vector.
  2. Similarity Search: Search the Vector Store for the $K$ text pieces whose vectors are closest to that of the question.
  3. Prompt Augmentation: Build a prompt like: "Here are relevant documents: [Context]. Based solely on these documents, answer the question: [Question]".
  4. Generation: The LLM generates the final response based on the provided evidence.

2. Technical Implementation with Spring AI

Prerequisites

  • JDK 17+, Spring Boot 3.x.
  • A Vector Store. We will use PGVector (PostgreSQL extension), the preferred choice for enterprises because it allows keeping transactional and vector data in the same place.

Step 1: Vector Store Configuration

Add the PGVector starter to your pom.xml and configure your access in application.yml:

spring:
  ai:
    vectorstore:
      pgvector:
        initialize-schema: true
        index-type: HNSW
        distance-type: COSINE_DISTANCE
        dimensions: 1536
Enter fullscreen mode Exit fullscreen mode

Step 2: Document Ingestion

Here's how to load and store your knowledge:

@Service
class KnowledgeIngestionService {
    private final VectorStore vectorStore;

    public KnowledgeIngestionService(VectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    public void ingest(Resource pdfResource) {
        TikaDocumentReader reader = new TikaDocumentReader(pdfResource);
        TokenTextSplitter splitter = new TokenTextSplitter();
        List<Document> chunks = splitter.apply(reader.get());
        vectorStore.accept(chunks);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Chat with Contextual Memory

Thanks to the QuestionAnswerAdvisor, Spring AI automates the entire retrieval process.

@RestController
class RagController {
    private final ChatClient chatClient;

    public RagController(ChatClient.Builder builder, VectorStore vectorStore) {
        var ragAdvisor = QuestionAnswerAdvisor.builder(vectorStore)
                .searchRequest(SearchRequest.builder().topK(4).similarityThreshold(0.7).build())
                .build();

        this.chatClient = builder
                .defaultAdvisors(ragAdvisor)
                .build();
    }

    @GetMapping("/ask")
    public String ask(@RequestParam String question) {
        return chatClient.prompt()
                .user(question)
                .call()
                .content();
    }
}
Enter fullscreen mode Exit fullscreen mode

Pro Tip: If you notice the AI answering "I don't know" when the info is present, lower the similarityThreshold (e.g., 0.6). If it hallucinates too much, increase it (e.g., 0.8).

3. Analysis & Optimization: Going Further

RAG is not a "magic" solution; it requires tuning:

The Chunking Challenge
Chunk size is crucial. Pieces that are too small lose the global context. Pieces that are too large introduce noise. A common strategy is "Overlapping": making chunks overlap to avoid cutting an important sentence in half.

Embeddings: The Search Engine
The choice of the embedding model (e.g., text-embedding-3-small vs a local model via HuggingFace) directly impacts precision. A good embedding model understands that "car" and "automobile" are semantically close.

Confidentiality vs Fine-Tuning
Why choose RAG over fine-tuning?

  1. Instant Updates: Add a document to the Vector Store, and the AI knows it immediately. No retraining needed.
  2. Traceability: RAG allows citing sources ("According to document X, page 4..."), which is impossible with fine-tuning.
  3. Security: You control exactly which document is retrieved based on the user's rights.

🏁 Conclusion & Opening

RAG transforms AI from a conversational assistant into a true business expert capable of exploiting your private data with precision and security. It is the foundation of any serious enterprise AI.

But we still have one step to go. Today, our AI answers. Tomorrow, it must act. This is where Autonomous Agents and the MCP protocol come in, which we will explore in the next article.

Your turn! Install PGVector, load your first documents, and see your Spring Boot application suddenly become very intelligent.


📚 Sources & Resources

Top comments (0)