第1坑:配置第三方仓库不生效, 提示在阿里云仓库没有找到 spring-ai-openai-spring-boot-starter
第2坑:升级jdk17后,springboot项目启动报错
Internal error (java.lang.reflect.InaccessibleObjectException): Unable to make protected void java.util.ResourceBundle.setParent(java.util.ResourceBundle) accessible: module java.base does not "opens java.util" to unnamed module @bef2d72
java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.util.ResourceBundle.setParent(java.util......
也可以尝试多次运行,一般第二次就会成功运行,原因不详!!! 本人尝试配置vm参数好像不会再出现这个情况,尝试的少,所以不好定论
3. 中间商
https://api.xty.app/token
4.application.properties配置
附上完整的pom文件:
<?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.ldj.ai</groupId><artifactId>open-ai</artifactId><version>0.0.1-SNAPSHOT</version><name>open-ai</name><description>openAI project for Spring Boot</description><properties><java.version>17</java.version><spring-ai.version>1.0.0-M1</spring-ai.version></properties><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><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><artifactId>junit-jupiter</artifactId><groupId>org.junit.jupiter</groupId></exclusion></exclusions></dependency><!--单测有点小bug,说找不到依赖,降低junit版本--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.5.0</version><scope>test</scope></dependency><!--版本继承spring-ai-bom--><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency></dependencies><!--第三方依赖库,注意配置了有可能不生效,需要修改maven setting配置文件,见截图--><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
参考官方示例:Chat Client API :: Spring AI Reference
后续补充:
package com.ldj.ai.openai.controller;import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;/*** User: ldj* Date: 2024/7/7* Time: 15:03* Description: AI 聊天controller*/
@RestController
@RequestMapping("/openai")
public class ChatController {private final ChatClient chatClient;public ChatController(ChatClient.Builder chatClientBuilder) {this.chatClient = chatClientBuilder.build();}@GetMapping("/chat")public String generation(@RequestParam(value = "question", defaultValue = "使用中文简短介绍自己") String question) {return this.chatClient.prompt().user(question).call().content();}}