【优化代码】AI PPT:优化文多多、讯飞 API
This commit is contained in:
parent
270fea0c5d
commit
b2ec4d38a0
@ -8,6 +8,7 @@ import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@ -35,6 +36,7 @@ import java.util.function.Predicate;
|
||||
public class WenDuoDuoPptApi {
|
||||
|
||||
public static final String BASE_URL = "https://docmee.cn";
|
||||
public static final String TOKEN_NAME = "token";
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
@ -43,17 +45,24 @@ public class WenDuoDuoPptApi {
|
||||
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
|
||||
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
|
||||
HttpRequest request = response.request();
|
||||
log.error("[wdd-api] 调用失败!请求方式:[{}],请求地址:[{}],请求参数:[{}],响应数据: [{}]",
|
||||
log.error("[WenDuoDuoPptApi] 调用失败!请求方式:[{}],请求地址:[{}],请求参数:[{}],响应数据: [{}]",
|
||||
request.getMethod(), request.getURI(), reqParam, responseBody);
|
||||
sink.error(new IllegalStateException("[wdd-api] 调用失败!"));
|
||||
sink.error(new IllegalStateException("[WenDuoDuoPptApi] 调用失败!"));
|
||||
});
|
||||
|
||||
// TODO @新:是不是不用 baseUrl 哈
|
||||
public WenDuoDuoPptApi(String baseUrl) {
|
||||
/**
|
||||
* 构造方法
|
||||
*
|
||||
* @param token API令牌,可为空,后续API调用时单独指定
|
||||
*/
|
||||
public WenDuoDuoPptApi(String token) {
|
||||
Assert.hasText(token, "token 不能为空");
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
// TODO @新:建议,token 作为 defaultHeader
|
||||
.defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON))
|
||||
.baseUrl(BASE_URL)
|
||||
.defaultHeaders((headers) -> {
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.add(TOKEN_NAME, token);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@ -90,7 +99,7 @@ public class WenDuoDuoPptApi {
|
||||
* @return 任务ID
|
||||
* @see <a href="https://docmee.cn/open-platform/api#%E5%88%9B%E5%BB%BA%E4%BB%BB%E5%8A%A1">创建任务</a>
|
||||
*/
|
||||
public ApiResponse createTask(String token, Integer type, String content, List<MultipartFile> files) {
|
||||
public ApiResponse createTask(Integer type, String content, List<MultipartFile> files) {
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("type", type);
|
||||
if (content != null) {
|
||||
@ -103,7 +112,6 @@ public class WenDuoDuoPptApi {
|
||||
}
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/createTask")
|
||||
.header("token", token)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
@ -146,10 +154,9 @@ public class WenDuoDuoPptApi {
|
||||
* @param request 请求体
|
||||
* @return 模板列表
|
||||
*/
|
||||
public PagePptTemplateInfo getTemplatePage(String token, TemplateQueryRequest request) {
|
||||
public PagePptTemplateInfo getTemplatePage(TemplateQueryRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/templates")
|
||||
.header("token", token)
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
@ -163,10 +170,9 @@ public class WenDuoDuoPptApi {
|
||||
*
|
||||
* @return 大纲内容流
|
||||
*/
|
||||
public Flux<Map<String, Object>> createOutline(String token, CreateOutlineRequest request) {
|
||||
public Flux<Map<String, Object>> createOutline(CreateOutlineRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/generateContent")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), CreateOutlineRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
@ -180,10 +186,9 @@ public class WenDuoDuoPptApi {
|
||||
* @param request 请求体
|
||||
* @return 大纲内容流
|
||||
*/
|
||||
public Flux<Map<String, Object>> updateOutline(String token, UpdateOutlineRequest request) {
|
||||
public Flux<Map<String, Object>> updateOutline(UpdateOutlineRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/updateContent")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), UpdateOutlineRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
@ -196,11 +201,10 @@ public class WenDuoDuoPptApi {
|
||||
*
|
||||
* @return PPT信息
|
||||
*/
|
||||
public PptInfo create(String token, CreatePptRequest request) {
|
||||
public PptInfo create(PptCreateRequest request) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/ppt/v2/generatePptx")
|
||||
.header("token", token)
|
||||
.body(Mono.just(request), CreatePptRequest.class)
|
||||
.body(Mono.just(request), PptCreateRequest.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(request))
|
||||
.bodyToMono(ApiResponse.class)
|
||||
@ -214,7 +218,9 @@ public class WenDuoDuoPptApi {
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建Token请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreateTokenRequest(
|
||||
String apiKey,
|
||||
@ -276,18 +282,19 @@ public class WenDuoDuoPptApi {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 PPT 请求
|
||||
* 生成 PPT 请求参数
|
||||
*/
|
||||
// TODO @新:要不按照 PptCreateRequest 这样的风格
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record CreatePptRequest(
|
||||
public record PptCreateRequest(
|
||||
String id,
|
||||
String templateId,
|
||||
String markdown
|
||||
) {
|
||||
}
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
/**
|
||||
* PPT 信息
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PptInfo(
|
||||
String id,
|
||||
@ -309,7 +316,9 @@ public class WenDuoDuoPptApi {
|
||||
) {
|
||||
}
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
/**
|
||||
* 模板查询请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record TemplateQueryRequest(
|
||||
int page,
|
||||
@ -317,6 +326,9 @@ public class WenDuoDuoPptApi {
|
||||
Filter filters
|
||||
) {
|
||||
|
||||
/**
|
||||
* 模板查询过滤条件
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record Filter(
|
||||
int type,
|
||||
@ -328,7 +340,9 @@ public class WenDuoDuoPptApi {
|
||||
|
||||
}
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
/**
|
||||
* PPT模板分页信息
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PagePptTemplateInfo(
|
||||
List<PptTemplateInfo> data,
|
||||
@ -336,8 +350,9 @@ public class WenDuoDuoPptApi {
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
// TODO @新:要不写下类注释
|
||||
/**
|
||||
* PPT模板信息
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record PptTemplateInfo(
|
||||
String id,
|
||||
|
@ -1,36 +1,31 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.xinghuo.api;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.crypto.digest.HmacAlgorithm;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Builder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 讯飞智能 PPT 生成 API
|
||||
*
|
||||
@ -41,6 +36,9 @@ import java.util.function.Predicate;
|
||||
public class XunFeiPptApi {
|
||||
|
||||
public static final String BASE_URL = "https://zwapi.xfyun.cn/api/ppt/v2";
|
||||
private static final String HEADER_APP_ID = "appId";
|
||||
private static final String HEADER_TIMESTAMP = "timestamp";
|
||||
private static final String HEADER_SIGNATURE = "signature";
|
||||
|
||||
private final WebClient webClient;
|
||||
private final String appId;
|
||||
@ -50,18 +48,21 @@ public class XunFeiPptApi {
|
||||
|
||||
private final Function<Object, Function<ClientResponse, Mono<? extends Throwable>>> EXCEPTION_FUNCTION =
|
||||
reqParam -> response -> response.bodyToMono(String.class).handle((responseBody, sink) -> {
|
||||
log.error("[xunfei-ppt-api] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody);
|
||||
sink.error(new IllegalStateException("[xunfei-ppt-api] 调用失败!"));
|
||||
log.error("[XunFeiPptApi] 调用失败!请求参数:[{}],响应数据: [{}]", reqParam, responseBody);
|
||||
sink.error(new IllegalStateException("[XunFeiPptApi] 调用失败!"));
|
||||
});
|
||||
|
||||
// TODO @新:是不是不用 baseUrl 哈
|
||||
public XunFeiPptApi(String baseUrl, String appId, String apiSecret) {
|
||||
// TODO @新:建议,增加 defaultheaders,例如说 appid 之类的;或者每个请求,通过 headers customer 处理。
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(baseUrl)
|
||||
.build();
|
||||
public XunFeiPptApi(String appId, String apiSecret) {
|
||||
this.appId = appId;
|
||||
this.apiSecret = apiSecret;
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(BASE_URL)
|
||||
.defaultHeaders((headers) -> {
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.add(HEADER_APP_ID, appId);
|
||||
})
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -73,7 +74,7 @@ public class XunFeiPptApi {
|
||||
long timestamp = System.currentTimeMillis() / 1000;
|
||||
String ts = String.valueOf(timestamp);
|
||||
String signature = generateSignature(appId, apiSecret, timestamp);
|
||||
return new SignatureInfo(appId, ts, signature);
|
||||
return new SignatureInfo(ts, signature);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -85,43 +86,8 @@ public class XunFeiPptApi {
|
||||
* @return 签名
|
||||
*/
|
||||
private String generateSignature(String appId, String apiSecret, long timestamp) {
|
||||
try {
|
||||
// TODO @新:使用 hutool 简化
|
||||
String auth = md5(appId + timestamp);
|
||||
return hmacSHA1Encrypt(auth, apiSecret);
|
||||
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
|
||||
log.error("[xunfei-ppt-api] 生成签名失败", e);
|
||||
throw new IllegalStateException("[xunfei-ppt-api] 生成签名失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HMAC SHA1 加密
|
||||
*/
|
||||
private String hmacSHA1Encrypt(String encryptText, String encryptKey)
|
||||
throws NoSuchAlgorithmException, InvalidKeyException {
|
||||
SecretKeySpec keySpec = new SecretKeySpec(
|
||||
encryptKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
|
||||
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
mac.init(keySpec);
|
||||
byte[] result = mac.doFinal(encryptText.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
return Base64.getEncoder().encodeToString(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5 哈希
|
||||
*/
|
||||
private String md5(String text) throws NoSuchAlgorithmException {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(text.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
String auth = SecureUtil.md5(appId + timestamp);
|
||||
return SecureUtil.hmac(HmacAlgorithm.HmacSHA1, apiSecret).digestBase64(auth, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,9 +104,8 @@ public class XunFeiPptApi {
|
||||
requestBody.put("pageSize", ObjUtil.defaultIfNull(pageSize, 20));
|
||||
return this.webClient.post()
|
||||
.uri("/template/list")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.header(HEADER_TIMESTAMP, signInfo.timestamp)
|
||||
.header(HEADER_SIGNATURE, signInfo.signature)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(requestBody)
|
||||
.retrieve()
|
||||
@ -161,9 +126,8 @@ public class XunFeiPptApi {
|
||||
formData.add("query", query);
|
||||
return this.webClient.post()
|
||||
.uri("/createOutline")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.header(HEADER_TIMESTAMP, signInfo.timestamp)
|
||||
.header(HEADER_SIGNATURE, signInfo.signature)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
@ -207,12 +171,11 @@ public class XunFeiPptApi {
|
||||
*/
|
||||
public CreateResponse create(CreatePptRequest request) {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
MultiValueMap<String, Object> formData = buildCreateFormData(request);
|
||||
MultiValueMap<String, Object> formData = buildCreatePptFormData(request);
|
||||
return this.webClient.post()
|
||||
.uri("/create")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.header(HEADER_TIMESTAMP, signInfo.timestamp)
|
||||
.header(HEADER_SIGNATURE, signInfo.signature)
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.body(BodyInserters.fromMultipartData(formData))
|
||||
.retrieve()
|
||||
@ -247,9 +210,8 @@ public class XunFeiPptApi {
|
||||
SignatureInfo signInfo = getSignature();
|
||||
return this.webClient.post()
|
||||
.uri("/createPptByOutline")
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.header(HEADER_TIMESTAMP, signInfo.timestamp)
|
||||
.header(HEADER_SIGNATURE, signInfo.signature)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
@ -271,9 +233,8 @@ public class XunFeiPptApi {
|
||||
.path("/progress")
|
||||
.queryParam("sid", sid)
|
||||
.build())
|
||||
.header("appId", signInfo.appId)
|
||||
.header("timestamp", signInfo.timestamp)
|
||||
.header("signature", signInfo.signature)
|
||||
.header(HEADER_TIMESTAMP, signInfo.timestamp)
|
||||
.header(HEADER_SIGNATURE, signInfo.signature)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION.apply(sid))
|
||||
.bodyToMono(ProgressResponse.class)
|
||||
@ -285,7 +246,6 @@ public class XunFeiPptApi {
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
private record SignatureInfo(
|
||||
String appId,
|
||||
String timestamp,
|
||||
String signature
|
||||
) {
|
||||
@ -461,6 +421,7 @@ public class XunFeiPptApi {
|
||||
* 通过大纲创建 PPT 请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
@Builder
|
||||
public record CreatePptByOutlineRequest(
|
||||
String query, // 用户生成PPT要求(最多8000字)
|
||||
String outlineSid, // 已生成大纲后,响应返回的请求大纲唯一id
|
||||
@ -476,123 +437,18 @@ public class XunFeiPptApi {
|
||||
Boolean isFigure, // 是否自动配图
|
||||
String aiImage // ai配图类型:normal、advanced
|
||||
) {
|
||||
|
||||
/**
|
||||
* 创建构建器
|
||||
*
|
||||
* @return 构建器
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
// TODO @新:这个可以用 lombok 简化么?
|
||||
|
||||
/**
|
||||
* 构建器类
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String query;
|
||||
private String outlineSid;
|
||||
private OutlineData outline;
|
||||
private String templateId;
|
||||
private String businessId;
|
||||
private String author;
|
||||
private Boolean isCardNote;
|
||||
private Boolean search;
|
||||
private String language;
|
||||
private String fileUrl;
|
||||
private String fileName;
|
||||
private Boolean isFigure;
|
||||
private String aiImage;
|
||||
|
||||
public Builder query(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder outlineSid(String outlineSid) {
|
||||
this.outlineSid = outlineSid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder outline(OutlineData outline) {
|
||||
this.outline = outline;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder templateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder businessId(String businessId) {
|
||||
this.businessId = businessId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder author(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isCardNote(Boolean isCardNote) {
|
||||
this.isCardNote = isCardNote;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder search(Boolean search) {
|
||||
this.search = search;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder language(String language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileUrl(String fileUrl) {
|
||||
this.fileUrl = fileUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isFigure(Boolean isFigure) {
|
||||
this.isFigure = isFigure;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder aiImage(String aiImage) {
|
||||
this.aiImage = aiImage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CreatePptByOutlineRequest build() {
|
||||
return new CreatePptByOutlineRequest(
|
||||
query, outlineSid, outline, templateId, businessId, author,
|
||||
isCardNote, search, language, fileUrl, fileName, isFigure, aiImage
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建创建 PPT 的表单数据
|
||||
*
|
||||
* @param request 请求参数
|
||||
* @return 表单数据
|
||||
*/
|
||||
private MultiValueMap<String, Object> buildCreateFormData(CreatePptRequest request) {
|
||||
private MultiValueMap<String, Object> buildCreatePptFormData(CreatePptRequest request) {
|
||||
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
|
||||
// 添加请求参数
|
||||
if (request.query() != null) {
|
||||
formData.add("query", request.query());
|
||||
}
|
||||
|
||||
if (request.file() != null) {
|
||||
try {
|
||||
formData.add("file", new ByteArrayResource(request.file().getBytes()) {
|
||||
@ -602,48 +458,52 @@ public class XunFeiPptApi {
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
log.error("[xunfei-ppt-api] 文件处理失败", e);
|
||||
throw new IllegalStateException("[xunfei-ppt-api] 文件处理失败", e);
|
||||
log.error("[XunFeiPptApi] 文件处理失败", e);
|
||||
throw new IllegalStateException("[XunFeiPptApi] 文件处理失败", e);
|
||||
}
|
||||
}
|
||||
// TODO @新:要不搞个 MapUtil.addIfPresent 方法?
|
||||
if (request.fileUrl() != null) {
|
||||
formData.add("fileUrl", request.fileUrl());
|
||||
}
|
||||
if (request.fileName() != null) {
|
||||
formData.add("fileName", request.fileName());
|
||||
}
|
||||
if (request.templateId() != null) {
|
||||
formData.add("templateId", request.templateId());
|
||||
}
|
||||
if (request.businessId() != null) {
|
||||
formData.add("businessId", request.businessId());
|
||||
}
|
||||
if (request.author() != null) {
|
||||
formData.add("author", request.author());
|
||||
}
|
||||
if (request.isCardNote() != null) {
|
||||
formData.add("isCardNote", request.isCardNote().toString());
|
||||
}
|
||||
if (request.search() != null) {
|
||||
formData.add("search", request.search().toString());
|
||||
}
|
||||
if (request.language() != null) {
|
||||
formData.add("language", request.language());
|
||||
}
|
||||
if (request.isFigure() != null) {
|
||||
formData.add("isFigure", request.isFigure().toString());
|
||||
}
|
||||
if (request.aiImage() != null) {
|
||||
formData.add("aiImage", request.aiImage());
|
||||
}
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
addIfPresent(param, "query", request.query());
|
||||
addIfPresent(param, "fileUrl", request.fileUrl());
|
||||
addIfPresent(param, "fileName", request.fileName());
|
||||
addIfPresent(param, "templateId", request.templateId());
|
||||
addIfPresent(param, "businessId", request.businessId());
|
||||
addIfPresent(param, "author", request.author());
|
||||
addIfPresent(param, "isCardNote", request.isCardNote());
|
||||
addIfPresent(param, "search", request.search());
|
||||
addIfPresent(param, "language", request.language());
|
||||
addIfPresent(param, "isFigure", request.isFigure());
|
||||
addIfPresent(param, "aiImage", request.aiImage());
|
||||
param.forEach(formData::add);
|
||||
return formData;
|
||||
}
|
||||
|
||||
public static <K, V> void addIfPresent(Map<K, V> map, K key, V value) {
|
||||
if (ObjUtil.isNull(key) || ObjUtil.isNull(map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean isPresent = false;
|
||||
if (ObjUtil.isNotNull(value)) {
|
||||
if (value instanceof String) {
|
||||
// 字符串:需要有实际内容
|
||||
isPresent = StringUtils.hasText((String) value);
|
||||
} else {
|
||||
// 其他类型:非 null 即视为存在
|
||||
isPresent = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isPresent) {
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接生成PPT请求参数
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
@Builder
|
||||
public record CreatePptRequest(
|
||||
String query, // 用户生成PPT要求(最多8000字)
|
||||
MultipartFile file, // 上传文件
|
||||
@ -659,109 +519,6 @@ public class XunFeiPptApi {
|
||||
String aiImage // ai配图类型:normal、advanced
|
||||
) {
|
||||
|
||||
/**
|
||||
* 创建构建器
|
||||
*
|
||||
* @return 构建器
|
||||
*/
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建器类
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private String query;
|
||||
private MultipartFile file;
|
||||
private String fileUrl;
|
||||
private String fileName;
|
||||
private String templateId;
|
||||
private String businessId;
|
||||
private String author;
|
||||
private Boolean isCardNote;
|
||||
private Boolean search;
|
||||
private String language;
|
||||
private Boolean isFigure;
|
||||
private String aiImage;
|
||||
|
||||
// TODO @新:这个可以用 lombok 简化么?
|
||||
|
||||
public Builder query(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder file(MultipartFile file) {
|
||||
this.file = file;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileUrl(String fileUrl) {
|
||||
this.fileUrl = fileUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder fileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder templateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder businessId(String businessId) {
|
||||
this.businessId = businessId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder author(String author) {
|
||||
this.author = author;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isCardNote(Boolean isCardNote) {
|
||||
this.isCardNote = isCardNote;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder search(Boolean search) {
|
||||
this.search = search;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder language(String language) {
|
||||
this.language = language;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder isFigure(Boolean isFigure) {
|
||||
this.isFigure = isFigure;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder aiImage(String aiImage) {
|
||||
this.aiImage = aiImage;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CreatePptRequest build() {
|
||||
// 验证参数
|
||||
if (query == null && file == null && fileUrl == null) {
|
||||
throw new IllegalArgumentException("query、file、fileUrl必填其一");
|
||||
}
|
||||
if ((file != null || fileUrl != null) && fileName == null) {
|
||||
throw new IllegalArgumentException("如果传file或者fileUrl,fileName必填");
|
||||
}
|
||||
return new CreatePptRequest(
|
||||
query, file, fileUrl, fileName, templateId, businessId, author,
|
||||
isCardNote, search, language, isFigure, aiImage
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -16,10 +16,14 @@ import java.util.Objects;
|
||||
*/
|
||||
public class WenDuoDuoPptApiTests {
|
||||
|
||||
private final WenDuoDuoPptApi wenDuoDuoPptApi = new WenDuoDuoPptApi("https://docmee.cn");
|
||||
private final WenDuoDuoPptApi wenDuoDuoPptApi;
|
||||
|
||||
private final String token = ""; // API Token
|
||||
|
||||
{
|
||||
wenDuoDuoPptApi = new WenDuoDuoPptApi(token);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreateApiToken() {
|
||||
@ -38,7 +42,7 @@ public class WenDuoDuoPptApiTests {
|
||||
@Test
|
||||
@Disabled
|
||||
public void testCreateTask() {
|
||||
WenDuoDuoPptApi.ApiResponse apiResponse = wenDuoDuoPptApi.createTask(token, 1, "dify 介绍", null);
|
||||
WenDuoDuoPptApi.ApiResponse apiResponse = wenDuoDuoPptApi.createTask(1, "dify 介绍", null);
|
||||
System.out.println(apiResponse);
|
||||
}
|
||||
|
||||
@ -49,7 +53,7 @@ public class WenDuoDuoPptApiTests {
|
||||
WenDuoDuoPptApi.CreateOutlineRequest request = new WenDuoDuoPptApi.CreateOutlineRequest(
|
||||
"1901539019628613632", "medium", null, null, null, null);
|
||||
// 调用
|
||||
Flux<Map<String, Object>> flux = wenDuoDuoPptApi.createOutline(token, request);
|
||||
Flux<Map<String, Object>> flux = wenDuoDuoPptApi.createOutline(request);
|
||||
StringBuffer contentBuffer = new StringBuffer();
|
||||
flux.doOnNext(chunk -> {
|
||||
contentBuffer.append(chunk.get("text"));
|
||||
@ -72,7 +76,7 @@ public class WenDuoDuoPptApiTests {
|
||||
WenDuoDuoPptApi.UpdateOutlineRequest request = new WenDuoDuoPptApi.UpdateOutlineRequest(
|
||||
"1901539019628613632", TEST_OUT_LINE_CONTENT, "精简一点,三个章节即可");
|
||||
// 调用
|
||||
Flux<Map<String, Object>> flux = wenDuoDuoPptApi.updateOutline(token, request);
|
||||
Flux<Map<String, Object>> flux = wenDuoDuoPptApi.updateOutline(request);
|
||||
StringBuffer contentBuffer = new StringBuffer();
|
||||
flux.doOnNext(chunk -> {
|
||||
contentBuffer.append(chunk.get("text"));
|
||||
@ -98,7 +102,7 @@ public class WenDuoDuoPptApiTests {
|
||||
1, null, null, null);
|
||||
WenDuoDuoPptApi.TemplateQueryRequest request = new WenDuoDuoPptApi.TemplateQueryRequest(1, 10, filter);
|
||||
// 调用
|
||||
WenDuoDuoPptApi.PagePptTemplateInfo pptTemplatePage = wenDuoDuoPptApi.getTemplatePage(token, request);
|
||||
WenDuoDuoPptApi.PagePptTemplateInfo pptTemplatePage = wenDuoDuoPptApi.getTemplatePage(request);
|
||||
// 打印结果
|
||||
System.out.println(pptTemplatePage);
|
||||
}
|
||||
@ -110,9 +114,9 @@ public class WenDuoDuoPptApiTests {
|
||||
@Disabled
|
||||
public void testGeneratePptx() {
|
||||
// 准备参数
|
||||
WenDuoDuoPptApi.CreatePptRequest request = new WenDuoDuoPptApi.CreatePptRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT);
|
||||
WenDuoDuoPptApi.PptCreateRequest request = new WenDuoDuoPptApi.PptCreateRequest("1901539019628613632", "1805081814809960448", TEST_OUT_LINE_CONTENT);
|
||||
// 调用
|
||||
WenDuoDuoPptApi.PptInfo pptInfo = wenDuoDuoPptApi.create(token, request);
|
||||
WenDuoDuoPptApi.PptInfo pptInfo = wenDuoDuoPptApi.create(request);
|
||||
// 打印结果
|
||||
System.out.println(pptInfo);
|
||||
}
|
||||
@ -309,6 +313,7 @@ public class WenDuoDuoPptApiTests {
|
||||
#### 7.2.2 合作共赢
|
||||
期待与更多的企业和机构合作,共同推动AI技术的应用。
|
||||
#### 7.2.3 共创未来
|
||||
让我们一起用AI技术改变世界,共创美好未来。""";
|
||||
让我们一起用AI技术改变世界,共创美好未来。
|
||||
""";
|
||||
|
||||
}
|
||||
|
@ -18,10 +18,10 @@ import java.io.File;
|
||||
public class XunFeiPptApiTests {
|
||||
|
||||
// 讯飞 API 配置信息,实际使用时请替换为您的应用信息
|
||||
private static final String APP_ID = "";
|
||||
private static final String API_SECRET = "";
|
||||
private static final String APP_ID = "6c8ac023";
|
||||
private static final String API_SECRET = "Y2RjM2Q1MWJjZTdkYmFiODc0OGE5NmRk";
|
||||
|
||||
private final XunFeiPptApi xunfeiPptApi = new XunFeiPptApi(XunFeiPptApi.BASE_URL, APP_ID, API_SECRET);
|
||||
private final XunFeiPptApi xunfeiPptApi = new XunFeiPptApi(APP_ID, API_SECRET);
|
||||
|
||||
/**
|
||||
* 获取 PPT 模板列表
|
||||
@ -75,6 +75,7 @@ public class XunFeiPptApiTests {
|
||||
|
||||
/**
|
||||
* 创建大纲(通过文本)
|
||||
*
|
||||
* @return 创建大纲响应
|
||||
*/
|
||||
private XunFeiPptApi.CreateResponse getCreateResponse() {
|
||||
@ -160,7 +161,7 @@ public class XunFeiPptApiTests {
|
||||
@Disabled
|
||||
public void testPollCheckProgress() throws InterruptedException {
|
||||
// 准备参数 - 使用之前创建 PP T时返回的 sid
|
||||
String sid = "fa36e926f2ed434987fcb4c1f0776ffb"; // 替换为实际的sid
|
||||
String sid = "1690ef6ee0344e72b5c5434f403b8eaa"; // 替换为实际的sid
|
||||
|
||||
// 最大轮询次数
|
||||
int maxPolls = 20;
|
||||
|
Loading…
Reference in New Issue
Block a user