全部产品
Search
文档中心

向量检索服务:快速开始

更新时间:Apr 11, 2024

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

前提条件

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

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

Step1. 创建Client

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

import dashvector

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
assert client
import com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.common.DashVectorException;


DashVectorClient client = new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");

Step2. 创建Collection

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

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

collection = client.get('quickstart')
assert collection
import com.aliyun.dashvector.models.responses.Response;
import com.aliyun.dashvector.DashVectorCollection;


Response<Void> response = client.create("quickstart", 4);
System.out.println(response);
DashVectorCollection collection = client.get("quickstart");

assert collection.isSuccess();
curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "quickstart", 
    "dimension": 4
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections
说明
  1. 在未指定距离度量参数时,将使用默认的Cosine距离度量方式。

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

Step3. 插入Doc

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'})    
    ]
)
import com.aliyun.dashvector.models.Vector;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.InsertDocRequest;
import com.aliyun.dashvector.models.responses.Response;

import java.util.Arrays;
import java.util.HashMap;


Doc doc1 = Doc.builder()
    .id("1")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f))
            .build()
    ).build();

Doc doc2 = Doc.builder()
    .id("2")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.2f, 0.3f, 0.4f, 0.5f))
            .build()
    ).fields(new HashMap<String, Object>(){{
        put("age", 20);
        put("name", "zhangsan");
    }}).build();

Doc doc3 = Doc.builder()
    .id("3")
    .field("anykey", "anyvalue")
    .vector(
        Vector.builder()
            .value(Arrays.asList(0.3f, 0.4f, 0.5f, 0.6f))
            .build()
    ).build();

InsertDocRequest request = InsertDocRequest.builder()
    .docs(Arrays.asList(doc1, doc2, doc3))
    .build();

Response<Void> response = collection.insert(request);
# 插入3条数据

curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "docs": [
      {"id": "1", "vector": [0.1, 0.2, 0.3, 0.4]},
      {"id": "2", "vector": [0.2, 0.3, 0.4, 0.5], "fields": {"age": 20, "name": "zhangsan"}},
      {"id": "3", "vector": [0.3, 0.4, 0.5, 0.6], "fields": {"anykey": "anyvalue"}}
    ]
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs

Step4. 相似性检索

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

print(rets)
import com.aliyun.dashvector.models.Vector;
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.QueryDocRequest;
import com.aliyun.dashvector.models.responses.Response;

import java.util.Arrays;
import java.util.List;


Vector vector = Vector.builder().value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)).build();
      	
QueryDocRequest request = QueryDocRequest.builder()
    .vector(vector)
    .topk(2)
    .build();

Response<List<Doc>> response = collection.query(request);
curl -XPOST \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "vector": [0.1, 0.2, 0.3, 0.4],
    "topk": 2
  }' https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/query

Step5. 删除Doc

# 删除1条数据
collection.delete(ids=['1'])
import com.aliyun.dashvector.models.Doc;
import com.aliyun.dashvector.models.requests.DeleteDocRequest;
import com.aliyun.dashvector.models.responses.Response;


DeleteDocRequest request = DeleteDocRequest.builder()
    .id("1")
    .build();
      
Response<List<Doc>> response = collection.delete(request);
curl -XDELETE \
  -H 'dashvector-auth-token: YOUR_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"ids": ["1"]}' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/docs

Step6. 查看Collection统计信息

stats = collection.stats()

print(stats)
import com.aliyun.dashvector.models.CollectionStats;
import com.aliyun.dashvector.models.responses.Response;


Response<CollectionStats> response = collection.stats();
curl -H 'dashvector-auth-token: YOUR_API_KEY' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart/stats

Step7. 删除Collection

client.delete('quickstart')
import com.aliyun.dashvector.models.responses.Response;


Response<Void> response = client.delete("quickstart");
curl -XDELETE -H 'dashvector-auth-token: YOUR_API_KEY' \
  https://YOUR_CLUSTER_ENDPOINT/v1/collections/quickstart