A demonstration of implementing the RAG pattern using Spring Boot, Spring AI, PostgreSQL with PGVector, and OpenAI's GPT models. This application ingests PDF documentation and provides intelligent Q&A capabilities through a command-line interface.
graph TB
subgraph "Application Layer"
A[Spring Boot Application]
B[Spring Shell CLI]
C[SpringAssistantCommand]
end
subgraph "AI Processing Layer"
D[Spring AI Framework]
E[OpenAI GPT-3.5 Turbo]
F[Document Reader<br/>PDF Processing]
G[Token Text Splitter]
end
subgraph "Data Layer"
H[PostgreSQL Database]
I[PGVector Extension]
J[Vector Store<br/>Embeddings]
end
subgraph "Document Sources"
K[Spring Boot Reference PDF]
L[Prompt Templates]
end
B --> C
C --> D
D --> E
D --> J
F --> G
G --> J
K --> F
L --> C
J --> I
I --> H
classDef appLayer fill:#e1f5fe
classDef aiLayer fill:#f3e5f5
classDef dataLayer fill:#e8f5e8
classDef docLayer fill:#fff3e0
class A,B,C appLayer
class D,E,F,G aiLayer
class H,I,J dataLayer
class K,L docLayer
- Document Ingestion: Automatically loads and processes PDF documents into vector embeddings
- Vector Database: PostgreSQL with PGVector extension for semantic similarity search
- AI Chat: OpenAI GPT integration for generating contextual responses
- CLI Interface: Spring Shell for interactive querying
- RAG Pipeline: Retrieves relevant documents and augments AI responses
- Java 21+
- Maven 3.6+
- Docker and Docker Compose
- OpenAI API Key
git clone <repository-url>
cd springboot-ai-rag-docingestexport OPENAI_API_KEY="your-openai-api-key-here"docker-compose up -ddocker run -d \
--name pgvector-db \
-e POSTGRES_DB=aidocs \
-e POSTGRES_USER=admin \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 \
ankane/pgvector:latest# Build the application
./mvnw clean compile
# Run the application
./mvnw spring-boot:runThe application will automatically:
- Connect to the PostgreSQL database
- Create the vector store schema
- Load and process the Spring Boot reference PDF
- Generate embeddings and store them in the vector database
Once started, you'll see a Spring Shell prompt. Use the following commands:
# Ask a question about Spring Boot
shell:> q "How do I configure a DataSource in Spring Boot?"
# Ask another question
shell:> q "What are Spring Boot starters?"
# Exit the application
shell:> exit- Document Loading: On startup,
ReferenceDocsLoaderchecks if documents exist in the vector store - PDF Processing: Uses Spring AI's
PagePdfDocumentReaderto extract text from PDF files - Text Splitting:
TokenTextSplitterbreaks documents into manageable chunks - Embedding Generation: OpenAI creates vector embeddings for each text chunk
- Storage: Embeddings are stored in PostgreSQL with PGVector extension
- Query Processing: User questions are converted to embeddings and matched against stored documents
- Response Generation: Retrieved documents provide context for GPT to generate accurate answers
- Host: localhost:5432
- Database: aidocs
- Username: admin
- Password: password
- Model: GPT-3.5 Turbo
- Embedding Dimensions: 1536
- Vector Distance: Cosine Distance
- Index Type: HNSW
- Add new documents: Place PDF files in
src/main/resources/docs/ - Modify prompts: Edit templates in
src/main/resources/prompts/ - Change AI model: Update
spring.ai.openai.chat.options.modelinapplication.yaml
src/
├── main/
│ ├── java/me/amiralles/aidocs/
│ │ ├── AidocsApplication.java # Main Spring Boot application
│ │ ├── ReferenceDocsLoader.java # Document ingestion component
│ │ └── SpringAssistantCommand.java # Shell command handler
│ └── resources/
│ ├── application.yaml # Application configuration
│ ├── schema.sql # Database schema
│ ├── docs/
│ │ └── spring-boot-reference.pdf # Source documentation
│ └── prompts/
│ └── spring-boot-reference.st # AI prompt template
└── test/
└── java/me/amiralles/aidocs/
└── AidocsApplicationTests.java # Basic application tests
- Spring Boot 3.2.5 - Application framework
- Spring AI 0.8.1 - AI integration framework
- Spring Shell 3.2.4 - CLI interface
- PostgreSQL - Primary database
- PGVector - Vector similarity search extension
- OpenAI GPT-3.5 - Language model for responses
- Maven - Build tool
- Docker - Containerization
Try asking questions like:
- "What is Spring Boot Auto Configuration?"
- "How do I create a REST controller?"
- "What are the different ways to configure properties?"
- "How does Spring Boot handle dependency injection?"
This project is a demonstration example for educational purposes.