一、说明
提示词在大模型使用中有着举足轻重的作用,好的提示词会让结果非常精准。今天要讲解的是spring-ai-alibaba中的提示词的使用。
另外,本章节统一使用之前的父工程,同时本章节使用的是chatClient(非下面文档中的chatModel)
二、官方文档
三、代码
代码结构:

POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.saa</groupId>
<artifactId>saa-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.saa</groupId>
<artifactId>saa-prompt-template</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>saa-prompt-template</name>
<description>saa-prompt-template</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>
</project>
YML文件:
spring:
application:
name: saa-prompt-template
ai:
dashscope:
api-key: sk-09c7b571687b46d5a2e25a03fbddxxxx
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
options:
model: qwen3-max
server:
port: 8084
servlet:
encoding:
enabled: true
force: true
charset: UTF-8
提示词模板文件:
讲一个关于{topic}的故事,并以{output_format}格式输出,字数在{wordCount}左右配置类:
package com.saa.prompt.template.config;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zhanxuewei
*/
@Configuration
public class SsaLLMConfig {
@Value("${spring.ai.dashscope.api-key}")
private String apiKey;
private static final String MODEL_DEEPSEEK = "deepseek-v3";
private static final String MODEL_QWEN = "qwen-max";
@Bean(name = "deepseek")
public ChatModel deepseek() {
return DashScopeChatModel.builder()
.dashScopeApi(DashScopeApi.builder().apiKey(apiKey).build())
.defaultOptions(DashScopeChatOptions.builder().withModel(MODEL_DEEPSEEK).build())
.build();
}
@Bean(name = "qwen")
public ChatModel qwen() {
return DashScopeChatModel.builder()
.dashScopeApi(DashScopeApi.builder().apiKey(apiKey).build())
.defaultOptions(DashScopeChatOptions.builder().withModel(MODEL_QWEN).build())
.build();
}
@Bean(name = "deepseekChatClient")
public ChatClient deepeekChatClient(@Qualifier("deepseek") ChatModel deepseek) {
return ChatClient.builder(deepseek)
.defaultOptions(ChatOptions.builder().model(MODEL_DEEPSEEK).build())
.build();
}
@Bean(name = "qwenChatClient")
public ChatClient qwenChatClient(@Qualifier("qwen") ChatModel qwen) {
return ChatClient.builder(qwen)
.defaultOptions(ChatOptions.builder().model(MODEL_QWEN).build())
.build();
}
}
控制器:
package com.saa.prompt.template.controller;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.beans.factory.annotation.Value;
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;
import reactor.core.publisher.Flux;
import java.util.List;
import java.util.Map;
@Slf4j
@RequestMapping("/prompt/template")
@RestController
public class PromptTemplateController {
@Resource(name = "deepseek")
private ChatModel deepseekChatModel;
@Resource(name = "qwen")
private ChatModel qwenChatModel;
@Resource(name = "deepseekChatClient")
private ChatClient deepseekChatClient;
@Resource(name = "qwenChatClient")
private ChatClient qwenChatClient;
@Value("classpath:/prompttemplate/story.txt")
private org.springframework.core.io.Resource userTemplate;
@GetMapping("/chat")
public Flux<String> chat(@RequestParam("topic") String topic,
@RequestParam("output_format") String output_format,
@RequestParam("wordCount") String wordCount) {
PromptTemplate promptTemplate = new PromptTemplate("讲一个关于{topic}的故事,并以{output_format}格式输出,字数在{wordCount}左右");
Prompt prompt = promptTemplate.create(Map.of(
"topic", topic,
"output_format", output_format,
"wordCount", wordCount
));
Flux<String> content = deepseekChatClient.prompt(prompt).stream().content();
return content;
}
@GetMapping("/chat2")
public Flux<String> chat2(@RequestParam("topic") String topic,
@RequestParam("output_format") String output_format,
@RequestParam("wordCount") String wordCount) {
PromptTemplate promptTemplate = new PromptTemplate(userTemplate);
Prompt prompt = promptTemplate.create(Map.of(
"topic", topic,
"output_format", output_format,
"wordCount", wordCount
));
Flux<String> content = deepseekChatClient.prompt(prompt).stream().content();
return content;
}
@GetMapping("/chat3")
public Flux<String> chat3(@RequestParam("systemTopic") String systemTopic,
@RequestParam("userTopic") String userTopic) {
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate("你是{systemTopic}助手,是回答{systemTopic},其它无可奉告,并以html格式返回结果");
Message systemPromptTemplateMessage = systemPromptTemplate.createMessage(Map.of("systemTopic", systemTopic));
PromptTemplate promptTemplate = new PromptTemplate("解释一下{userTopic}");
Message userMessage = promptTemplate.createMessage(Map.of("userTopic", userTopic));
Prompt prompt = new Prompt(List.of(systemPromptTemplateMessage, userMessage));
return deepseekChatClient.prompt(prompt).stream().content();
}
}
四、测试结果
