wordpress中文站typecho同步到wordpress

当前位置: 首页 > news >正文

wordpress中文站,typecho同步到wordpress,顾问,电脑制作网页用什么软件Spring AI介绍
Spring AI是AI工程师的一个应用框架#xff0c;它提供了一个友好的API和开发AI应用的抽象#xff0c;旨在简化AI应用的开发工序#xff0c;例如开发一款基于ChatGPT的对话、图片、音频等应用程序。 Spring AI已经集成了OpenAI的API#xff0c;因此我们不需… Spring AI介绍
Spring AI是AI工程师的一个应用框架它提供了一个友好的API和开发AI应用的抽象旨在简化AI应用的开发工序例如开发一款基于ChatGPT的对话、图片、音频等应用程序。 Spring AI已经集成了OpenAI的API因此我们不需要实现向OpenAI发送请求和接收响应的交互程序了Spring AI已经实现了这一内容我们只需要通过调用Spring AI为我们提供的接口即可 项目地址https://github.com/spring-projects-experimental/spring-ai 文档地址https://docs.spring.io/spring-ai/reference/ Spring AI能做什么 支持目前主流大语言模型平台例如 OpenAI、Microsoft、Amazon、Google 和 Huggingface支持阻塞与流式的文本对话支持图像生成(当前仅限OpenAI的dall-e-*模型和SD)支持嵌入模型支持LLM生成的内容转为POJO支持主流的向量数据库或平台Azure Vector Search, Chroma, Milvus, Neo4j, PostgreSQL/PGVector, PineCone, Qdrant, Redis 和 Weaviate支持函数调用支持自动装配和启动器与Spring Boot完美集成提供用于数据处理工程的ETL框架 项目实践 准备工作 版本说明
OpenAI的KeyOpenAI的ApiJDK 17Spring 6.x;Spring Boot 3.xSpring AI 0.8.1-SNAPSHOT pom引入
!– 仓库定义 – repositoriesrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repositoryrepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/repository/repositories !– 依赖管理配置 – dependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion0.8.1-SNAPSHOT/versiontypepom/typescopeimport/scope/dependency/dependencies /dependencyManagement dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai-spring-boot-starter/artifactId /dependency 用于请求OpenAI平台相关模型例如对话用的ChatGPT、画图用的Dall-e-2/3、文本嵌入text-embedding-ada-002以及音频合成与识别的whisper和tts等相关模型。 配置文件application.yml
将相关key和api信息进行填写 spring:ai:openai:api-key: 123base-url: https://api.openai.com 快速对话ChatClient Slf4j RestController RequestMapping(/chat) public class ChatController {Autowiredprivate ChatClient chatClient;GetMapping(/demo)public String chat(String prompt){return chatClient.call(prompt);}} 运行结果 流式对话StreamingChatClient
流失对话的核心就是流式传输AI的响应数据是一点一点传过来的不用等AI将文本全部生成出来了才传过来。一定程度上能够提高使用上的响应速度给用户一个非常好的体验。 Slf4j RestController RequestMapping(/chat) public class ChatController {Autowiredprivate StreamingChatClient streamingChatClient; // 流式调用 将produces声明为文本事件流GetMapping(value /stream,produces MediaType.TEXT_EVENT_STREAM_VALUE)public FluxString stream(String prompt){long startTime System.currentTimeMillis();FluxString resstreamingChatClient.stream(prompt).flatMapSequential(Flux::just);long endTime System.currentTimeMillis();long duration endTime - startTime;log.info(流式调用执行时间:{},duration);// 将流中的内容按顺序返回return res;} } 运行结果 上下文对话
ChatGPT上下文对话的实现原理较为简单本质上其实就是将不同角色的聊天信息依次存储在一个队列中发送给ChatGPT即可然后ChatGPT会根据整个聊天信息对回复内容进行判断。在OpenAI提供的接口中每条信息的角色总共分为三类 SystemMessage系统限制信息这种信息在对话中的权重很大AI会优先依据SystemMessage里的内容进行回复UserMessage用户信息AssistantMessageAI回复信息 不过根据OpenAI的计费规则你的消息队列越长单次问询需要的费用就会越高因此我们需要对这个消息列表的长度进行限制。 Slf4j RestController RequestMapping(/chat) public class ChatController {Autowiredprivate ChatClient chatClient;// 历史消息列表static ListMessage historyMessage new ArrayList();// 历史消息列表的最大长度static int maxLen 10;GetMapping(/context)public String context(String prompt) {// 用户输入的文本是UserMessagehistoryMessage.add(new UserMessage(prompt));// 发给AI前对历史消息对列的长度进行检查if(historyMessage.size() maxLen){historyMessage historyMessage.subList(historyMessage.size()-maxLen-1,historyMessage.size());}// 获取AssistantMessageChatResponse chatResponse chatClient.call(new Prompt(historyMessage));AssistantMessage assistantMessage chatResponse.getResult().getOutput();// 将AI回复的消息放到历史消息列表中historyMessage.add(assistantMessage);return assistantMessage.getContent();}} 人设设定
人设设定功能来自于“提示词prompt工程”的理论基础可用来提高大语言模型处理复杂任务场景的能力 上面介绍Message的时候提到SystemMessage对AI生成的内容影响权重较大人设设定就是需要靠SystemMessage实现。我们提供一个SystemMessage放入历史消息列表中并让SystemMessage在每次发给AI时始终在历史消息列表中。 Slf4j RestController RequestMapping(/chat) public class ChatController {Autowiredprivate ChatClient chatClient;// 历史消息列表final String systemPrompt你现在是一个喜欢扮可爱的人说话嗲嗲的;ListMessage historyMessage new ArrayList(List.of(new SystemMessage(systemPrompt)));// 历史消息列表的最大长度static int maxLen 10;GetMapping(/context)public String context(String prompt) {// 用户输入的文本是UserMessagehistoryMessage.add(new UserMessage(prompt));// 发给AI前对历史消息对列的长度进行检查if(historyMessage.size() maxLen){historyMessage historyMessage.subList(historyMessage.size()-maxLen-1,historyMessage.size());}// 获取AssistantMessageChatResponse chatResponse chatClient.call(new Prompt(historyMessage));AssistantMessage assistantMessage chatResponse.getResult().getOutput();// 将AI回复的消息放到历史消息列表中historyMessage.add(assistantMessage);return assistantMessage.getContent();}} 运行结果 模板语法PromptTemplate
Spring AI为我们提供了提示词模板允许我们通过一些模板快速地动态生成提示词并发起提问 Slf4j RestController RequestMapping(/prompts) public class PromptController {Autowiredprivate ChatClient chatClient;Value(classpath:prompt.st)private Resource templateResource;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();}GetMapping(/config/template)public String promptConfigTemplate(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();}} 请问{author}最受欢迎的书是哪本书什么时候发布的书的内容是什么OutputParser 生成解析器
SpringAi还为我们提供了OutputParser解析器该解析器可以将AI生成的内容解析为Java Bean对象。该解析器类似于ORM框架中的Mapper将AI的生成内容映射为Java对象。 Slf4j RestController RequestMapping(/parser) public class ParserController {Autowiredprivate ChatClient chatClient;GetMapping(/bean)public Movie getBookByAuthor(String actor) {final String template 请告诉我{actor}最受欢迎的电影是哪个什么时间上映大概讲了什么{format};// 定义一个输出解析器OutputParserMovie movieParser new BeanOutputParser(Movie.class);PromptTemplate promptTemplate new PromptTemplate(template);Prompt prompt promptTemplate.create(Map.of(actor, actor, format, movieParser.getFormat()));ChatResponse chatResponse chatClient.call(prompt);AssistantMessage assistantMessage chatResponse.getResult().getOutput();// 解析为一个Bean对象Movie movie movieParser.parse(assistantMessage.getContent());return movie;} }Data AllArgsConstructor NoArgsConstructor public class Movie {private String actor;private String movieName;private String publishedDate;private String description; } 绘图ImageClient
Spring AI提供了图片生成接口该接口可以用于与各种专门用于图像生成的人工智能模型进行交互。 在调用绘图时我们只需要像调用对话一样传入一个Prompt:ImagePrompt。ImagePrompt中包含了我们需要绘制的图片信息包括ImageMessage(绘图指令)、ImageOptions(图片数、图片配置、返回的图片格式、绘图模型等)。AI拿到我们的Prompt后会根据里面的内容对图像进行生产 ImageOptions重要属性 model绘图模型默认dall-e-3 dall-e-31024 x 1024 、 1024 x 1792、1792 x 1024;dall-e-2: 256 x 256、512 x 512 、 1024 x 1024;responseFormat返回的图片格式url 和 b64_json Slf4j RestController RequestMapping(/image) public class ImageController {Autowiredprivate ImageClient imageClient;GetMapping(/image)public String image(String prompt) {ImagePrompt imagePrompt new ImagePrompt(prompt, OpenAiImageOptions.builder().withModel(OpenAiImageApi.ImageModel.DALL_E_3.getValue()).withHeight(1024).withWidth(1024).withResponseFormat(url) // URL or b64_json.build());ImageResponse imageResponse imageClient.call(imagePrompt);ListImageGeneration results imageResponse.getResults();// 图片urlString url results.get(0).getOutput().getUrl();return String.format(img src%s alt%s,url,prompt);} } AI自查实现对话和绘图
通过AI自查手段将文本模型和图片生成模型进行组合实现一个既可以生成文本也可以生成AI的接口。这个关键点就是利用提示词限制AI的回复内容以达到一个自查手段 AI自查就是让AI判断你的问题是画一个图还是简简单单的对话。 用户输入文本prompt;先让AI判断文本prompt是否需要图片;如果需要图片调用绘图模型获取绘图结果如果不需要图片直接调用对话模型;’ Slf4j RestController RequestMapping(/judge) public class JudgeByAiController {Autowiredprivate ChatClient chatClient;Autowiredprivate ImageClient imageClient;Value(classpath:judge.st)private Resource templateResource;RequestMapping(/ai)public String ai(String prompt){try {return judge(prompt)?image(prompt):chat(prompt);} catch (Exception e) {return error;}}private boolean judge(String promptString){PromptTemplate promptTemplate new PromptTemplate(templateResource);// 动态地将prompt填充进去Prompt prompt promptTemplate.create(Map.of(prompt, promptString));ChatResponse chatResponse chatClient.call(prompt);AssistantMessage assistantMessage chatResponse.getResult().getOutput();String judgeResultassistantMessage.getContent();return judgeResult.toLowerCase().contains(yes)?true:false;}private String chat(String prompt){String reschatClient.call(prompt);return res;}private String image(String prompt) {ImagePrompt imagePrompt new ImagePrompt(prompt, OpenAiImageOptions.builder().withModel(OpenAiImageApi.ImageModel.DALL_E_3.getValue()).withHeight(1024).withWidth(1024).withResponseFormat(url) // URL or b64_json.build());ImageResponse imageResponse imageClient.call(imagePrompt);ListImageGeneration results imageResponse.getResults();// 图片urlString url results.get(0).getOutput().getUrl();return String.format(img src%s alt%s,url,prompt);} }Does this message want to generate an AI picture, image, art or anything similar? {prompt} . Simply answer with a yes or no. LLM领域新浪潮即将来临
openAI真正走向CloseAILLM国产替代浪潮即将到来 危OpenAI 将限制中国开发者访问 API 服务-CSDN博客