Commit ecc3bd281c3680865546b83772d8bc8d2b3f2177
1 parent
c79273c5
【功能新增】AI:讯飞 PPT API 对接,测试用例完善
Showing
4 changed files
with
1112 additions
and
50 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/wenduoduo/api/WddApi.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/wenduoduo/api/WddPptApi.java
| ... | ... | @@ -33,7 +33,7 @@ import java.util.function.Predicate; |
| 33 | 33 | * @author xiaoxin |
| 34 | 34 | */ |
| 35 | 35 | @Slf4j |
| 36 | -public class WddApi { | |
| 36 | +public class WddPptApi { | |
| 37 | 37 | |
| 38 | 38 | public static final String BASE_URL = "https://docmee.cn"; |
| 39 | 39 | |
| ... | ... | @@ -49,7 +49,7 @@ public class WddApi { |
| 49 | 49 | sink.error(new IllegalStateException("[wdd-api] 调用失败!")); |
| 50 | 50 | }); |
| 51 | 51 | |
| 52 | - public WddApi(String baseUrl) { | |
| 52 | + public WddPptApi(String baseUrl) { | |
| 53 | 53 | this.webClient = WebClient.builder() |
| 54 | 54 | .baseUrl(baseUrl) |
| 55 | 55 | .defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON)) |
| ... | ... | @@ -162,71 +162,67 @@ public class WddApi { |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | - * 生成大纲内容 | |
| 165 | + * 分页查询PPT模板 | |
| 166 | 166 | * |
| 167 | - * @return 大纲内容流 | |
| 167 | + * @param token 令牌 | |
| 168 | + * @param request 请求体 | |
| 169 | + * @return 模板列表 | |
| 168 | 170 | */ |
| 169 | - public Flux<Map<String, Object>> generateOutlineContent(String token, GenerateOutlineRequest request) { | |
| 171 | + public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) { | |
| 170 | 172 | return this.webClient.post() |
| 171 | - .uri("/api/ppt/v2/generateContent") | |
| 173 | + .uri("/api/ppt/templates") | |
| 172 | 174 | .header("token", token) |
| 173 | - .body(Mono.just(request), GenerateOutlineRequest.class) | |
| 175 | + .bodyValue(request) | |
| 174 | 176 | .retrieve() |
| 175 | 177 | .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) |
| 176 | - .bodyToFlux(new ParameterizedTypeReference<>() { | |
| 177 | - }); | |
| 178 | + .bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() { | |
| 179 | + }) | |
| 180 | + .block(); | |
| 178 | 181 | } |
| 179 | 182 | |
| 180 | 183 | /** |
| 181 | - * 修改大纲内容 | |
| 184 | + * 生成大纲内容 | |
| 182 | 185 | * |
| 183 | - * @param id 任务ID | |
| 184 | - * @param markdown 大纲内容markdown | |
| 185 | - * @param question 用户修改建议 | |
| 186 | 186 | * @return 大纲内容流 |
| 187 | 187 | */ |
| 188 | - public Flux<Map<String, Object>> updateOutlineContent(String token, UpdateOutlineRequest request) { | |
| 188 | + public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) { | |
| 189 | 189 | return this.webClient.post() |
| 190 | - .uri("/api/ppt/v2/updateContent") | |
| 190 | + .uri("/api/ppt/v2/generateContent") | |
| 191 | 191 | .header("token", token) |
| 192 | - .body(Mono.just(request), UpdateOutlineRequest.class) | |
| 192 | + .body(Mono.just(request), CreateOutlineRequest.class) | |
| 193 | 193 | .retrieve() |
| 194 | 194 | .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) |
| 195 | 195 | .bodyToFlux(new ParameterizedTypeReference<>() { |
| 196 | 196 | }); |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - | |
| 200 | 199 | /** |
| 201 | - * 分页查询PPT模板 | |
| 200 | + * 修改大纲内容 | |
| 202 | 201 | * |
| 203 | - * @param token 令牌 | |
| 204 | 202 | * @param request 请求体 |
| 205 | - * @return 模板列表 | |
| 203 | + * @return 大纲内容流 | |
| 206 | 204 | */ |
| 207 | - public PagePptTemplateInfo getPptTemplatePage(String token, TemplateQueryRequest request) { | |
| 205 | + public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) { | |
| 208 | 206 | return this.webClient.post() |
| 209 | - .uri("/api/ppt/templates") | |
| 207 | + .uri("/api/ppt/v2/updateContent") | |
| 210 | 208 | .header("token", token) |
| 211 | - .bodyValue(request) | |
| 209 | + .body(Mono.just(request), UpdateOutlineRequest.class) | |
| 212 | 210 | .retrieve() |
| 213 | 211 | .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) |
| 214 | - .bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() { | |
| 215 | - }) | |
| 216 | - .block(); | |
| 212 | + .bodyToFlux(new ParameterizedTypeReference<>() { | |
| 213 | + }); | |
| 217 | 214 | } |
| 218 | 215 | |
| 219 | - | |
| 220 | 216 | /** |
| 221 | 217 | * 生成PPT |
| 222 | 218 | * |
| 223 | 219 | * @return PPT信息 |
| 224 | 220 | */ |
| 225 | - public PptInfo generatePptx(String token, GeneratePptxRequest request) { | |
| 221 | + public PptInfo create(String token, CreatePptRequest request) { | |
| 226 | 222 | return this.webClient.post() |
| 227 | 223 | .uri("/api/ppt/v2/generatePptx") |
| 228 | 224 | .header("token", token) |
| 229 | - .body(Mono.just(request), GeneratePptxRequest.class) | |
| 225 | + .body(Mono.just(request), CreatePptRequest.class) | |
| 230 | 226 | .retrieve() |
| 231 | 227 | .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) |
| 232 | 228 | .bodyToMono(ApiResponse.class) |
| ... | ... | @@ -278,7 +274,7 @@ public class WddApi { |
| 278 | 274 | * 生成大纲内容请求 |
| 279 | 275 | */ |
| 280 | 276 | @JsonInclude(value = JsonInclude.Include.NON_NULL) |
| 281 | - public record GenerateOutlineRequest( | |
| 277 | + public record CreateOutlineRequest( | |
| 282 | 278 | String id, |
| 283 | 279 | String length, |
| 284 | 280 | String scene, |
| ... | ... | @@ -303,7 +299,7 @@ public class WddApi { |
| 303 | 299 | * 生成PPT请求 |
| 304 | 300 | */ |
| 305 | 301 | @JsonInclude(value = JsonInclude.Include.NON_NULL) |
| 306 | - public record GeneratePptxRequest( | |
| 302 | + public record CreatePptRequest( | |
| 307 | 303 | String id, |
| 308 | 304 | String templateId, |
| 309 | 305 | String markdown | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/xunfei/api/XunfeiPptApi.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.core.model.xunfei.api; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; | |
| 5 | +import lombok.extern.slf4j.Slf4j; | |
| 6 | +import org.springframework.core.io.ByteArrayResource; | |
| 7 | +import org.springframework.http.HttpStatusCode; | |
| 8 | +import org.springframework.http.MediaType; | |
| 9 | +import org.springframework.util.LinkedMultiValueMap; | |
| 10 | +import org.springframework.util.MultiValueMap; | |
| 11 | +import org.springframework.web.multipart.MultipartFile; | |
| 12 | +import org.springframework.web.reactive.function.BodyInserters; | |
| 13 | +import org.springframework.web.reactive.function.client.ClientResponse; | |
| 14 | +import org.springframework.web.reactive.function.client.WebClient; | |
| 15 | +import reactor.core.publisher.Mono; | |
| 16 | + | |
| 17 | +import javax.crypto.Mac; | |
| 18 | +import javax.crypto.spec.SecretKeySpec; | |
| 19 | +import java.io.IOException; | |
| 20 | +import java.nio.charset.StandardCharsets; | |
| 21 | +import java.security.InvalidKeyException; | |
| 22 | +import java.security.MessageDigest; | |
| 23 | +import java.security.NoSuchAlgorithmException; | |
| 24 | +import java.util.Base64; | |
| 25 | +import java.util.HashMap; | |
| 26 | +import java.util.List; | |
| 27 | +import java.util.Map; | |
| 28 | +import java.util.function.Function; | |
| 29 | +import java.util.function.Predicate; | |
| 30 | + | |
| 31 | +/** | |
| 32 | + * 讯飞智能PPT生成 API | |
| 33 | + * <p> | |
| 34 | + * 对接讯飞:<a href="https://www.xfyun.cn/doc/spark/PPTv2.html">智能 PPT 生成 API</a> | |
| 35 | + * | |
| 36 | + * @author xiaoxin | |
| 37 | + */ | |
| 38 | +@Slf4j | |
| 39 | +public class XunfeiPptApi { | |
| 40 | + | |
| 41 | + public static final String BASE_URL = "https://zwapi.xfyun.cn/api/ppt/v2"; | |
| 42 | + | |
| 43 | + private final WebClient webClient; | |
| 44 | + private final String appId; | |
| 45 | + private final String apiSecret; | |
| 46 | + | |
| 47 | + private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful(); | |
| 48 | + | |
| 49 | + private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION = | |
| 50 | + reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> { | |
| 51 | + log.error("[xunfei-ppt-api] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody); | |
| 52 | + sink.error(new IllegalStateException("[xunfei-ppt-api] 调用失败!")); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + public XunfeiPptApi(String baseUrl, String appId, String apiSecret) { | |
| 56 | + this.webClient = WebClient.builder() | |
| 57 | + .baseUrl(baseUrl) | |
| 58 | + .build(); | |
| 59 | + this.appId = appId; | |
| 60 | + this.apiSecret = apiSecret; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * 获取签名 | |
| 65 | + * | |
| 66 | + * @return 签名信息 | |
| 67 | + */ | |
| 68 | + private SignatureInfo getSignature() { | |
| 69 | + long timestamp = System.currentTimeMillis() / 1000; | |
| 70 | + String ts = String.valueOf(timestamp); | |
| 71 | + String signature = generateSignature(appId, apiSecret, timestamp); | |
| 72 | + return new SignatureInfo(appId, ts, signature); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * 生成签名 | |
| 77 | + * | |
| 78 | + * @param appId 应用ID | |
| 79 | + * @param apiSecret 应用密钥 | |
| 80 | + * @param timestamp 时间戳(秒) | |
| 81 | + * @return 签名 | |
| 82 | + */ | |
| 83 | + private String generateSignature(String appId, String apiSecret, long timestamp) { | |
| 84 | + try { | |
| 85 | + String auth = md5(appId + timestamp); | |
| 86 | + return hmacSHA1Encrypt(auth, apiSecret); | |
| 87 | + } catch (NoSuchAlgorithmException | InvalidKeyException e) { | |
| 88 | + log.error("[xunfei-ppt-api] 生成签名失败", e); | |
| 89 | + throw new IllegalStateException("[xunfei-ppt-api] 生成签名失败"); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * HMAC SHA1 加密 | |
| 95 | + */ | |
| 96 | + private String hmacSHA1Encrypt(String encryptText, String encryptKey) | |
| 97 | + throws NoSuchAlgorithmException, InvalidKeyException { | |
| 98 | + SecretKeySpec keySpec = new SecretKeySpec( | |
| 99 | + encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1"); | |
| 100 | + | |
| 101 | + Mac mac = Mac.getInstance("HmacSHA1"); | |
| 102 | + mac.init(keySpec); | |
| 103 | + byte[] result = mac.doFinal(encryptText.getBytes(StandardCharsets.UTF_8)); | |
| 104 | + | |
| 105 | + return Base64.getEncoder().encodeToString(result); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * MD5 哈希 | |
| 110 | + */ | |
| 111 | + private String md5(String text) throws NoSuchAlgorithmException { | |
| 112 | + MessageDigest md = MessageDigest.getInstance("MD5"); | |
| 113 | + byte[] digest = md.digest(text.getBytes(StandardCharsets.UTF_8)); | |
| 114 | + | |
| 115 | + StringBuilder sb = new StringBuilder(); | |
| 116 | + for (byte b : digest) { | |
| 117 | + sb.append(String.format("%02x", b)); | |
| 118 | + } | |
| 119 | + return sb.toString(); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * 获取PPT模板列表 | |
| 124 | + * | |
| 125 | + * @param style 风格,如"商务" | |
| 126 | + * @param pageSize 每页数量 | |
| 127 | + * @return 模板列表 | |
| 128 | + */ | |
| 129 | + public TemplatePageResponse getTemplatePage(String style, Integer pageSize) { | |
| 130 | + SignatureInfo signInfo = getSignature(); | |
| 131 | + Map<String, Object> requestBody = new HashMap<>(); | |
| 132 | + requestBody.put("style", style); | |
| 133 | + requestBody.put("pageSize", pageSize != null ? pageSize.toString() : "10"); | |
| 134 | + | |
| 135 | + return this.webClient.post() | |
| 136 | + .uri("/template/list") | |
| 137 | + .header("appId", signInfo.appId) | |
| 138 | + .header("timestamp", signInfo.timestamp) | |
| 139 | + .header("signature", signInfo.signature) | |
| 140 | + .contentType(MediaType.APPLICATION_JSON) | |
| 141 | + .bodyValue(requestBody) | |
| 142 | + .retrieve() | |
| 143 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(requestBody)) | |
| 144 | + .bodyToMono(TemplatePageResponse.class) | |
| 145 | + .block(); | |
| 146 | + } | |
| 147 | + | |
| 148 | + /** | |
| 149 | + * 创建大纲(通过文本) | |
| 150 | + * | |
| 151 | + * @param query 查询文本 | |
| 152 | + * @return 大纲创建响应 | |
| 153 | + */ | |
| 154 | + public CreateResponse createOutline(String query) { | |
| 155 | + SignatureInfo signInfo = getSignature(); | |
| 156 | + MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>(); | |
| 157 | + formData.add("query", query); | |
| 158 | + | |
| 159 | + return this.webClient.post() | |
| 160 | + .uri("/createOutline") | |
| 161 | + .header("appId", signInfo.appId) | |
| 162 | + .header("timestamp", signInfo.timestamp) | |
| 163 | + .header("signature", signInfo.signature) | |
| 164 | + .contentType(MediaType.MULTIPART_FORM_DATA) | |
| 165 | + .body(BodyInserters.fromMultipartData(formData)) | |
| 166 | + .retrieve() | |
| 167 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData)) | |
| 168 | + .bodyToMono(CreateResponse.class) | |
| 169 | + .block(); | |
| 170 | + } | |
| 171 | + | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * 直接创建PPT(简化版 - 通过文本) | |
| 175 | + * | |
| 176 | + * @param query 查询文本 | |
| 177 | + * @return 创建响应 | |
| 178 | + */ | |
| 179 | + public CreateResponse create(String query) { | |
| 180 | + CreatePptRequest request = CreatePptRequest.builder() | |
| 181 | + .query(query) | |
| 182 | + .build(); | |
| 183 | + return create(request); | |
| 184 | + } | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * 直接创建PPT(简化版 - 通过文件) | |
| 188 | + * | |
| 189 | + * @param file 文件 | |
| 190 | + * @param fileName 文件名 | |
| 191 | + * @return 创建响应 | |
| 192 | + */ | |
| 193 | + public CreateResponse create(MultipartFile file, String fileName) { | |
| 194 | + CreatePptRequest request = CreatePptRequest.builder() | |
| 195 | + .file(file) | |
| 196 | + .fileName(fileName) | |
| 197 | + .build(); | |
| 198 | + return create(request); | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * 直接创建PPT(完整版) | |
| 203 | + * | |
| 204 | + * @param request 请求参数 | |
| 205 | + * @return 创建响应 | |
| 206 | + */ | |
| 207 | + public CreateResponse create(CreatePptRequest request) { | |
| 208 | + SignatureInfo signInfo = getSignature(); | |
| 209 | + MultiValueMap<String, Object> formData = buildCreateFormData(request); | |
| 210 | + | |
| 211 | + return this.webClient.post() | |
| 212 | + .uri("/create") | |
| 213 | + .header("appId", signInfo.appId) | |
| 214 | + .header("timestamp", signInfo.timestamp) | |
| 215 | + .header("signature", signInfo.signature) | |
| 216 | + .contentType(MediaType.MULTIPART_FORM_DATA) | |
| 217 | + .body(BodyInserters.fromMultipartData(formData)) | |
| 218 | + .retrieve() | |
| 219 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData)) | |
| 220 | + .bodyToMono(CreateResponse.class) | |
| 221 | + .block(); | |
| 222 | + } | |
| 223 | + | |
| 224 | + | |
| 225 | + /** | |
| 226 | + * 通过大纲创建PPT(简化版) | |
| 227 | + * | |
| 228 | + * @param outline 大纲内容 | |
| 229 | + * @param query 查询文本 | |
| 230 | + * @return 创建响应 | |
| 231 | + */ | |
| 232 | + public CreateResponse createPptByOutline(OutlineData outline, String query) { | |
| 233 | + CreatePptByOutlineRequest request = CreatePptByOutlineRequest.builder() | |
| 234 | + .outline(outline) | |
| 235 | + .query(query) | |
| 236 | + .build(); | |
| 237 | + return createPptByOutline(request); | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * 通过大纲创建PPT(完整版) | |
| 242 | + * | |
| 243 | + * @param request 请求参数 | |
| 244 | + * @return 创建响应 | |
| 245 | + */ | |
| 246 | + public CreateResponse createPptByOutline(CreatePptByOutlineRequest request) { | |
| 247 | + SignatureInfo signInfo = getSignature(); | |
| 248 | + | |
| 249 | + return this.webClient.post() | |
| 250 | + .uri("/createPptByOutline") | |
| 251 | + .header("appId", signInfo.appId) | |
| 252 | + .header("timestamp", signInfo.timestamp) | |
| 253 | + .header("signature", signInfo.signature) | |
| 254 | + .contentType(MediaType.APPLICATION_JSON) | |
| 255 | + .bodyValue(request) | |
| 256 | + .retrieve() | |
| 257 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 258 | + .bodyToMono(CreateResponse.class) | |
| 259 | + .block(); | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * 检查PPT生成进度 | |
| 264 | + * | |
| 265 | + * @param sid 任务ID | |
| 266 | + * @return 进度响应 | |
| 267 | + */ | |
| 268 | + public ProgressResponse checkProgress(String sid) { | |
| 269 | + SignatureInfo signInfo = getSignature(); | |
| 270 | + | |
| 271 | + return this.webClient.get() | |
| 272 | + .uri(uriBuilder -> uriBuilder | |
| 273 | + .path("/progress") | |
| 274 | + .queryParam("sid", sid) | |
| 275 | + .build()) | |
| 276 | + .header("appId", signInfo.appId) | |
| 277 | + .header("timestamp", signInfo.timestamp) | |
| 278 | + .header("signature", signInfo.signature) | |
| 279 | + .retrieve() | |
| 280 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(sid)) | |
| 281 | + .bodyToMono(ProgressResponse.class) | |
| 282 | + .block(); | |
| 283 | + } | |
| 284 | + | |
| 285 | + /** | |
| 286 | + * 签名信息 | |
| 287 | + */ | |
| 288 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 289 | + private record SignatureInfo( | |
| 290 | + String appId, | |
| 291 | + String timestamp, | |
| 292 | + String signature | |
| 293 | + ) { | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * 模板列表响应 | |
| 298 | + */ | |
| 299 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 300 | + public record TemplatePageResponse( | |
| 301 | + boolean flag, | |
| 302 | + int code, | |
| 303 | + String desc, | |
| 304 | + Integer count, | |
| 305 | + TemplatePageData data | |
| 306 | + ) { | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * 模板列表数据 | |
| 311 | + */ | |
| 312 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 313 | + public record TemplatePageData( | |
| 314 | + String total, | |
| 315 | + List<TemplateInfo> records, | |
| 316 | + Integer pageNum | |
| 317 | + ) { | |
| 318 | + } | |
| 319 | + | |
| 320 | + /** | |
| 321 | + * 模板信息 | |
| 322 | + */ | |
| 323 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 324 | + public record TemplateInfo( | |
| 325 | + String templateIndexId, | |
| 326 | + Integer pageCount, | |
| 327 | + String type, | |
| 328 | + String color, | |
| 329 | + String industry, | |
| 330 | + String style, | |
| 331 | + String detailImage | |
| 332 | + ) { | |
| 333 | + } | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * 创建响应 | |
| 337 | + */ | |
| 338 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 339 | + public record CreateResponse( | |
| 340 | + boolean flag, | |
| 341 | + int code, | |
| 342 | + String desc, | |
| 343 | + Integer count, | |
| 344 | + CreateResponseData data | |
| 345 | + ) { | |
| 346 | + } | |
| 347 | + | |
| 348 | + /** | |
| 349 | + * 创建响应数据 | |
| 350 | + */ | |
| 351 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 352 | + public record CreateResponseData( | |
| 353 | + String sid, | |
| 354 | + String coverImgSrc, | |
| 355 | + String title, | |
| 356 | + String subTitle, | |
| 357 | + OutlineData outline | |
| 358 | + ) { | |
| 359 | + } | |
| 360 | + | |
| 361 | + /** | |
| 362 | + * 大纲数据结构 | |
| 363 | + */ | |
| 364 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 365 | + public record OutlineData( | |
| 366 | + String title, | |
| 367 | + String subTitle, | |
| 368 | + List<Chapter> chapters | |
| 369 | + ) { | |
| 370 | + /** | |
| 371 | + * 章节结构 | |
| 372 | + */ | |
| 373 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 374 | + public record Chapter( | |
| 375 | + String chapterTitle, | |
| 376 | + List<ChapterContent> chapterContents | |
| 377 | + ) { | |
| 378 | + /** | |
| 379 | + * 章节内容 | |
| 380 | + */ | |
| 381 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 382 | + public record ChapterContent( | |
| 383 | + String chapterTitle | |
| 384 | + ) { | |
| 385 | + } | |
| 386 | + } | |
| 387 | + | |
| 388 | + /** | |
| 389 | + * 将大纲对象转换为JSON字符串 | |
| 390 | + * | |
| 391 | + * @return 大纲JSON字符串 | |
| 392 | + */ | |
| 393 | + public String toJsonString() { | |
| 394 | + return JsonUtils.toJsonString(this); | |
| 395 | + } | |
| 396 | + } | |
| 397 | + | |
| 398 | + /** | |
| 399 | + * 进度响应 | |
| 400 | + */ | |
| 401 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 402 | + public record ProgressResponse( | |
| 403 | + int code, | |
| 404 | + String desc, | |
| 405 | + ProgressResponseData data | |
| 406 | + ) { | |
| 407 | + } | |
| 408 | + | |
| 409 | + /** | |
| 410 | + * 进度响应数据 | |
| 411 | + */ | |
| 412 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 413 | + public record ProgressResponseData( | |
| 414 | + int process, | |
| 415 | + String pptId, | |
| 416 | + String pptUrl, | |
| 417 | + String pptStatus, // PPT构建状态:building(构建中),done(已完成),build_failed(生成失败) | |
| 418 | + String aiImageStatus, // ai配图状态:building(构建中),done(已完成) | |
| 419 | + String cardNoteStatus, // 演讲备注状态:building(构建中),done(已完成) | |
| 420 | + String errMsg, // 生成PPT的失败信息 | |
| 421 | + Integer totalPages, // 生成PPT的总页数 | |
| 422 | + Integer donePages // 生成PPT的完成页数 | |
| 423 | + ) { | |
| 424 | + /** | |
| 425 | + * 是否全部完成 | |
| 426 | + * | |
| 427 | + * @return 是否全部完成 | |
| 428 | + */ | |
| 429 | + public boolean isAllDone() { | |
| 430 | + return "done".equals(pptStatus) | |
| 431 | + && ("done".equals(aiImageStatus) || aiImageStatus == null) | |
| 432 | + && ("done".equals(cardNoteStatus) || cardNoteStatus == null); | |
| 433 | + } | |
| 434 | + | |
| 435 | + /** | |
| 436 | + * 是否失败 | |
| 437 | + * | |
| 438 | + * @return 是否失败 | |
| 439 | + */ | |
| 440 | + public boolean isFailed() { | |
| 441 | + return "build_failed".equals(pptStatus); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * 获取进度百分比 | |
| 446 | + * | |
| 447 | + * @return 进度百分比 | |
| 448 | + */ | |
| 449 | + public int getProgressPercent() { | |
| 450 | + if (totalPages == null || totalPages == 0 || donePages == null) { | |
| 451 | + return process; // 兼容旧版返回 | |
| 452 | + } | |
| 453 | + return (int) (donePages * 100.0 / totalPages); | |
| 454 | + } | |
| 455 | + } | |
| 456 | + | |
| 457 | + /** | |
| 458 | + * 通过大纲创建PPT请求参数 | |
| 459 | + */ | |
| 460 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 461 | + public record CreatePptByOutlineRequest( | |
| 462 | + String query, // 用户生成PPT要求(最多8000字) | |
| 463 | + String outlineSid, // 已生成大纲后,响应返回的请求大纲唯一id | |
| 464 | + OutlineData outline, // 大纲内容 | |
| 465 | + String templateId, // 模板ID | |
| 466 | + String businessId, // 业务ID(非必传) | |
| 467 | + String author, // PPT作者名 | |
| 468 | + Boolean isCardNote, // 是否生成PPT演讲备注 | |
| 469 | + Boolean search, // 是否联网搜索 | |
| 470 | + String language, // 语种 | |
| 471 | + String fileUrl, // 文件地址 | |
| 472 | + String fileName, // 文件名(带文件名后缀) | |
| 473 | + Boolean isFigure, // 是否自动配图 | |
| 474 | + String aiImage // ai配图类型:normal、advanced | |
| 475 | + ) { | |
| 476 | + /** | |
| 477 | + * 创建构建器 | |
| 478 | + * | |
| 479 | + * @return 构建器 | |
| 480 | + */ | |
| 481 | + public static Builder builder() { | |
| 482 | + return new Builder(); | |
| 483 | + } | |
| 484 | + | |
| 485 | + /** | |
| 486 | + * 构建器类 | |
| 487 | + */ | |
| 488 | + public static class Builder { | |
| 489 | + private String query; | |
| 490 | + private String outlineSid; | |
| 491 | + private OutlineData outline; | |
| 492 | + private String templateId; | |
| 493 | + private String businessId; | |
| 494 | + private String author; | |
| 495 | + private Boolean isCardNote; | |
| 496 | + private Boolean search; | |
| 497 | + private String language; | |
| 498 | + private String fileUrl; | |
| 499 | + private String fileName; | |
| 500 | + private Boolean isFigure; | |
| 501 | + private String aiImage; | |
| 502 | + | |
| 503 | + public Builder query(String query) { | |
| 504 | + this.query = query; | |
| 505 | + return this; | |
| 506 | + } | |
| 507 | + | |
| 508 | + public Builder outlineSid(String outlineSid) { | |
| 509 | + this.outlineSid = outlineSid; | |
| 510 | + return this; | |
| 511 | + } | |
| 512 | + | |
| 513 | + public Builder outline(OutlineData outline) { | |
| 514 | + this.outline = outline; | |
| 515 | + return this; | |
| 516 | + } | |
| 517 | + | |
| 518 | + public Builder templateId(String templateId) { | |
| 519 | + this.templateId = templateId; | |
| 520 | + return this; | |
| 521 | + } | |
| 522 | + | |
| 523 | + public Builder businessId(String businessId) { | |
| 524 | + this.businessId = businessId; | |
| 525 | + return this; | |
| 526 | + } | |
| 527 | + | |
| 528 | + public Builder author(String author) { | |
| 529 | + this.author = author; | |
| 530 | + return this; | |
| 531 | + } | |
| 532 | + | |
| 533 | + public Builder isCardNote(Boolean isCardNote) { | |
| 534 | + this.isCardNote = isCardNote; | |
| 535 | + return this; | |
| 536 | + } | |
| 537 | + | |
| 538 | + public Builder search(Boolean search) { | |
| 539 | + this.search = search; | |
| 540 | + return this; | |
| 541 | + } | |
| 542 | + | |
| 543 | + public Builder language(String language) { | |
| 544 | + this.language = language; | |
| 545 | + return this; | |
| 546 | + } | |
| 547 | + | |
| 548 | + public Builder fileUrl(String fileUrl) { | |
| 549 | + this.fileUrl = fileUrl; | |
| 550 | + return this; | |
| 551 | + } | |
| 552 | + | |
| 553 | + public Builder fileName(String fileName) { | |
| 554 | + this.fileName = fileName; | |
| 555 | + return this; | |
| 556 | + } | |
| 557 | + | |
| 558 | + public Builder isFigure(Boolean isFigure) { | |
| 559 | + this.isFigure = isFigure; | |
| 560 | + return this; | |
| 561 | + } | |
| 562 | + | |
| 563 | + public Builder aiImage(String aiImage) { | |
| 564 | + this.aiImage = aiImage; | |
| 565 | + return this; | |
| 566 | + } | |
| 567 | + | |
| 568 | + public CreatePptByOutlineRequest build() { | |
| 569 | + return new CreatePptByOutlineRequest( | |
| 570 | + query, outlineSid, outline, templateId, businessId, author, | |
| 571 | + isCardNote, search, language, fileUrl, fileName, isFigure, aiImage | |
| 572 | + ); | |
| 573 | + } | |
| 574 | + } | |
| 575 | + } | |
| 576 | + | |
| 577 | + /** | |
| 578 | + * 构建创建PPT的表单数据 | |
| 579 | + * | |
| 580 | + * @param request 请求参数 | |
| 581 | + * @return 表单数据 | |
| 582 | + */ | |
| 583 | + private MultiValueMap<String, Object> buildCreateFormData(CreatePptRequest request) { | |
| 584 | + MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>(); | |
| 585 | + // 添加请求参数 | |
| 586 | + if (request.query() != null) { | |
| 587 | + formData.add("query", request.query()); | |
| 588 | + } | |
| 589 | + | |
| 590 | + if (request.file() != null) { | |
| 591 | + try { | |
| 592 | + formData.add("file", new ByteArrayResource(request.file().getBytes()) { | |
| 593 | + @Override | |
| 594 | + public String getFilename() { | |
| 595 | + return request.file().getOriginalFilename(); | |
| 596 | + } | |
| 597 | + }); | |
| 598 | + } catch (IOException e) { | |
| 599 | + log.error("[xunfei-ppt-api] 文件处理失败", e); | |
| 600 | + throw new IllegalStateException("[xunfei-ppt-api] 文件处理失败", e); | |
| 601 | + } | |
| 602 | + } | |
| 603 | + | |
| 604 | + if (request.fileUrl() != null) { | |
| 605 | + formData.add("fileUrl", request.fileUrl()); | |
| 606 | + } | |
| 607 | + | |
| 608 | + if (request.fileName() != null) { | |
| 609 | + formData.add("fileName", request.fileName()); | |
| 610 | + } | |
| 611 | + | |
| 612 | + if (request.templateId() != null) { | |
| 613 | + formData.add("templateId", request.templateId()); | |
| 614 | + } | |
| 615 | + | |
| 616 | + if (request.businessId() != null) { | |
| 617 | + formData.add("businessId", request.businessId()); | |
| 618 | + } | |
| 619 | + | |
| 620 | + if (request.author() != null) { | |
| 621 | + formData.add("author", request.author()); | |
| 622 | + } | |
| 623 | + | |
| 624 | + if (request.isCardNote() != null) { | |
| 625 | + formData.add("isCardNote", request.isCardNote().toString()); | |
| 626 | + } | |
| 627 | + | |
| 628 | + if (request.search() != null) { | |
| 629 | + formData.add("search", request.search().toString()); | |
| 630 | + } | |
| 631 | + | |
| 632 | + if (request.language() != null) { | |
| 633 | + formData.add("language", request.language()); | |
| 634 | + } | |
| 635 | + | |
| 636 | + if (request.isFigure() != null) { | |
| 637 | + formData.add("isFigure", request.isFigure().toString()); | |
| 638 | + } | |
| 639 | + | |
| 640 | + if (request.aiImage() != null) { | |
| 641 | + formData.add("aiImage", request.aiImage()); | |
| 642 | + } | |
| 643 | + | |
| 644 | + return formData; | |
| 645 | + } | |
| 646 | + | |
| 647 | + /** | |
| 648 | + * 直接生成PPT请求参数 | |
| 649 | + */ | |
| 650 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 651 | + public record CreatePptRequest( | |
| 652 | + String query, // 用户生成PPT要求(最多8000字) | |
| 653 | + MultipartFile file, // 上传文件 | |
| 654 | + String fileUrl, // 文件地址 | |
| 655 | + String fileName, // 文件名(带文件名后缀) | |
| 656 | + String templateId, // 模板ID | |
| 657 | + String businessId, // 业务ID(非必传) | |
| 658 | + String author, // PPT作者名 | |
| 659 | + Boolean isCardNote, // 是否生成PPT演讲备注 | |
| 660 | + Boolean search, // 是否联网搜索 | |
| 661 | + String language, // 语种 | |
| 662 | + Boolean isFigure, // 是否自动配图 | |
| 663 | + String aiImage // ai配图类型:normal、advanced | |
| 664 | + ) { | |
| 665 | + /** | |
| 666 | + * 创建构建器 | |
| 667 | + * | |
| 668 | + * @return 构建器 | |
| 669 | + */ | |
| 670 | + public static Builder builder() { | |
| 671 | + return new Builder(); | |
| 672 | + } | |
| 673 | + | |
| 674 | + /** | |
| 675 | + * 构建器类 | |
| 676 | + */ | |
| 677 | + public static class Builder { | |
| 678 | + private String query; | |
| 679 | + private MultipartFile file; | |
| 680 | + private String fileUrl; | |
| 681 | + private String fileName; | |
| 682 | + private String templateId; | |
| 683 | + private String businessId; | |
| 684 | + private String author; | |
| 685 | + private Boolean isCardNote; | |
| 686 | + private Boolean search; | |
| 687 | + private String language; | |
| 688 | + private Boolean isFigure; | |
| 689 | + private String aiImage; | |
| 690 | + | |
| 691 | + public Builder query(String query) { | |
| 692 | + this.query = query; | |
| 693 | + return this; | |
| 694 | + } | |
| 695 | + | |
| 696 | + public Builder file(MultipartFile file) { | |
| 697 | + this.file = file; | |
| 698 | + return this; | |
| 699 | + } | |
| 700 | + | |
| 701 | + public Builder fileUrl(String fileUrl) { | |
| 702 | + this.fileUrl = fileUrl; | |
| 703 | + return this; | |
| 704 | + } | |
| 705 | + | |
| 706 | + public Builder fileName(String fileName) { | |
| 707 | + this.fileName = fileName; | |
| 708 | + return this; | |
| 709 | + } | |
| 710 | + | |
| 711 | + public Builder templateId(String templateId) { | |
| 712 | + this.templateId = templateId; | |
| 713 | + return this; | |
| 714 | + } | |
| 715 | + | |
| 716 | + public Builder businessId(String businessId) { | |
| 717 | + this.businessId = businessId; | |
| 718 | + return this; | |
| 719 | + } | |
| 720 | + | |
| 721 | + public Builder author(String author) { | |
| 722 | + this.author = author; | |
| 723 | + return this; | |
| 724 | + } | |
| 725 | + | |
| 726 | + public Builder isCardNote(Boolean isCardNote) { | |
| 727 | + this.isCardNote = isCardNote; | |
| 728 | + return this; | |
| 729 | + } | |
| 730 | + | |
| 731 | + public Builder search(Boolean search) { | |
| 732 | + this.search = search; | |
| 733 | + return this; | |
| 734 | + } | |
| 735 | + | |
| 736 | + public Builder language(String language) { | |
| 737 | + this.language = language; | |
| 738 | + return this; | |
| 739 | + } | |
| 740 | + | |
| 741 | + public Builder isFigure(Boolean isFigure) { | |
| 742 | + this.isFigure = isFigure; | |
| 743 | + return this; | |
| 744 | + } | |
| 745 | + | |
| 746 | + public Builder aiImage(String aiImage) { | |
| 747 | + this.aiImage = aiImage; | |
| 748 | + return this; | |
| 749 | + } | |
| 750 | + | |
| 751 | + public CreatePptRequest build() { | |
| 752 | + // 验证参数 | |
| 753 | + if (query == null && file == null && fileUrl == null) { | |
| 754 | + throw new IllegalArgumentException("query、file、fileUrl必填其一"); | |
| 755 | + } | |
| 756 | + if ((file != null || fileUrl != null) && fileName == null) { | |
| 757 | + throw new IllegalArgumentException("如果传file或者fileUrl,fileName必填"); | |
| 758 | + } | |
| 759 | + return new CreatePptRequest( | |
| 760 | + query, file, fileUrl, fileName, templateId, businessId, author, | |
| 761 | + isCardNote, search, language, isFigure, aiImage | |
| 762 | + ); | |
| 763 | + } | |
| 764 | + } | |
| 765 | + } | |
| 766 | +} | |
| 0 | 767 | \ No newline at end of file | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/wdd/WddApiTests.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/wdd/WddPptApiTests.java
| 1 | 1 | package cn.iocoder.yudao.framework.ai.ppt.wdd; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddApi; | |
| 3 | +import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddPptApi; | |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 5 | 5 | import org.junit.jupiter.api.Disabled; |
| 6 | 6 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -11,26 +11,26 @@ import java.util.Objects; |
| 11 | 11 | |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | - * {@link WddApi} 集成测试 | |
| 14 | + * {@link WddPptApi} 集成测试 | |
| 15 | 15 | * |
| 16 | 16 | * @author xiaoxin |
| 17 | 17 | */ |
| 18 | -public class WddApiTests { | |
| 18 | +public class WddPptApiTests { | |
| 19 | 19 | |
| 20 | - private final WddApi wddApi = new WddApi("https://docmee.cn"); | |
| 20 | + private final WddPptApi wddPptApi = new WddPptApi("https://docmee.cn"); | |
| 21 | 21 | |
| 22 | 22 | |
| 23 | - private final String token = "sk_FJo7sKErrrEs5CIZz1"; | |
| 23 | + private final String token = ""; | |
| 24 | 24 | |
| 25 | 25 | |
| 26 | 26 | @Test //获取token |
| 27 | 27 | @Disabled |
| 28 | 28 | public void testCreateApiToken() { |
| 29 | 29 | // 准备参数 |
| 30 | - String apiKey = "ak_RK1rm7TrEv3E3JSWIK"; | |
| 31 | - WddApi.CreateTokenRequest request = new WddApi.CreateTokenRequest(apiKey); | |
| 30 | + String apiKey = ""; | |
| 31 | + WddPptApi.CreateTokenRequest request = new WddPptApi.CreateTokenRequest(apiKey); | |
| 32 | 32 | // 调用方法 |
| 33 | - String token = wddApi.createApiToken(request); | |
| 33 | + String token = wddPptApi.createApiToken(request); | |
| 34 | 34 | // 打印结果 |
| 35 | 35 | System.out.println(token); |
| 36 | 36 | } |
| ... | ... | @@ -39,7 +39,7 @@ public class WddApiTests { |
| 39 | 39 | @Test // 创建任务 |
| 40 | 40 | @Disabled |
| 41 | 41 | public void testCreateTask() { |
| 42 | - WddApi.ApiResponse apiResponse = wddApi.createTask(token, 1, "dify 介绍", null); | |
| 42 | + WddPptApi.ApiResponse apiResponse = wddPptApi.createTask(token, 1, "dify 介绍", null); | |
| 43 | 43 | System.out.println(apiResponse); |
| 44 | 44 | } |
| 45 | 45 | |
| ... | ... | @@ -47,9 +47,9 @@ public class WddApiTests { |
| 47 | 47 | @Test // 创建大纲 |
| 48 | 48 | @Disabled |
| 49 | 49 | public void testGenerateOutlineRequest() { |
| 50 | - WddApi.GenerateOutlineRequest request = new WddApi.GenerateOutlineRequest("1901449466163105792", "medium", null, null, null, null); | |
| 50 | + WddPptApi.CreateOutlineRequest request = new WddPptApi.CreateOutlineRequest("1901539019628613632", "medium", null, null, null, null); | |
| 51 | 51 | //调用 |
| 52 | - Flux<Map<String, Object>> flux = wddApi.generateOutlineContent(token, request); | |
| 52 | + Flux<Map<String, Object>> flux = wddPptApi.createOutline(token, request); | |
| 53 | 53 | StringBuffer contentBuffer = new StringBuffer(); |
| 54 | 54 | flux.doOnNext(chunk -> { |
| 55 | 55 | contentBuffer.append(chunk.get("text")); |
| ... | ... | @@ -66,10 +66,10 @@ public class WddApiTests { |
| 66 | 66 | |
| 67 | 67 | @Test // 修改大纲 |
| 68 | 68 | @Disabled |
| 69 | - public void testUpdateOutlineContentRequest() { | |
| 70 | - WddApi.UpdateOutlineRequest request = new WddApi.UpdateOutlineRequest("1901449466163105792", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可"); | |
| 69 | + public void testUpdateOutlineRequest() { | |
| 70 | + WddPptApi.UpdateOutlineRequest request = new WddPptApi.UpdateOutlineRequest("1901539019628613632", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可"); | |
| 71 | 71 | //调用 |
| 72 | - Flux<Map<String, Object>> flux = wddApi.updateOutlineContent(token, request); | |
| 72 | + Flux<Map<String, Object>> flux = wddPptApi.updateOutline(token, request); | |
| 73 | 73 | StringBuffer contentBuffer = new StringBuffer(); |
| 74 | 74 | flux.doOnNext(chunk -> { |
| 75 | 75 | contentBuffer.append(chunk.get("text")); |
| ... | ... | @@ -88,10 +88,10 @@ public class WddApiTests { |
| 88 | 88 | @Disabled |
| 89 | 89 | public void testGetPptTemplatePage() { |
| 90 | 90 | // 准备参数 |
| 91 | - WddApi.TemplateQueryRequest.Filter filter = new WddApi.TemplateQueryRequest.Filter(1, null, null, null); | |
| 92 | - WddApi.TemplateQueryRequest request = new WddApi.TemplateQueryRequest(1, 10, filter); | |
| 91 | + WddPptApi.TemplateQueryRequest.Filter filter = new WddPptApi.TemplateQueryRequest.Filter(1, null, null, null); | |
| 92 | + WddPptApi.TemplateQueryRequest request = new WddPptApi.TemplateQueryRequest(1, 10, filter); | |
| 93 | 93 | //调用 |
| 94 | - WddApi.PagePptTemplateInfo pptTemplatePage = wddApi.getPptTemplatePage(token, request); | |
| 94 | + WddPptApi.PagePptTemplateInfo pptTemplatePage = wddPptApi.getTemplatePage(token, request); | |
| 95 | 95 | // 打印结果 |
| 96 | 96 | System.out.println(pptTemplatePage); |
| 97 | 97 | } |
| ... | ... | @@ -101,9 +101,9 @@ public class WddApiTests { |
| 101 | 101 | @Disabled |
| 102 | 102 | public void testGeneratePptx() { |
| 103 | 103 | // 准备参数 |
| 104 | - WddApi.GeneratePptxRequest request = new WddApi.GeneratePptxRequest("1900913633555255296", "1804885538940116992", TEST_OUT_LINE_CONTENT); | |
| 104 | + WddPptApi.CreatePptRequest request = new WddPptApi.CreatePptRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT); | |
| 105 | 105 | //调用 |
| 106 | - WddApi.PptInfo pptInfo = wddApi.generatePptx("", request); | |
| 106 | + WddPptApi.PptInfo pptInfo = wddPptApi.create(token, request); | |
| 107 | 107 | // 打印结果 |
| 108 | 108 | System.out.println(pptInfo); |
| 109 | 109 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/ppt/xunfei/XunfeiPptApiTests.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.ppt.xunfei; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.ai.core.model.xunfei.api.XunfeiPptApi; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 5 | +import org.junit.jupiter.api.Disabled; | |
| 6 | +import org.junit.jupiter.api.Test; | |
| 7 | +import org.springframework.mock.web.MockMultipartFile; | |
| 8 | +import org.springframework.web.multipart.MultipartFile; | |
| 9 | + | |
| 10 | +import java.io.File; | |
| 11 | +import java.io.FileInputStream; | |
| 12 | +import java.io.IOException; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * {@link XunfeiPptApi} 集成测试 | |
| 16 | + * | |
| 17 | + * @author xiaoxin | |
| 18 | + */ | |
| 19 | +public class XunfeiPptApiTests { | |
| 20 | + | |
| 21 | + // 讯飞API配置信息,实际使用时请替换为您的应用信息 | |
| 22 | + private static final String APP_ID = ""; | |
| 23 | + private static final String API_SECRET = ""; | |
| 24 | + | |
| 25 | + private final XunfeiPptApi xunfeiPptApi = new XunfeiPptApi(XunfeiPptApi.BASE_URL, APP_ID, API_SECRET); | |
| 26 | + | |
| 27 | + @Test // 获取PPT模板列表 | |
| 28 | + @Disabled | |
| 29 | + public void testGetTemplatePage() { | |
| 30 | + // 调用方法 | |
| 31 | + XunfeiPptApi.TemplatePageResponse response = xunfeiPptApi.getTemplatePage("商务", 10); | |
| 32 | + // 打印结果 | |
| 33 | + System.out.println("模板列表响应:" + JsonUtils.toJsonString(response)); | |
| 34 | + | |
| 35 | + if (response != null && response.data() != null && response.data().records() != null) { | |
| 36 | + System.out.println("模板总数:" + response.data().total()); | |
| 37 | + System.out.println("当前页码:" + response.data().pageNum()); | |
| 38 | + System.out.println("模板数量:" + response.data().records().size()); | |
| 39 | + | |
| 40 | + // 打印第一个模板的信息(如果存在) | |
| 41 | + if (!response.data().records().isEmpty()) { | |
| 42 | + XunfeiPptApi.TemplateInfo firstTemplate = response.data().records().get(0); | |
| 43 | + System.out.println("模板ID:" + firstTemplate.templateIndexId()); | |
| 44 | + System.out.println("模板风格:" + firstTemplate.style()); | |
| 45 | + System.out.println("模板颜色:" + firstTemplate.color()); | |
| 46 | + System.out.println("模板行业:" + firstTemplate.industry()); | |
| 47 | + } | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Test // 创建大纲(通过文本) | |
| 52 | + @Disabled | |
| 53 | + public void testCreateOutline() { | |
| 54 | + XunfeiPptApi.CreateResponse response = getCreateResponse(); | |
| 55 | + // 打印结果 | |
| 56 | + System.out.println("创建大纲响应:" + JsonUtils.toJsonString(response)); | |
| 57 | + | |
| 58 | + // 保存sid和outline用于后续测试 | |
| 59 | + if (response != null && response.data() != null) { | |
| 60 | + System.out.println("sid: " + response.data().sid()); | |
| 61 | + if (response.data().outline() != null) { | |
| 62 | + // 使用OutlineData的toJsonString方法 | |
| 63 | + System.out.println("outline: " + response.data().outline().toJsonString()); | |
| 64 | + // 将outline对象转换为JSON字符串,用于后续createPptByOutline测试 | |
| 65 | + String outlineJson = response.data().outline().toJsonString(); | |
| 66 | + System.out.println("可用于createPptByOutline的outline字符串: " + outlineJson); | |
| 67 | + } | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + // 创建大纲(通过文本) | |
| 72 | + private XunfeiPptApi.CreateResponse getCreateResponse() { | |
| 73 | + // 准备参数 | |
| 74 | + String param = "智能体平台 Dify 介绍"; | |
| 75 | + // 调用方法 | |
| 76 | + return xunfeiPptApi.createOutline(param); | |
| 77 | + } | |
| 78 | + | |
| 79 | + @Test // 通过大纲创建PPT(完整参数) | |
| 80 | + @Disabled | |
| 81 | + public void testCreatePptByOutlineWithFullParams() { | |
| 82 | + // 创建大纲对象 | |
| 83 | + XunfeiPptApi.CreateResponse createResponse = getCreateResponse(); | |
| 84 | + // 调用方法 | |
| 85 | + XunfeiPptApi.CreateResponse response = xunfeiPptApi.createPptByOutline(createResponse.data().outline(), "精简一些,不要超过6个章节"); | |
| 86 | + // 打印结果 | |
| 87 | + System.out.println("通过大纲创建PPT响应:" + JsonUtils.toJsonString(response)); | |
| 88 | + | |
| 89 | + // 保存sid用于后续进度查询 | |
| 90 | + if (response != null && response.data() != null) { | |
| 91 | + System.out.println("sid: " + response.data().sid()); | |
| 92 | + if (response.data().coverImgSrc() != null) { | |
| 93 | + System.out.println("封面图片: " + response.data().coverImgSrc()); | |
| 94 | + } | |
| 95 | + } | |
| 96 | + } | |
| 97 | + | |
| 98 | + @Test // 检查PPT生成进度 | |
| 99 | + @Disabled | |
| 100 | + public void testCheckProgress() { | |
| 101 | + // 准备参数 - 使用之前创建PPT时返回的sid | |
| 102 | + String sid = "e96dac09f2ec4ee289f029a5fb874ecd"; // 替换为实际的sid | |
| 103 | + | |
| 104 | + // 调用方法 | |
| 105 | + XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid); | |
| 106 | + // 打印结果 | |
| 107 | + System.out.println("检查进度响应:" + JsonUtils.toJsonString(response)); | |
| 108 | + | |
| 109 | + // 安全地访问响应数据 | |
| 110 | + if (response != null && response.data() != null) { | |
| 111 | + XunfeiPptApi.ProgressResponseData data = response.data(); | |
| 112 | + | |
| 113 | + // 打印PPT生成状态 | |
| 114 | + System.out.println("PPT构建状态: " + data.pptStatus()); | |
| 115 | + System.out.println("AI配图状态: " + data.aiImageStatus()); | |
| 116 | + System.out.println("演讲备注状态: " + data.cardNoteStatus()); | |
| 117 | + | |
| 118 | + // 打印进度信息 | |
| 119 | + if (data.totalPages() != null && data.donePages() != null) { | |
| 120 | + System.out.println("总页数: " + data.totalPages()); | |
| 121 | + System.out.println("已完成页数: " + data.donePages()); | |
| 122 | + System.out.println("完成进度: " + data.getProgressPercent() + "%"); | |
| 123 | + } else { | |
| 124 | + System.out.println("进度: " + data.process() + "%"); | |
| 125 | + } | |
| 126 | + | |
| 127 | + // 检查是否完成 | |
| 128 | + if (data.isAllDone()) { | |
| 129 | + System.out.println("PPT生成已完成!"); | |
| 130 | + System.out.println("PPT下载链接: " + data.pptUrl()); | |
| 131 | + } | |
| 132 | + // 检查是否失败 | |
| 133 | + else if (data.isFailed()) { | |
| 134 | + System.out.println("PPT生成失败!"); | |
| 135 | + System.out.println("错误信息: " + data.errMsg()); | |
| 136 | + } | |
| 137 | + // 正在进行中 | |
| 138 | + else { | |
| 139 | + System.out.println("PPT生成中,请稍后再查询..."); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + @Test // 轮询检查PPT生成进度直到完成 | |
| 145 | + @Disabled | |
| 146 | + public void testPollCheckProgress() throws InterruptedException { | |
| 147 | + // 准备参数 - 使用之前创建PPT时返回的sid | |
| 148 | + String sid = "fa36e926f2ed434987fcb4c1f0776ffb"; // 替换为实际的sid | |
| 149 | + | |
| 150 | + // 最大轮询次数 | |
| 151 | + int maxPolls = 20; | |
| 152 | + // 轮询间隔(毫秒)- 讯飞API限流为3秒一次 | |
| 153 | + long pollInterval = 3500; | |
| 154 | + | |
| 155 | + for (int i = 0; i < maxPolls; i++) { | |
| 156 | + System.out.println("第" + (i + 1) + "次查询进度..."); | |
| 157 | + | |
| 158 | + // 调用方法 | |
| 159 | + XunfeiPptApi.ProgressResponse response = xunfeiPptApi.checkProgress(sid); | |
| 160 | + | |
| 161 | + // 安全地访问响应数据 | |
| 162 | + if (response != null && response.data() != null) { | |
| 163 | + XunfeiPptApi.ProgressResponseData data = response.data(); | |
| 164 | + | |
| 165 | + // 打印进度信息 | |
| 166 | + System.out.println("PPT构建状态: " + data.pptStatus()); | |
| 167 | + if (data.totalPages() != null && data.donePages() != null) { | |
| 168 | + System.out.println("完成进度: " + data.donePages() + "/" + data.totalPages() | |
| 169 | + + " (" + data.getProgressPercent() + "%)"); | |
| 170 | + } | |
| 171 | + | |
| 172 | + // 检查是否完成 | |
| 173 | + if (data.isAllDone()) { | |
| 174 | + System.out.println("PPT生成已完成!"); | |
| 175 | + System.out.println("PPT下载链接: " + data.pptUrl()); | |
| 176 | + break; | |
| 177 | + } | |
| 178 | + // 检查是否失败 | |
| 179 | + else if (data.isFailed()) { | |
| 180 | + System.out.println("PPT生成失败!"); | |
| 181 | + System.out.println("错误信息: " + data.errMsg()); | |
| 182 | + break; | |
| 183 | + } | |
| 184 | + // 正在进行中,继续轮询 | |
| 185 | + else { | |
| 186 | + System.out.println("PPT生成中,等待" + (pollInterval / 1000) + "秒后继续查询..."); | |
| 187 | + Thread.sleep(pollInterval); | |
| 188 | + } | |
| 189 | + } else { | |
| 190 | + System.out.println("查询失败,等待" + (pollInterval / 1000) + "秒后重试..."); | |
| 191 | + Thread.sleep(pollInterval); | |
| 192 | + } | |
| 193 | + } | |
| 194 | + } | |
| 195 | + | |
| 196 | + @Test // 直接创建PPT(通过文本) | |
| 197 | + @Disabled | |
| 198 | + public void testCreatePptByText() { | |
| 199 | + // 准备参数 | |
| 200 | + String query = "合肥天气趋势分析,包括近5年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响"; | |
| 201 | + | |
| 202 | + // 调用方法 | |
| 203 | + XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(query); | |
| 204 | + // 打印结果 | |
| 205 | + System.out.println("直接创建PPT响应:" + JsonUtils.toJsonString(response)); | |
| 206 | + | |
| 207 | + // 保存sid用于后续进度查询 | |
| 208 | + if (response != null && response.data() != null) { | |
| 209 | + System.out.println("sid: " + response.data().sid()); | |
| 210 | + if (response.data().coverImgSrc() != null) { | |
| 211 | + System.out.println("封面图片: " + response.data().coverImgSrc()); | |
| 212 | + } | |
| 213 | + System.out.println("标题: " + response.data().title()); | |
| 214 | + System.out.println("副标题: " + response.data().subTitle()); | |
| 215 | + } | |
| 216 | + } | |
| 217 | + | |
| 218 | + @Test // 直接创建PPT(通过文件) | |
| 219 | + @Disabled | |
| 220 | + public void testCreatePptByFile() throws IOException { | |
| 221 | + // 准备参数 | |
| 222 | + File file = new File("src/test/resources/test.txt"); // 请确保此文件存在 | |
| 223 | + MultipartFile multipartFile = convertFileToMultipartFile(file); | |
| 224 | + | |
| 225 | + // 调用方法 | |
| 226 | + XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(multipartFile, file.getName()); | |
| 227 | + // 打印结果 | |
| 228 | + System.out.println("通过文件创建PPT响应:" + JsonUtils.toJsonString(response)); | |
| 229 | + | |
| 230 | + // 保存sid用于后续进度查询 | |
| 231 | + if (response != null && response.data() != null) { | |
| 232 | + System.out.println("sid: " + response.data().sid()); | |
| 233 | + if (response.data().coverImgSrc() != null) { | |
| 234 | + System.out.println("封面图片: " + response.data().coverImgSrc()); | |
| 235 | + } | |
| 236 | + System.out.println("标题: " + response.data().title()); | |
| 237 | + System.out.println("副标题: " + response.data().subTitle()); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 241 | + @Test // 直接创建PPT(完整参数) | |
| 242 | + @Disabled | |
| 243 | + public void testCreatePptWithFullParams() throws IOException { | |
| 244 | + // 准备参数 | |
| 245 | + String query = "合肥天气趋势分析,包括近5年的气温变化、降水量变化、极端天气事件,以及对城市生活的影响"; | |
| 246 | + | |
| 247 | + // 创建请求对象 | |
| 248 | + XunfeiPptApi.CreatePptRequest request = XunfeiPptApi.CreatePptRequest.builder() | |
| 249 | + .query(query) | |
| 250 | + .language("cn") | |
| 251 | + .isCardNote(true) | |
| 252 | + .search(true) | |
| 253 | + .isFigure(true) | |
| 254 | + .aiImage("advanced") | |
| 255 | + .author("测试用户") | |
| 256 | + .build(); | |
| 257 | + | |
| 258 | + // 调用方法 | |
| 259 | + XunfeiPptApi.CreateResponse response = xunfeiPptApi.create(request); | |
| 260 | + // 打印结果 | |
| 261 | + System.out.println("使用完整参数创建PPT响应:" + JsonUtils.toJsonString(response)); | |
| 262 | + | |
| 263 | + // 保存sid用于后续进度查询 | |
| 264 | + if (response != null && response.data() != null) { | |
| 265 | + String sid = response.data().sid(); | |
| 266 | + System.out.println("sid: " + sid); | |
| 267 | + if (response.data().coverImgSrc() != null) { | |
| 268 | + System.out.println("封面图片: " + response.data().coverImgSrc()); | |
| 269 | + } | |
| 270 | + System.out.println("标题: " + response.data().title()); | |
| 271 | + System.out.println("副标题: " + response.data().subTitle()); | |
| 272 | + | |
| 273 | + // 立即查询一次进度 | |
| 274 | + System.out.println("立即查询进度..."); | |
| 275 | + XunfeiPptApi.ProgressResponse progressResponse = xunfeiPptApi.checkProgress(sid); | |
| 276 | + if (progressResponse != null && progressResponse.data() != null) { | |
| 277 | + XunfeiPptApi.ProgressResponseData progressData = progressResponse.data(); | |
| 278 | + System.out.println("PPT构建状态: " + progressData.pptStatus()); | |
| 279 | + if (progressData.totalPages() != null && progressData.donePages() != null) { | |
| 280 | + System.out.println("完成进度: " + progressData.donePages() + "/" + progressData.totalPages() | |
| 281 | + + " (" + progressData.getProgressPercent() + "%)"); | |
| 282 | + } | |
| 283 | + } | |
| 284 | + } | |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 288 | + * 将File转换为MultipartFile | |
| 289 | + */ | |
| 290 | + private MultipartFile convertFileToMultipartFile(File file) throws IOException { | |
| 291 | + FileInputStream input = new FileInputStream(file); | |
| 292 | + return new MockMultipartFile( | |
| 293 | + "file", | |
| 294 | + file.getName(), | |
| 295 | + "text/plain", | |
| 296 | + input.readAllBytes() | |
| 297 | + ); | |
| 298 | + } | |
| 299 | + | |
| 300 | +} | |
| 0 | 301 | \ No newline at end of file | ... | ... |