Commit 000bcf11433943762cb59f2e113c1db518e7d117
1 parent
f15ecda7
【增加】MidjourneyProxyClient 增加批量拉去任务信息
Showing
1 changed file
with
25 additions
and
8 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/MidjourneyProxyClient.java
| @@ -3,15 +3,20 @@ package cn.iocoder.yudao.module.ai.client; | @@ -3,15 +3,20 @@ package cn.iocoder.yudao.module.ai.client; | ||
| 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneyActionReqVO; | 4 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneyActionReqVO; |
| 5 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneyImagineReqVO; | 5 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneyImagineReqVO; |
| 6 | +import cn.iocoder.yudao.module.ai.client.vo.MidjourneyNotifyReqVO; | ||
| 6 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneySubmitRespVO; | 7 | import cn.iocoder.yudao.module.ai.client.vo.MidjourneySubmitRespVO; |
| 8 | +import cn.iocoder.yudao.module.ai.config.MidjourneyProperties; | ||
| 9 | +import com.google.common.collect.ImmutableMap; | ||
| 7 | import jakarta.validation.constraints.NotNull; | 10 | import jakarta.validation.constraints.NotNull; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | -import org.springframework.beans.factory.annotation.Value; | ||
| 10 | import org.springframework.http.*; | 12 | import org.springframework.http.*; |
| 11 | import org.springframework.stereotype.Component; | 13 | import org.springframework.stereotype.Component; |
| 12 | import org.springframework.validation.annotation.Validated; | 14 | import org.springframework.validation.annotation.Validated; |
| 13 | import org.springframework.web.client.RestTemplate; | 15 | import org.springframework.web.client.RestTemplate; |
| 14 | 16 | ||
| 17 | +import java.util.Collection; | ||
| 18 | +import java.util.List; | ||
| 19 | + | ||
| 15 | // TODO @fan:这个写到 starter-ai 里哈。搞个 MidjourneyApi,参考 https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApi.java 的风格写哈 | 20 | // TODO @fan:这个写到 starter-ai 里哈。搞个 MidjourneyApi,参考 https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApi.java 的风格写哈 |
| 16 | /** | 21 | /** |
| 17 | * Midjourney Proxy 客户端 | 22 | * Midjourney Proxy 客户端 |
| @@ -25,12 +30,10 @@ public class MidjourneyProxyClient { | @@ -25,12 +30,10 @@ public class MidjourneyProxyClient { | ||
| 25 | 30 | ||
| 26 | private static final String URI_IMAGINE = "/submit/imagine"; | 31 | private static final String URI_IMAGINE = "/submit/imagine"; |
| 27 | private static final String URI_ACTON = "/submit/action"; | 32 | private static final String URI_ACTON = "/submit/action"; |
| 33 | + private static final String URI_LIST_BY_CONDITION = "/task/list-by-condition"; | ||
| 28 | 34 | ||
| 29 | - @Value("${ai.midjourney-proxy.url:http://127.0.0.1:8080/mj}") | ||
| 30 | - private String url; | ||
| 31 | - @Value("${ai.midjourney-proxy.key}") | ||
| 32 | - private String key; | ||
| 33 | - | 35 | + @Autowired |
| 36 | + private MidjourneyProperties midjourneyProperties; | ||
| 34 | @Autowired | 37 | @Autowired |
| 35 | private RestTemplate restTemplate; | 38 | private RestTemplate restTemplate; |
| 36 | 39 | ||
| @@ -59,14 +62,28 @@ public class MidjourneyProxyClient { | @@ -59,14 +62,28 @@ public class MidjourneyProxyClient { | ||
| 59 | return JsonUtils.parseObject(response.getBody(), MidjourneySubmitRespVO.class); | 62 | return JsonUtils.parseObject(response.getBody(), MidjourneySubmitRespVO.class); |
| 60 | } | 63 | } |
| 61 | 64 | ||
| 65 | + /** | ||
| 66 | + * 批量查询 task 任务 | ||
| 67 | + * | ||
| 68 | + * @param taskIds | ||
| 69 | + * @return | ||
| 70 | + */ | ||
| 71 | + public List<MidjourneyNotifyReqVO> listByCondition(Collection<String> taskIds) { | ||
| 72 | + // 1、发送 post 请求 | ||
| 73 | + ResponseEntity<String> res = post(URI_LIST_BY_CONDITION, ImmutableMap.of("ids", taskIds)); | ||
| 74 | + // 2、转换 对象 | ||
| 75 | + return JsonUtils.parseArray(res.getBody(), MidjourneyNotifyReqVO.class); | ||
| 76 | + } | ||
| 77 | + | ||
| 62 | private ResponseEntity<String> post(String uri, Object body) { | 78 | private ResponseEntity<String> post(String uri, Object body) { |
| 63 | // 1、创建 HttpHeaders 对象 | 79 | // 1、创建 HttpHeaders 对象 |
| 64 | HttpHeaders headers = new HttpHeaders(); | 80 | HttpHeaders headers = new HttpHeaders(); |
| 65 | headers.setContentType(MediaType.APPLICATION_JSON); | 81 | headers.setContentType(MediaType.APPLICATION_JSON); |
| 66 | - headers.set("Authorization", "Bearer ".concat(key)); | 82 | + headers.set("Authorization", "Bearer ".concat(midjourneyProperties.getKey())); |
| 67 | // 2、创建 HttpEntity 对象,将 HttpHeaders 和请求体传递给它 | 83 | // 2、创建 HttpEntity 对象,将 HttpHeaders 和请求体传递给它 |
| 68 | HttpEntity<String> requestEntity = new HttpEntity<>(JsonUtils.toJsonString(body), headers); | 84 | HttpEntity<String> requestEntity = new HttpEntity<>(JsonUtils.toJsonString(body), headers); |
| 69 | // 3、发送 post 请求 | 85 | // 3、发送 post 请求 |
| 70 | - return restTemplate.exchange(url.concat(uri), HttpMethod.POST, requestEntity, String.class); | 86 | + return restTemplate.exchange(midjourneyProperties.getUrl().concat(uri), HttpMethod.POST, requestEntity, String.class); |
| 71 | } | 87 | } |
| 88 | + | ||
| 72 | } | 89 | } |