Spring AI :
Step 1 : Add Spring AI dependency
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-ollama</artifactId>
</dependency>
Step 2 : Add the properties
spring.ai.ollama.chat.model=gemma4:12b
spring.ai.ollama.base-url=http://localhost:11434
Step 3 : Using ChatClient we can call the any type of LLM
Step 4 : Using ChatMemoryRepository we can maintain the data like sesssion and store the memory.
But this will store in the running server RAM, so we need to introduce the RedisChatMemory to Store into InMemory
Step 4 : By changing the application properties we can connect different types of LLM's.
Need to pass the required keys to connect the LLM's.
Step 5 : Using @Tool we can make any type of business logic into a tool and provide the old services
Spring AI with RAG Implementation :
Step 1 : Add below depdencies to Spring AI
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-examples</artifactId>
<version>2.0.30</version>
</dependency>
Step 2 : Use below properties and connect with Embeddeding models.
spring.ai.ollama.chat.options.model=llama3.2:latest
spring.ai.ollama.embedding.enabled=true
spring.ai.ollama.embedding.options.model=llama3.2:latest
file.path=classpath:data/sample.pdf
Step 3 : Read the pdf file postConstruct your application and store the data into vectorStore which needs to build from embededding model.
Step 4 : Write a simple api to connect the Vector Store and retrive the info.
String relevantDocs = vectorStore.similaritySearch(request.getQuery()).stream().map(Document::getText)
.collect(Collectors.joining());
Step 5 : use the below code and use the query prompt and get the relavant info from vector store.
// Augmented
Message systemMessage = new SystemPromptTemplate(template).createMessage(Map.of("documents", relevantDocs));
// Generation
Message userMessage = new UserMessage(request.getQuery());
Prompt prompt = new Prompt(List.of(systemMessage, userMessage));
ChatClient.CallResponseSpec res = chatClient.prompt(prompt).call();
ChatResponse chatResponse = new ChatResponse();
chatResponse.setResponse(res.content());
chatResponse.setResponseId(request.getConversationId().toString());
System.out.println("Response send ..........");
return chatResponse;