Contract Q&A
Java / Spring Boot
Contract Q&A is a Spring Boot service that stores a PDF, embeds its text, and answers a question from those passages. A question with no nearby passage never reaches the model. The default process uses a deterministic local embedding and returns the top passage, which is what the tests run. The gemini profile switches both calls to Spring AI on Gemini: gemini-embedding-001 and gemini-3.5-flash. One Thymeleaf page uploads the PDF and shows the cited answer. The running service is https://contract-qa.apps.onpoint.host. Source is https://github.com/launchthatbrand/contract-qa.
768-dimension vectors, the size requested from gemini-embedding-001
Chunks farther than this cosine distance are not sent to the answer client
The Problem
A contract question is only useful when the answer points at the clause it came from. Calling a model with the whole file, or with passages that do not match the question, produces an answer that cannot be checked.
The Solution
Extract the PDF with PDFBox, chunk it, and store each chunk in Postgres with pgvector. Embed the question the same way and keep the chunks inside a cosine-distance cutoff. The answer client sees only those passages. Citations are the passages that were retrieved.
How the page is rendered
The browser receives finished HTML. PageController returns the view name index or document, Thymeleaf fills that template, and Tomcat sends the page. The JSON routes POST /api/documents and POST /api/questions call the same DocumentService. This page does not use them.
Technical Decisions
Key architecture decisions and their outcomes
The model only sees retrieved passages
An answer that is not tied to a stored passage cannot be checked against the PDF.
Nearest chunks are filtered by cosine distance before the answer client is called. An empty set returns a not-found result and no citations.
A question about a subject the PDF does not contain comes back with found set to false.
Gemini is a profile
The tests have to run without a Google API key, and a reviewer has to be able to start the app the same way.
The default beans are a deterministic embedding and an answer client that returns the top passage. The Gemini autoconfiguration stays off unless the gemini profile is active. That profile turns on Spring AI's Gemini chat model and gemini-embedding-001.
mvn test does not call Google. The same interfaces serve the Gemini profile when GEMINI_API_KEY is set.
The browser receives finished HTML
The upload form has to be a page someone can open, and the answer has to appear on that same page.
PageController returns the view name index or document. Thymeleaf fills the template on the server. POST /api/documents and POST /api/questions are a second door on the same DocumentService.
https://contract-qa.apps.onpoint.host sends HTML. The page does not assemble itself from the JSON API.
Engineering Details
- Flyway creates the vector extension and an HNSW index on the chunk embedding
- The vector column is vector(768). The Gemini embedding client rejects a vector of any other length
- POST /api/documents accepts the PDF. POST /api/questions returns the answer, a found flag, and citations
- Testcontainers starts pgvector/pgvector:pg16 for the ingest, citation, and not-found cases
Key Highlights
- Java 21 and Spring Boot 3
- PDFBox extraction and chunking into pgvector
- Questions outside the distance cutoff return no citations
- Tests run against the deterministic answer client
- Spring AI Gemini is a profile, not the default
- One Thymeleaf page for upload and the cited answer