Java SDK
更新时间:
Java SDK
环境依赖
Java 1.8+
Maven/Gradle
安装
Maven用户
<dependency>
<groupId>com.alibaba.xingchen</groupId>
<artifactId>xingchen-java-client</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
Gradle用户
compile "com.alibaba.xingchen:xingchen-java-client:1.1.1"
知识库管理
准备
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.auth.HttpBearerAuth;
public class TestBaseV2 {
public static String baseUrl = "https://nlp.aliyuncs.com";
public ApiClient initClient() {
ApiClient defaultClient = new ApiClient();
defaultClient.setBasePath(baseUrl);
// Configure HTTP bearer authorization: Authorization
HttpBearerAuth authorization = (HttpBearerAuth) defaultClient.getAuthentication("Authorization");
authorization.setBearerToken("{API-KEY}");
return defaultClient;
}
}
知识库创建
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseCreateDTO;
import com.alibaba.xingchen.model.ResultDTOKnowledgeBaseDTO;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void create() throws ApiException {
KnowledgeBaseCreateDTO create = KnowledgeBaseCreateDTO.builder()
.name("知识库名称")
.description("知识库描述")
.build();
ResultDTOKnowledgeBaseDTO result = api.create(create);
Assert.assertTrue(result.getSuccess());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库修改
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseUpdateDTO;
import com.alibaba.xingchen.model.ResultDTOBoolean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void update() throws ApiException {
KnowledgeBaseUpdateDTO update = KnowledgeBaseUpdateDTO.builder()
.knowledgeBaseId("知识库id")
.name("更新知识库名称")
.description("更新知识库描述")
.build();
ResultDTOBoolean result = api.update(update);
Assert.assertTrue(result.getSuccess());
Assert.assertTrue(result.getData());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库搜索
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseQueryDTO;
import com.alibaba.xingchen.model.ResultDTOPageResultKnowledgeBaseDTO;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void search() throws ApiException {
KnowledgeBaseQueryDTO search = KnowledgeBaseQueryDTO.builder()
.pageNum(1)
.pageSize(10)
.build();
ResultDTOPageResultKnowledgeBaseDTO result = api.search(search);
Assert.assertTrue(result.getSuccess());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库删除
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseDeleteDTO;
import com.alibaba.xingchen.model.ResultDTOBoolean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void delete() throws ApiException {
KnowledgeBaseDeleteDTO delete = KnowledgeBaseDeleteDTO.builder()
.knowledgeBaseId("知识库id")
.build();
ResultDTOBoolean result = api.delete(delete);
Assert.assertTrue(result.getSuccess());
Assert.assertTrue(result.getData());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库详情文件上传
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.FileInfoVO;
import com.alibaba.xingchen.model.KnowledgeBaseDetailUploadDTO;
import com.alibaba.xingchen.model.ResultDTOBoolean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void detailUpload() throws ApiException {
KnowledgeBaseDetailUploadDTO upload = KnowledgeBaseDetailUploadDTO.builder()
.knowledgeBaseId("知识库id")
.type("text")
.fileInfos(Arrays.asList(
FileInfoVO.builder()
.filename("guanyu.txt")
.fileUrl("https://lang.alicdn.com/xingchen/guanyu.txt")
.build()))
.build();
ResultDTOBoolean result = api.detailUpload(upload);
Assert.assertTrue(result.getSuccess());
Assert.assertTrue(result.getData());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库详情文件修改
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseDetailUpdateDTO;
import com.alibaba.xingchen.model.ResultDTOBoolean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void detailUpdate() throws ApiException {
KnowledgeBaseDetailUpdateDTO update = KnowledgeBaseDetailUpdateDTO.builder()
.knowledgeBaseId("知识库id")
.name("知识库详情名称")
.newName("修改的知识库详情名称")
.build();
ResultDTOBoolean result = api.detailUpdate(update);
Assert.assertTrue(result.getSuccess());
Assert.assertTrue(result.getData());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库详情文件搜索
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseDetailQueryDTO;
import com.alibaba.xingchen.model.ResultDTOPageResultKnowledgeBaseDetailDTO;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void detailSearch() throws ApiException {
KnowledgeBaseDetailQueryDTO search = KnowledgeBaseDetailQueryDTO.builder()
.knowledgeBaseId("知识库id")
.pageNum(1)
.pageSize(10)
.build();
ResultDTOPageResultKnowledgeBaseDetailDTO result = api.detailSearch(search);
Assert.assertTrue(result.getSuccess());
System.out.println(JSONObject.toJSONString(result));
}
}
知识库详情文件删除
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xingchen.ApiClient;
import com.alibaba.xingchen.ApiException;
import com.alibaba.xingchen.api.KnowledgeBaseApiSub;
import com.alibaba.xingchen.model.KnowledgeBaseDetailDeleteDTO;
import com.alibaba.xingchen.model.ResultDTOBoolean;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class KnowledgeBaseApiTest extends TestBaseV2 {
private KnowledgeBaseApiSub api = new KnowledgeBaseApiSub();
@Before
public void init() {
ApiClient apiClient = super.initClient();
api.setApiClient(apiClient);
}
@Test
public void detailDelete() throws ApiException {
KnowledgeBaseDetailDeleteDTO delete = KnowledgeBaseDetailDeleteDTO.builder()
.knowledgeBaseId("知识库id")
.name("知识库详情名称")
.build();
ResultDTOBoolean result = api.detailDelete(delete);
Assert.assertTrue(result.getSuccess());
Assert.assertTrue(result.getData());
System.out.println(JSONObject.toJSONString(result));
}
}
文档内容是否对您有帮助?