Spring AI - Chat Model
ChatModel is the core model of the Spring AI project. It provides a simple and flexible way to create chatbots and conversational interfaces. The model is designed to be easy to use and extend, allowing developers to quickly build and deploy chatbots for a wide range of applications.
Maven Dependency
1 | <properties> |
Configuration
OpenAI Chat Properties are explained here https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html. use spring.ai.openai.api-key to define the OpenAI API key and spring.ai.openai.chat.options.model to define the model to use.
You can select between models such as: gpt-5, gpt-5-mini, gpt-5-nano, and more. See the OpenAI Models page for more information.
API access cost a small fee. You can get a API key by signing up at https://platform.openai.com/settings/organization/api-keys
Example of application.properties:
1 | spring.ai.openai.api-key=${OPENAI_API_KEY} |
Usage
Spring will automatically create a ChatModel bean for you. You can inject this bean into your service or controller and use it to generate responses to user prompts.
You can use call method of ChatModel to call the OpenAI API and get the response.
call method is a simple wrapper around the OpenAI API that takes care of the details of making the API call and parsing the response. You can pass a simple string prompt to the call method, and it will return the response from the OpenAI API as a string.
Example of a controller that uses ChatModel‘s call method to get the weather information:
1 | private final ChatModel chatModel; |
It is recommended to use a service layer to call the ChatModel instead of calling it directly from the controller. This allows you to separate the concerns of handling HTTP requests and generating responses from the logic of calling the OpenAI API.
1 |
|
You can override the default options defined in application.properties by passing an instance of OpenAiChatOptions to the call method. This allows you to customize the behavior of the OpenAI API for specific prompts.
1 | ChatResponse response = chatModel.call(new Prompt( |
see https://platform.openai.com/docs/api-reference/chat/create for more information on the OpenAI API.
PromptTemplate
You can use PromptTemplate to define a template for prompts. The template can include placeholders that will be replaced with values at run-time. This can be useful for generating prompts that are based on dynamic data.
1 | public String callUsingPromptTemplate(String adjective, String topic) { |
Structured Output
The ability of LLMs to produce structured outputs is important for downstream applications that rely on reliably parsing output values.
List Output
Use ListOutputConverter to convert the output of a prompt into a list of strings. This is useful when you want to get a list of items from the OpenAI API, such as a list of ice cream flavors.
1 | List<String> flavors = ChatClient.create(chatModel).prompt() |
Entity Output
Use entity method to convert the output of a prompt into a Java record or class. This is useful when you want to get a structured response from the OpenAI API, such as the filmography of an actor.
1 | record ActorFilms(String actor, List<String> movies) {} |