DashText は、DashVector に推奨されるスパースベクトルエンコーダーです。DashText は、Best Match 25(BM25)アルゴリズムを使用して、元のテキストをスパースベクトルに変換できます。これにより、DashVector のキーワード対応セマンティック検索機能の使用が大幅に簡素化されます。
サンプルコードが正しく実行されるように、YOUR_API_KEY を API キーに、YOUR_CLUSTER_ENDPOINT をクラスターのエンドポイントに置き換える必要があります。
このトピックでは、検索でスパースベクトルを使用する方法について説明します。簡単にするために、密ベクトルの次元数は 4 に設定されています。実際のシナリオでは、必要に応じて設定してください。詳細については、「ベクトルの概要」をご参照ください。
ステップ 1. スパースベクトルをサポートするコレクションを作成する
import dashvector
client = dashvector.Client(api_key='YOUR_API_KEY', endpoint='YOUR_CLUSTER_ENDPOINT')
assert client
ret = client.create('hybrid_collection', dimension=4, metric='dotproduct')
assert ret
collection = client.get('hybrid_collection')
assert collectionimport com.aliyun.dashvector.DashVectorClient;
import com.aliyun.dashvector.DashVectorCollection;
import com.aliyun.dashvector.models.requests.CreateCollectionRequest;
import com.aliyun.dashvector.models.responses.Response;
import com.aliyun.dashvector.proto.CollectionInfo;
DashVectorClient client =
new DashVectorClient("YOUR_API_KEY", "YOUR_CLUSTER_ENDPOINT");
CreateCollectionRequest request = CreateCollectionRequest.builder()
.name("hybrid_collection")
.dimension(4)
.metric(CollectionInfo.Metric.dotproduct)
.dataType(CollectionInfo.DataType.FLOAT)
.build();
Response<Void> response = client.create(request);
System.out.println(response);
DashVectorCollection collection = client.get("hybrid_collection");ドット積メトリック(metric='dotproduct')を使用するコレクションのみがスパースベクトルをサポートします。
ステップ 2. スパースベクトルエンコーダーを作成する
組み込みエンコーダーを使用する
from dashtext import SparseVectorEncoder
encoder = SparseVectorEncoder.default()import com.aliyun.dashtext.encoder.SparseVectorEncoder;
SparseVectorEncoder encoder = SparseVectorEncoder.getDefaultInstance();組み込みエンコーダーは中国語版 Wikipedia コーパスでトレーニングされており、中国語のテキストセグメンテーションにはJieba が使用されます。
独自のコーパスに基づいてエンコーダーを作成する
from dashtext import SparseVectorEncoder
encoder = SparseVectorEncoder()
# Your own corpus.
corpus = [
"向量检索服务DashVector基于阿里云自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务",
"DashVector将其强大的向量管理、向量查询等多样化能力,通过简洁易用的SDK/API接口透出,方便被上层AI应用迅速集成",
"从而为包括大模型生态、多模态AI搜索、分子结构分析在内的多种应用场景,提供所需的高效向量检索能力",
"简单灵活、开箱即用的SDK,使用极简代码即可实现向量管理",
"自研向量相似性比对算法,快速高效稳定服务",
"Schema-free设计,通过Schema实现任意条件下的组合过滤查询"
]
# Train the encoder by using your own corpus.
encoder.train(corpus)import com.aliyun.dashtext.encoder.SparseVectorEncoder;
import java.util.*;
SparseVectorEncoder encoder = new SparseVectorEncoder();
// Your own corpus.
List<String> corpus = Arrays.asList(
"向量检索服务DashVector基于阿里云自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务",
"DashVector将其强大的向量管理、向量查询等多样化能力,通过简洁易用的SDK/API接口透出,方便被上层AI应用迅速集成",
"从而为包括大模型生态、多模态AI搜索、分子结构分析在内的多种应用场景,提供所需的高效向量检索能力",
"简单灵活、开箱即用的SDK,使用极简代码即可实现向量管理",
"自研向量相似性比对算法,快速高效稳定服务",
"Schema-free設計,通過Schema實現任意條件下的組合過濾查詢" // Translate only the comments
);
// Train the encoder by using your own corpus.
encoder.train(corpus);組み込みエンコーダーは、元のコーパスでトレーニングする必要なくすぐに使用できるため、ユーザーフレンドリーで汎化性に優れています。ただし、元のコーパスに多くの用語が含まれている場合、組み込みエンコーダーの精度は低くなります。
独自のコーパスに基づいてエンコーダーを作成するには、事前にエンコーダーを完全なコーパスでトレーニングする必要があります。このようにして、エンコーダーはより高い精度を提供します。詳細については、「高度な使用方法」をご参照ください。
ビジネス要件に基づいてエンコーダーを選択する必要があります。ビジネスに特定の分野に固有の多数の用語が含まれる場合は、独自のコーパスに基づいてエンコーダーを作成することをお勧めします。
ステップ 3. スパースベクトルを含むドキュメントを挿入する
from dashvector import Doc
document = "向量检索服务DashVector基于阿里云自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务。"
doc_sparse_vector = encoder.encode_documents(document)
print(doc_sparse_vector)
# 組み込みエンコーダーに基づく出力:
# {380823393: 0.7262431704356519, 414191989: 0.7262431704356519, 565176162: 0.7262431704356519, 904594806: 0.7262431704356519, 1005505802: 0.7262431704356519, 1169440797: 0.8883757984694465, 1240922502: 0.7262431704356519, 1313971048: 0.7262431704356519, 1317077351: 0.7262431704356519, 1490140460: 0.7262431704356519, 1574737055: 0.7262431704356519, 1760434515: 0.7262431704356519, 2045788977: 0.8414146776926797, 2141666983: 0.7262431704356519, 2509543087: 0.7262431704356519, 3180265193: 0.7262431704356519, 3845702398: 0.7262431704356519, 4106887295: 0.7262431704356519}
collection.insert(Doc(
id='A',
vector=[0.1, 0.2, 0.3, 0.4],
sparse_vector=doc_sparse_vector
))String document = "向量检索服务DashVector基于达摩院自研的高效向量引擎Proxima内核,提供具备水平拓展能力的云原生、全托管的向量检索服务。";
Map<Long, Float> sparseVector = encoder.encodeDocuments(document);
System.out.println(sparseVector);
// 組み込みエンコーダーに基づく出力:
// {380823393: 0.7262431704356519, 414191989: 0.7262431704356519, 565176162: 0.7262431704356519, 904594806: 0.7262431704356519, 1005505802: 0.7262431704356519, 1169440797: 0.8883757984694465, 1240922502: 0.7262431704356519, 1313971048: 0.7262431704356519, 1317077351: 0.7262431704356519, 1490140460: 0.7262431704356519, 1574737055: 0.7262431704356519, 1760434515: 0.7262431704356519, 2045788977: 0.8414146776926797, 2141666983: 0.7262431704356519, 2509543087: 0.7262431704356519, 3180265193: 0.7262431704356519, 3845702398: 0.7262431704356519, 4106887295: 0.7262431704356519}
Vector vector = Vector.builder().value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)).build();
// スパースベクトルを含むDocオブジェクトを構築します。
Doc doc = Doc.builder()
.id("28")
.sparseVector(sparseVector)
.vector(vector)
.build();
// スパースベクトルを含むドキュメントを挿入します。
Response<Void> response = collection.insert(InsertDocRequest.builder().doc(doc).build());ステップ 4. キーワード対応セマンティック検索を実行する
query = "什么是向量检索服务?"
sparse_vector = encoder.encode_queries(query)
print(sparse_vector)
# 組み込みエンコーダーに基づく出力:
# {1169440797: 0.2947158712590364, 2045788977: 0.7052841287409635}
docs = collection.query(
vector=[0.1, 0.1, 0.1, 0.1],
sparse_vector=sparse_vector
)String query = "什么是向量检索服务?";
Map<Long, Float> sparseVector = encoder.encodeQueries(query);
System.out.println(sparseVector);
// 組み込みエンコーダーに基づく出力:
// {1169440797: 0.2947158712590364, 2045788977: 0.7052841287409635}
Vector vector = Vector.builder().value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)).build();
// QueryDocRequest オブジェクトをビルドします。
QueryDocRequest request = QueryDocRequest.builder()
.vector(vector)
.sparse_ector(sparseVector)
.topk(100)
.includeVector(true)
.build();
Response<List<Doc>> response = collection.query(request);
System.out.println(response);ステップ 5. 重み係数付きキーワード対応セマンティックベクトル検索を実行する
from dashtext import combine_dense_and_sparse
query = "什么是向量检索服务?"
sparse_vector = encoder.encode_queries(query)
# 重み係数を指定します。
alpha = 0.7
dense_vector = [0.1, 0.1, 0.1, 0.1]
scaled_dense_vector, scaled_sparse_vector = combine_dense_and_sparse(dense_vector, sparse_vector, alpha)
docs = collection.query(
vector=scaled_dense_vector,
sparse_vector=scaled_sparse_vector
)String query = "什么是向量检索服务?";
Map<Long, Float> sparseVector = encoder.encodeQueries(query);
System.out.println(sparse_vector);
// 組み込みエンコーダーに基づく出力:
// {1169440797: 0.2947158712590364, 2045788977: 0.7052841287409635}
Vector denseVector = Vector.builder().value(Arrays.asList(0.1f, 0.2f, 0.3f, 0.4f)).build();
// アルファ係数に基づいて、密ベクトルと疎ベクトルの重みを調整します。
float alpha = 0.1;
sparse_vector.forEach((key, value) -> sparse_vector.put(key, value * alpha));
denseVector = Vector.builder().value(
denseVector.getValue().stream().map(number -> number.floatValue() * alpha).collect(Collectors.toList())
).build();
// QueryDocRequest オブジェクトを構築します。
QueryDocRequest request = QueryDocRequest.builder()
.vector(denseVector)
.sparse_ector(sparseVector)
.topk(100)
.includeVector(true)
.build();
Response<List<Doc>> response = collection.query(request);
System.out.println(response);alpha パラメーターは、密ベクトルとスパースベクトルの重み付き距離を制御します。 0.0 に設定すると、スパースベクトルのみが距離測定に使用されます。 1.0 に設定すると、密ベクトルのみが距離測定に使用されます。
API リファレンス
DashText API の詳細については、以下を参照してください。
Python 用 SDK: https://pypi.org/project/dashtext/