如何通过Python SDK创建一个新的Collection
本文介绍如何通过Python SDK创建一个新的Collection。前提条件
[*]已创建Cluster:创建Cluster
[*]已获得API-KEY:API-KEY管理
[*]已安装最新版SDK:安装DashVector SDK
接口定义
Python示例:
Client.create(
name: str,
dimension: int,
dtype: Union, Type]=float,
fields_schema: Optional, Type, Type, Type, Type]]]=None,
metric: str='cosine',
extra_params: Dict=None,
timeout: Optional=None,
vectors: Union] = None,
sparse_vectors: Union] = None,
) -> DashVectorResponse使用示例
说明
需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。
创建单向量集合
Python示例:
import dashvector
client = dashvector.Client(
api_key='YOUR_API_KEY',
endpoint='YOUR_CLUSTER_ENDPOINT'
)
# 创建一个名称为quickstart、向量维度为4、
# 向量数据类型为float(默认)、
# 距离度量方式为dotproduct(内积)的Collection
# 并预先定义四个Field,名称为name、weight、age、id,数据类型分别为str、float、int、dashvector.long
# timeout为-1 ,开启create接口异步模式
ret = client.create(
name='quickstart',
dimension=4,
metric='dotproduct',
dtype=float,
# 为保留类型注解的语义明确性,DashVector通过typing模块定义long类型,为需要显式标注大整数场景提供了类型标注支持
fields_schema={'name': str, 'weight': float, 'age': int, 'id': dashvector.long},
timeout=-1
)
# 判断collection是否创建成功
if ret:
print('create collection success!')
# 等同于下列代码
# from dashvector import DashVectorCode
# if ret.code == DashVectorCode.Success:
# print('create collection success!')创建多向量集合
ret = client.create(
'multi_vector_demo',
vectors={
"title": VectorParam(4),
"content": VectorParam(6, metric="euclidean"),
},
sparse_vectors={
"abstruct": VectorParam(metric="dotproduct"),
"keywords": VectorParam(metric="dotproduct"),
# 稀疏向量索引目前仅支持内积度量,dimension/dtype使用默认值无需设置
},
fields_schema={
'author': str,
}
)
assert ret说明
多向量不支持仅有1条稠密向量+1条稀疏向量collection的建立,如有此需求,请创建单向量Collection,度量方式选内积。
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]