一、提示词是什么?
Prompt(提示词) 是你与大模型(如通义千问、ChatGPT等)交互时的输入指令或问题。它就像你给模型下达的任务说明书,告诉模型你需要它做什么、以什么方式回答。
简单来说:
-
用户:写Prompt
-
模型:根据Prompt生成回复
二、Prompt中的四大角色
在Spring AI Alibaba中,Prompt由多个Message组成,每个Message都有不同的角色(Role)。主要有四大角色:
2.1 System Message(系统消息)
-
作用:给模型设定全局行为准则,如身份、语气、约束条件
-
优先级:最高,模型会严格遵守
-
示例
你是一个专业的数学老师,用通俗易懂的方式解释概念
2.2 User Message(用户消息)
-
作用:代表当前用户的输入,即真正的问题或指令
-
特点:对话中的主要部分
-
示例:
请解释一下勾股定理
2.3 Assistant Message(助手消息)
-
作用:代表模型的历史回复,用于多轮对话
-
用途:让模型"记住"之前的对话内容
-
示例:
勾股定理是指直角三角形两条直角边的平方和等于斜边的平方
2.4 Tool/Function Message(工具消息)
-
作用:代表外部工具调用的结果,如查询数据库、调用API
-
用途:让模型能使用外部工具获取信息
-
示例:
查询到北京今天的天气是晴天,温度25℃
三、四大角色的关系图
系统消息(System) ──→ 设定全局规则
↓
用户消息(User) ────→ 提出问题
↓
大模型
↓
助手消息(Assistant) ─→ 生成回复
↓
工具消息(Tool) ←──→ 调用外部工具(如果需要)
四、demo示例
4.1 配置类
package com.wx.config;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description
* @author: wangxin
* @date: 2026/3/11 22:11
*/
@Configuration
public class LLMConfig {
@Bean
public DashScopeApi dashScopeApi() {
return DashScopeApi.builder().apiKey(System.getenv("aliQwen-api")).build();
}
@Bean
public ChatClient chatClient(ChatModel dashscopeChatModel)
{
return ChatClient.builder(dashscopeChatModel).build();
}
}
4.2 配置文件
server:
port: 8004
servlet:
encoding:
enabled: true
force: true
charset: utf-8
spring:
application:
name: demo4
# ====ollama Config=============
ai:
dashscope:
api-key: ${aliQwen-api}
4.3 controller
package com.wx.controller;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Description
* @author: wangxin
* @date: 2026/3/7 20:42
*/
@RestController
public class ChatClientController {
@Resource(name = "dashscopeChatModel")
private ChatModel chatModel;
@GetMapping("/math-tutor")
public String mathTutor() {
// 1. System Message:设定AI角色
SystemMessage systemMessage = new SystemMessage(
"你是一个初中数学老师,说话要耐心、易懂,用生活化的例子解释概念"
);
// 2. User Message:学生提问
UserMessage userMessage = new UserMessage(
"老师,我不太明白什么是负数,能给我举个生活中的例子吗?"
);
// 3. 构建Prompt,包含多个Message
List<Message> messages = List.of(systemMessage, userMessage);
Prompt prompt = new Prompt(messages);
// 4. 调用模型
ChatResponse response = chatModel.call(prompt);
String answer = response.getResult().getOutput().getText();
// 5. 将模型的回复作为Assistant Message保存(用于多轮对话)
AssistantMessage assistantMessage = new AssistantMessage(answer);
return answer;
}
}
4.4 测试

五、提示词模版Prompt Template
5.1 简单使用
package com.wx.controller;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.util.List;
import java.util.Map;
/**
* @Description
* @author: wangxin
* @date: 2026/3/7 20:42
*/
@RestController
public class ChatClientController {
@Resource(name = "dashscopeChatModel")
private ChatModel chatModel;
@Resource
private ChatClient chatClient;
@GetMapping("/prompt/chat")
public Flux<String> chat(String topic, String output_format, String wordCount)
{
PromptTemplate promptTemplate = new PromptTemplate("" +
"讲一个关于{topic}的故事" +
"并以{output_format}格式输出," +
"字数在{wordCount}左右");
// PromptTempate -> Prompt
Prompt prompt = promptTemplate.create(Map.of(
"topic", topic,
"output_format",output_format,
"wordCount",wordCount));
return chatClient.prompt(prompt).stream().content();
}
}

5.2 读取文件方式
新建txt文件放在resources目录下
讲一个关于{topic}的故事,并以{output_format}格式输出,字数在{wordCount}左右
测试:
@GetMapping("/prompt/chat1")
public Flux<String> chat1(String topic, String output_format, String wordCount)
{
PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
// PromptTempate -> Prompt
Prompt prompt = promptTemplate.create(Map.of(
"topic", topic,
"output_format",output_format,
"wordCount",wordCount));
return chatClient.prompt(prompt).stream().content();
}

六、实际应用场景
七、总结

核心要点:
System Message 最重要,决定了AI的"人设"
多轮对话需要保存User和Assistant的历史消息
在Spring AI中通过Prompt对象组合多个Message
使用PromptTemplate可以更方便地构建复杂Prompt





