免费体验阿里云高性能向量检索服务:https://www.aliyun.com/product/ai/dashvector 

如何快速使用向量检索服务DashVector?_人工智能

编辑


 本文将介绍如何快速上手使用向量检索服务DashVector。

前提条件

说明

1. 需要使用您的api-key替换示例中的YOUR_API_KEY、您的Cluster Endpoint替换示例中的YOUR_CLUSTER_ENDPOINT,代码才能正常运行。

2. Cluster Endpoint,可在控制台“Cluster详情”中查看。

Step1. 创建Client

使用HTTP API时可跳过本步骤。

Python示例:

import dashvector

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
assert client

Step2. 创建Collection

创建一个名称为quickstart,向量维度为4的collection。

Python示例:

client.create(name='quickstart', dimension=4)

collection = client.get('quickstart')
assert collection

说明

1.在未指定距离度量参数时,将使用默认的Cosine距离度量方式。

2.在未指定向量数据类型时,将使用默认的Float数据类型。

Step3. 插入Doc

Python示例:

from dashvector import Doc

# 通过dashvector.Doc对象,插入单条数据
collection.insert(Doc(id='1', vector=[0.1, 0.2, 0.3, 0.4]))

# 通过dashvector.Doc对象,批量插入2条数据
collection.insert(
    [
        Doc(id='2', vector=[0.2, 0.3, 0.4, 0.5], fields={'age': 20, 'name': 'zhangsan'}),
        Doc(id='3', vector=[0.3, 0.4, 0.5, 0.6], fields={'anykey': 'anyvalue'})    
    ]
)

Step4. 相似性检索

Python示例:

rets = collection.query([0.1, 0.2, 0.3, 0.4], topk=2)

print(rets)

Step5. 删除Doc

Python示例:

# 删除1条数据
collection.delete(ids=['1'])

Step6. 查看Collection统计信息

Python示例:

stats = collection.stats()

print(stats)

Step7. 删除Collection

Python示例:

client.delete('quickstart')


体验阿里云高性能向量检索服务:https://www.aliyun.com/product/ai/dashvector 

如何快速使用向量检索服务DashVector?_Python_02

编辑