A Spring Boot demo showcasing Retrieval-Augmented Generation (RAG) using Spring AI and OpenAI GPT models. This project enables intelligent querying of local PDF documents by combining the power of LLMs with semantic search.
- Ingest and vectorize PDF documents
- Perform semantic search with Spring AI
- Use GPT models with document context (RAG)
- Expose an API endpoint for intelligent querying
- Java 23
- Maven
- Docker Desktop (for PostgreSQL with PGVector)
- OpenAI API Key
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pdf-document-reader</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
</dependencies>
OPENAI_API_KEY=your_api_key_here
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.model=gpt-4
spring.ai.vectorstore.pgvector.initialize-schema=true
Place your documents in:
src/main/resources/docs/
- Start Docker Desktop (for PostgreSQL + PGVector).
- Launch the Spring Boot app:
./mvnw spring-boot:run
Handles PDF ingestion 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()));
}
}
REST controller for document-aware queries:
@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();
}
}
Send a query:
curl http://localhost:8080/
Response will include GPT-generated content enhanced with document context.
Component | Description |
---|---|
Document Ingestion | Reads & splits PDF documents |
Vector Store | Stores embeddings using PGVector |
Semantic Search | Retrieves relevant document chunks |
GPT Integration | Generates responses with embedded document knowledge |
- Avoid reinitializing vector store unnecessarily
- Schedule periodic ingestion for updated documents
- Monitor API token usage
- Cache frequent queries
- Rate-limit endpoint to avoid misuse
- Secure API endpoints with authentication
- Encrypt and protect document content
- Store API keys securely (avoid hardcoding)