← Back to list

Getting Started with Spring AI: A Beginner’s Guide to Chat Model, Prompts, and Responses

In this article, we’ll explore the Chat Model interface in Spring AI and break down its key components — such as messages, prompts, and…

QA-init · 2025-08-29 00:41 · 3 claps · 3.3 min read
#spring-ai #openai-api #chat-model #chat-api
Open on Medium ↗
Wiki topics: LLM · Large Language Models

Getting Started with Spring AI: A Beginner’s Guide to Chat Model, Prompts, and Responses

In this article, we’ll explore the Chat Model interface in Spring AI and break down its key components — such as messages, prompts, and responses. We’ll also talk about how we should approach the official Spring AI documentation to build a solid understanding of this model.

https://docs.spring.io/spring-ai/reference/api/chatmodel.html

https://docs.spring.io/spring-ai/reference/api/chatmodel.html

What is Spring AI?

Spring AI is an extension of the Spring ecosystem that allows us to easily connect with AI models (like OpenAI, Azure OpenAI, etc.). It provides:

  • Abstraction: Instead of calling APIs directly, we are using Spring’s familiar interfaces.
  • Flexibility: Supports multiple AI providers.
  • Integration: Plays nicely with existing Spring Boot projects.

For developers new to AI, Spring AI feels natural because it uses concepts we already know — interfaces, beans, and configuration properties.

Official Documentation:

Below link is the reference to the chat model API under SpringAI project.

[embed]Chat Model API The Chat Model API offers developers the ability to integrate AI-powered chat completion capabilities into their…docs.spring.io

The ChatModel API

At the core of Spring AI’s conversational capabilities is the ChatModel interface. Think of it as the contract for how a user can send a request to an AI model and receive a response.

A typical flow looks like this:

  1. Create a message or prompt
  2. Send it to the ChatModel
  3. Receive a response

This makes working with AI conversational models as simple as working with a REST client in Spring.

The Chat Model API lets developers add AI chat features to their apps. It uses smart language models like GPT to create responses that sound like a person is talking. When someone types a message, the API sends it to the model. The model then uses what it has learned to come up with a reply that fits naturally. Once the reply is ready, the app gets it and can show it to the user or use it for more tasks.

Big picture of the chat model api

Big picture of the chat model api

Key Components

1. ChatModel

A core interface for interacting with AI-powered chat models.

👉 Example:

public interface ChatModel extends Model<Prompt, ChatResponse>, StreamingChatModel {
    default String call(String message) { ... }
    ChatResponse call(Prompt prompt);
}

What it does:

  • The call(String message) method offers a quick, plain-text API for simple use.
  • The call(Prompt prompt) method provides deeper control and customization via structured prompts.
  • If streaming responses are needed (completing a response in parts), use methods inherited from StreamingChatModel like stream(Prompt)

2. Messages

Messages are the building blocks of conversations. In a chat system, a message might come from either the user or the assistant (the AI).

Spring AI models this through classes like:

  • UserMessage – Represents a user’s input.
  • AssistantMessage – Represents the model’s output.
  • SystemMessage – Provides instructions to guide the model’s behavior.

They inform the model who is speaking and what role each message plays in the conversation

3. Prompts

A prompt represents the input a user sends to the AI model.

Can contain:

  • Multiple messages, categorized by role (system, user, tool, etc.).
  • Optional ChatOptions to control behavior like model selection, response format, temperature, and more reference here.
  • Think of Prompt as the full context: conversation setup, instruction tone, and any configuration tweaks all bundled together.

👉 Example:

Prompt prompt = new Prompt(
    new SystemMessage("You are a helpful Java assistant."),
    new UserMessage("What is Spring AI?")
);

Prompts are powerful because they let control how the AI responds by including system instructions along with user input.

4. Chart Options

Optional settings attached to a Prompt.

Examples include:

  • Selecting a specific model (e.g., GPT-4 or Anthropic Claude).
  • Adjusting temperature for creativity or determinism.
  • Limiting response length with max tokens.
  • Specifying response formats, like JSON schema compliance
  • Options can be configured programmatically via a builder (e.g., OpenAiChatOptions.builder()…) or through application.properties. [More reference](https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html?utm_source=chatgpt.com)
  • Example of openAI chat options is provided below

https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html

https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html

5. Responses

Once a prompt is sent to the ChatModel, we will get back a ChatResponse. This response contains:

  • The Assistant’s reply (text, structured data, etc.)
  • Metadata (like token usage, finish reason, etc.)

👉 Example:

ChatResponse response = chatModel.call(prompt);
String reply = response.getResult().getOutput().getText();
System.out.println(reply);

In summary:

openapi, #chatmodelapi, #springai


메타데이터
post_id
3fe19c1b3437
slug
getting-started-with-spring-ai-a-beginners-guide-to-chat-model-prompts-and-responses-3fe19c1b3437
url
https://medium.com/@QA-initi/getting-started-with-spring-ai-a-beginners-guide-to-chat-model-prompts-and-responses-3fe19c1b3437
canonical_url
https://medium.com/@QA-initi/getting-started-with-spring-ai-a-beginners-guide-to-chat-model-prompts-and-responses-3fe19c1b3437
author_url
https://medium.com/@QA-initi
status
ok
fetched_at
2026-07-17 21:46:37