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 中集成各大模型,并实现高度可配置的系统提示词管理。

相关推荐
愿你天黑有灯下雨有伞3 小时前
Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
java·spring boot·spring
KNeeg_5 小时前
Spring循环依赖以及三个级别缓存
java·spring·缓存
AI_Gump6 小时前
【AI阅读】20250717阅读输入
java·spring boot·spring
ldj20206 小时前
Docker的docker-compose类比Spring的ApplicationContext
spring·docker
懂得节能嘛.8 小时前
【SpringAI实战】ChatPDF实现RAG知识库
java·后端·spring
探索java8 小时前
Spring 解析 XML 配置文件的过程(从读取 XML 到生成 BeanDefinition)
xml·java·spring·xmlbeanfactory
daixin884810 小时前
SpringMVC的请求执行流程是什么样的?
java·开发语言·spring
Pigwantofly11 小时前
SpringAI入门及浅实践,实战 Spring‎ AI 调用大模型、提示词工程、对话记忆、Adv‎isor 的使用
java·大数据·人工智能·spring
追风少年浪子彦12 小时前
mybatis-plus实体类主键生成策略
java·数据库·spring·mybatis·mybatis-plus
java叶新东老师15 小时前
spring gateway 配置http和websocket路由转发规则
spring·http·gateway