使用 Faiss 连接器 (预览版)

警告

语义内核矢量存储功能为预览版功能,在发布之前仍可能会发生需要重大更改的改进,但这种情况有限。

目前不支持

概述

Faiss Vector Store 连接器是由语义内核提供的矢量存储实现,它不使用外部数据库,并将数据存储在 Faiss 索引中的内存和向量中。 它使用 InMemoryVectorCollection 处理记录的其他部分,同时使用 Faiss 索引来进行搜索。 此矢量存储在原型开发应用场景或需要高速内存中操作的情况下非常有用。

连接器具有以下特征。

功能区域 支持
集合映射至 内存中和 Faiss 索引字典
支持的键属性类型 允许作为字典键的任何内容,请在此处查看 python 文档了解详细信息
支持的数据属性类型 任意类型
支持的向量属性类型
  • list[float]
  • 列表[整数]
  • numpy 数组
支持的索引类型 平面(请参阅自定义索引
支持的距离函数
  • 点积相似性
  • 欧几里得平方距离
支持记录中的多个向量 是的
是否支持 is_filterable? 是的
是否支持 is_full_text_searchable? 是的

入门

将语义内核包添加到项目。

pip install semantic-kernel[faiss]

在下面的代码片段中,假定你有一个名为“DataModel”的数据模型类。

from semantic_kernel.connectors.faiss import FaissStore

vector_store = FaissStore()
vector_collection = vector_store.get_collection("collection_name", DataModel)

可以构造对命名集合的直接引用。

from semantic_kernel.connectors.faiss import FaissCollection

vector_collection = FaissCollection(DataModel, collection_name="collection_name")

自定义索引

Faiss 连接器只能用于平面索引类型。

鉴于 Faiss 索引的复杂性,你可以自由地创建自己的索引(es),包括生成 faiss-gpu 包以及使用该包中的索引。 执行此作时,将忽略在向量字段上定义的任何指标。 如果数据模型中有多个矢量,则只能为所需矢量传入自定义索引,并允许创建内置索引,将在模型中定义平面索引和指标。

请务必注意,如果索引需要训练,则确保也这样做,每当我们使用索引时,都会对索引的 is_trained 属性执行检查。

该索引始终在集合的 indexes 属性中可用(自定义或内置)。 可以用这个来获取索引并对其执行任何操作,以便之后可以进行训练,只需确保在对索引使用任何CRUD之前完成这些操作。

若要传入自定义索引,请使用以下任一项:


import faiss

from semantic_kernel.connectors.faiss import FaissCollection

index = faiss.IndexHNSW(d=768, M=16, efConstruction=200) # or some other index
vector_collection = FaissCollection(
    record_type=DataModel, 
    collection_name="collection_name", 
    indexes={"vector_field_name": index}
)

或者:


import faiss

from semantic_kernel.connectors.faiss import FaissCollection

index = faiss.IndexHNSW(d=768, M=16, efConstruction=200) # or some other index
vector_collection = FaissCollection(
    record_type=DataModel,
    collection_name="collection_name", 
)
await vector_collection.ensure_collection_exists(
    indexes={"vector_field_name": index}
)
# or when you have only one vector field:
await vector_collection.ensure_collection_exists(
    index=index
)

目前不支持