|
2 | 2 |
|
3 | 3 | import com.ashin.config.GptConfig; |
4 | 4 | import com.ashin.config.ProxyConfig; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.theokanning.openai.client.OpenAiApi; |
5 | 7 | import com.theokanning.openai.service.OpenAiService; |
6 | 8 | import lombok.Getter; |
7 | 9 | import lombok.extern.slf4j.Slf4j; |
| 10 | +import okhttp3.OkHttpClient; |
8 | 11 | import org.springframework.stereotype.Component; |
| 12 | +import retrofit2.Retrofit; |
| 13 | +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; |
| 14 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
9 | 15 |
|
10 | 16 | import javax.annotation.PostConstruct; |
11 | 17 | import javax.annotation.Resource; |
@@ -39,9 +45,21 @@ public void init() { |
39 | 45 | for (String apiKey : gptConfig.getApiKey()) { |
40 | 46 | apiKey = apiKey.trim(); |
41 | 47 | if (!apiKey.isEmpty()) { |
42 | | - openAiServiceList.add(new OpenAiService(apiKey, Duration.ofSeconds(gptConfig.getOfSeconds()))); |
| 48 | + openAiServiceList.add(customOpenAiService(apiKey, gptConfig.getBaseUrl())); |
43 | 49 | log.info("apiKey为 {} 的账号初始化成功", apiKey); |
44 | 50 | } |
45 | 51 | } |
46 | 52 | } |
| 53 | + |
| 54 | + private OpenAiService customOpenAiService(String token, String baseUrl) { |
| 55 | + ObjectMapper mapper = OpenAiService.defaultObjectMapper(); |
| 56 | + OkHttpClient client = OpenAiService.defaultClient(token, Duration.ofSeconds(gptConfig.getOfSeconds())); |
| 57 | + Retrofit retrofit = new Retrofit.Builder() |
| 58 | + .baseUrl(baseUrl) |
| 59 | + .client(client) |
| 60 | + .addConverterFactory(JacksonConverterFactory.create(mapper)) |
| 61 | + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| 62 | + .build(); |
| 63 | + return new OpenAiService(retrofit.create(OpenAiApi.class), client.dispatcher().executorService()); |
| 64 | + } |
47 | 65 | } |
0 commit comments