Skip to main content

Spring AI RAG Engine

A retrieval augmented generation library for Spring Boot on Spring AI. Ingests documents into a vector store, answers questions with cited sources, and adds change detection, tenant scoping and optional REST endpoints.

Stack: Java / Spring Boot / Spring AI / PostgreSQL / pgvector / Flyway / Ollama / Docker

Early release (0.1.0-SNAPSHOT)Licence: see repository (opens in a new tab)

What it does

The sample app uses a local Ollama model (llama3.2 for chat, nomic-embed-text for embeddings), so no hosted service or API key is needed. The first start downloads about 2 GB of models.

The artifacts are not on Maven Central yet. Run ./mvnw install -DskipTests in the repository to install them into your local Maven repository.

Ingestion

  • Configured sources and uploads. Ingest documents from configured sources (for example a filesystem folder) or upload single files through the optional upload endpoints.
  • Change detection. A document is re-ingested when its content, chunking strategy, chunk size or overlap changes. A change of embedding model is not detected and needs a manual full re-ingest.
  • Optional persistence. Ingestion state is in memory by default. Add rag-engine-jpa to keep it in the database, with the schema delivered as a Flyway repeatable migration.

Answers

  • Cited answers. Ask a question and get an answer with its sources. A request can limit the sources (up to 20) and set how many chunks are retrieved (1 to 50).
  • Tenant scoping. The tenant id never comes from the body, query string or headers. SINGLE mode uses one default tenant. CUSTOM mode needs a TenantResolver bean, or startup fails.

Safety

  • Opt-in REST layer. The web module is off by default, requires an authenticated user, and keeps ingestion and job endpoints admin only. It has no Spring Security dependency: it checks the request principal and role, so any mechanism that sets them works.
  • Upload checks. File names are sanitised, the extension must be supported, a .pdf must start with %PDF, uploads stream to a temp file with a hard size cap, and a per-tenant document limit applies.

Run it

Get the code

git clone https://github.com/agentworkshq/spring-ai-rag-engine.git
cd spring-ai-rag-engine

Start the sample app

Needs Java 21, Docker with Compose and about 6 GB of free RAM. The first start downloads about 2 GB of models, so it takes a few minutes. The sample runs with authentication off, for local demos only.

docker compose -f rag-engine-sample/compose.yaml up -d
./mvnw install -DskipTests # Windows PowerShell: .\mvnw.cmd install -DskipTests
cd rag-engine-sample && ../mvnw spring-boot:run

The app listens on http://localhost:8080.

Ingest and ask

Run these from a second terminal, inside rag-engine-sample.

curl -X POST http://localhost:8080/api/rag/sources/docs/ingest
curl -X POST http://localhost:8080/api/rag/ask \
-H "Content-Type: application/json" \
-d '{"question": "How do I reset the device?"}'

To stop, run docker compose -f compose.yaml down. Add -v to delete the data.

Use it in your own app

Add the dependencies (group dev.agentworkshq, version 0.1.0-SNAPSHOT): rag-engine-spring-boot-starter and rag-engine-web. Your app also needs a Spring AI chat model, an embedding model and a vector store.

rag-engine:
web:
enabled: true
sources:
- name: docs
type: filesystem
path: ./docs

Versions, from the build files: Java 21, Artifact version 0.1.0-SNAPSHOT, Group ID dev.agentworkshq.

Environment variables

Names only. Set them in .env or in your shell.

NameWhat it does
rag-engine.web.enabledRegisters the REST endpoints. Default false.
rag-engine.web.base-pathPrefix for all endpoints. Default /api/rag.
rag-engine.web.require-authenticationRequests without a principal get 401. Default true.
rag-engine.web.admin-roleRole for admin endpoints. Default RAG_ADMIN. Blank disables the check.
rag-engine.web.tenant.modeSINGLE or CUSTOM. Default SINGLE.
rag-engine.web.tenant.default-tenantTenant used in SINGLE mode. Default is default.
rag-engine.web.upload.enabledRegisters the upload endpoints. Default false.
rag-engine.web.upload.require-adminUploads need the admin role. Default true.
rag-engine.web.upload.max-documents-per-tenantDocument limit per tenant. Default 1000.
rag-engine.answer.max-question-lengthLongest accepted question.
rag-engine.limits.max-file-sizeLargest accepted upload. Also set spring.servlet.multipart limits.

API at a glance

MethodPathPurpose
POST/api/rag/askAsk a question (authenticated)
POST/api/rag/sources/{name}/ingestIngest a configured source, synchronously (admin)
GET/api/rag/sources/{name}/jobsRecent jobs for a source (admin)
GET/api/rag/jobs/{id}One ingestion job (admin)
POST/api/rag/documentsUpload a document (upload endpoints enabled)
GET/api/rag/documentsList the tenant's documents, paged
GET/api/rag/documents/{externalId}Get one document
DELETE/api/rag/documents/{externalId}Delete a document and its chunks

Why it exists

It packages the parts of a RAG backend that tutorials skip: change detection, tenant scoping, upload safety and job tracking. It follows the ideas in the RAG with Spring AI and pgvector article. The README lists its limits, for example that ingestion is synchronous, the ingestion lock works within one JVM, and the ask endpoint has no rate limiting.