惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
J
Java Code Geeks
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
V
V2EX
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Jina AI
Jina AI
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
B
Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building a Local RAG Application with Spring AI, Ollama, ...
Pranjit Medh · 2026-05-16 · via DEV Community

Pranjit Medhi

Retrieval-Augmented Generation (RAG) is a powerful design pattern that allows you to ground Large Language Models (LLMs) with your proprietary, real-time context. This prevents hallucinations and eliminates the need for expensive model fine-tuning.
This comprehensive guide walks you through building a completely local, production-ready RAG application using the Spring ai.

1. Prerequisites and Local Environment Setup

Before touching Java code, you need to set up the infrastructure. Create a compose.yml file to spin up PostgreSQL (with the pgvector extension) and Ollama:

docker-compose.yml
services:
  postgres:
    image: pgvector/pgvector:pg16
    container_name: spring-ai-rag-postgres
    environment:
      POSTGRES_DB: spring_ai_rag
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"

  ollama:
    image: ollama/ollama:latest
    container_name: spring-ai-rag-ollama
    ports:
      - "11434:11434"

Enter fullscreen mode Exit fullscreen mode

Pulling the Local AI Models
Start your Docker containers by running docker compose up -d. Next, allocate models to your local Ollama engine via your terminal:

docker exec -it spring-ai-rag-ollama ollama pull llama3.2
docker exec -it spring-ai-rag-ollama ollama pull nomic-embed-text

Enter fullscreen mode Exit fullscreen mode

2. Project Setup & Dependencies

Head over to start.spring.io and create a standard Spring Boot project using Maven or Gradle. Add the following dependencies to your pom.xml:

    <!-- Spring Web for REST APIs -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring AI Ollama Support -->
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
    </dependency>

    <!-- Spring AI PGVector Store Starter -->
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
    </dependency>

    <!-- Apache Tika Document Reader Support -->
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-tika-document-reader</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>`

Enter fullscreen mode Exit fullscreen mode

3. Configuration Management

Add the following properties to your src/main/resources/application.yml file to stitch your components together:

spring:
  application:
    name: spring-ai-local-rag

  # Database Connection Details
  datasource:
    url: jdbc:postgresql://localhost:5432/spring_ai_rag
    username: postgres
    password: postgres

  ai:
    # Ollama Model Configurations
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: llama3.2
      embedding:
        options:
          model: nomic-embed-text

    # Vector Database Settings
    vectorstore:
      pgvector:
        initialize-schema: true
        table-name: rag_documents

Enter fullscreen mode Exit fullscreen mode

4. Implementing Retrieval and Chat Generation

Next, build the execution pipeline. We configure a structured ChatClient. This component automatically queries PGVector behind the scenes based on incoming prompts and attaches relevant context.

public class RagService {

    @Value("classpath:rag-guide.txt")
    Resource textfile;

    ChatClient chatClient;
    VectorStore vectorStore;

    public RagService(ChatClient.Builder chatClient, VectorStore vectorStore) {
        this.chatClient = chatClient.build();
        this.vectorStore = vectorStore;
    }

    public void ingestText() {

        System.out.println("Reading document...");

        TikaDocumentReader reader = new TikaDocumentReader(textfile);

        List<Document> documents = reader.get();

        System.out.println("Documents loaded: " + documents.size());

        vectorStore.add(documents);

        System.out.println("Documents added to vector store");
    }


    public String askSimpleQuestion() {
        String question = "What is RAG?";
        System.out.println("Starting similarity search...");
        SearchRequest searchRequest = SearchRequest.builder()
                .query(question)
                .topK(3)
                .build();
        List<Document> documents =
                vectorStore.similaritySearch(searchRequest);
        System.out.println("Similarity search completed");
        StringBuilder context = new StringBuilder();
        for (Document document : documents) {
            context.append(document.getText()).append("\n");
        }
        System.out.println("Calling LLM...");

        String prompt = """
                Given the following context information,
                answer the question.
                Context:
                %s
                Question:
                %s
                """.formatted(context, question);

        String response = chatClient.prompt(prompt)
                .call()
                .content();

        System.out.println("LLM response received");

        return response;
    }
}

Enter fullscreen mode Exit fullscreen mode

5. Data foler

Create a store rag-guide.txt file in resources folder

6. Running the application

We will run the application using CommandLineRuner interface of springboot as show below:

@SpringBootApplication
public class RagCliApplication implements CommandLineRunner{

    @Autowired
    private RagService service;

    public static void main(String[] args) {
        SpringApplication.run(SpringAiCliApplication.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
         service.ingestText();
        final String s = service.askSimpleQuestion();
        System.out.println(s);
    }
}

Enter fullscreen mode Exit fullscreen mode