I have a folder of Markdown notes and a Spring Boot service at home, and I wanted to ask questions about the notes without sending them anywhere. Spring AI gives Java services one way to talk to models and vector stores, and it works with a local Ollama server. If I were only exploring the idea, I would prototype in Python, where there are more examples. But the rest of my small services are Java, and I prefer one language and one way to deploy.
Dependencies
With Spring Boot and the Spring AI BOM in place, add the Ollama starter:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-advisors-vector-store</artifactId>
</dependency>
This service still runs on Spring AI 1.1 with Spring Boot 3.5. I have not moved it to 2.0 yet (my plan for that is in the June post). The code below is for 1.1. If you start on 2.0, check the Spring AI reference for your version, because the property keys changed.
Configuration
spring:
ai:
ollama:
base-url: http://raspberrypi.local:11434
chat:
options:
model: llama3.2:3b
embedding:
options:
model: nomic-embed-text
On Spring AI 2.0 the options level is gone from these keys: they become spring.ai.ollama.chat.model and spring.ai.ollama.embedding.model.
Pull the embedding model on the Ollama host first: ollama pull nomic-embed-text. My Ollama host is another machine on the LAN.
Load documents into a vector store
SimpleVectorStore keeps vectors in memory, which is fine for a notes folder:
@Configuration
class RagConfig {
@Bean
VectorStore vectorStore(EmbeddingModel embeddingModel) {
return SimpleVectorStore.builder(embeddingModel).build();
}
@Bean
ApplicationRunner ingest(VectorStore store, @Value("file:notes/*.md") Resource[] files) {
return args -> {
var splitter = new TokenTextSplitter();
for (Resource file : files) {
var docs = new TextReader(file).get();
store.add(splitter.apply(docs));
}
};
}
}
Ask questions
@RestController
class AskController {
private final ChatClient chat;
AskController(ChatClient.Builder builder, VectorStore store) {
this.chat = builder
.defaultAdvisors(QuestionAnswerAdvisor.builder(store).build())
.build();
}
@GetMapping("/api/ask")
String ask(@RequestParam String q) {
return chat.prompt().user(q).call().content();
}
@GetMapping(value = "/api/ask/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> stream(@RequestParam String q) {
return chat.prompt().user(q).stream().content();
}
}
The advisor retrieves the most similar chunks and adds them to the prompt before the model sees your question.
Serving it behind nginx
The streaming endpoint needs buffering off, otherwise the browser sees the whole answer at once:
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_buffering off;
proxy_read_timeout 300s;
}
Before I trust the answers
Retrieval quality matters more than the model here. Chunk size changes a lot, so try different splitter settings and look at what the advisor actually retrieves before you blame the model.
Small local models also make things up when the retrieved context is thin. I tell the model in the system prompt to say “I don’t know” when the notes do not contain the answer, and I test that with a few questions I know are not covered. It works most of the time, not always.
I keep the model name and the base URL in configuration only, so I can point the same code at a bigger machine or a hosted model later. LangChain4j would be the other Java option; I have not compared them.