本页面提供一个知识库 viking_knowledgebase通过 Python SDK 创建知识库、上传文档和检索查询的完整流程请求示例,注意使用时根据实际情况填充账户、知识库、文件和查询信息。
from volcengine.viking_knowledgebase import VikingKnowledgeBaseService, Collection, Doc, Point from volcengine.viking_knowledgebase.common import Field, FieldType, IndexType, EmbddingModelType viking_knowledgebase_service = VikingKnowledgeBaseService(host="api-knowledgebase.mlp.cn-beijing.volces.com", scheme="https") viking_knowledgebase_service.set_ak("Your ak") viking_knowledgebase_service.set_sk("Your sk") collection_name = "your_collection_name" description = "" # 默认参数构建知识库 # my_collection = viking_knowledgebase_service.create_collection(collection_name) # 自定义index配置、preprocess文档配置构建知识库 index = { "index_type": IndexType.HNSW_HYBRID, "index_config": { "fields": [{ "field_name": "chunk_len", "field_type": FieldType.Int64, "default_val": 0 }], "cpu_quota": 1, "embedding_model":EmbddingModelType.EmbeddingModelBgeLargeZhAndM3 } } preprocessing = { "chunk_length" :200 } my_collection = viking_knowledgebase_service.create_collection(collection_name=collection_name, description=description, index=index, preprocessing=preprocessing) # 获取collection详细信息 my_collection = viking_knowledgebase_service.get_collection(collection_name=collection_name) # 由tos路径上传doc tos_path = "" my_collection.add_doc(add_type="tos", tos_path=tos_path) # 由url上传doc url = "your url" my_collection.add_doc(add_type="url", doc_id="your_doc_id", doc_name="your_doc_name", doc_type="", url=url) # 查询 query = "" points = viking_knowledgebase_service.search_collection(collection_name=collection_name, query=query) for point in points: print(point.content) print(point.score)