Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cnblogs/semantic-kernel-dashscope
Semantic Kernel Connector to DashScope
https://github.com/cnblogs/semantic-kernel-dashscope
dashscope qwen semantic-kernel
Last synced: 2 months ago
JSON representation
Semantic Kernel Connector to DashScope
- Host: GitHub
- URL: https://github.com/cnblogs/semantic-kernel-dashscope
- Owner: cnblogs
- License: mit
- Created: 2024-02-11T09:35:27.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-03-23T14:25:59.000Z (10 months ago)
- Last Synced: 2024-04-28T02:04:57.690Z (9 months ago)
- Topics: dashscope, qwen, semantic-kernel
- Language: C#
- Homepage:
- Size: 1.16 MB
- Stars: 36
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-semantickernel - SemanticKernel.DashScope
README
# SemanticKernel.DashScope
Make DashScope work with Semantic Kernel and Kernel Memory.
## Get started with SemanticKernel
Add the NuGet package to your project.
```shell
dotnet add package Cnblogs.SemanticKernel.Connectors.DashScope
``````cs
using Microsoft.SemanticKernel;var builder = Kernel.CreateBuilder();
builder.Services.AddDashScopeChatCompletion("your-api-key", "qwen-max");
var kernel = builder.Build();var prompt = "Tell me about the Cnblogs";
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`
```json
{
"dashScope": {
"apiKey": "your-key",
"chatCompletionModelId": "qwen-max",
"textEmbeddingModelId": "text-embedding-v3"
}
}
````Program.cs`
```csharp
// 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(), waitForIngestionToComplete: true),
"memory");
return new Kernel(sp, plugins);
});
```Services
```csharp
public class YourService(Kernel kernel, IKernelMemory memory)
{
public async Task 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 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();
}
}
```