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 為:介紹 魯迅,列出其生平經歷和文學成就