|
1 | 1 | package org.devlive.sdk.openai; |
2 | 2 |
|
| 3 | +import com.google.common.collect.Maps; |
3 | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | +import okhttp3.MultipartBody; |
4 | 6 | import okhttp3.OkHttpClient; |
| 7 | +import okhttp3.RequestBody; |
5 | 8 | import org.apache.commons.lang3.ObjectUtils; |
| 9 | +import org.apache.commons.lang3.StringUtils; |
6 | 10 | import org.devlive.sdk.openai.entity.CompletionChatEntity; |
7 | 11 | import org.devlive.sdk.openai.entity.CompletionEntity; |
8 | 12 | import org.devlive.sdk.openai.entity.ImageEntity; |
|
15 | 19 | import org.devlive.sdk.openai.response.ImageResponse; |
16 | 20 | import org.devlive.sdk.openai.response.ModelResponse; |
17 | 21 | import org.devlive.sdk.openai.response.UserKeyResponse; |
| 22 | +import org.devlive.sdk.openai.utils.MultipartBodyUtils; |
18 | 23 | import org.devlive.sdk.openai.utils.ProviderUtils; |
19 | 24 |
|
| 25 | +import java.io.File; |
| 26 | +import java.util.Map; |
| 27 | + |
20 | 28 | @Slf4j |
21 | 29 | public abstract class DefaultClient implements AutoCloseable |
22 | 30 | { |
@@ -66,6 +74,30 @@ public ImageResponse createImages(ImageEntity configure) |
66 | 74 | .blockingGet(); |
67 | 75 | } |
68 | 76 |
|
| 77 | + public ImageResponse editImages(File image, File mask, ImageEntity configure) |
| 78 | + { |
| 79 | + MultipartBody.Part imageBody = MultipartBodyUtils.getPart(image, "image"); |
| 80 | + MultipartBody.Part maskBody = null; |
| 81 | + if (ObjectUtils.isNotEmpty(mask)) { |
| 82 | + maskBody = MultipartBodyUtils.getPart(mask, "mask"); |
| 83 | + } |
| 84 | + |
| 85 | + Map<String, RequestBody> map = Maps.newConcurrentMap(); |
| 86 | + map.put("prompt", RequestBody.create(MultipartBodyUtils.TYPE, configure.getPrompt())); |
| 87 | + map.put("n", RequestBody.create(MultipartBodyUtils.TYPE, configure.getCount().toString())); |
| 88 | + map.put("size", RequestBody.create(MultipartBodyUtils.TYPE, configure.getSize())); |
| 89 | + map.put("response_format", RequestBody.create(MultipartBodyUtils.TYPE, configure.getFormat())); |
| 90 | + if (StringUtils.isNotEmpty(configure.getUser())) { |
| 91 | + map.put("user", RequestBody.create(MultipartBodyUtils.TYPE, configure.getUser())); |
| 92 | + } |
| 93 | + |
| 94 | + return this.api.fetchImagesEdits(ProviderUtils.getUrl(provider, UrlModel.FETCH_EDITS_GENERATIONS), |
| 95 | + imageBody, |
| 96 | + maskBody, |
| 97 | + map) |
| 98 | + .blockingGet(); |
| 99 | + } |
| 100 | + |
69 | 101 | public void close() |
70 | 102 | { |
71 | 103 | if (ObjectUtils.isNotEmpty(this.client)) { |
|
0 commit comments