Commit 1db3b867aa4ab5b4cc9a85b502aac0e425f962e0
Committed by
Gitee
!1293 【功能新增】AI:讯飞、文多多 PPT API 对接
Merge pull request !1293 from 小新/feature/ai
Showing
4 changed files
with
1761 additions
and
0 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/wenduoduo/api/WddPptApi.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 4 | +import com.fasterxml.jackson.annotation.JsonFormat; | |
| 5 | +import com.fasterxml.jackson.annotation.JsonInclude; | |
| 6 | +import lombok.extern.slf4j.Slf4j; | |
| 7 | +import org.springframework.core.ParameterizedTypeReference; | |
| 8 | +import org.springframework.http.HttpRequest; | |
| 9 | +import org.springframework.http.HttpStatusCode; | |
| 10 | +import org.springframework.http.MediaType; | |
| 11 | +import org.springframework.util.LinkedMultiValueMap; | |
| 12 | +import org.springframework.util.MultiValueMap; | |
| 13 | +import org.springframework.web.multipart.MultipartFile; | |
| 14 | +import org.springframework.web.reactive.function.BodyInserters; | |
| 15 | +import org.springframework.web.reactive.function.client.ClientResponse; | |
| 16 | +import org.springframework.web.reactive.function.client.WebClient; | |
| 17 | +import reactor.core.publisher.Flux; | |
| 18 | +import reactor.core.publisher.Mono; | |
| 19 | + | |
| 20 | +import java.time.LocalDateTime; | |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Map; | |
| 23 | +import java.util.Objects; | |
| 24 | +import java.util.function.Function; | |
| 25 | +import java.util.function.Predicate; | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * 文多多 API | |
| 29 | + * <p> | |
| 30 | + * <p> | |
| 31 | + * * 对接文多多:<a href="https://docmee.cn/open-platform/api">PPT 生成 API</a> | |
| 32 | + * | |
| 33 | + * @author xiaoxin | |
| 34 | + */ | |
| 35 | +@Slf4j | |
| 36 | +public class WddPptApi { | |
| 37 | + | |
| 38 | + public static final String BASE_URL = "https://docmee.cn"; | |
| 39 | + | |
| 40 | + private final WebClient webClient; | |
| 41 | + | |
| 42 | + private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful(); | |
| 43 | + | |
| 44 | + private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION = | |
| 45 | + reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> { | |
| 46 | + HttpRequest request = response.request(); | |
| 47 | + log.error("[wdd-api] 调用失败!请求方式:[{}],请求地址:[{}],请求参数:[{}],响应数据: [{}]", | |
| 48 | + request.getMethod(), request.getURI(), reqParam, responseBody); | |
| 49 | + sink.error(new IllegalStateException("[wdd-api] 调用失败!")); | |
| 50 | + }); | |
| 51 | + | |
| 52 | + public WddPptApi(String baseUrl) { | |
| 53 | + this.webClient = WebClient.builder() | |
| 54 | + .baseUrl(baseUrl) | |
| 55 | + .defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON)) | |
| 56 | + .build(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * 创建 token | |
| 62 | + * | |
| 63 | + * @param request 请求信息 | |
| 64 | + * @return token | |
| 65 | + */ | |
| 66 | + public String createApiToken(CreateTokenRequest request) { | |
| 67 | + return this.webClient.post() | |
| 68 | + .uri("/api/user/createApiToken") | |
| 69 | + .header("Api-Key", request.apiKey) | |
| 70 | + .body(Mono.just(request), CreateTokenRequest.class) | |
| 71 | + .retrieve() | |
| 72 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 73 | + .bodyToMono(ApiResponse.class) | |
| 74 | + .<String>handle((response, sink) -> { | |
| 75 | + if (response.code != 0) { | |
| 76 | + sink.error(new IllegalStateException("创建 token 异常," + response.message)); | |
| 77 | + return; | |
| 78 | + } | |
| 79 | + sink.next(response.data.get("token").toString()); | |
| 80 | + }) | |
| 81 | + .block(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * 创建任务 | |
| 87 | + * | |
| 88 | + * @param type 类型 | |
| 89 | + * 1.智能生成(主题、要求) | |
| 90 | + * 2.上传文件生成 | |
| 91 | + * 3.上传思维导图生成 | |
| 92 | + * 4.通过word精准转ppt | |
| 93 | + * 5.通过网页链接生成 | |
| 94 | + * 6.粘贴文本内容生成 | |
| 95 | + * 7.Markdown大纲生成 | |
| 96 | + * @param content 内容 | |
| 97 | + * type=1 用户输入主题或要求(不超过1000字符) | |
| 98 | + * type=2、4 不传 | |
| 99 | + * type=3 幕布等分享链接 | |
| 100 | + * type=5 网页链接地址(http/https) | |
| 101 | + * type=6 粘贴文本内容(不超过20000字符) | |
| 102 | + * type=7 大纲内容(markdown) | |
| 103 | + * @param files 文件列表 | |
| 104 | + * 文件列表(文件数不超过5个,总大小不超过50M): | |
| 105 | + * type=1 上传参考文件(非必传,支持多个) | |
| 106 | + * type=2 上传文件(支持多个) | |
| 107 | + * type=3 上传思维导图(xmind/mm/md)(仅支持一个) | |
| 108 | + * type=4 上传word文件(仅支持一个) | |
| 109 | + * type=5、6、7 不传 | |
| 110 | + * <p> | |
| 111 | + * 支持格式:doc/docx/pdf/ppt/pptx/txt/md/xls/xlsx/csv/html/epub/mobi/xmind/mm | |
| 112 | + * @return 任务ID | |
| 113 | + */ | |
| 114 | + public ApiResponse createTask(String token, Integer type, String content, List<MultipartFile> files) { | |
| 115 | + MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>(); | |
| 116 | + formData.add("type", type); | |
| 117 | + if (content != null) { | |
| 118 | + formData.add("content", content); | |
| 119 | + } | |
| 120 | + if (files != null) { | |
| 121 | + for (MultipartFile file : files) { | |
| 122 | + formData.add("file", file.getResource()); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + return this.webClient.post() | |
| 127 | + .uri("/api/ppt/v2/createTask") | |
| 128 | + .header("token", token) | |
| 129 | + .contentType(MediaType.MULTIPART_FORM_DATA) | |
| 130 | + .body(BodyInserters.fromMultipartData(formData)) | |
| 131 | + .retrieve() | |
| 132 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(formData)) | |
| 133 | + .bodyToMono(ApiResponse.class) | |
| 134 | + .block(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * 获取生成选项 | |
| 139 | + * | |
| 140 | + * @param lang 语种 | |
| 141 | + * @return 生成选项 | |
| 142 | + */ | |
| 143 | + public Map<String, Object> getOptions(String lang) { | |
| 144 | + String uri = "/api/ppt/v2/options"; | |
| 145 | + if (lang != null) { | |
| 146 | + uri += "?lang=" + lang; | |
| 147 | + } | |
| 148 | + return this.webClient.get() | |
| 149 | + .uri(uri) | |
| 150 | + .retrieve() | |
| 151 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(lang)) | |
| 152 | + .bodyToMono(new ParameterizedTypeReference<ApiResponse>() { | |
| 153 | + }) | |
| 154 | + .<Map<String, Object>>handle((response, sink) -> { | |
| 155 | + if (response.code != 0) { | |
| 156 | + sink.error(new IllegalStateException("获取生成选项异常," + response.message)); | |
| 157 | + return; | |
| 158 | + } | |
| 159 | + sink.next(response.data); | |
| 160 | + }) | |
| 161 | + .block(); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * 分页查询PPT模板 | |
| 166 | + * | |
| 167 | + * @param token 令牌 | |
| 168 | + * @param request 请求体 | |
| 169 | + * @return 模板列表 | |
| 170 | + */ | |
| 171 | + public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) { | |
| 172 | + return this.webClient.post() | |
| 173 | + .uri("/api/ppt/templates") | |
| 174 | + .header("token", token) | |
| 175 | + .bodyValue(request) | |
| 176 | + .retrieve() | |
| 177 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 178 | + .bodyToMono(new ParameterizedTypeReference<PagePptTemplateInfo>() { | |
| 179 | + }) | |
| 180 | + .block(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * 生成大纲内容 | |
| 185 | + * | |
| 186 | + * @return 大纲内容流 | |
| 187 | + */ | |
| 188 | + public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) { | |
| 189 | + return this.webClient.post() | |
| 190 | + .uri("/api/ppt/v2/generateContent") | |
| 191 | + .header("token", token) | |
| 192 | + .body(Mono.just(request), CreateOutlineRequest.class) | |
| 193 | + .retrieve() | |
| 194 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 195 | + .bodyToFlux(new ParameterizedTypeReference<>() { | |
| 196 | + }); | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * 修改大纲内容 | |
| 201 | + * | |
| 202 | + * @param request 请求体 | |
| 203 | + * @return 大纲内容流 | |
| 204 | + */ | |
| 205 | + public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) { | |
| 206 | + return this.webClient.post() | |
| 207 | + .uri("/api/ppt/v2/updateContent") | |
| 208 | + .header("token", token) | |
| 209 | + .body(Mono.just(request), UpdateOutlineRequest.class) | |
| 210 | + .retrieve() | |
| 211 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 212 | + .bodyToFlux(new ParameterizedTypeReference<>() { | |
| 213 | + }); | |
| 214 | + } | |
| 215 | + | |
| 216 | + /** | |
| 217 | + * 生成PPT | |
| 218 | + * | |
| 219 | + * @return PPT信息 | |
| 220 | + */ | |
| 221 | + public PptInfo create(String token, CreatePptRequest request) { | |
| 222 | + return this.webClient.post() | |
| 223 | + .uri("/api/ppt/v2/generatePptx") | |
| 224 | + .header("token", token) | |
| 225 | + .body(Mono.just(request), CreatePptRequest.class) | |
| 226 | + .retrieve() | |
| 227 | + .onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request)) | |
| 228 | + .bodyToMono(ApiResponse.class) | |
| 229 | + .<PptInfo>handle((response, sink) -> { | |
| 230 | + if (response.code != 0) { | |
| 231 | + sink.error(new IllegalStateException("生成 PPT 异常," + response.message)); | |
| 232 | + return; | |
| 233 | + } | |
| 234 | + sink.next(Objects.requireNonNull(JsonUtils.parseObject(JsonUtils.toJsonString(response.data.get("pptInfo")), PptInfo.class))); | |
| 235 | + }) | |
| 236 | + .block(); | |
| 237 | + } | |
| 238 | + | |
| 239 | + | |
| 240 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 241 | + public record CreateTokenRequest( | |
| 242 | + String apiKey, | |
| 243 | + String uid, | |
| 244 | + Integer limit | |
| 245 | + ) { | |
| 246 | + public CreateTokenRequest(String apiKey) { | |
| 247 | + this(apiKey, null, null); | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * API 通用响应 | |
| 253 | + */ | |
| 254 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 255 | + public record ApiResponse( | |
| 256 | + Integer code, | |
| 257 | + String message, | |
| 258 | + Map<String, Object> data | |
| 259 | + ) { | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * 创建任务 | |
| 264 | + */ | |
| 265 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 266 | + public record CreateTaskRequest( | |
| 267 | + Integer type, | |
| 268 | + String content, | |
| 269 | + List<MultipartFile> files | |
| 270 | + ) { | |
| 271 | + } | |
| 272 | + | |
| 273 | + /** | |
| 274 | + * 生成大纲内容请求 | |
| 275 | + */ | |
| 276 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 277 | + public record CreateOutlineRequest( | |
| 278 | + String id, | |
| 279 | + String length, | |
| 280 | + String scene, | |
| 281 | + String audience, | |
| 282 | + String lang, | |
| 283 | + String prompt | |
| 284 | + ) { | |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 288 | + * 修改大纲内容请求 | |
| 289 | + */ | |
| 290 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 291 | + public record UpdateOutlineRequest( | |
| 292 | + String id, | |
| 293 | + String markdown, | |
| 294 | + String question | |
| 295 | + ) { | |
| 296 | + } | |
| 297 | + | |
| 298 | + /** | |
| 299 | + * 生成PPT请求 | |
| 300 | + */ | |
| 301 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 302 | + public record CreatePptRequest( | |
| 303 | + String id, | |
| 304 | + String templateId, | |
| 305 | + String markdown | |
| 306 | + ) { | |
| 307 | + } | |
| 308 | + | |
| 309 | + | |
| 310 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 311 | + public record PptInfo( | |
| 312 | + String id, | |
| 313 | + String name, | |
| 314 | + String subject, | |
| 315 | + String coverUrl, | |
| 316 | + String fileUrl, | |
| 317 | + String templateId, | |
| 318 | + String pptxProperty, | |
| 319 | + String userId, | |
| 320 | + String userName, | |
| 321 | + int companyId, | |
| 322 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
| 323 | + LocalDateTime updateTime, | |
| 324 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
| 325 | + LocalDateTime createTime, | |
| 326 | + String createUser, | |
| 327 | + String updateUser | |
| 328 | + ) { | |
| 329 | + } | |
| 330 | + | |
| 331 | + | |
| 332 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 333 | + public record TemplateQueryRequest( | |
| 334 | + int page, | |
| 335 | + int size, | |
| 336 | + Filter filters | |
| 337 | + ) { | |
| 338 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 339 | + public record Filter( | |
| 340 | + int type, | |
| 341 | + String category, | |
| 342 | + String style, | |
| 343 | + String themeColor | |
| 344 | + ) { | |
| 345 | + } | |
| 346 | + } | |
| 347 | + | |
| 348 | + | |
| 349 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 350 | + public record PagePptTemplateInfo( | |
| 351 | + List<PptTemplateInfo> data, | |
| 352 | + String total | |
| 353 | + ) { | |
| 354 | + | |
| 355 | + } | |
| 356 | + | |
| 357 | + | |
| 358 | + @JsonInclude(value = JsonInclude.Include.NON_NULL) | |
| 359 | + public record PptTemplateInfo( | |
| 360 | + String id, | |
| 361 | + int type, | |
| 362 | + Integer subType, | |
| 363 | + String layout, | |
| 364 | + String category, | |
| 365 | + String style, | |
| 366 | + String themeColor, | |
| 367 | + String lang, | |
| 368 | + boolean animation, | |
| 369 | + String subject, | |
| 370 | + String coverUrl, | |
| 371 | + String fileUrl, | |
| 372 | + List<String> pageCoverUrls, | |
| 373 | + String pptxProperty, | |
| 374 | + int sort, | |
| 375 | + int num, | |
| 376 | + Integer imgNum, | |
| 377 | + int isDeleted, | |
| 378 | + String userId, | |
| 379 | + int companyId, | |
| 380 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
| 381 | + LocalDateTime updateTime, | |
| 382 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
| 383 | + LocalDateTime createTime, | |
| 384 | + String createUser, | |
| 385 | + String updateUser | |
| 386 | + ) { | |
| 387 | + } | |
| 388 | + | |
| 389 | +} | |
| 0 | 390 | \ No newline at end of file | ... | ... |
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/WddPptApiTests.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.ppt.wdd; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.ai.core.model.wenduoduo.api.WddPptApi; | |
| 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 reactor.core.publisher.Flux; | |
| 8 | + | |
| 9 | +import java.util.Map; | |
| 10 | +import java.util.Objects; | |
| 11 | + | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * {@link WddPptApi} 集成测试 | |
| 15 | + * | |
| 16 | + * @author xiaoxin | |
| 17 | + */ | |
| 18 | +public class WddPptApiTests { | |
| 19 | + | |
| 20 | + private final WddPptApi wddPptApi = new WddPptApi("https://docmee.cn"); | |
| 21 | + | |
| 22 | + | |
| 23 | + private final String token = ""; | |
| 24 | + | |
| 25 | + | |
| 26 | + @Test //获取token | |
| 27 | + @Disabled | |
| 28 | + public void testCreateApiToken() { | |
| 29 | + // 准备参数 | |
| 30 | + String apiKey = ""; | |
| 31 | + WddPptApi.CreateTokenRequest request = new WddPptApi.CreateTokenRequest(apiKey); | |
| 32 | + // 调用方法 | |
| 33 | + String token = wddPptApi.createApiToken(request); | |
| 34 | + // 打印结果 | |
| 35 | + System.out.println(token); | |
| 36 | + } | |
| 37 | + | |
| 38 | + | |
| 39 | + @Test // 创建任务 | |
| 40 | + @Disabled | |
| 41 | + public void testCreateTask() { | |
| 42 | + WddPptApi.ApiResponse apiResponse = wddPptApi.createTask(token, 1, "dify 介绍", null); | |
| 43 | + System.out.println(apiResponse); | |
| 44 | + } | |
| 45 | + | |
| 46 | + | |
| 47 | + @Test // 创建大纲 | |
| 48 | + @Disabled | |
| 49 | + public void testGenerateOutlineRequest() { | |
| 50 | + WddPptApi.CreateOutlineRequest request = new WddPptApi.CreateOutlineRequest("1901539019628613632", "medium", null, null, null, null); | |
| 51 | + //调用 | |
| 52 | + Flux<Map<String, Object>> flux = wddPptApi.createOutline(token, request); | |
| 53 | + StringBuffer contentBuffer = new StringBuffer(); | |
| 54 | + flux.doOnNext(chunk -> { | |
| 55 | + contentBuffer.append(chunk.get("text")); | |
| 56 | + if (Objects.equals(Integer.parseInt(String.valueOf(chunk.get("status"))), 4)) { | |
| 57 | + // status 为 4,最终 markdown 结构树 | |
| 58 | + System.out.println(JsonUtils.toJsonString(chunk.get("result"))); | |
| 59 | + System.out.println(" ########################################################################"); | |
| 60 | + } | |
| 61 | + }).then().block(); | |
| 62 | + // 打印结果 | |
| 63 | + System.out.println(contentBuffer); | |
| 64 | + | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Test // 修改大纲 | |
| 68 | + @Disabled | |
| 69 | + public void testUpdateOutlineRequest() { | |
| 70 | + WddPptApi.UpdateOutlineRequest request = new WddPptApi.UpdateOutlineRequest("1901539019628613632", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可"); | |
| 71 | + //调用 | |
| 72 | + Flux<Map<String, Object>> flux = wddPptApi.updateOutline(token, request); | |
| 73 | + StringBuffer contentBuffer = new StringBuffer(); | |
| 74 | + flux.doOnNext(chunk -> { | |
| 75 | + contentBuffer.append(chunk.get("text")); | |
| 76 | + if (Objects.equals(Integer.parseInt(String.valueOf(chunk.get("status"))), 4)) { | |
| 77 | + // status 为 4,最终 markdown 结构树 | |
| 78 | + System.out.println(JsonUtils.toJsonString(chunk.get("result"))); | |
| 79 | + System.out.println(" ########################################################################"); | |
| 80 | + } | |
| 81 | + }).then().block(); | |
| 82 | + // 打印结果 | |
| 83 | + System.out.println(contentBuffer); | |
| 84 | + | |
| 85 | + } | |
| 86 | + | |
| 87 | + @Test // 获取 PPT 模版分页 | |
| 88 | + @Disabled | |
| 89 | + public void testGetPptTemplatePage() { | |
| 90 | + // 准备参数 | |
| 91 | + WddPptApi.TemplateQueryRequest.Filter filter = new WddPptApi.TemplateQueryRequest.Filter(1, null, null, null); | |
| 92 | + WddPptApi.TemplateQueryRequest request = new WddPptApi.TemplateQueryRequest(1, 10, filter); | |
| 93 | + //调用 | |
| 94 | + WddPptApi.PagePptTemplateInfo pptTemplatePage = wddPptApi.getTemplatePage(token, request); | |
| 95 | + // 打印结果 | |
| 96 | + System.out.println(pptTemplatePage); | |
| 97 | + } | |
| 98 | + | |
| 99 | + | |
| 100 | + @Test // 生成 PPT | |
| 101 | + @Disabled | |
| 102 | + public void testGeneratePptx() { | |
| 103 | + // 准备参数 | |
| 104 | + WddPptApi.CreatePptRequest request = new WddPptApi.CreatePptRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT); | |
| 105 | + //调用 | |
| 106 | + WddPptApi.PptInfo pptInfo = wddPptApi.create(token, request); | |
| 107 | + // 打印结果 | |
| 108 | + System.out.println(pptInfo); | |
| 109 | + } | |
| 110 | + | |
| 111 | + | |
| 112 | + private final String TEST_OUT_LINE_CONTENT = """ | |
| 113 | + # Dify:新一代AI应用开发平台 | |
| 114 | + | |
| 115 | + ## 1 什么是Dify | |
| 116 | + ### 1.1 Dify定义:AI应用开发平台 | |
| 117 | + #### 1.1.1 低代码开发 | |
| 118 | + Dify是一个低代码AI应用开发平台,旨在简化AI应用的构建过程,让开发者无需编写大量代码即可快速创建各种智能应用。 | |
| 119 | + #### 1.1.2 核心功能 | |
| 120 | + Dify的核心功能包括数据集成、模型选择、流程编排和应用部署,提供一站式解决方案,加速AI应用的落地和迭代。 | |
| 121 | + #### 1.1.3 开源与商业 | |
| 122 | + Dify提供开源版本和商业版本,满足不同用户的需求,开源版本适合个人开发者和小型团队,商业版本则提供更强大的功能和技术支持。 | |
| 123 | + | |
| 124 | + ### 1.2 Dify解决的问题:AI开发痛点 | |
| 125 | + #### 1.2.1 开发周期长 | |
| 126 | + 传统AI应用开发周期长,需要大量的人力和时间投入,Dify通过可视化界面和预置组件,大幅缩短开发周期。 | |
| 127 | + #### 1.2.2 技术门槛高 | |
| 128 | + AI技术门槛高,需要专业的知识和技能,Dify降低技术门槛,让更多开发者能够参与到AI应用的开发中来。 | |
| 129 | + #### 1.2.3 部署和维护复杂 | |
| 130 | + AI应用的部署和维护复杂,需要专业的运维团队,Dify提供自动化的部署和维护工具,简化流程,降低成本。 | |
| 131 | + | |
| 132 | + ### 1.3 Dify发展历程 | |
| 133 | + #### 1.3.1 早期探索 | |
| 134 | + Dify的早期版本主要关注于自然语言处理领域的应用,通过集成各种NLP模型,提供文本分类、情感分析等功能。 | |
| 135 | + #### 1.3.2 功能扩展 | |
| 136 | + 随着用户需求的不断增长,Dify的功能逐渐扩展到图像识别、语音识别等领域,支持更多类型的AI应用。 | |
| 137 | + #### 1.3.3 生态建设 | |
| 138 | + Dify积极建设开发者生态,提供丰富的文档、教程和案例,帮助开发者更好地使用Dify平台,共同推动AI技术的发展。 | |
| 139 | + | |
| 140 | + ## 2 Dify的核心功能 | |
| 141 | + ### 2.1 数据集成:连接各种数据源 | |
| 142 | + #### 2.1.1 支持多种数据源 | |
| 143 | + Dify支持连接各种数据源,包括关系型数据库、NoSQL数据库、文件系统、云存储等,满足不同场景的数据需求。 | |
| 144 | + #### 2.1.2 数据转换和清洗 | |
| 145 | + Dify提供数据转换和清洗功能,可以将不同格式的数据转换为统一的格式,并去除无效数据,提高数据质量。 | |
| 146 | + #### 2.1.3 数据安全 | |
| 147 | + Dify注重数据安全,采用各种安全措施保护用户的数据,包括数据加密、访问控制、权限管理等。 | |
| 148 | + | |
| 149 | + ### 2.2 模型选择:丰富的AI模型库 | |
| 150 | + #### 2.2.1 预置模型 | |
| 151 | + Dify预置了丰富的AI模型,包括自然语言处理、图像识别、语音识别等领域的模型,开发者可以直接使用这些模型,无需自行训练,极大的简化了开发流程。 | |
| 152 | + #### 2.2.2 自定义模型 | |
| 153 | + Dify支持开发者上传自定义模型,满足个性化的需求。开发者可以将自己训练的模型部署到Dify平台上,与其他开发者共享。 | |
| 154 | + #### 2.2.3 模型评估 | |
| 155 | + Dify提供模型评估功能,可以对不同模型进行评估,选择最优的模型,提高应用性能。 | |
| 156 | + | |
| 157 | + ### 2.3 流程编排:可视化流程设计器 | |
| 158 | + #### 2.3.1 可视化界面 | |
| 159 | + Dify提供可视化的流程设计器,开发者可以通过拖拽组件的方式,设计AI应用的流程,无需编写代码,简单高效。 | |
| 160 | + #### 2.3.2 灵活的流程控制 | |
| 161 | + Dify支持灵活的流程控制,可以根据不同的条件执行不同的分支,实现复杂的业务逻辑。 | |
| 162 | + #### 2.3.3 实时调试 | |
| 163 | + Dify提供实时调试功能,可以在设计流程的过程中,实时查看流程的执行结果,及时发现和解决问题。 | |
| 164 | + | |
| 165 | + ### 2.4 应用部署:一键部署和管理 | |
| 166 | + #### 2.4.1 快速部署 | |
| 167 | + Dify提供一键部署功能,可以将AI应用快速部署到各种环境,包括本地环境、云环境、容器环境等。 | |
| 168 | + #### 2.4.2 自动伸缩 | |
| 169 | + Dify支持自动伸缩,可以根据应用的负载自动调整资源,保证应用的稳定性和性能。 | |
| 170 | + #### 2.4.3 监控和告警 | |
| 171 | + Dify提供监控和告警功能,可以实时监控应用的状态,并在出现问题时及时告警,方便运维人员进行处理。 | |
| 172 | + | |
| 173 | + ## 3 Dify的特点和优势 | |
| 174 | + ### 3.1 低代码:降低开发门槛 | |
| 175 | + #### 3.1.1 可视化开发 | |
| 176 | + Dify采用可视化开发模式,开发者无需编写大量代码,只需通过拖拽组件即可完成AI应用的开发,降低了开发门槛。 | |
| 177 | + #### 3.1.2 预置组件 | |
| 178 | + Dify预置了丰富的组件,包括数据源组件、模型组件、流程控制组件等,开发者可以直接使用这些组件,提高开发效率。 | |
| 179 | + #### 3.1.3 减少代码量 | |
| 180 | + Dify可以显著减少代码量,降低开发难度,让更多开发者能够参与到AI应用的开发中来。 | |
| 181 | + | |
| 182 | + ### 3.2 灵活:满足不同场景需求 | |
| 183 | + #### 3.2.1 支持多种数据源 | |
| 184 | + Dify支持多种数据源,可以连接各种数据源,满足不同场景的数据需求。 | |
| 185 | + #### 3.2.2 支持自定义模型 | |
| 186 | + Dify支持自定义模型,开发者可以将自己训练的模型部署到Dify平台上,满足个性化的需求。 | |
| 187 | + #### 3.2.3 灵活的流程控制 | |
| 188 | + Dify支持灵活的流程控制,可以根据不同的条件执行不同的分支,实现复杂的业务逻辑。 | |
| 189 | + | |
| 190 | + ### 3.3 高效:加速应用落地 | |
| 191 | + #### 3.3.1 快速开发 | |
| 192 | + Dify通过可视化界面和预置组件,大幅缩短开发周期,加速AI应用的落地。 | |
| 193 | + #### 3.3.2 快速部署 | |
| 194 | + Dify提供一键部署功能,可以将AI应用快速部署到各种环境,提高部署效率。 | |
| 195 | + #### 3.3.3 自动化运维 | |
| 196 | + Dify提供自动化的运维工具,简化运维流程,降低运维成本。 | |
| 197 | + | |
| 198 | + ### 3.4 开放:构建繁荣生态 | |
| 199 | + #### 3.4.1 开源社区 | |
| 200 | + Dify拥有活跃的开源社区,开发者可以在社区中交流经验、分享资源、共同推动Dify的发展。 | |
| 201 | + #### 3.4.2 丰富的文档 | |
| 202 | + Dify提供丰富的文档、教程和案例,帮助开发者更好地使用Dify平台。 | |
| 203 | + #### 3.4.3 API支持 | |
| 204 | + Dify提供API支持,开发者可以通过API将Dify集成到自己的系统中,扩展Dify的功能。 | |
| 205 | + | |
| 206 | + ## 4 Dify的使用场景 | |
| 207 | + ### 4.1 智能客服:提升客户服务质量 | |
| 208 | + #### 4.1.1 自动回复 | |
| 209 | + Dify可以用于构建智能客服系统,实现自动回复客户的常见问题,提高客户服务效率。 | |
| 210 | + #### 4.1.2 情感分析 | |
| 211 | + Dify可以对客户的语音或文本进行情感分析,判断客户的情绪,并根据情绪提供个性化的服务。 | |
| 212 | + #### 4.1.3 知识库问答 | |
| 213 | + Dify可以构建知识库问答系统,让客户通过提问的方式获取所需的信息,提高客户满意度。 | |
| 214 | + | |
| 215 | + ### 4.2 金融风控:提高风险识别能力 | |
| 216 | + #### 4.2.1 欺诈检测 | |
| 217 | + Dify可以用于构建金融风控系统,实现欺诈检测,识别可疑交易,降低风险。 | |
| 218 | + #### 4.2.2 信用评估 | |
| 219 | + Dify可以对用户的信用进行评估,并根据评估结果提供不同的金融服务。 | |
| 220 | + #### 4.2.3 反洗钱 | |
| 221 | + Dify可以用于反洗钱,识别可疑资金流动,防止犯罪行为。 | |
| 222 | + | |
| 223 | + ### 4.3 智慧医疗:提升医疗服务水平 | |
| 224 | + #### 4.3.1 疾病诊断 | |
| 225 | + Dify可以用于辅助疾病诊断,提高诊断准确率,缩短诊断时间。 | |
| 226 | + #### 4.3.2 药物研发 | |
| 227 | + Dify可以用于药物研发,加速新药的发现和开发。 | |
| 228 | + #### 4.3.3 智能健康管理 | |
| 229 | + Dify可以构建智能健康管理系统,为用户提供个性化的健康建议和服务。 | |
| 230 | + | |
| 231 | + ### 4.4 智慧城市:提升城市管理效率 | |
| 232 | + #### 4.4.1 交通优化 | |
| 233 | + Dify可以用于交通优化,提高交通效率,缓解交通拥堵。 | |
| 234 | + #### 4.4.2 环境监测 | |
| 235 | + Dify可以用于环境监测,实时监测空气质量、水质等环境指标,及时发现和解决环境问题。 | |
| 236 | + #### 4.4.3 智能安防 | |
| 237 | + Dify可以用于智能安防,提高城市安全水平,预防犯罪行为。 | |
| 238 | + | |
| 239 | + ## 5 Dify的成功案例 | |
| 240 | + ### 5.1 Case 1:某电商平台的智能客服 | |
| 241 | + #### 5.1.1 项目背景 | |
| 242 | + 该电商平台客户服务压力大,人工客服成本高,需要一种智能化的解决方案。 | |
| 243 | + #### 5.1.2 解决方案 | |
| 244 | + 使用Dify构建智能客服系统,实现自动回复客户的常见问题,并根据客户的情绪提供个性化的服务。 | |
| 245 | + #### 5.1.3 效果 | |
| 246 | + 客户服务效率提高50%,客户满意度提高20%,人工客服成本降低30%。 | |
| 247 | + | |
| 248 | + ### 5.2 Case 2:某银行的金融风控系统 | |
| 249 | + #### 5.2.1 项目背景 | |
| 250 | + 该银行面临日益增长的金融风险,需要一种更有效的风险识别和控制手段。 | |
| 251 | + #### 5.2.2 解决方案 | |
| 252 | + 使用Dify构建金融风控系统,实现欺诈检测、信用评估和反洗钱等功能,提高风险识别能力。 | |
| 253 | + #### 5.2.3 效果 | |
| 254 | + 欺诈交易识别率提高40%,信用评估准确率提高30%,洗钱风险降低25%。 | |
| 255 | + | |
| 256 | + ### 5.3 Case 3:某医院的辅助疾病诊断系统 | |
| 257 | + #### 5.3.1 项目背景 | |
| 258 | + 该医院医生工作压力大,疾病诊断准确率有待提高,需要一种辅助诊断工具。 | |
| 259 | + #### 5.3.2 解决方案 | |
| 260 | + 使用Dify构建辅助疾病诊断系统,根据患者的病历和症状,提供诊断建议,提高诊断准确率。 | |
| 261 | + #### 5.3.3 效果 | |
| 262 | + 疾病诊断准确率提高20%,诊断时间缩短15%,医生工作效率提高10%。 | |
| 263 | + | |
| 264 | + ## 6 Dify的未来展望 | |
| 265 | + ### 6.1 技术升级 | |
| 266 | + #### 6.1.1 模型优化 | |
| 267 | + Dify将不断优化预置模型,提高模型性能,并支持更多类型的AI模型。 | |
| 268 | + #### 6.1.2 流程引擎升级 | |
| 269 | + Dify将升级流程引擎,提高流程的灵活性和可扩展性,支持更复杂的业务逻辑。 | |
| 270 | + #### 6.1.3 平台性能优化 | |
| 271 | + Dify将不断优化平台性能,提高平台的稳定性和可靠性,满足大规模应用的需求。 | |
| 272 | + | |
| 273 | + ### 6.2 生态建设 | |
| 274 | + #### 6.2.1 社区建设 | |
| 275 | + Dify将继续加强开源社区建设,吸引更多开发者参与,共同推动Dify的发展。 | |
| 276 | + #### 6.2.2 合作伙伴拓展 | |
| 277 | + Dify将拓展合作伙伴,与更多的企业和机构合作,共同推动AI技术的应用。 | |
| 278 | + #### 6.2.3 应用商店 | |
| 279 | + Dify将构建应用商店,让开发者可以分享自己的应用,用户可以购买和使用这些应用,构建繁荣的生态系统。 | |
| 280 | + | |
| 281 | + ### 6.3 应用领域拓展 | |
| 282 | + #### 6.3.1 智能制造 | |
| 283 | + Dify将拓展到智能制造领域,为企业提供智能化的生产管理和质量控制解决方案。 | |
| 284 | + #### 6.3.2 智慧农业 | |
| 285 | + Dify将拓展到智慧农业领域,为农民提供智能化的种植和养殖管理解决方案。 | |
| 286 | + #### 6.3.3 更多领域 | |
| 287 | + Dify将拓展到更多领域,为各行各业提供智能化的解决方案,推动社会发展。 | |
| 288 | + | |
| 289 | + ## 7 总结 | |
| 290 | + ### 7.1 Dify的价值 | |
| 291 | + #### 7.1.1 降低AI开发门槛 | |
| 292 | + Dify通过低代码的方式,让更多开发者能够参与到AI应用的开发中来。 | |
| 293 | + #### 7.1.2 加速AI应用落地 | |
| 294 | + Dify提供一站式解决方案,加速AI应用的落地和迭代。 | |
| 295 | + #### 7.1.3 构建繁荣的AI生态 | |
| 296 | + Dify通过开源社区和应用商店,构建繁荣的AI生态系统。 | |
| 297 | + | |
| 298 | + ### 7.2 共同发展 | |
| 299 | + #### 7.2.1 欢迎加入Dify社区 | |
| 300 | + 欢迎更多开发者加入Dify社区,共同推动Dify的发展。 | |
| 301 | + #### 7.2.2 合作共赢 | |
| 302 | + 期待与更多的企业和机构合作,共同推动AI技术的应用。 | |
| 303 | + #### 7.2.3 共创未来 | |
| 304 | + 让我们一起用AI技术改变世界,共创美好未来。"""; | |
| 305 | + | |
| 306 | +} | ... | ... |
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 | ... | ... |