Spring AI Alibaba 使用 Nacos 的配置中心能力来动态管理 AI 应用的 Prompt。以此来实现动态更新 Prompt 的功能。
环境准备
Nacos: 具备配置中心能力的 Nacos,本例中使用 Nacos 3.0.2。Nacos 2.X 亦可,
spring-ai版本1.0.0 ,spring-ai-alibaba版本1.0.0.3 ,jdk17
https://github.com/TalkIsCheapGiveMeMoney/spring-ai-alibaba-examples/tree/main/spring-ai-alibaba-nacos-prompt-exampleExample 工程地址:https://github.com/TalkIsCheapGiveMeMoney/spring-ai-alibaba-examples/tree/main/spring-ai-alibaba-nacos-prompt-example
Pom.xml
Tips: 项目中已经引入了 Spring AI Alibaba Bom 和 Spring Boot Bom。因此这里省略了版本号。有关 bom 定义参考如上的 Github 仓库地址。
<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>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-nacos-prompt</artifactId>
</dependency>
Application.yml
在配置文件中加入 Nacos 监听的 DataID 以及 Nacos Server 的用户名和密码等信息。
# Copyright 2024-2025 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.server:port: 10010spring:application:name: spring-ai-alibaba-nacos-prompt-example# 指定监听的 prompt 配置config:import:- "optional:nacos:prompt-config.json"nacos:namespace: 8279be91-ac4e-465b-a87a-bbaa1fd66d26server-addr: 127.0.0.1:8848username: nacospassword: nacosai:# 开启 nacos 的 prompt tmpl 监听功能nacos:prompt:template:enabled: true
Controller
/** Copyright 2024 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package com.alibaba.cloud.ai.example.nacos.controller;import java.util.Map;import com.alibaba.cloud.ai.prompt.ConfigurablePromptTemplate;
import com.alibaba.cloud.ai.prompt.ConfigurablePromptTemplateFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import reactor.core.publisher.Flux;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
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;/*** @author : huangzhen*/@RestController
@RequestMapping("/nacos")
public class PromptController {private static final Logger logger = org.slf4j.LoggerFactory.getLogger(PromptController.class);private final ChatClient client;private final ConfigurablePromptTemplateFactory promptTemplateFactory;public PromptController(ChatModel chatModel,ConfigurablePromptTemplateFactory promptTemplateFactory) {this.client = ChatClient.builder(chatModel).build();this.promptTemplateFactory = promptTemplateFactory;}@GetMapping("/books")public Flux<String> generateJoke(@RequestParam(value = "author", required = false, defaultValue = "鲁迅") String authorName,HttpServletResponse response) {// 防止输出乱码response.setCharacterEncoding("UTF-8");// 使用 nacos 的 prompt tmpl 创建 promptConfigurablePromptTemplate template = promptTemplateFactory.create("author","please list the three most famous books by this {author}.");Prompt prompt = template.create(Map.of("author", authorName));logger.info("最终构建的 prompt 为:{}", prompt.getContents());return client.prompt(prompt).stream().content();}}
Nacos 配置添加
-
启动 Nacos 服务;
-
写入配置,dataId 为:spring.ai.alibaba.configurable.prompt
-
在配置中写入如下配置:
[{"name":"author","template":"列出 {author} 有名的著作","model":{"key":"余华"}}
]
功能演示
完成上述配置之后,启动项目:
-
在启动日志中,可以看到如下输出,表明已经开始监听此 DataID 的配置:
2025-07-30T09:25:52.794+08:00 INFO 25312 --- [spring-ai-alibaba-nacos-prompt-example] [ main] .c.a.p.ConfigurablePromptTemplateFactory : OnPromptTemplateConfigChange,templateName:author,template:列出 {author} 有名的著作,model:{key=余华}
发送请求查看输出:
Tips: 这里输出了鲁迅的作品集是因为在 controller 中设置了 defaultValue 为鲁迅.
查看控制台输出:
2025-07-30T09:27:27.937+08:00 INFO 25312 --- [spring-ai-alibaba-nacos-prompt-example] [io-10010-exec-1] c.a.c.a.e.n.controller.PromptController : 最终构建的 prompt 为:列出 鲁迅 有名的著作
动态更新 Nacos 的 Prompt 配置,再次查看请求查看效果
Tips: 因为 controller 中设置了 defaultValue 为鲁迅,因此 Prompt 变更仍然和文学作家相关。
变更 Prompt 为:
[{"name": "author","template": "介绍 {author},列出其生平经历和文学成就","model": {"key": "余华"}}
]
点击发布之后,看到控制台输出如下,证明变更成功:
2025-07-30T09:28:38.227+08:00 INFO 25312 --- [spring-ai-alibaba-nacos-prompt-example] [listener.task-0] .c.a.p.ConfigurablePromptTemplateFactory : OnPromptTemplateConfigChange,templateName:author,template:介绍 {author},列出其生平经历和文学成就,model:{key=余华}
再次发送请求:
最终构建的 Prompt 为:
2025-07-30T09:28:44.736+08:00 INFO 25312 --- [spring-ai-alibaba-nacos-prompt-example] [io-10010-exec-5] c.a.c.a.e.n.controller.PromptController : 最终构建的 prompt 为:介绍 鲁迅,列出其生平经历和文学成就