spring ai alibaba 使用 SystemPromptTemplate 很方便的集成 系统提示词

系统提示词可以是.st 文件了,便于修改和维护

1提示词内容:

st 复制代码
你是一个有用的AI助手。
你是一个帮助人们查找信息的人工智能助手。
您的名字是{name}
你应该用你的名字和{voice}的风格回复用户的请求。
每一次回答的时候都要增加一个65字以内的标题形如:【这是标题】
然后在展开回答用户的问题

2赋值方式

java 复制代码
    @Value("classpath:/prompts/system-message.st")
    private Resource systemResource;
    @PostMapping(value = "/askQuestion" ,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> streamForSystemMessage(@RequestBody
      QuestionVO questionVO, HttpServletRequest request
    ) {
         String prompt=questionVO.getPrompt();
        String platform=questionVO.getPlatform();
           UserMessage userMessage = new UserMessage(prompt);
            SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
            //这里是赋值 也可以从前端页面传递过来
            Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "张三", "voice", "鲁迅"));
            return  ChatClient.builder(dashScopeChatModel).build().prompt(new Prompt(List.of(userMessage,systemMessage))).stream().content()
                    .map(content -> {
                        stringBuffer.append(content);
                        System.out.println(stringBuffer.toString());
                        JSONObject  jsonObject=new  JSONObject();
                        jsonObject.put("content",content);
                        String jobject=jsonObject.toString();
                        return ServerSentEvent.builder(jobject).event("message").build();
                    })
                    //问题回答结速标识,以便前端消息展示处理
                    .concatWithValues(ServerSentEvent.builder("[DONE]").build())
                    .onErrorResume(e -> Flux.just(ServerSentEvent.builder("Error: " +e.getMessage()).event("error").build()));
 }   

解释: Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "张三", "voice", "鲁迅")); 张三和 鲁迅都可以从页面传递过来

3 展示结果

4:ollama openAI qwen deepseek 等大模型都支持

5、总结

  1. 核心步骤

    • 使用 SystemPromptTemplate 直接加载 .st 模板文件。
  2. 优势

    • 代码简洁:无需手动解析模板,直接使用 Spring AI 提供的工具类。
    • 灵活性高:支持动态变量、条件逻辑和多模板管理。
    • 维护方便:模板与代码分离,修改提示词无需重新编译。
  3. 注意事项

    • 变量名需与模板中的占位符一致(如 {name} 对应 `Map.of("name", "张三", "voice", "鲁迅"))。

通过这种方式,可以高效地在 Spring AI 中集成各大模型,并实现高度可配置的系统提示词管理。

相关推荐
码界奇点6 分钟前
基于Spring MVC与AngularJS的API接口管理系统设计与实现
spring·毕业设计·yapi·mvc·angular.js·源代码管理
梵得儿SHI13 分钟前
SpringCloud 核心组件精讲:OpenFeign 实战指南-服务调用优雅实现方案(含自定义拦截器、超时重试、LoadBalance 整合避坑)
spring boot·spring·spring cloud·负载均衡·openfeign的核心应用·微服务调用·熔断组件
YDS8291 小时前
SpringCloud —— 配置管理
java·spring·spring cloud
长征coder1 小时前
SpringCloud服务优雅下线LoadBalancer 缓存配置方案
java·后端·spring
没有bug.的程序员1 小时前
微服务的本质:不是拆服务,而是拆复杂度
java·jvm·spring·微服务·云原生·容器·架构
程序员阿明1 小时前
spring boot 3集成spring security6
spring boot·后端·spring
后端小张1 小时前
【JAVA 进阶】深入拆解SpringBoot自动配置:从原理到实战的完整指南
java·开发语言·spring boot·后端·spring·spring cloud·springboot
大佐不会说日语~12 小时前
Spring AI Alibaba 的 ChatClient 工具注册与 Function Calling 实践
人工智能·spring boot·python·spring·封装·spring ai
serendipity_hky13 小时前
【SpringCloud | 第5篇】Seata分布式事务
分布式·后端·spring·spring cloud·seata·openfeign
Honmaple14 小时前
Spring AI 2.x 发布:全面拥抱 Java 21,Redis 史诗级增强
java·人工智能·spring