Cnblogs.KernelMemory.AI.DashScope 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Cnblogs.KernelMemory.AI.DashScope --version 0.4.0                
NuGet\Install-Package Cnblogs.KernelMemory.AI.DashScope -Version 0.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cnblogs.KernelMemory.AI.DashScope" Version="0.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cnblogs.KernelMemory.AI.DashScope --version 0.4.0                
#r "nuget: Cnblogs.KernelMemory.AI.DashScope, 0.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cnblogs.KernelMemory.AI.DashScope as a Cake Addin
#addin nuget:?package=Cnblogs.KernelMemory.AI.DashScope&version=0.4.0

// Install Cnblogs.KernelMemory.AI.DashScope as a Cake Tool
#tool nuget:?package=Cnblogs.KernelMemory.AI.DashScope&version=0.4.0                

SemanticKernel.DashScope

Make DashScope work with Semantic Kernel and Kernel Memory.

Get started with SemanticKernel

Add the NuGet package to your project.

dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
using Microsoft.SemanticKernel;

var builder = Kernel.CreateBuilder();
builder.Services.AddDashScopeChatCompletion("your-api-key", "qwen-max");
var kernel = builder.Build();

var prompt = "<message role=\"user\">Tell me about the Cnblogs</message>";
var response = await kernel.InvokePromptAsync(prompt);
Console.WriteLine(response);

ASP.NET Core with KernelMemory support

Install Nuget package Cnblogs.KernelMemory.AI.DashScope

Install Nuget package Microsoft.KernelMemory.Core

Install Nuget package Microsoft.KernelMemory.SemanticKernelPlugin

appsettings.json

{
  "dashScope": {
    "apiKey": "your-key",
    "chatCompletionModelId": "qwen-max",
    "textEmbeddingModelId": "text-embedding-v3"
  }
}

Program.cs

// Kernel Memory stuff
var memory = new KernelMemoryBuilder(builder.Services).WithDashScope(builder.Configuration).Build();
builder.Services.AddSingleton(memory);

// SK stuff
builder.Services.AddDashScopeChatCompletion(builder.Configuration);
builder.Services.AddSingleton(
    sp =>
    {
        var plugins = new KernelPluginCollection();
        plugins.AddFromObject(
            new MemoryPlugin(sp.GetRequiredService<IKernelMemory>(), waitForIngestionToComplete: true),
            "memory");
        return new Kernel(sp, plugins);
    });

Services

public class YourService(Kernel kernel, IKernelMemory memory)
{
    public async Task<string> GetCompletionAsync(string prompt)
    {
        var chatResult = await kernel.InvokePromptAsync(prompt);
        return chatResult.ToString();
    }

    public async Task ImportDocumentAsync(string filePath, string documentId)
    {
        await memory.ImportDocumentAsync(filePath, documentId);
    }

    public async Task<string> AskMemoryAsync(string question)
    {
        // use memory.ask to query kernel memory
        var skPrompt = """
                       Question to Kernel Memory: {{$input}}

                       Kernel Memory Answer: {{memory.ask $input}}

                       If the answer is empty say 'I don't know' otherwise reply with a preview of the answer, truncated to 15 words.
                       """;

        // you can bundle created functions into a singleton service to reuse them
        var myFunction = kernel.CreateFunctionFromPrompt(skPrompt);
        var result = await myFunction.InvokeAsync(question);
        return result.ToString();
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.1 94 10/22/2024
0.4.0 109 10/3/2024
0.3.0 866 8/4/2024
0.2.0 169 7/8/2024
0.1.0 1,766 3/13/2024