Skip to content

feat: 支持针对okhttpclient进行自定义 #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/com/plexpt/chatgpt/ChatGPT.java
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
import com.plexpt.chatgpt.entity.images.ImagesRensponse;
import com.plexpt.chatgpt.entity.images.Variations;
import com.plexpt.chatgpt.exception.ChatException;
import com.plexpt.chatgpt.util.OkHttpCustomizer;
import com.plexpt.chatgpt.util.fastjson.JSON;
import io.reactivex.Single;
import lombok.*;
@@ -70,6 +71,10 @@ public class ChatGPT {
private String apiHost = Api.DEFAULT_API_HOST;
private Api apiClient;
private OkHttpClient okHttpClient;
/**
* 用于自定义okhttp client
*/
private OkHttpCustomizer okHttpCustomizer;
/**
* 超时 默认300
*/
@@ -123,6 +128,9 @@ public ChatGPT init() {
if (Objects.nonNull(proxy)) {
client.proxy(proxy);
}
if (Objects.nonNull(okHttpCustomizer)) {
client = okHttpCustomizer.customize(client);
}
this.okHttpClient = client.build();

this.apiClient = new Retrofit.Builder()
9 changes: 9 additions & 0 deletions src/main/java/com/plexpt/chatgpt/ChatGPTStream.java
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.plexpt.chatgpt.api.Api;
import com.plexpt.chatgpt.entity.chat.ChatCompletion;
import com.plexpt.chatgpt.entity.chat.Message;
import com.plexpt.chatgpt.util.OkHttpCustomizer;
import com.plexpt.chatgpt.util.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -42,6 +43,10 @@ public class ChatGPTStream {
private List<String> apiKeyList;

private OkHttpClient okHttpClient;
/**
* 用于自定义okhttp client
*/
private OkHttpCustomizer okHttpCustomizer;
/**
* 连接超时
*/
@@ -71,6 +76,10 @@ public ChatGPTStream init() {
client.proxy(proxy);
}

if (Objects.nonNull(okHttpCustomizer)) {
client = okHttpCustomizer.customize(client);
}

okHttpClient = client.build();

return this;
23 changes: 23 additions & 0 deletions src/main/java/com/plexpt/chatgpt/util/OkHttpCustomizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.plexpt.chatgpt.util;

import okhttp3.OkHttpClient;

/**
* okhhttp自定义程序接口
*
* <p>
* date: 2024/11/12 20:22
*
* @author imyuyu
*/
@FunctionalInterface
public interface OkHttpCustomizer {

/**
* 自定义okhttp client builder
* @param builder
* @return
*/
OkHttpClient.Builder customize(OkHttpClient.Builder builder);

}
6 changes: 6 additions & 0 deletions src/test/java/com/plexpt/chatgpt/StreamTest.java
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import com.plexpt.chatgpt.listener.SseStreamListener;
import com.plexpt.chatgpt.util.Proxys;

import okhttp3.Dispatcher;
import org.junit.Before;
import org.junit.Test;
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -15,6 +16,7 @@
import java.net.Proxy;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;

/**
* 测试类
@@ -33,6 +35,10 @@ public void before() {
.apiKey("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")
.proxy(proxy)
.timeout(600)
.okHttpCustomizer(builder -> {
// 自定义一个dispatcher
return builder.dispatcher(new Dispatcher(Executors.newSingleThreadExecutor()));
})
.apiHost("https://api.openai.com/")
.build()
.init();