使用 Weaviate Vector Store 连接器 (预览版)

警告

语义内核向量存储功能处于预览状态,需要中断性变更的改进可能仍发生在发布前的有限情况下。

概述

Weaviate Vector Store 连接器可用于访问和管理 Weaviate 中的数据。 连接器具有以下特征。

功能区域 支持
集合映射到 Weaviate 集合
支持的键属性类型 Guid
支持的数据属性类型
  • string
  • byte
  • short
  • int
  • long
  • double
  • float
  • 十进制
  • 布尔
  • DateTime
  • DateTimeOffset
  • Guid
  • 和枚举每种类型
支持的向量属性类型
  • ReadOnlyMemory<float>
  • ReadOnlyMemory<double>
支持的索引类型
  • Hnsw
  • 平面
  • 动态
支持的距离函数
  • CosineDistance
  • 负点积相似度
  • EuclideanSquaredDistance
  • 汉明
  • 曼哈顿
支持的过滤器语句
  • AnyTagEqualTo
  • EqualTo
支持记录中的多个向量
是否支持Filterable?
是否支持FullTextSearchable?
支持 StoragePropertyName? 否,请改用 JsonSerializerOptionsJsonPropertyNameAttribute 改用。 有关详细信息,请参阅此处。

限制

值得注意的 Weaviate 连接器功能限制。

功能区域 解决方法
不支持对单个向量对象使用“vector”属性 支持改用“vectors”属性。

警告

Weaviate 要求集合名称以大写字母开头。 如果未提供大写字母的集合名称,则尝试创建集合时,Weaviate 将返回错误。 您将看到的错误是 Cannot query field "mycollection" on type "GetObjectsObj". Did you mean "Mycollection"?,其中 mycollection 是您的集合名称。 在此示例中,如果将集合名称改为 Mycollection,则会修复错误。

入门

将 Weaviate Vector Store 连接器 NuGet 包添加到项目。

dotnet add package Microsoft.SemanticKernel.Connectors.Weaviate --prerelease

可以使用语义内核提供的扩展方法将向量存储添加到可用的 KernelBuilder 依赖项注入容器或 IServiceCollection 依赖项注入容器。 Weaviate 向量存储使用与 HttpClient Weaviate 服务通信。 有两个选项可用于为 Weaviate 服务提供 URL/终结点。 可以通过选项或通过设置该 HttpClient对象的基址来提供它。

第一个示例演示如何通过选项设置服务 URL。 另请注意,这些方法将检索从 HttpClient 依赖项注入服务提供商调用 Weaviate 服务的实例。

using Microsoft.SemanticKernel;

// Using Kernel Builder.
var kernelBuilder = Kernel
    .CreateBuilder()
    .AddWeaviateVectorStore(options: new() { Endpoint = new Uri("http://localhost:8080/v1/") });
using Microsoft.SemanticKernel;

// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddWeaviateVectorStore(options: new() { Endpoint = new Uri("http://localhost:8080/v1/") });

还可以在其中指定自己的 HttpClient 重载。 在这种情况下,可以通过选项设置服务 URL HttpClientBaseAddress

using System.Net.Http;
using Microsoft.SemanticKernel;

// Using Kernel Builder.
var kernelBuilder = Kernel.CreateBuilder();
using HttpClient client = new HttpClient { BaseAddress = new Uri("http://localhost:8080/v1/") };
kernelBuilder.AddWeaviateVectorStore(client);
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;

// Using IServiceCollection with ASP.NET Core.
var builder = WebApplication.CreateBuilder(args);
using HttpClient client = new HttpClient { BaseAddress = new Uri("http://localhost:8080/v1/") };
builder.Services.AddWeaviateVectorStore(client);

也可以直接构造 Weaviate 矢量存储实例。

using System.Net.Http;
using Microsoft.SemanticKernel.Connectors.Weaviate;

var vectorStore = new WeaviateVectorStore(
    new HttpClient { BaseAddress = new Uri("http://localhost:8080/v1/") });

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

using System.Net.Http;
using Microsoft.SemanticKernel.Connectors.Weaviate;

var collection = new WeaviateVectorStoreRecordCollection<Hotel>(
    new HttpClient { BaseAddress = new Uri("http://localhost:8080/v1/") },
    "Skhotels");

如果需要,可以使用上述任何机制(例如)将 Api 密钥作为选项传递。

using Microsoft.SemanticKernel;

var kernelBuilder = Kernel
    .CreateBuilder()
    .AddWeaviateVectorStore(options: new() { Endpoint = new Uri("http://localhost:8080/v1/"), ApiKey = secretVar });

数据映射

从数据模型到存储时,Weaviate Vector Store 连接器提供默认映射器。 Weaviate 要求将属性映射到 ID、有效负载和向量分组。 默认映射器使用模型注释或记录定义来确定每个属性的类型并执行此映射。

  • 批注为键的数据模型属性将映射到 Weaviate id 属性。
  • 批注为数据的数据模型属性将映射到 Weaviate properties 对象。
  • 作为向量批注的数据模型属性将映射到 Weaviate vectors 对象。

默认映射器用于 System.Text.Json.JsonSerializer 转换为存储架构。 这意味着,如果需要与数据模型属性名称不同的存储名称,则支持使用该 JsonPropertyNameAttribute 名称。

下面是具有 JsonPropertyNameAttribute 集的数据模型示例,以及如何在 Weaviate 中表示这些数据模型。

using System.Text.Json.Serialization;
using Microsoft.Extensions.VectorData;

public class Hotel
{
    [VectorStoreRecordKey]
    public ulong HotelId { get; set; }

    [VectorStoreRecordData(IsFilterable = true)]
    public string HotelName { get; set; }

    [VectorStoreRecordData(IsFullTextSearchable = true)]
    public string Description { get; set; }

    [JsonPropertyName("HOTEL_DESCRIPTION_EMBEDDING")]
    [VectorStoreRecordVector(4, DistanceFunction.EuclideanDistance, IndexKind.QuantizedFlat)]
    public ReadOnlyMemory<float>? DescriptionEmbedding { get; set; }
}
{
    "id": 1,
    "properties": { "HotelName": "Hotel Happy", "Description": "A place where everyone can be happy." },
    "vectors": {
        "HOTEL_DESCRIPTION_EMBEDDING": [0.9, 0.1, 0.1, 0.1],
    }
}

即将推出

更多信息即将推出。

即将推出

更多信息即将推出。