Skip to content

Commit

Permalink
Pinecone memory connector (microsoft#1065)
Browse files Browse the repository at this point in the history
Pinecone vector database implementation
Original PR: microsoft#770
---------

Co-authored-by: Kevdome3000 <[email protected]>
Co-authored-by: tech <[email protected]>
Co-authored-by: Shawn Callegari <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
  • Loading branch information
5 people authored May 18, 2023
1 parent 120e400 commit ca8d614
Show file tree
Hide file tree
Showing 44 changed files with 4,727 additions and 1 deletion.
2 changes: 1 addition & 1 deletion FEATURE_MATRIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
| Qdrant (Memory) ||| Vector optimized |
| ChromaDb (Memory) || 🔄 | |
| Milvus (Memory) ||| Vector optimized |
| Pinecone (Memory) | || Vector optimized |
| Pinecone (Memory) | || Vector optimized |
| Weaviate (Memory) ||| Vector optimized |
| CosmosDB (Memory) ||| CosmosDB is not optimized for vector storage |
| Sqlite (Memory) ||| Sqlite is not optimized for vector storage |
Expand Down
7 changes: 7 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Planning.SequentialPlanner"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Memory.AzureCognitiveSearch", "src\Connectors\Connectors.Memory.AzureCognitiveSearch\Connectors.Memory.AzureCognitiveSearch.csproj", "{EC3BB6D1-2FB2-4702-84C6-F791DE533ED4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Memory.Pinecone", "src\Connectors\Connectors.Memory.Pinecone\Connectors.Memory.Pinecone.csproj", "{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Skills.Grpc", "src\Skills\Skills.Grpc\Skills.Grpc.csproj", "{E52F805C-794A-4CA9-B684-DFF358B18820}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.AI.HuggingFace", "src\Connectors\Connectors.AI.HuggingFace\Connectors.AI.HuggingFace.csproj", "{136823BE-8665-4D57-87E0-EF41535539E2}"
Expand Down Expand Up @@ -212,6 +214,10 @@ Global
{EC3BB6D1-2FB2-4702-84C6-F791DE533ED4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC3BB6D1-2FB2-4702-84C6-F791DE533ED4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC3BB6D1-2FB2-4702-84C6-F791DE533ED4}.Release|Any CPU.Build.0 = Release|Any CPU
{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E}.Release|Any CPU.Build.0 = Release|Any CPU
{E52F805C-794A-4CA9-B684-DFF358B18820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E52F805C-794A-4CA9-B684-DFF358B18820}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E52F805C-794A-4CA9-B684-DFF358B18820}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -257,6 +263,7 @@ Global
{F51017A9-15C8-472D-893C-080046D710A6} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{A350933D-F9D5-4AD3-8C4F-B856B5020297} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{EC3BB6D1-2FB2-4702-84C6-F791DE533ED4} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{4D226C2F-AE9F-4EFB-AF2D-45C8FE5CB34E} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{E52F805C-794A-4CA9-B684-DFF358B18820} = {9ECD1AA0-75B3-4E25-B0B5-9F0945B64974}
{136823BE-8665-4D57-87E0-EF41535539E2} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{4D3DAE63-41C6-4E1C-A35A-E77BDFC40675} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!-- THIS PROPERTY GROUP MUST COME FIRST -->
<AssemblyName>Microsoft.SemanticKernel.Connectors.Memory.Pinecone</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<!-- IMPORT NUGET PACKAGE SHARED PROPERTIES -->
<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />

<PropertyGroup>
<!-- NuGet Package Settings -->
<Title>Semantic Kernel - Pinecone Connector</Title>
<Description>Pinecone connector for Semantic Kernel skills and semantic memory</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel\SemanticKernel.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net.Http;
using System.Text.Json.Serialization;
using Microsoft.SemanticKernel.Connectors.Memory.Pinecone.Model;

namespace Microsoft.SemanticKernel.Connectors.Memory.Pinecone.Http.ApiSchema;

/// <summary>
/// This operation specifies the pod type and number of replicas for an index.
/// See https://docs.pinecone.io/reference/configure_index
/// </summary>
internal sealed class ConfigureIndexRequest
{
public string IndexName { get; set; }

/// <summary>
/// Gets or Sets PodType
/// </summary>
[JsonPropertyName("pod_type")]
public PodType PodType { get; set; }

/// <summary>
/// The desired number of replicas for the index.
/// </summary>
/// <value>The desired number of replicas for the index.</value>
[JsonPropertyName("replicas")]
public int Replicas { get; set; }

public static ConfigureIndexRequest Create(string indexName)
{
return new ConfigureIndexRequest(indexName);
}

public ConfigureIndexRequest WithPodType(PodType podType)
{
this.PodType = podType;
return this;
}

public ConfigureIndexRequest NumberOfReplicas(int replicas)
{
this.Replicas = replicas;
return this;
}

public HttpRequestMessage Build()
{
HttpRequestMessage? request = HttpRequest.CreatePatchRequest(
$"/databases/{this.IndexName}", this);

request.Headers.Add("accept", "text/plain");

return request;
}

#region private ================================================================================

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureIndexRequest" /> class.
/// </summary>
/// <param name="indexName"> Name of the index.</param>
private ConfigureIndexRequest(string indexName)
{
this.IndexName = indexName;
}

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net.Http;

namespace Microsoft.SemanticKernel.Connectors.Memory.Pinecone.Http.ApiSchema;

/// <summary>
/// Deletes an index and all its data.
/// See https://docs.pinecone.io/reference/delete_index
/// </summary>
internal sealed class DeleteIndexRequest
{
public static DeleteIndexRequest Create(string indexName)
{
return new DeleteIndexRequest(indexName);
}

public HttpRequestMessage Build()
{
HttpRequestMessage request = HttpRequest.CreateDeleteRequest(
$"/databases/{this._indexName}");

request.Headers.Add("accept", "text/plain");

return request;
}

#region private ================================================================================

private readonly string _indexName;

private DeleteIndexRequest(string indexName)
{
this._indexName = indexName;
}

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;

namespace Microsoft.SemanticKernel.Connectors.Memory.Pinecone.Http.ApiSchema;

/// <summary>
/// DeleteRequest
/// See https://docs.pinecone.io/reference/delete_post
/// </summary>
internal sealed class DeleteRequest
{
/// <summary>
/// The ids of the vectors to delete
/// </summary>
[JsonPropertyName("ids")]
public IEnumerable<string>? Ids { get; set; }

/// <summary>
/// Whether to delete all vectors
/// </summary>
[JsonPropertyName("deleteAll")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public bool? DeleteAll { get; set; }

/// <summary>
/// The namespace to delete vectors from
/// </summary>
[JsonPropertyName("namespace")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Namespace { get; set; }

/// <summary>
/// If this parameter is present, the operation only affects vectors that satisfy the filter. See https://www.pinecone.io/docs/metadata-filtering/.
/// </summary>
[JsonPropertyName("filter")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, object>? Filter { get; set; }

public static DeleteRequest GetDeleteAllVectorsRequest()
{
return new DeleteRequest(true);
}

public static DeleteRequest ClearNamespace(string indexNamespace)
{
return new DeleteRequest(true)
{
Namespace = indexNamespace
};
}

public static DeleteRequest DeleteVectors(IEnumerable<string>? ids)
{
return new DeleteRequest(ids);
}

public DeleteRequest FilterBy(Dictionary<string, object>? filter)
{
this.Filter = filter;
return this;
}

public DeleteRequest FromNamespace(string? indexNamespace)
{
this.Namespace = indexNamespace;
return this;
}

public DeleteRequest Clear(bool deleteAll)
{
this.DeleteAll = deleteAll;
return this;
}

public HttpRequestMessage Build()
{
if (this.Filter != null)
{
this.Filter = PineconeUtils.ConvertFilterToPineconeFilter(this.Filter);
}

HttpRequestMessage? request = HttpRequest.CreatePostRequest(
"/vectors/delete",
this);

request.Headers.Add("accept", "application/json");

return request;
}

/// <inheritdoc />
public override string ToString()
{
var sb = new StringBuilder();

sb.Append("DeleteRequest: ");

if (this.Ids != null)
{
sb.Append($"Deleting {this.Ids.Count()} vectors, {string.Join(", ", this.Ids)},");
}

if (this.DeleteAll != null)
{
sb.Append("Deleting All vectors,");
}

if (this.Namespace != null)
{
sb.Append($"From Namespace: {this.Namespace}, ");
}

if (this.Filter == null)
{
return sb.ToString();
}

sb.Append("With Filter: ");

foreach (var pair in this.Filter)
{
sb.Append($"{pair.Key}={pair.Value}, ");
}

return sb.ToString();
}

#region private ================================================================================

private DeleteRequest(IEnumerable<string>? ids)
{
this.Ids = ids ?? new List<string>();
}

private DeleteRequest(bool clear)
{
this.Ids = new List<string>();
this.DeleteAll = clear;
}

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net.Http;

namespace Microsoft.SemanticKernel.Connectors.Memory.Pinecone.Http.ApiSchema;

/// <summary>
/// Get information about an index.
/// See https://docs.pinecone.io/reference/describe_index
/// </summary>
internal sealed class DescribeIndexRequest
{
/// <summary>
/// The unique name of an index.
/// </summary>
public string IndexName { get; }

public static DescribeIndexRequest Create(string indexName)
{
return new DescribeIndexRequest(indexName);
}

public HttpRequestMessage Build()
{
HttpRequestMessage? request = HttpRequest.CreateGetRequest(
$"/databases/{this.IndexName}");

request.Headers.Add("accept", "application/json");

return request;
}

#region private ================================================================================

private DescribeIndexRequest(string indexName)
{
this.IndexName = indexName;
}

#endregion
}
Loading

0 comments on commit ca8d614

Please sign in to comment.