Building AI Assistants with Spring AI
Artificial Intelligence (AI) assistants are increasingly becoming an essential part of modern applications. They can enhance user experience by offering conversational interfaces, automating tasks, and providing intelligent insights. In this article, we’ll explore how to implement an AI Assistant using Spring AI, a modern framework for integrating AI capabilities into Java applications.
Prerequisites
Before starting the implementation, make sure you have the following:
- A basic understanding of Spring Boot and Java.
- Java Development Kit (JDK) 17 or later.
- Maven or Gradle for dependency management.
- AI API Key: Access to an AI service like OpenAI, Azure AI, or similar.
- Basic Knowledge: Familiarity with Java and Spring Boot.
1. Key Features and Benefits of Spring AI
Spring AI offers a range of advanced features that make it easier for developers to integrate artificial intelligence capabilities into Java-based applications. These features streamline the development process and ensure that AI is not just a feature but an integral part of the application architecture. Below, we will explore some key features and how they contribute to creating powerful AI-driven applications.
1.1 Effortless Integration with the Spring Ecosystem
One of the standout features of Spring AI is its seamless integration with the broader Spring ecosystem. Since Spring Boot is widely used for building production-grade Java applications, the integration of AI into these applications becomes simple and consistent. With Spring AI, developers can leverage familiar Spring annotations, configuration settings, and dependency management tools.
1.2 Function Calling API: Bringing Interactivity to AI
The Function Calling API in Spring AI allows applications to call AI-powered functions or services based on user input dynamically. This feature is crucial in scenarios where AI needs to interact with the application’s business logic, execute specific operations, or generate responses in real time. For example, it can allow the AI assistant to trigger backend services for task creation, data retrieval, or even complex workflows. This level of dynamic function invocation enables developers to create personalized, context-aware experiences for end users.
1.3 Advisors API: Tailored Advice for Users
The Advisors API allows Spring AI to generate highly contextual, personalized advice. Whether it’s for a customer service chatbot, a travel recommendation assistant, or a medical advice system, the Advisors API can analyze user queries and generate structured advice based on predefined logic and business rules. This is especially useful in applications where recommendations or guidance need to be tailored to the specific needs of the user.
For instance, in a travel planning app, the AI can take into account parameters such as season, budget, and location preferences to recommend destinations.
1.4 Structured Output API: Precision and Formatting
The Structured Output API enables AI to generate responses in predefined formats such as JSON, XML, or even custom-defined structures. This feature is particularly useful in scenarios where consistency and precision are required in AI responses. For example, in financial or technical applications, an AI assistant might need to return structured data that can be parsed by other systems or used directly in reports. This ensures that AI-generated responses are not just conversational but also machine-readable and easily consumable by other applications or services.
1.5 Retrieval-Augmented Generation (RAG): Augmenting Context with External Knowledge
RAG (Retrieval-Augmented Generation) is a cutting-edge feature in Spring AI that combines the power of large language models (LLMs) with the ability to retrieve relevant information from external knowledge bases or documents. In traditional AI models, responses are generated purely based on the model’s pre-existing knowledge. However, with RAG, AI can query external sources—like databases, documents, or even APIs—to augment its responses with the most relevant, up-to-date information.
This is particularly useful in scenarios where accurate and timely information is crucial, such as technical support, research assistance, or customer service.
1.6 Enhanced Security and Privacy
Security is a top priority for any application, and Spring AI is designed with privacy and data protection in mind. Since AI models can handle sensitive information, Spring AI provides tools and guidelines to ensure secure interaction with AI services. It supports various encryption and authentication mechanisms to protect user data both in transit and at rest.
1.7 Extensibility and Customization
Another notable feature of Spring AI is its extensibility. Developers can easily extend or customize the AI assistant’s capabilities by adding custom functions, logic, and integrations. The framework supports third-party AI models, plugins, and APIs, which makes it highly adaptable to specific business needs.
2. Setting Up Spring AI
Adding Dependencies
Add the necessary Spring AI dependencies to your pom.xml
or build.gradle
.
01 02 03 04 05 06 07 08 09 10 | < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.ai</ groupId > < artifactId >spring-ai-openai-spring-boot-starter</ artifactId > </ dependency > </ dependencies > |
Configuration
Next, set up your application.properties
or application.yml
with AI provider details:
1 2 | spring.ai.openai.api-key=${OPENAI_API_KEY} spring.ai.openai.model=gpt-4 |
Ensure that you set your OpenAI API key as an environment variable to maintain security.
3. Simple AI Chat Response with Function Call
In this section, we will explore how to implement a simple AI-driven chat response system using Spring AI, with a focus on defining and invoking a function dynamically. The following example demonstrates how to set up a basic endpoint that interacts with an AI model to process a user’s prompt.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | @RestController public class AiAssistantController { private final ChatClient chatClient; public AiAssistantController(ChatClient.Builder chatClientBuilder) { this .chatClient = chatClientBuilder.build(); } @GetMapping ( "/greet" ) public ChatResponse greetUser() { String greetingMessage = "Hi there, I’m exploring AI with Spring!" ; return chatClient.prompt().user(greetingMessage).call().chatResponse(); } } |
The ChatClient
is the client used to interact with the AI model, built through the provided ChatClient.Builder
for easy configuration. The method greetUser()
, mapped to the /greet
endpoint, sends a greeting message to the AI model and returns its response. The greetingMessage
variable holds the input prompt, which in this case is a friendly greeting. Finally, chatResponse()
retrieves and returns the response from the AI model based on the provided input.
Run the following command in your terminal to see the response:
1 | curl --location 'http://localhost:8080/greet' |
4. Generating Structured AI Responses with Custom Entities
This section demonstrates how to use Spring AI to generate structured responses in a custom entity format. The example illustrates creating a REST API endpoint that requests the AI model to generate details about a fictional recipe and maps the response to a predefined Java record.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public record Recipe(String name, String ingredients, String instructions) { } @RestController public class RecipeAiController { private final ChatClient chatClient; public RecipeAiController(ChatClient.Builder chatClientBuilder) { this .chatClient = chatClientBuilder.build(); } @GetMapping ( "/recipe" ) public Recipe generateRecipe() { String recipePrompt = "" " Create a fictional recipe with the following details: - Name of the recipe - Ingredients as a comma-separated list - Step-by-step instructions Your response must be in JSON format and adhere to RFC8259 standards. Avoid explanations or any extraneous text—only return the JSON response. "" "; return chatClient.prompt().user(recipePrompt).call().entity(Recipe. class ); } } |
The Recipe
record is defined with three fields—name
, ingredients
, and instructions
—serving as the target structure for the AI-generated JSON response. The recipePrompt
string provides detailed instructions to the AI model, specifying the desired output format and emphasizing adherence to JSON standards.
The chatClient
processes this prompt using the prompt()
method and maps the AI-generated JSON response directly to the Recipe
class via the entity(Recipe.class)
method. Finally, the /recipe
endpoint enables clients to interact with the AI model and retrieve a structured recipe response in JSON format.
Use curl
or any REST client to test the endpoint:
1 | curl --location 'http://localhost:8080/recipe' |
5. Spring AI Chat Memory Advisor
The following example demonstrates how to create a dynamic chat endpoint in a Spring Boot application using Spring AI. This implementation incorporates a chat memory advisor to provide context-aware responses, ensuring that the AI retains conversational history during interactions.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | @RestController public class AssistantController { private final ChatClient assistantClient; public AssistantController(ChatClient.Builder clientBuilder) { this .assistantClient = clientBuilder .defaultAdvisors( new MessageChatMemoryAdvisor( new InMemoryChatMemory())) .build(); } @GetMapping ( "/chat" ) public String handleChat( @RequestParam String query) { return assistantClient.prompt() .user(query) .call() .content(); } } |
In this example, the assistantClient
variable interacts with the AI model, utilizing a MessageChatMemoryAdvisor
to preserve conversational context. The /chat
endpoint accepts user queries via the query
parameter, processes them through the AI model, and returns the generated response. By incorporating InMemoryChatMemory
, the AI retains conversational history, allowing it to produce context-aware replies.
Run the following curl command to test the
1 | curl --location 'http://localhost:8080/chat?query=What is the weather like today?' |
This approach is ideal for building interactive chat-based applications, such as customer support systems or virtual assistants, that benefit from contextual understanding.
6. Expanding AI Assistant Capabilities: Structured Output API and RAG Integration
In addition to implementing chat-based assistants, Spring AI offers powerful tools to build other types of AI-driven assistants, including those utilizing the Spring AI Structured Output API and Spring AI RAG (Retrieval-Augmented Generation). These tools enable us to create advanced and specialized AI assistants tailored to specific needs.
6.1 Spring AI Structured Output API
The Structured Output API enables the generation of structured and machine-readable responses, such as JSON or XML, from the AI model. This capability is useful in scenarios where the output must be consumed programmatically or integrated into other systems, such as data pipelines or dashboards. For instance, a financial assistant could use this API to generate a detailed investment report in JSON format, which can be directly used by frontend applications or third-party tools.
By defining a Java entity and instructing the AI model to return output that maps directly to the entity, we can ensure compliance with strict data formats. This approach enhances reliability and simplifies integration, making it suitable for applications like data processing, e-commerce, or report generation.
6.2 Spring AI RAG (Retrieval-Augmented Generation)
RAG combines the power of generative AI with external knowledge sources to create more accurate and contextually relevant responses. By integrating AI models with knowledge bases, document stores, or search systems, Spring AI RAG allows developers to build assistants capable of answering complex questions or retrieving precise information from vast datasets.
For example, a customer support assistant implemented with RAG can search a knowledge base to provide accurate responses to user queries, even if the information exceeds the AI model’s training data. This hybrid approach improves the accuracy and utility of AI assistants, making them invaluable for use cases like legal research, technical support, or educational applications.
7. Conclusion
In this article, we explored how to implement AI assistants using Spring AI, focusing on the tools and features available within the framework. We demonstrated how to build a basic chat-based assistant and discussed the potential of advanced capabilities like the Structured Output API and Retrieval-Augmented Generation (RAG) for specialized use cases. Spring AI enables us to build efficient AI-driven applications with minimal effort. By leveraging its features like function calling, structured output, and RAG, we can craft AI assistants tailored to specific use cases.
8. Download the Source Code
You can download the full source code of this example here: spring ai assistant