Unlocking the New Era of AI Development: Exploring Spring AI Core Classes

This article walks through Spring AI’s three core classes—Message, Prompt, and ChatModel—explaining their roles, showing concrete code examples for constructing messages, building prompts, and invoking a large language model via a REST controller, and provides a complete demo repository.

Coder Circle
Coder Circle
Coder Circle
Unlocking the New Era of AI Development: Exploring Spring AI Core Classes

Message (org.springframework.ai.chat.messages.Message)

Message represents the content sent to a large language model. Four MessageType variants exist: UserMessage (user input), SystemMessage (system instructions), AssistantMessage (model’s previous reply), and ToolMessage (tool output).

UserMessage userMessage = new UserMessage(message);
SystemMessage systemMessage = new SystemMessage(sysMsg);

Prompt (org.springframework.ai.chat.prompt.Prompt)

Prompt encapsulates a complete model input and can contain multiple Message objects and optional ChatOptions such as temperature.

Prompt prompt = new Prompt(userMessage, systemMessage);

ChatModel (org.springframework.ai.chat.model.ChatModel)

All conversational LLMs are abstracted by the ChatModel interface, which defines two primary methods: call – synchronous interaction. stream – streaming interaction.

The concrete implementation OpenAiChatModel is injected into the Spring container to handle communication with the underlying model.

Demo REST controller

Example controller wires the components and performs a chat call:

@RestController
public class TestController {
    @Resource
    private OpenAiChatModel chatModel;

    @GetMapping(value = "/deepseek/chat/call2")
    public String call2(@RequestParam(value = "message") String message,
                       @RequestParam(value = "sysMsg") String sysMsg) {
        UserMessage userMessage = new UserMessage(message);
        SystemMessage systemMessage = new SystemMessage(sysMsg);
        Prompt prompt = new Prompt(userMessage, systemMessage);
        ChatResponse chatResponse = this.chatModel.call(prompt);
        return Optional.ofNullable(chatResponse)
            .map(ChatResponse::getResult)
            .map(Generation::getOutput)
            .map(AssistantMessage::getText)
            .orElse("");
    }
}

Repository: https://github.com/Why1214/spring-demo.git (branch: spring-ai)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaLLMOpenAISpring AIMessageChatModelPrompt
Coder Circle
Written by

Coder Circle

Limited experience, continuously learning and summarizing knowledge, aiming to join a top tech company.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.