Spring AI提示词模板如何使用
本文重点介绍Prompt提示词和PromptTemplate提示词模板,在Spring AI框架里,Prompt类的设计旨在简化与LLM之间的交互过程,同时提供了足够的灵活性来满足不同类型的对话需求。通过合理利用Prompt及其内部的消息结构,开发者可以有效地引导LLM生成高质量的回答,提升用户体验。
1、Spring Boot集成Spring AI框架
如何基于Springboot集成Spring AI框架,并调用阿里云AI服务,请参考“Spring AI Alibaba入门示例Hello World”文章,这里不再赘述。https://lowcode.blog.csdn.net/article/details/145259344
提示词模板PromptTemplate用法
在Spring AI框架中,Prompt类用于构建和管理与大型语言模型(LLM)交互时所需的提示词(prompt)。通过创建一个Prompt对象,并向其中添加用户消息(userMessage)和系统消息(systemMessage),你可以定义与LLM对话的上下文和规则。具体来说,Prompt prompt = new Prompt(List.of(userMessage, systemMessage));这种提示词模板的用法有如下好处:
1)多轮对话支持
- List.of(userMessage, systemMessage)构建了一个包含多个消息项的不可变列表。每个消息项代表对话中的一个回合,可以是用户输入或系统响应。
- 通过这种方式,开发者可以在一次请求中传递多条历史对话记录给LLM,帮助其更好地理解当前对话的背景信息,从而生成更连贯、更准确的回答。
2)角色区分
- 在多轮对话中,明确区分哪些是用户发出的消息(userMessage),哪些是由系统(如AI助手)产生的消息(systemMessage),有助于LLM正确地模拟对话角色,提供更加自然的交互体验。
- 这种做法对于实现复杂的对话逻辑特别有用,比如客服机器人、虚拟助手等应用场景。
3)上下文设定
- systemMessage可以用来设置对话的整体语境或规则,例如指定回答风格、领域知识范围等。这对于确保LLM生成的内容符合特定要求非常重要。
- 例如,如果希望LLM以专业且礼貌的方式回应,则可以在systemMessage中加入相应的指示。
4)动态调整
- 使用List.of()构造函数使得每次构建Prompt对象时都可以灵活地传入不同的消息组合,便于根据实际需求动态调整对话内容。
- 开发者可以根据用户的实时反馈或其他条件变化,适时更新对话上下文,使LLM能够适应不断变化的对话场景。
3、开发代码使用PromptTemplate
在springboot项目中,创建一个普通 Controller Bean类,并使用PromptTemplate提示词模板技术跟AI大模型进行对话 。
package com.yuncheng;
import java.util.List;
import java.util.Map;
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.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ai")
public class PromptTemplateController {
private final ChatClient chatClient;
public PromptTemplateController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/prompt1")
public AssistantMessage completion1(@RequestParam(value = "topic", defaultValue = "西游记") String topic) {
PromptTemplate promptTemplate = new PromptTemplate("请给我讲一个关于{topic}主题的故事");
Prompt prompt = promptTemplate.create(Map.of( "topic", topic));
return chatClient.prompt(prompt)
.call()
.chatResponse()
.getResult()
.getOutput();
}
@GetMapping("/prompt2")
public AssistantMessage completion2(
@RequestParam(value = "topic", defaultValue = "西游记") String topic,
@RequestParam(value = "voice", defaultValue = "诙谐") String voice
) {
String userText = "请给我讲一个关于{topic}主题的故事";
PromptTemplate promptTemplate = new PromptTemplate(userText);
Message userMessage = promptTemplate.createMessage(Map.of("topic", topic));
String systemText = "你是一个擅长讲中国古典故事的高手,请你用 {voice} 的语言风格回复用户的请求。";
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("voice", voice));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
return chatClient.prompt(prompt)
.call()
.chatResponse()
.getResult()
.getOutput();
}
}
4、启动Springboot工程并验证
工程启动成功后,可以向大模型提问了。
简单的提示词验证,在浏览器地址栏输入:
http://localhost:8080/ai/prompt1?topic=红楼梦
AI大模型回复:
{
"messageType": "ASSISTANT",
"metadata": {
"finishReason": "STOP",
"id": "545da850-4e9a-99b0-90b2-1fa3fe1b4a95",
"role": "ASSISTANT",
"messageType": "ASSISTANT"
},
"toolCalls": [],
"content": "故事内容....."
}
多个角色消息构成的提示词验证,在浏览器地址栏输入:
http://localhost:8080/ai/prompt2?topic=水浒传&voice=幽默
AI大模型回复:
{
"messageType": "ASSISTANT",
"metadata": {
"finishReason": "STOP",
"id": "0b639385-f4d8-9a45-abbe-8461b08f7d05",
"role": "ASSISTANT",
"messageType": "ASSISTANT"
},
"toolCalls": [],
"content": "故事内容....."
}

浙公网安备 33010602011771号