初步整合
java version 17
springboot选中
pom文件
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<dependency>
<groupId>org.springframework.experimental.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
配置文件
spring:
ai:
openai:
api-key: key自己获取哦
base-url: 请求api地址
示例代码及其访问方式 ps:响应会有一定延迟 或异常第一次已挂 一般免费api有每月免费额度
//访问地址 localhost:8887/api/v1/chat?message=java是什么
@RestController
@RequestMapping("/api/v1")
public class AiController {
private final AiClient aiClient;
public AiController(AiClient aiClient) {
this.aiClient = aiClient;
}
@GetMapping("/chat")
public String chat(
@RequestParam(value = "message",defaultValue = "Hi") String message
){
return aiClient.generate(message);
}
}