本文提供妙读写作链路 API的几个最佳实践,帮助您快速入门并开发您自己的业务应用。
前提条件
已开通服务;
获取WorkSpaceId 获取Workspace ID;
引入妙读SDK 注意获取最新SDK版本。
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-aimiaobi20230801</artifactId>
<version>${latest}</version>
</dependency>
操作步骤
上传文档到文档库
基于妙读提供的API,通过上传文档到妙读文档库,使用者可以对文档库中的内容进行搜索和问答,本章通过代码示例来帮助您熟悉上传文档API的使用。
上传调用demo如下:
package com.aliyun.sdk.service.aimiaobi20230801;
import com.alibaba.fastjson.JSON;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.gateway.pop.Configuration;
import com.aliyun.sdk.gateway.pop.auth.SignatureVersion;
import com.aliyun.sdk.service.aimiaobi20230801.models.UploadDocRequest;
import com.aliyun.sdk.service.aimiaobi20230801.models.UploadDocResponse;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* packageName quanmiao
*
* @author zhujin
* @version JDK 8
* @className uploadDocTest
* @date 2024/8/15
* @description 文档上传测试
*/
public class uploadDocTest {
public static void main(String[] args) {
try{
StaticCredentialProvider provider = StaticCredentialProvider.create(
Credential.builder()
.accessKeyId(Constant.accessKeyId)
.accessKeySecret(Constant.accessKeySecret)
.build()
);
AsyncClient client = AsyncClient.builder()
.region("cn-beijing") // Region ID
.credentialsProvider(provider)
.serviceConfiguration(Configuration.create()
.setSignatureVersion(SignatureVersion.V3)
)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setProtocol("HTTPS")
.setEndpointOverride("aimiaobi.cn-beijing.aliyuncs.com")
)
.build();
List<UploadDocRequest.Docs> docsList = new ArrayList<>();
UploadDocRequest.Docs doc = UploadDocRequest.Docs.builder()
.docName("测试文档.pdf")
.fileUrl("https://xxx.oss-cn-beijing.aliyuncs.com/test/测试文档.pdf")
.build();
docsList.add(doc);
UploadDocRequest request = UploadDocRequest.builder()
.workspaceId(Constant.workspaceId)
.docs(docsList)
.build();
CompletableFuture<UploadDocResponse> future = client.uploadDoc(request);
UploadDocResponse response = future.get();
System.out.println("ALL***********************");
System.out.println("请求成功的请求头值:");
System.out.println(response.getStatusCode());
System.out.println(response.getHeaders());
String jsonStr = JSON.toJSONString(response.getBody());
System.out.println(jsonStr);
}catch (Exception ex){
System.out.println("///////exception happen is " + ex);
}
}
}
为指定的文档生成导读内容
基于妙读提供的API,使用者可以为指定的文档生成导读内容,该内容包括文档简介、文档内容要点和文档章节导读,本章通过代码示例,来帮助您熟悉文档导读生成API的使用。
生成调用demo如下:
package com.aliyun.sdk.service.aimiaobi20230801;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.gateway.pop.Configuration;
import com.aliyun.sdk.gateway.pop.auth.SignatureVersion;
import com.aliyun.sdk.service.aimiaobi20230801.models.*;
import com.google.gson.Gson;
import darabonba.core.ResponseIterable;
import darabonba.core.ResponseIterator;
import darabonba.core.client.ClientOverrideConfiguration;
/**
* packageName quanmiao
*
* @author zhujin
* @version JDK 8
* @className runDocIntroductionTest
* @date 2024/8/15
* @description 文档导读测试
*/
public class runDocIntroductionTest {
public static void main(String[] args) {
try{
StaticCredentialProvider provider = StaticCredentialProvider.create(
Credential.builder()
.accessKeyId(Constant.accessKeyId)
.accessKeySecret(Constant.accessKeySecret)
.build()
);
AsyncClient client = AsyncClient.builder()
.region("cn-beijing") // Region ID
.credentialsProvider(provider)
.serviceConfiguration(Configuration.create()
.setSignatureVersion(SignatureVersion.V3)
)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setProtocol("HTTPS")
.setEndpointOverride("aimiaobi.cn-beijing.aliyuncs.com")
)
.build();
RunDocIntroductionRequest request = RunDocIntroductionRequest.builder()
.workspaceId(Constant.workspaceId)
.sessionId("sessionId_xxx")
.docId("xxxx-xxx-xxxx-xxx-xxxxxxx")
.build();
ResponseIterable<RunDocIntroductionResponseBody> x = client.runDocIntroductionWithResponseIterable(request);
ResponseIterator<RunDocIntroductionResponseBody> iterator = x.iterator();
while (iterator.hasNext()) {
System.out.println("----event----");
RunDocIntroductionResponseBody event = iterator.next();
System.out.println(new Gson().toJson(event));
//System.out.println(event.getMessage());
}
System.out.println("ALL***********************");
System.out.println("请求成功的请求头值:");
System.out.println(x.getStatusCode());
System.out.println(x.getHeaders());
}catch (Exception ex){
System.out.println("///////exception happen is " + ex);
}
}
}
对文档库中的文档进行实时问答
基于妙读提供的API,使用者可以针对文档库中的内容进行问答,本章通过代码示例,来帮助您熟悉文档问答API的使用。
问答调用demo如下:
package com.aliyun.sdk.service.aimiaobi20230801;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.gateway.pop.Configuration;
import com.aliyun.sdk.gateway.pop.auth.SignatureVersion;
import com.aliyun.sdk.service.aimiaobi20230801.models.RunDocQaRequest;
import com.aliyun.sdk.service.aimiaobi20230801.models.RunDocQaResponseBody;
import com.google.gson.Gson;
import darabonba.core.ResponseIterable;
import darabonba.core.ResponseIterator;
import darabonba.core.client.ClientOverrideConfiguration;
import java.util.ArrayList;
import java.util.List;
/**
* packageName quanmiao
*
* @author zhujin
* @version JDK 8
* @className runDocQATest
* @date 2024/8/15
* @description 文档问答测试
*/
public class runDocQATest {
public static void main(String[] args) {
try{
StaticCredentialProvider provider = StaticCredentialProvider.create(
Credential.builder()
.accessKeyId(Constant.accessKeyId)
.accessKeySecret(Constant.accessKeySecret)
.build()
);
AsyncClient client = AsyncClient.builder()
.region("cn-beijing") // Region ID
.credentialsProvider(provider)
.serviceConfiguration(Configuration.create()
.setSignatureVersion(SignatureVersion.V3)
)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setProtocol("HTTPS")
.setEndpointOverride("aimiaobi.cn-beijing.aliyuncs.com")
)
.build();
List<String> docIds = new ArrayList<>();
docIds.add("xxx-xxxx-xxxx-xxx-xxxxxxx");
RunDocQaRequest request = RunDocQaRequest .builder()
.workspaceId(Constant.workspaceId)
.sessionId("sessionId_xxx")
.searchSource("fromDoc")
.query("ai妙读能够实现什么功能")
.docIds(docIds)
.build();
ResponseIterable<RunDocQaResponseBody> x = client.runDocQaWithResponseIterable(request);
ResponseIterator<RunDocQaResponseBody> iterator = x.iterator();
while (iterator.hasNext()) {
System.out.println("----event----");
RunDocQaResponseBody event = iterator.next();
System.out.println(new Gson().toJson(event));
//System.out.println(event.getMessage());
}
System.out.println("ALL***********************");
System.out.println("请求成功的请求头值:");
System.out.println(x.getStatusCode());
System.out.println(x.getHeaders());
}catch (Exception ex){
System.out.println("///////exception happen is " + ex);
}
}
}
文档内容是否对您有帮助?