Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DashVector For Semantic Kernel Memory Store #30

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions DashScope.net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
.github\workflows\nuget-build.yml = .github\workflows\nuget-build.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashScope.UnitTests", "tests\DashScope.UnitTests\DashScope.UnitTests.csproj", "{837CD31F-A2EA-4379-B325-3A20285F7577}"
Expand All @@ -38,6 +39,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashVector.UnitTests", "tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DashVector.Sample", "samples\DashVector.Sample\DashVector.Sample.csproj", "{0B09CBA0-491C-49B5-A498-940AEB7210DF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DashVector.SemanticKernel", "src\DashVector.SemanticKernel\DashVector.SemanticKernel.csproj", "{648ABFAD-C62C-4D58-B968-91F6BEF0831D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +87,10 @@ Global
{0B09CBA0-491C-49B5-A498-940AEB7210DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B09CBA0-491C-49B5-A498-940AEB7210DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B09CBA0-491C-49B5-A498-940AEB7210DF}.Release|Any CPU.Build.0 = Release|Any CPU
{648ABFAD-C62C-4D58-B968-91F6BEF0831D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{648ABFAD-C62C-4D58-B968-91F6BEF0831D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{648ABFAD-C62C-4D58-B968-91F6BEF0831D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{648ABFAD-C62C-4D58-B968-91F6BEF0831D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -99,6 +106,7 @@ Global
{580E981D-49BB-4C34-B4BA-2A1C85B6016D} = {BB7B834A-464F-4F81-A649-E63AA0C53C24}
{FFF43DC0-8591-4817-ABEF-C11598BE6D24} = {ED489DAA-351A-43F9-A3DF-7DBEBF7677CD}
{0B09CBA0-491C-49B5-A498-940AEB7210DF} = {2FDFB5A9-FE06-4822-933E-5D1C6D9160C9}
{648ABFAD-C62C-4D58-B968-91F6BEF0831D} = {BB7B834A-464F-4F81-A649-E63AA0C53C24}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4DB8E739-7470-4879-9DCE-E9BACBA58715}
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="Microsoft.KernelMemory.Core" Version="0.28.240212.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.4.0" />
<PackageVersion Include="Microsoft.SemanticKernel.Abstractions" Version="1.4.0" />
<PackageVersion Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.1" />
Expand Down
50 changes: 49 additions & 1 deletion samples/SK-DashScope.Sample/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using DashScope.SemanticKernel;
using Microsoft.SemanticKernel.TextGeneration;
using Microsoft.SemanticKernel.Memory;

namespace SK_DashScope.Sample.Controllers
{
Expand All @@ -14,10 +15,13 @@ namespace SK_DashScope.Sample.Controllers
public class ApiController : ControllerBase
{
private readonly Kernel kernel;
private readonly ISemanticTextMemory memory;
const string CollectionName = "DashScopeMemoryStore";

public ApiController(Kernel kernel)
public ApiController(Kernel kernel, ISemanticTextMemory memory)
{
this.kernel = kernel;
this.memory = memory;
}

[HttpPost]
Expand Down Expand Up @@ -151,5 +155,49 @@ public async Task<IActionResult> SemanticAsync([FromBody] UserInput input, Cance
return Ok(new { value, usage });

}

[HttpPost("save_memory")]
public async Task<string> SaveMemory([FromBody] UserInput input, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(input.Text))
{
return string.Empty;
}
var id = Guid.NewGuid().ToString("N");

return await memory.SaveInformationAsync(CollectionName, input.Text, id, cancellationToken: cancellationToken);
}

[HttpPost("get_memory")]
public async Task<MemoryQueryResult?> GetMemory([FromBody] UserInput input, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(input.Text))
{
return null;
}

var id = input.Text;
return await memory.GetAsync(CollectionName, id, true, cancellationToken: cancellationToken);
}

[HttpPost("query_memory")]
public async Task<IEnumerable<MemoryQueryResult>> QueryMemory([FromBody] UserInput input, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(input.Text))
{
return Array.Empty<MemoryQueryResult>();
}

var query = input.Text;
var records = memory.SearchAsync(CollectionName, query, 10, 0, true, cancellationToken: cancellationToken);

var results = new List<MemoryQueryResult>();
await foreach (var record in records)
{
results.Add(record);
}

return results;
}
}
}
11 changes: 11 additions & 0 deletions samples/SK-DashScope.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using DashVector;
using DashVector.SemanticKernel;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;

Expand All @@ -23,6 +25,15 @@
{
return new MemoryBuilder()
.WithDashScopeTextEmbeddingGenerationService(builder.Configuration["DashScope:ApiKey"]!)
.WithMemoryStore(factory =>
{
var dashVectorClient = new DashVectorClient(builder.Configuration["DashVector:ApiKey"]!,
builder.Configuration["DashVector:Endpoint"]!);
return new DashVectorMemoryStore(dashVectorClient, new DashVectorCollectionOptions()
{
Dimension = 1536
});
})
.Build();
});

Expand Down
1 change: 1 addition & 0 deletions samples/SK-DashScope.Sample/SK-DashScope.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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

</Project>
16 changes: 16 additions & 0 deletions src/DashVector.SemanticKernel/DashVector.SemanticKernel.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" />
</ItemGroup>

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

</Project>
Loading
Loading