本文將介紹如何快速上手使用向量檢索服務DashVector。
前提條件
已創建Cluster:創建Cluster。
已獲得API-KEY:API-KEY管理。
已安裝最新版SDK:安裝DashVector SDK。
說明
需要使用您的api-key替換示例中的YOUR_API_KEY、您的Cluster Endpoint替換示例中的YOUR_CLUSTER_ENDPOINT,代碼才能正常運行。
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
說明
在未指定距離度量參數時,將使用默認的Cosine距離度量方式。
在未指定向量數據類型時,將使用默認的
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
文檔內容是否對您有幫助?