|
| 1 | +package com.plexpt.chatgpt; |
| 2 | + |
| 3 | +import cn.hutool.core.util.RandomUtil; |
| 4 | +import cn.hutool.http.ContentType; |
| 5 | +import cn.hutool.http.Header; |
| 6 | +import com.alibaba.fastjson.JSON; |
| 7 | +import com.plexpt.chatgpt.api.Api; |
| 8 | +import com.plexpt.chatgpt.entity.BaseResponse; |
| 9 | +import com.plexpt.chatgpt.entity.images.Edits; |
| 10 | +import com.plexpt.chatgpt.entity.images.Generations; |
| 11 | +import com.plexpt.chatgpt.entity.images.ImagesRensponse; |
| 12 | +import com.plexpt.chatgpt.entity.images.Variations; |
| 13 | +import com.plexpt.chatgpt.exception.ChatException; |
| 14 | +import io.reactivex.Single; |
| 15 | +import lombok.*; |
| 16 | +import lombok.extern.slf4j.Slf4j; |
| 17 | +import okhttp3.*; |
| 18 | +import retrofit2.Retrofit; |
| 19 | +import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; |
| 20 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 21 | + |
| 22 | +import java.io.File; |
| 23 | +import java.net.Proxy; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +/** |
| 31 | + * @Author matoooo |
| 32 | + * @Date 2023/8/25 11:08 |
| 33 | + * @Description: 调用图片相关接口 |
| 34 | + */ |
| 35 | +@Slf4j |
| 36 | +@Getter |
| 37 | +@Setter |
| 38 | +@Builder |
| 39 | +@AllArgsConstructor |
| 40 | +@NoArgsConstructor |
| 41 | +public class Images { |
| 42 | + /** |
| 43 | + * keys |
| 44 | + */ |
| 45 | + private String apiKey; |
| 46 | + |
| 47 | + private List<String> apiKeyList; |
| 48 | + /** |
| 49 | + * 自定义api host使用builder的方式构造client |
| 50 | + */ |
| 51 | + @Builder.Default |
| 52 | + private String apiHost = Api.DEFAULT_API_HOST; |
| 53 | + private Api apiClient; |
| 54 | + private OkHttpClient okHttpClient; |
| 55 | + /** |
| 56 | + * 超时 默认300 |
| 57 | + */ |
| 58 | + @Builder.Default |
| 59 | + private long timeout = 300; |
| 60 | + /** |
| 61 | + * okhttp 代理 |
| 62 | + */ |
| 63 | + @Builder.Default |
| 64 | + private Proxy proxy = Proxy.NO_PROXY; |
| 65 | + |
| 66 | + /** |
| 67 | + * 初始化 |
| 68 | + */ |
| 69 | + public Images init() { |
| 70 | + OkHttpClient.Builder client = new OkHttpClient.Builder(); |
| 71 | + client.addInterceptor(chain -> { |
| 72 | + Request original = chain.request(); |
| 73 | + String key = apiKey; |
| 74 | + if (apiKeyList != null && !apiKeyList.isEmpty()) { |
| 75 | + key = RandomUtil.randomEle(apiKeyList); |
| 76 | + } |
| 77 | + |
| 78 | + Request request = original.newBuilder() |
| 79 | + .header(Header.AUTHORIZATION.getValue(), "Bearer " + key) |
| 80 | + .header(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()) |
| 81 | + .method(original.method(), original.body()) |
| 82 | + .build(); |
| 83 | + return chain.proceed(request); |
| 84 | + }).addInterceptor(chain -> { |
| 85 | + Request original = chain.request(); |
| 86 | + Response response = chain.proceed(original); |
| 87 | + if (!response.isSuccessful()) { |
| 88 | + String errorMsg = response.body().string(); |
| 89 | + |
| 90 | + log.error("请求异常:{}", errorMsg); |
| 91 | + BaseResponse baseResponse = JSON.parseObject(errorMsg, BaseResponse.class); |
| 92 | + if (Objects.nonNull(baseResponse.getError())) { |
| 93 | + log.error(baseResponse.getError().getMessage()); |
| 94 | + throw new ChatException(baseResponse.getError().getMessage()); |
| 95 | + } |
| 96 | + throw new ChatException("error"); |
| 97 | + } |
| 98 | + return response; |
| 99 | + }); |
| 100 | + |
| 101 | + client.connectTimeout(timeout, TimeUnit.SECONDS); |
| 102 | + client.writeTimeout(timeout, TimeUnit.SECONDS); |
| 103 | + client.readTimeout(timeout, TimeUnit.SECONDS); |
| 104 | + if (Objects.nonNull(proxy)) { |
| 105 | + client.proxy(proxy); |
| 106 | + } |
| 107 | + OkHttpClient httpClient = client.build(); |
| 108 | + this.okHttpClient = httpClient; |
| 109 | + |
| 110 | + |
| 111 | + this.apiClient = new Retrofit.Builder() |
| 112 | + .baseUrl(this.apiHost) |
| 113 | + .client(okHttpClient) |
| 114 | + .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) |
| 115 | + .addConverterFactory(JacksonConverterFactory.create()) |
| 116 | + .build() |
| 117 | + .create(Api.class); |
| 118 | + |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + public ImagesRensponse generations(Generations generations){ |
| 123 | + Single<ImagesRensponse> imagesRensponse = |
| 124 | + this.apiClient.imageGenerations(generations); |
| 125 | + return imagesRensponse.blockingGet(); |
| 126 | + } |
| 127 | + |
| 128 | + public ImagesRensponse edits(File image,File mask,Edits edits){ |
| 129 | + RequestBody i = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), image); |
| 130 | + MultipartBody.Part iPart = MultipartBody.Part.createFormData("image", image.getName(), i); |
| 131 | + |
| 132 | + RequestBody m = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), mask); |
| 133 | + MultipartBody.Part mPart = MultipartBody.Part.createFormData("mask", mask.getName(), m); |
| 134 | + |
| 135 | + Single<ImagesRensponse> imagesRensponse = |
| 136 | + this.apiClient.imageEdits(iPart,mPart,edits); |
| 137 | + return imagesRensponse.blockingGet(); |
| 138 | + } |
| 139 | + |
| 140 | + public ImagesRensponse variations(File image,Variations variations){ |
| 141 | + RequestBody i = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), image); |
| 142 | + MultipartBody.Part iPart = MultipartBody.Part.createFormData("image", image.getName(), i); |
| 143 | + Single<ImagesRensponse> imagesRensponse = |
| 144 | + this.apiClient.imageVariations(iPart,variations); |
| 145 | + return imagesRensponse.blockingGet(); |
| 146 | + } |
| 147 | +} |
0 commit comments