Semantic Kernel使用连接器进行向量搜索

360影视 欧美动漫 2025-09-08 08:40 3

摘要:Semantic Kernel 提供了向量存储(Vector Store)抽象层中的向量搜索功能,支持多种选项如过滤、分页等。本文将详细介绍其用法。

Semantic Kernel 提供了向量存储(Vector Store)抽象层中的向量搜索功能,支持多种选项如过滤、分页等。本文将详细介绍其用法。

SearchAsync方法允许基于已向量化的数据进行搜索。该方法接收一个向量和可选的VectorSearchOptions注意:VectorStoreCollection实现了。

如下是VectorStoreCollection类的定义:

public abstract class VectorStoreCollectionTKey, TRecord> : IVectorSearchableTRecord>, IDisposable
where TKey : notnull
where TRecord : class
{
.....
}
SearchAsync⚠️ 搜索向量类型必须与目标向量类型一致。例如,同一条记录中有两个不同类型的向量,需确保提供的搜索向量与目标向量类型匹配。若有多个向量,可通过VectorProperty指定目标。通过可配置以下选项:VectorProperty

指定要搜索的向量属性。如果未指定且模型仅包含一个向量,则使用该向量。若没有或有多个向量而未指定,则会抛出异常。

public async IAsyncEnumerabledouble? Score)> SearchByTextAsync(
string query, int topK = 5, CancellationToken ct = default)
{
var queryVector = await _emb.CreateAsync(query, ct);

var col = GetCollection;

var options = new VectorSearchOptions
{
//Filter = h => h.HotelName == "Tokyo",
VectorProperty = h => h.DescriptionEmbedding,
Skip = 0,
IncludeVectors = false
};

awaitforeach (var r in col.SearchAsync(queryVector, topK, options, ct))
{
yield return (r.Record, r.Score);
}
}
Top 和 Skip

用于分页。

Top:返回前 N 条结果Skip:跳过前 N 条结果var vectorSearchOptions = new VectorSearchOptions
{
Skip = 40
};

var searchResult = collection.SearchAsync(searchVector, top: 20, vectorSearchOptions);
IncludeVectors

指定是否返回结果中的向量属性。

默认值: false(节省带宽与处理成本) 若为 true,则返回完整向量数据var vectorSearchOptions = new VectorSearchOptions
{
IncludeVectors = true
};
Filter

用于在向量搜索前先对记录进行过滤。

好处:

⚠️ 很多存储需要字段设置为IsIndexed = true才能参与过滤。 public async IAsyncEnumerabledouble? Score)> SearchByTextAsync(
string query, int topK = 5, CancellationToken ct = default)
{
var queryVector = await _emb.CreateAsync(query, ct);

var col = GetCollection;

var options = new VectorSearchOptions
{
Filter = h => h.HotelName == "Tokyo",
VectorProperty = h => h.DescriptionEmbedding,
Skip = 0,
IncludeVectors = false
};

awaitforeach (var r in col.SearchAsync(queryVector, topK, options, ct))
{
yield return (r.Record, r.Score);
}
}
SearchAsync:执行相似度搜索VectorProperty:选择目标向量Top / Skip:支持分页IncludeVectors:是否返回向量Filter:先过滤后搜索,提高性能和安全性

这些功能让你能够在不同存储(如 InMemory、Qdrant 等)上轻松实现向量化搜索和检索。

来源:opendotnet

相关推荐