spring-ai 第二提示词介绍

spring-ai 第二提示词介绍

提示词介绍

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

  • 入口链路

ChatModel -> call() -> Message

针对Message,角色分类如下

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

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

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

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

  • 整体结构图

提示模板

关键组件是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}
启动命令
测试结果

http://127.0.0.1:8082/ai/simple

http://127.0.0.1:8082/ai/prompt

相关推荐
蓦然回首却已人去楼空2 分钟前
【分词:中文分词】BPE字节级分词算法实现汉字表达!
java·算法·中文分词
asdfg12589639 分钟前
一文理解Java中的泛型
java·开发语言
飞翔中文网9 分钟前
Java学习笔记之反射
java·笔记·学习
河阿里11 分钟前
Spring Boot:整合Quartz集群部署指南
java·spring boot·后端
小肥君24 分钟前
gpu安装milvus问题解决
java·eureka·milvus
砍材农夫25 分钟前
物联网实战:Spring Boot MQTT | 模拟器Paho客户端拆解高性能
java·javascript·spring boot·后端·物联网·struts
电商API_1800790524733 分钟前
免 TOP 入驻,第三方淘宝商品详情 API 快速接入与代码示例
java·大数据·开发语言·数据库·爬虫·数据分析
IT空门:门主35 分钟前
Java AI 开发框架终极对比:Spring AI vs Spring AI Alibaba vs AgentScope-Java
java·人工智能·spring·spring ai·ai alibaba·agentscope-java
未若君雅裁1 小时前
多线程项目场景:CountDownLatch、Future、Semaphore
java
小科先生1 小时前
初学者安装java
java·开发语言