Python SDK

更新时间:

环境依赖

1.Python>=3.7

安装

pip install 直接通过git安装

pip install xingchen==1.1.5

知识库管理

准备

from xingchen import Configuration, ApiClient, KnowledgeBaseApiSub, KnowledgeBaseCreateDTO, KnowledgeBaseUpdateDTO, \
    KnowledgeBaseDeleteDTO, \
    KnowledgeBaseQueryDTO, KnowledgeBaseDetailUploadDTO, FileInfoVO, KnowledgeBaseDetailUpdateDTO, \
    KnowledgeBaseDetailQueryDTO, KnowledgeBaseDetailDeleteDTO


# 设置 host 和角色管理 api_instance
def init_client():
    configuration = Configuration(
        host="https://nlp.aliyuncs.com"
    )
    configuration.access_token = "{YOUR-API-KEY}"
    with ApiClient(configuration) as api_client:
        api_instance = KnowledgeBaseApiSub(api_client)
    return api_instance

知识库创建

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseCreateDTO(
    name="知识库名称",
    description="知识库详情"
)
result = api_instance.create(knowledge_base_create_dto=body)
print(result)

知识库修改

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseUpdateDTO(
    knowledgeBaseId="知识库id",
    name="知识库名称",
    description="知识库描述"
)
result = api_instance.update(knowledge_base_update_dto=body)
print(result)

知识库搜索

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseQueryDTO(
    pageNum=1,
    pageSize=10,
)
result = api_instance.search(knowledge_base_query_dto=body)
print(result)

知识库删除

# 知识库管理准备部分已省略
api_instance = self.init_client()
    body = KnowledgeBaseDeleteDTO(
        knowledge_base_id="知识库id"
    )
    result = api_instance.delete(knowledge_base_delete_dto=body)
    print(result)

知识库文件详情上传

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseDetailUploadDTO(
    knowledgeBaseId="知识库id",
    type="text",
    fileInfos=[
        FileInfoVO(
            filename="文件名.txt",
            fileUrl="公网可访问域名"
        )
    ]
)
result = api_instance.detail_upload(knowledge_base_detail_upload_dto=body)
print(result)

知识库文件详情修改

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseDetailUpdateDTO(
    knowledgeBaseId="知识库id",
    name="知识库文件详情名称",
    newName="知识库修改文件详情名称"
)
result = api_instance.detail_update(knowledge_base_detail_update_dto=body)
print(result)

知识库文件详情搜索

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseDetailQueryDTO(
    pageNum=1,
    pageSize=10,
    knowledgeBaseId="知识库id",
)
result = api_instance.detail_search(knowledge_base_detail_query_dto=body)
print(result)

知识库文件详情删除

# 知识库管理准备部分已省略
api_instance = self.init_client()
body = KnowledgeBaseDetailDeleteDTO(
    knowledgeBaseId="知识库id",
    name="知识库详情名称"
)
result = api_instance.detail_delete(knowledge_base_detail_delete_dto=body)
print(result)