砍材农夫砍材农夫
  • 微信记账小程序
  • java
  • redis
  • mysql
  • 场景类
  • 框架类
  • vuepress搭建
  • hexo搭建
  • 云图
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI插件
  • AI集成框架
  • 相关算法
  • AI训练师
  • 量化交易
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • 思
  • 微信记账小程序
  • java
  • redis
  • mysql
  • 场景类
  • 框架类
  • vuepress搭建
  • hexo搭建
  • 云图
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI插件
  • AI集成框架
  • 相关算法
  • AI训练师
  • 量化交易
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • 思
  • 首页

    • 介绍
    • 框架比较
  • spring-ai

    • spring-ai 第一步入门
    • spring-ai 第二提示词介绍
    • spring-ai 第三结构化输出
    • spring-ai 第四多模态API
    • spring-ai 第五模型介绍
    • spring-ai 第六模型介绍-聊天
    • spring-ai 第七模型介绍-Embeddings
  • spring-alibaba-ai

    • 搭建demo
    • 相关概念
    • demo搭建springAIAlibaba
  • AgentScopeJava
    • 搭建demo
    • demo搭建springAIAlibaba
  • langchain

    • v1.0

      • 如何升级版本
      • 环境搭建
      • 架构结构
      • 模型
      • langchain核心
      • langchain-model
      • langchain提示词模板
      • langchain语言模型
      • 调用本地大模型
      • langChain中Chain链使用
      • langChain流程编排
      • langChain服务部署与链路监控
      • langChain消息管理与聊天历史存储
      • langchain_loader
      • langchain中RAG
      • langchain中Agent
  • dify

    • dify入门和安装
    • dify模型配置和验证
  • n8n

    • n8n入门和安装
  • coze

    • coze入门和安装
  • 提示词介绍
  • 针对Message,角色分类如下
  • 提示模板
    • demo(Prompt来构建SystemPromptTemplate实例)
    • 加载外部提示词模板
  • 整体demo
    • 源码
    • 模型(智谱)
    • 启动命令
    • 测试结果

提示词介绍

提示词(Prompt)是引导AI模型生成特定输出的输入 随着时间的推移,提示由只是简单的字符串发展为占位符

  • 入口链路

ChatModel -> call() -> Message

针对Message,角色分类如下

  • “USER:” 用户角色:代表用户输入一一他们的询问、指令或对AI的陈述

  • “ASSISTANT:” 助手角色:AI 对用户输入的回应,不仅仅是回答或反应,保持对话的连贯性至关重要

  • “SYSTEM:” 系统角色:指导AI的行为和响应风格,设定AI解释和回复输入的参数或规则

  • TOOL:” 工具/功能角色:工具/功能角色专注于在响应工具调用助手消息时返回附加信息

  • 整体结构图 Image text

提示模板

关键组件是PromptTemplate类,主要为了创建结构化的提示,将这些提示发送到AI模型进行处理

  • 通过StTemplateRenderer 进行渲染

demo(Prompt来构建SystemPromptTemplate实例)

String userText = """
    Tell me about three famous pirates from the Golden Age of Piracy and why they did.
    Write at least a sentence for each pirate.
    """;

Message userMessage = new UserMessage(userText);

String systemText = """
  You are a helpful AI assistant that helps people find information.
  Your name is {name}
  You should reply to the user's request with your name and also in the style of a {voice}.
  """;

SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));

Prompt prompt = new Prompt(List.of(userMessage, systemMessage));

List<Generation> response = chatModel.call(prompt).getResults();

加载外部提示词模板

@Value("classpath:/prompts/system-message.st")
private Resource systemResource;

SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);

整体demo

源码

https://gitee.com/kcnf_open/spring-ai-sample/tree/master/spring-ai/spring-ai-sample02

模型(智谱)

充值前 还得人脸识别(坑),随便一点充值

https://open.bigmodel.cn/

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-zhipuai</artifactId>
</dependency>
# In application.yml
spring:
  ai:
    zhipuai:
      api-key: ${ZHIPUAI_API_KEY}

启动命令

Image text

测试结果

http://127.0.0.1:8082/ai/simple

Image text

http://127.0.0.1:8082/ai/prompt

Image text

最近更新: 2026/3/25 23:49
Contributors: kcnf
Prev
spring-ai 第一步入门
Next
spring-ai 第三结构化输出