快速学习SpringAi
创始人
2025-01-08 15:35:27
0

Spring AI是AI工程师的一个应用框架,它提供了一个友好的API和开发AI应用的抽象,旨在简化AI应用的开发工序,例如开发一款基于ChatGPT的对话应用程序。通过使用Spring Ai使我们更简单直接使用chatgpt

1.创建项目

jdk17

image-20240514105741327

引入依赖

image-20240514105741327

2.依赖配置

配置spring仓库

                          spring-milestones             Spring Milestones             https://repo.spring.io/milestone                              false                                            spring-snapshots             Spring Snapshots             https://repo.spring.io/snapshot                              false                             

整个pom.xml

      4.0.0              org.springframework.boot         spring-boot-starter-parent         3.2.5                    com.example     springai-test     0.0.1-SNAPSHOT     springai-test     Demo project for Spring Boot              17         0.8.1                                org.springframework.boot             spring-boot-starter-web                               org.springframework.ai             spring-ai-openai-spring-boot-starter             0.8.1                                  org.projectlombok             lombok             true                               org.springframework.boot             spring-boot-starter-test             test                               org.springframework.ai             spring-ai-openai             0.8.1                                                            org.springframework.ai                 spring-ai-bom                 ${spring-ai.version}                 pom                 import                                                                        org.springframework.boot                 spring-boot-maven-plugin                                                                                            org.projectlombok                             lombok                                                                                                                      spring-milestones             Spring Milestones             https://repo.spring.io/milestone                              false                                            spring-snapshots             Spring Snapshots             https://repo.spring.io/snapshot                              false                                

因为阿里仓库暂时还没有对应的spring-ai-openai-spring-boot-
starter 依赖,所以Maven的仓库源需要使用Maven默认的仓库
源,在Maven的settings.xml中进行修改即可

 	maven-default-http-blocker 	external:http:* 	Pseudo repository to mirror external repositories 	initially using HTTP . 	http://0.0.0.0/ 	true  

如果依赖还没引入成功,可以下载jar包,地址repo.spring.io

将jar包安装至本地仓库后使用pom文件直接引入

mvn install:install-file -Dfile=你的jar包路径/你的jar包名字 -DgroupId=org.springframework.ai -DartifactId=spring-ai-openai-spring-boot-starter -Dversion=0.8.1 -Dpackaging=jar

3.代码编写

application.properties

spring.application.name=demo server.port=8080  spring.ai.openai.api-key=你的api-key #最好找一个中转url,不然得配置代理 spring.ai.openai.base-url=http://api.openai.com spring.ai.openai.chat.options.model=gpt-3.5-turbo 

controller

一个直接返回结果,一个流式返回

package com.example.demo;  import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ai.chat.ChatResponse; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.prompt.Prompt;  import org.springframework.ai.openai.OpenAiChatClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux;   import java.util.Map;  @RestController public class ChatController {     //日志打印     private final static Logger Log =             LoggerFactory.getLogger(ChatController.class);      //注入OpenAiChatClient     @Autowired     private OpenAiChatClient chatClient;       @GetMapping("/ai/generate")     public Map generate(@RequestParam(value = "message", defaultValue = "给我讲个笑话") String message) {          Log.info("发送的消息是:{}", message);         String result = chatClient.call(message);         Log.info("返回的消息是:{}", result);         return Map.of("generation", result);     }       @GetMapping(value = "/ai/generateStream", produces = "text/event-stream")     public Flux generateStream(@RequestParam(value = "message", defaultValue = "给我讲个笑话") String message) {         Log.info("发送的消息是:{}", message);         Prompt prompt = new Prompt(new UserMessage(message));         System.out.println(chatClient.stream(prompt));         return chatClient.stream(prompt);     } }  

4.运行结果

image-20240514105741327

image-20240514105741327
最后附上代码
https://github.com/smx1024/springai-demo

相关内容

热门资讯

今天下午!aapoker万能辅... 今天下午!aapoker万能辅助器,aapoker怎么提高中牌率(透视)手筋教程(总是有挂)-哔哩哔...
透视策略!wejoker辅助器... 透视策略!wejoker辅助器要钱玩吗,wepoker软件辅助程序(脚本)了解教程(原来有挂)-哔哩...
透视总结!wpk俱乐部有没有辅... 透视总结!wpk俱乐部有没有辅助(透视)wpk俱乐部辅助器,教程手段(有挂方法)-哔哩哔哩wpk俱乐...
现就发布提示!aapoker破... 现就发布提示!aapoker破解侠是真的吗,aapoker怎么设置抽水(透视)手段教程(竟然真的是有...
透视模块!wepoker轻量版... 透视模块!wepoker轻量版透视,wepoker轻量版有透视吗(脚本)曝光教程(原来真的有挂)-哔...
透视推荐!wpk辅助器(透视)... 透视推荐!wpk辅助器(透视)wpk德州局透视,教程妙计(有挂头条)-哔哩哔哩该软件可以轻松地帮助玩...
近日!aapoker怎么拿好牌... 近日!aapoker怎么拿好牌,aapoker透视脚本下载(透视)讲义教程(总是是有挂)-哔哩哔哩该...
透视操作!wepoker辅助是... 透视操作!wepoker辅助是真的假的,wepoker买脚本靠谱吗(脚本)曝光教程(一贯是真的挂)-...
透视科普!wpk软件是真的吗(... 透视科普!wpk软件是真的吗(透视)wpk透视辅助靠谱吗,教程机巧(有挂存在)-哔哩哔哩1、让任何用...
今天上午!aapoker发牌逻... 今天上午!aapoker发牌逻辑,aapoker ai插件(透视)总结教程(总是真的是有挂)-哔哩哔...