1 )核心代码结构设计
2 ) pom 文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>gpt-clientartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.28version>
<scope>compilescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>2.0.7version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.3.7version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.3.7version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.15.2version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.8.19version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.33version>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>okhttp-sseartifactId>
<version>3.14.9version>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>logging-interceptorartifactId>
<version>3.14.9version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>retrofitartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>converter-jacksonartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>com.squareup.retrofit2groupId>
<artifactId>adapter-rxjava2artifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>com.knuddelsgroupId>
<artifactId>jtokkitartifactId>
<version>0.4.0version>
dependency>
dependencies>
project>
3 )entity 目录
ChatCompletion.java
package com.xxx.gpt.client.entity;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.xxx.gpt.client.util.TokensUtil;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
@Data
@Builder
@Slf4j
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ChatCompletion implements Serializable {
@NonNull
@Builder.Default
private String model = Model.GPT_3_5_TURBO_0613.getName();
@NonNull
private List<Message> messages;
/**
* 使用什么取样温度,0到2之间。越高越奔放。越低越保守。
*
* 不要同时改这个和topP
*/
@Builder.Default
private double temperature = 0.9;
/**
* 0-1
* 建议0.9
* 不要同时改这个和temperature
*/
@JsonProperty("top_p")
@Builder.Default
private double topP = 0.9;
/**
* auto
*/
String function_call;
List<ChatFunction> functions;
/**
* 结果数。
*/
@Builder.Default
private Integer n = 1;
/**
* 是否流式输出.
* default:false
*/
@Builder.Default
private boolean stream = false;
/**
* 停用词
*/
private List<String> stop;
/**
* 3.5 最大支持4096
* 4.0 最大32k
*/
@JsonProperty("max_tokens")
private Integer maxTokens;
@JsonProperty("presence_penalty")
private double presencePenalty;
/**
* -2.0 ~~ 2.0
*/
@JsonProperty("frequency_penalty")
private double frequencyPenalty;
@JsonProperty("logit_bias")
private Map logitBias;
/**
* 用户唯一值,确保接口不被重复调用
*/
private String user;
public int countTokens() {
return TokensUtil.tokens(this.model, this.messages);
}
}
ChatCompletionResponse.java
package com.xxx.gpt.client.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class ChatCompletionResponse implements Serializable {
private String id;
private String object;
private long created;
private String model;
private List<ChatChoice> choices;
private Usage usage;
}
ChatChoice.java
package com.xxx.gpt.client.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
@Data
public class ChatChoice implements Serializable {
private long index;
/**
* 请求参数stream为true返回是delta
*/
@JsonProperty("delta")
private Message delta;
@JsonProperty("message")
private Message message;
@JsonProperty("finish_reason")
private String finishReason;
}
3 )util 目录
Proxys.java
package com.xxx.gpt.client.util;
import java.net.InetSocketAddress;
import java.net.Proxy;
public class Proxys {
public static Proxy http(String ip, int port) {
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
}
public static Proxy socks5(String ip, int port) {
return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ip, port));
}
}
4 )创建 ChatApi 接口
v1/chat/completions
ChatCompletionResponse
,参数是 ChatCompleination
ChatApi.java
package com.xxx.gpt.client;
import com.xxx.gpt.client.entity.ChatCompletion;
import com.xxx.gpt.client.entity.ChatCompletionResponse;
import io.reactivex.Single;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface ChatApi {
String CHAT_GPT_API_HOST = "https://api.openai.com/";
@POST("v1/chat/completions")
Single<ChatCompletionResponse> chatCompletion(@Body ChatCompletion chatCompletion);
}
5 )添加 ChatGPTClient
在这个类里面定义一些属性: apiKey
, apiHost
, chatApi
, okHttpClient
, timeout
, proxy
再来添加一个init方法
这样就完成了一个ChatGPTClient的一个实例化
实例化完成之后呢,我们添加一个调用的方法 chatCompletion,返回值就是我们请求的response
现在已经完成了java版本的ChatGPT的client
ChatGPTClient.java
package com.xxx.gpt.client;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import com.alibaba.fastjson.JSON;
import com.xxx.gpt.client.entity.BaseResponse;
import com.xxx.gpt.client.entity.ChatCompletion;
import com.xxx.gpt.client.entity.ChatCompletionResponse;
import com.xxx.gpt.client.entity.Message;
import io.reactivex.Single;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.net.Proxy;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Slf4j
@Getter
@Setter
@Builder
public class ChatGPTClient {
private String apiKey;
private List<String> apiKeyList;
@Builder.Default
private String apiHost = ChatApi.CHAT_GPT_API_HOST;
private ChatApi apiClient;
private OkHttpClient okHttpClient;
/**
* 超时 默认300
*/
@Builder.Default
private long timeout = 300;
/**
* okhttp 代理
*/
@Builder.Default
private Proxy proxy = Proxy.NO_PROXY;
public ChatGPTClient init() {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.addInterceptor(chain -> {
Request original = chain.request();
String key = apiKey;
if (apiKeyList != null && !apiKeyList.isEmpty()) {
key = RandomUtil.randomEle(apiKeyList);
}
Request request = original.newBuilder()
.header(Header.AUTHORIZATION.getValue(), "Bearer " + key)
.header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue())
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}).addInterceptor(chain -> {
Request original = chain.request();
Response response = chain.proceed(original);
if (!response.isSuccessful()) {
String errorMsg = response.body().string();
log.error("请求异常:{}", errorMsg);
BaseResponse baseResponse = JSON.parseObject(errorMsg, BaseResponse.class);
if (Objects.nonNull(baseResponse.getError())) {
log.error(baseResponse.getError().getMessage());
throw new RuntimeException(baseResponse.getError().getMessage());
}
throw new RuntimeException(errorMsg);
}
return response;
});
client.connectTimeout(timeout, TimeUnit.SECONDS);
client.writeTimeout(timeout, TimeUnit.SECONDS);
client.readTimeout(timeout, TimeUnit.SECONDS);
if (Objects.nonNull(proxy)) {
client.proxy(proxy);
}
OkHttpClient httpClient = client.build();
this.okHttpClient = httpClient;
this.apiClient = new Retrofit.Builder()
.baseUrl(this.apiHost)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.build()
.create(ChatApi.class);
return this;
}
public ChatCompletionResponse chatCompletion(ChatCompletion chatCompletion) {
Single<ChatCompletionResponse> chatCompletionResponse =
this.apiClient.chatCompletion(chatCompletion);
return chatCompletionResponse.blockingGet();
}
public String chat(String message) {
ChatCompletion chatCompletion = ChatCompletion.builder()
.messages(Arrays.asList(Message.of(message)))
.build();
ChatCompletionResponse response = this.chatCompletion(chatCompletion);
return response.getChoices().get(0).getMessage().getContent();
}
}
6 )添加测试类
ChatGPTClientTest.java
package com.xxx.gpt.client.test;
import com.xxx.gpt.client.ChatGPTClient;
import com.xxx.gpt.client.entity.ChatCompletion;
import com.xxx.gpt.client.entity.ChatCompletionResponse;
import com.xxx.gpt.client.entity.Message;
import com.xxx.gpt.client.entity.Model;
import com.xxx.gpt.client.util.Proxys;
import org.junit.Before;
import org.junit.Test;
import java.net.Proxy;
import java.util.Arrays;
public class ChatGPTClientTest {
private ChatGPTClient chatGPTClient;
@Before
public void before() {
Proxy proxy = Proxys.socks5("127.0.0.1", 7890);
chatGPTClient = ChatGPTClient.builder()
.apiKey("sk-6kchn0DjDasdsdfdqOJqkc3aIso5ct")
.timeout(900)
.proxy(proxy)
.apiHost("https://api.openai.com/")
.build()
.init();
}
@Test
public void chat() {
Message system = Message.ofSystem("你是一个作家,学习过很多古诗");
Message message = Message.of("写一首关于青春的七言绝句");
ChatCompletion chatCompletion = ChatCompletion.builder()
.model(Model.GPT_3_5_TURBO.getName())
.messages(Arrays.asList(system, message))
.maxTokens(3000)
.temperature(0.9)
.build();
ChatCompletionResponse response = chatGPTClient.chatCompletion(chatCompletion);
Message res = response.getChoices().get(0).getMessage();
System.out.println(res.getContent());
}
// @Test
public void tokens() {
Message system = Message.ofSystem("你是一个作家,学习过很多古诗");
Message message = Message.of("写一首关于青春的七言绝句");
ChatCompletion chatCompletion1 = ChatCompletion.builder()
.model(Model.GPT_3_5_TURBO.getName())
.messages(Arrays.asList(system, message))
.maxTokens(3000)
.temperature(0.9)
.build();
ChatCompletion chatCompletion2 = ChatCompletion.builder()
.model(Model.TEXT_DAVINCI_003.getName())
.messages(Arrays.asList(system, message))
.maxTokens(3000)
.temperature(0.9)
.build();
System.out.println(chatCompletion1.countTokens());
System.out.println(chatCompletion2.countTokens());
}
}