免费使用
- 🚀申请领取内测免费API Key
- 免费版支持gpt-3.5-turbo, embedding, gpt-4。其中gpt-4由于价格过高,每24小时限制10次调用,且不支持流式传输。需要更稳定快速的gpt-4请使用付费版。
- 转发Host1:
https://api.chatanywhere.tech
(国内中转,延时更低,host1和host2二选一) - 转发Host2:
https://api.chatanywhere.com.cn
(国内中转,延时更低,host1和host2二选一) - 转发Host3:
https://api.chatanywhere.cn
(国外使用,国内需要全局代理)
我们会定期根据使用量进行相应的扩容,只要不被官方制裁我们会一直提供免费API,如果该项目对你有帮助,还请为我们点一个Star。如果遇到问题可以在Issues中反馈,有空会解答。
该API Key用于转发API,需要将Host改为api.chatanywhere.tech
(国内首选)或者api.chatanywhere.cn
(国外使用,国内需要全局代理)。
改下pom文件
原先:
修改后:
原先:
修改后:
完整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>
<!-- Generated by https://start.springboot.io -->
<!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
<groupId>com.ysl</groupId>
<artifactId>SpringAi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>SpringAi</name>
<description>SpringAi</description>
<modules>
<module>spring-ai-01-chat</module>
</modules>
<properties>
<java.version>17</java.version>
<!-- 快照版本-->
<spring-ai.version>1.0.0-SNAPSHOT</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
</dependencies>
<!-- 相当于继承一个父项目:spring-ai-bom-->
<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>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<!--~配置本项目的仓库:因maven中心仓库还设有更新spring ai的jar包-->
<repositories>
<repository>
<!-- 里程碑版本releases的仓库,改成快照版本的snapshot-->
<id>spring-snapshot</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
</project>
Controller层
package com.zzq.controller;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
public class ChatController {
/**
* OpenAi自动装配,可以直接注入使用
*/
@Resource
private OpenAiChatClient openAiChatClient;
/**
* 调用OpenAi的接口,call方法为同步的api
* @param msg 你要问的问题
* @return
*/
@RequestMapping("/ai/chat")
public String chat(@RequestParam("msg") String msg) {
String called = openAiChatClient.call(msg);
return called;
}
/**
* 调用OpenAi的接口
* @param msg 你要问的问题
* @return Object--json对象
*/
@RequestMapping ("/ai/chat1")
public Object chat1(@RequestParam("msg") String msg) {
ChatResponse response = openAiChatClient.call(new Prompt(msg));
return response;
// return response.getResult().getOutput().getContent();//只拿到内容
}
/**
* 调用OpenAi的接口
* @param msg 你要问的问题
* @return
*/
@RequestMapping ("/ai/chat3")
public String chat3(@RequestParam("msg") String msg) {
//可选参数在yml配置,同时在代码中也配置,那么会以代码为准
ChatResponse response = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()
// .withModel("gpt-4-32k")//使用的模型,32k是参数量//参数量越高,准确率越高
.withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
.build()));
return response.getResult().getOutput().getContent();
}
/**
* 调用OpenAi的接口 stream是流式的api
* @param msg 你要问的问题
* @return
*/
@RequestMapping ("/ai/chat4")
public Object chat4(@RequestParam("msg") String msg) {
//可选参数在yml配置,同时在代码中也配置,那么会以代码为准
Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder()
// .withModel("gpt-3.5")//使用的模型
.withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
.build()));
flux.toStream().forEach(chatResponse ->{
System.out.println(chatResponse.getResult().getOutput().getContent());
});
return flux.collectList();//数据的序列
}
}