Skip to content

Oussemasahbeni/spring-ai-rag-with-pg-vector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring AI Demo

A demonstration project showcasing Retrieval Augmented Generation (RAG) implementation using Spring AI and GEMINI model. This application enables intelligent document querying by combining the power of Large Language Models (LLMs) with local document context.

Overview

This project demonstrates how to:

  • Ingest PDF documents into a vector database
  • Perform semantic searches using Spring AI
  • Augment LLM responses with relevant document context
  • Create an API endpoint for document-aware chat interactions

Project Requirements

Running the Application

  1. Start Docker Desktop

  2. Launch the application:

./mvnw spring-boot:run

The application will:

  • Start a PostgreSQL database with PGVector extension
  • Initialize the vector store schema
  • Ingest documents from the configured location
  • Start a web server on port 8080

Key Components

IngestionService

The IngestionService handles document processing and vector store population:

@Component
public class IngestionService implements CommandLineRunner {
    private final VectorStore vectorStore;
    
    @Value("classpath:/docs/your-document.pdf")
    private Resource marketPDF;
    
    @Override
    public void run(String... args) {
        var pdfReader = new ParagraphPdfDocumentReader(marketPDF);
        TextSplitter textSplitter = new TokenTextSplitter();
        vectorStore.accept(textSplitter.apply(pdfReader.get()));
    }
}

ChatController

The ChatController provides the REST endpoint for querying documents:

@RestController
public class ChatController {
    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder, VectorStore vectorStore) {
        this.chatClient = builder
                .defaultAdvisors(new QuestionAnswerAdvisor(vectorStore))
                .build();
    }

    @GetMapping("/")
    public String chat() {
        return chatClient.prompt()
                .user("Your question here")
                .call()
                .content();
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages