Spring AI 系列之二十八 - Spring AI Alibaba-基于Nacos的prompt模版

发布于:2025-08-01 ⋅ 阅读:(13) ⋅ 点赞:(0)

之前做个几个大模型的应用,都是使用Python语言,后来有一个项目使用了Java,并使用了Spring AI框架。随着Spring AI不断地完善,最近它发布了1.0正式版,意味着它已经能很好的作为企业级生产环境的使用。对于Java开发者来说真是一个福音,其功能已经能满足基于大模型开发企业级应用。借着这次机会,给大家分享一下Spring AI框架。

注意由于框架不同版本改造会有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19
代码参考: https://github.com/forever1986/springai-study

上一章演示了Spring AI Alibaba的基本入门,本章将演示基于Nacos的动态prompt模版。基于Nacos的prompt是Spring AI Alibaba对prompt模版的扩展功能,在实际企业级应用中可能对于提示词的管理需要一个统一的地方,因此采用Nacos统一进行版本管理和发布是一个比较好的实践。本章就通过示例来演示如何基于Nacos管理prompt模版。

1 示例演示

代码参考lesson24子模块下的ali-prompt子模块

示例说明:通过Nacos动态加载提示词模版,从而使得无需启动服务器可以调试或者修改prompt模版

1.1 前提准备

1)需要准备一个nacos,这里使用版本是3.0.2

在这里插入图片描述

注意:Nacos3的控制台端口默认也是8080,因此自己可以在配置文件中修改或者演示项目ali-prompt改一下端口,避免端口冲突

2)在Nacos下面配置一个prompt的配置文件

在Nacos的public命名空间下,创建一个名字为:spring.ai.alibaba.configurable.prompt 其group为:DEFAULT_GROUP

在这里插入图片描述

[
  {
    "name": "author",
    "template": "请介绍 {author} 的生平简介",
    "model": {
      "author": "余华"
    }
  }
]

说明:这里解释一下参数:
1)name:就是提示词模版的名称,也是代码中使用它来决定加载哪个模版
2)template:提示词模版
3)model:参数,可以用于提示词模版里面

1.2 代码实现

1)在lesson24子模块下,新建ali-prompt子模块,其pom引入如下:

<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>com.alibaba.cloud.ai</groupId>
        <artifactId>spring-ai-alibaba-starter-nacos-prompt</artifactId>
    </dependency>
</dependencies>

2)新建application.properties配置文件

spring.ai.dashscope.api-key=你的阿里百炼API KEY

spring.config.import=nacos:prompt-application.properties
spring.nacos.serverAddr=localhost:8848
spring.nacos.username=nacos
spring.nacos.password=nacos

# 开启 nacos 的 prompt tmpl 监听功能
spring.ai.nacos.prompt.template.enabled=true

3)新建DemoController演示类:

import com.alibaba.cloud.ai.prompt.ConfigurablePromptTemplate;
import com.alibaba.cloud.ai.prompt.ConfigurablePromptTemplateFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.util.StringUtils;
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.Map;

@RestController
public class DemoController {

    private final ChatClient dashScopeChatClient;
    private final ConfigurablePromptTemplateFactory promptTemplateFactory;


    public DemoController(ChatClient.Builder chatClientBuilder, ConfigurablePromptTemplateFactory promptTemplateFactory) {
        this.dashScopeChatClient = chatClientBuilder.build();
        this.promptTemplateFactory = promptTemplateFactory;
    }

    @GetMapping(value = "/ai/generate", produces = "text/html;charset=UTF-8")
    public Flux<String> generate(@RequestParam(value = "authorName", required = false, defaultValue = "") String authorName) {
        // 使用 nacos 的 prompt tmpl 创建 prompt
        ConfigurablePromptTemplate template = promptTemplateFactory.create(
                // 配置的模版name
                "author",
                // 如果找不到,会使用这个默认模版
                "请列出这位{author}最著名的三本书。"
        );
        Prompt prompt = null;
        if(!StringUtils.hasText(authorName)){
            // 但参数为空时,使用nacos默认的author关键字
            prompt = template.create();
        }else{
            prompt = template.create(Map.of("author", authorName));
        }
        return dashScopeChatClient.prompt(prompt).stream().content();
    }

}

4)新建启动Lesson24PromptApplication类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Lesson24PromptApplication {

    public static void main(String[] args) {
        SpringApplication.run(Lesson24PromptApplication.class, args);
    }

}

1.3 演示

1)不传参访问

http://localhost:8080/ai/generate

在这里插入图片描述

说明:会默认读取Nacos的模版

2)传参访问

http://localhost:8080/ai/generate?authorName=鲁迅

在这里插入图片描述

说明:通过传参方式,结合Nacos的prompt模版返回的结果

3)修改Nacos配置

在这里插入图片描述

4)再次访问

http://localhost:8080/ai/generate?authorName=鲁迅

在这里插入图片描述

说明:可以看到新的prompt模版已经生效,这样就实现了动态修改模版

2 底层原理

下面来看看其底层原理

1)可以看到自动化配置了ConfigurablePromptTemplateFactory类

在这里插入图片描述

2)在演示类DemoController中也可以看到是通过注册一个ConfigurablePromptTemplateFactory类,并从中获取到nacos配置的提示词模版,下面是关键源码

在这里插入图片描述

说明:ConfigurablePromptTemplateFactory类源码比较关键的2个点
1)@NacosConfigListener,其监听了group为DEFAULT_GROUP,名称为:spring.ai.alibaba.configurable.prompt 的配置作为提示词配置
2)Nacos的配置会解析为ConfigurablePromptTemplateModel类,里面有name、template和model三个参数

结语:本章通过演示Spring AI Alibaba-基于Nacos的prompt模版,并简单解析了其实现原理。下一章将讲解Spring AI Alibaba对于聊天记忆的扩展。

Spring AI系列上一章:《Spring AI 系列之二十七 - Spring AI Alibaba-入门

Spring AI系列下一章:《Spring AI 系列之二十九 - Spring AI Alibaba-聊天记忆


网站公告

今日签到

点亮在社区的每一天
去签到