快速学习SpringAi

avatar
作者
筋斗云
阅读量:2

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

1.创建项目

jdk17

image-20240514105741327

引入依赖

image-20240514105741327

2.依赖配置

配置spring仓库

    <repositories>         <repository>             <id>spring-milestones</id>             <name>Spring Milestones</name>             <url>https://repo.spring.io/milestone</url>             <snapshots>                 <enabled>false</enabled>             </snapshots>         </repository>         <repository>             <id>spring-snapshots</id>             <name>Spring Snapshots</name>             <url>https://repo.spring.io/snapshot</url>             <releases>                 <enabled>false</enabled>             </releases>         </repository>      </repositories> 

整个pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>3.2.5</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>     <groupId>com.example</groupId>     <artifactId>springai-test</artifactId>     <version>0.0.1-SNAPSHOT</version>     <name>springai-test</name>     <description>Demo project for Spring Boot</description>     <properties>         <java.version>17</java.version>         <spring-ai.version>0.8.1</spring-ai.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.ai</groupId>             <artifactId>spring-ai-openai-spring-boot-starter</artifactId>             <version>0.8.1</version> <!--            <scope>system</scope>--> <!--            <systemPath>${project.basedir}/src/main/resources/lib/spring-ai-core-0.8.1.jar</systemPath>-->         </dependency>          <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <optional>true</optional>         </dependency>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.springframework.ai</groupId>             <artifactId>spring-ai-openai</artifactId>             <version>0.8.1</version>         </dependency>      </dependencies>      <dependencyManagement>         <dependencies>             <dependency>                 <groupId>org.springframework.ai</groupId>                 <artifactId>spring-ai-bom</artifactId>                 <version>${spring-ai.version}</version>                 <type>pom</type>                 <scope>import</scope>             </dependency>         </dependencies>     </dependencyManagement>      <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>                             <groupId>org.projectlombok</groupId>                             <artifactId>lombok</artifactId>                         </exclude>                     </excludes> <!--                    <includeSystemScope>true</includeSystemScope>-->                 </configuration>             </plugin>         </plugins>     </build>     <repositories>         <repository>             <id>spring-milestones</id>             <name>Spring Milestones</name>             <url>https://repo.spring.io/milestone</url>             <snapshots>                 <enabled>false</enabled>             </snapshots>         </repository>         <repository>             <id>spring-snapshots</id>             <name>Spring Snapshots</name>             <url>https://repo.spring.io/snapshot</url>             <releases>                 <enabled>false</enabled>             </releases>         </repository>      </repositories>  </project>  

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

<mirror> 	<id>maven-default-http-blocker</id> 	<mirrorOf>external:http:*</mirrorOf> 	<name>Pseudo repository to mirror external repositories 	initially using HTTP .</name> 	<url>http://0.0.0.0/</url> 	<blocked>true</blocked> </mirror> 

如果依赖还没引入成功,可以下载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<ChatResponse> 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

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!