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

相关推荐
呱牛do it1 小时前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 3)
java·vue
神の愛2 小时前
左连接查询数据 left join
java·服务器·前端
南境十里·墨染春水2 小时前
linux学习进展 线程同步——互斥锁
java·linux·学习
雨奔3 小时前
Kubernetes 联邦 Deployment 指南:跨集群统一管理 Pod
java·容器·kubernetes
杨凯凡3 小时前
【021】反射与注解:Spring 里背后的影子
java·后端·spring
lulu12165440783 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
riNt PTIP3 小时前
SpringBoot创建动态定时任务的几种方式
java·spring boot·spring
老星*3 小时前
AI选股核心设计思路
java·ai·开源·软件开发
それども4 小时前
Comparator.comparing 和 拆箱问题
java·jvm
星晨羽4 小时前
西门子机床opc ua协议实现变量读写及NC文件上传下载
java·spring boot