Spring AI教程(二)Chat API之Prompts模板语法

2024-04-23 01:04

本文主要是介绍Spring AI教程(二)Chat API之Prompts模板语法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Prompts 模板语法

 上节我们介绍了提示词工程,并通过设定SystemMessage获得了一个会骂人的AI。而本节介绍的内容仍然与提示词有关。

 Spring AI为我们提供了提示词模板,允许我们通过一些模板,快速地动态生成提示词并发起提问。除此之外,我们还能使用Spring AI为我们提供的输出解析器将AI回复的内容解析为Bean对象。

5.1 PromptTemplate

PromptTemplate能够帮助我们创建结构化提示词,是Spring AI提示词工程中的关键组件,该类实现了三个接口:PromptTemplateStringActionsPromptTemplateActionsPromptTemplateMessageActions,这些接口的主要功能也有所不同:

  • PromptTemplateStringActions: 主要用于创建和渲染提示词字符串,接口的返回值类型均是String类型,这是提示词的基本形式。
  • PromptTemplateActions: 主要用于创建Prompt对象,该对象可直接传递给ChatClient以生成响应。
  • PromptTemplateMessageActions:主要用于创建Message对象,这允许我们针对Message对象进行其他的相关操作。

 例如,我们想定义一个这样的提示词:提供作者姓名,返回该作者最受欢迎的书,出版时间和书的内容概述

    @GetMapping("/template")public String promptTemplate(String author){// 提示词final String template = "请问{author}最受欢迎的书是哪本书?什么时候发布的?书的内容是什么?";PromptTemplate promptTemplate = new PromptTemplate(template);// 动态地将author填充进去Prompt prompt = promptTemplate.create(Map.of("author", author));ChatResponse chatResponse = chatClient.call(prompt);AssistantMessage assistantMessage = chatResponse.getResult().getOutput();return assistantMessage.getContent();}

 我们除了可以通过定义字符串加载Template以外,我们还可以以Resource的形式加载Template,例如,我们在resouces下创建prompt.st(文件后缀名合理即可),将刚刚的提示词模板写入到该文件中。

package com.ningning0111.controller;import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
public class ChatController {private final ChatClient chatClient;@Value("classpath:prompt.st")private Resource templateResource;public ChatController(ChatClient chatClient) {this.chatClient = chatClient;}@GetMapping("/template")public String promptTemplate(String author){// 提示词PromptTemplate promptTemplate = new PromptTemplate(templateResource);// 动态地将author填充进去Prompt prompt = promptTemplate.create(Map.of("author", author));ChatResponse chatResponse = chatClient.call(prompt);AssistantMessage assistantMessage = chatResponse.getResult().getOutput();return assistantMessage.getContent();}}

5.2 实现代码生成器

 上面已经将Prompt的使用介绍得比较清楚了,接着我们可以使用Prompt来创建一个代码生成器的接口,通过传入描述消息、语言信息和方法名称来得到响应代码。提示词模板code.st文件内容如下:

/*** @language {language}* @method {methodName}* @describe {description}**/

接口代码如下:

@Value("classpath:code.st")private Resource codeTemplate;@GetMapping("/code")public String generateCode(@RequestParam String description, @RequestParam String language, @RequestParam String methodName) {PromptTemplate promptTemplate = new PromptTemplate(codeTemplate);Prompt prompt = promptTemplate.create(Map.of("description", description, "language", language, "methodName", methodName));ChatResponse chatResponse = chatClient.call(prompt);AssistantMessage assistantMessage = chatResponse.getResult().getOutput();return assistantMessage.getContent();}

效果如下:

 在设计代码生成器接口时,我们还可以提供我们项目代码的上下文信息,这样,AI就能根据我们项目里的代码信息,更加准确的生成我们可使用的业务代码了。

5.3 OutputParser 生成解析器

Spring AI不仅为我们提供了PromptTemplate让我们快速的构建用于输入AI的提示词,还为我们提供了OutputParser解析器,该解析器可以将AI生成的内容解析为Java Bean对象。该解析器类似于ORM框架中的Mapper,将AI的生成内容映射为Java对象。
OutputParser结合了Parser<T>FormatProvider
FormatProvider接口用于提供一些文本指令,来限制AI的输出格式,这里就用到了提示词,我们可以通过阅读源码来查看Spring AI内部设定的相关提示词:



Parser<T>接口用于解析AI生成的内容并将其转换为Java对象返回。
在Spring AI中,OutputParser接口有三个具体的实现类:

  • BeanOutputParser: 通过让AI生成JSON格式的文本,然后通过JSON反序列化为Java对象返回;
  • MapOutputParser: 与BeanOutputParser的功能类似,但会将JSON反序列化为Map对象;
  • ListOutputParser: 让AI生成以逗号分隔的列表;

一般的,我们会先使用**FormatProvider**获取输出限制的提示词对AI生成的文本格式进行限制,然后用**Parser<T>**来解析我们生成的内容作为一个Bean对象

 接下来将主要演示下BeanOutputParser解析器的使用。

5.4 OutputParser解析器示例

 功能描述:通过传入作者名称,返回包含作者名、该作者最受欢迎的书的名称,出版时间以及书中内容描述的Java对象。
定义一个Book类,创建构造器和相关Get、Set方法:

package com.ningning0111.model;public class Book {private String author;private String bookName;private String publishedDate;private String description;public Book(){};public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public Book(String author, String publishedDate, String description, String bookName) {this.author = author;this.publishedDate = publishedDate;this.description = description;this.bookName = bookName;}public String getAuthor() {return author;}public String getPublishedDate() {return publishedDate;}public String getDescription() {return description;}public void setAuthor(String author) {this.author = author;}public void setPublishedDate(String publishedDate) {this.publishedDate = publishedDate;}public void setDescription(String description) {this.description = description;}
}

接口代码:

    @GetMapping("/bean")public Book getBookByAuthor(String author) {final String template = """请告诉我{author}最受欢迎的书是哪本?什么时间出版的?书的内容描述了什么?{format}""";// 定义一个输出解析器OutputParser<Book> bookParser = new BeanOutputParser<>(Book.class);PromptTemplate promptTemplate = new PromptTemplate(template);Prompt prompt = promptTemplate.create(Map.of("author", author, "format", bookParser.getFormat()));ChatResponse chatResponse = chatClient.call(prompt);AssistantMessage assistantMessage = chatResponse.getResult().getOutput();// 解析为一个Bean对象Book book = bookParser.parse(assistantMessage.getContent());return book;}



 可以看到,我们通过提示词对AI生成的内容格式进行限制后,就能很轻松地将输出内容转换为Java对象供我们使用了。

这篇关于Spring AI教程(二)Chat API之Prompts模板语法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/927367

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与