博文 18034059 完整示例代码

using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;

var apiKey = "xxxx-xxxxxx";

#pragma warning disable SKEXP0011 
#pragma warning disable SKEXP0003 
#pragma warning disable SKEXP0052 
ISemanticTextMemory memory = new MemoryBuilder()
    .WithOpenAITextEmbeddingGeneration("text-embedding-ada-002", apiKey)
    .WithMemoryStore(new VolatileMemoryStore())
    .Build();
#pragma warning restore SKEXP0052 
#pragma warning restore SKEXP0003 
#pragma warning restore SKEXP0011 

var sampleData = new Dictionary<string, string>
{
    ["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
        = "README: Installation, getting started, and how to contribute",
    ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/02-running-prompts-from-file.ipynb"]
        = "Jupyter notebook describing how to pass prompts from a file to a semantic plugin or function"
};

var i = 0;
foreach (var entry in sampleData)
{
    await memory.SaveReferenceAsync(
        collection: "SKGitHub",
        externalSourceName: "GitHub",
        externalId: entry.Key,
        description: entry.Value,
        text: entry.Value);

    Console.WriteLine($"  #{++i} saved.");
}

var query = "How do I get started?";
var memoryResults = memory.SearchAsync("SKGitHub", query, limit: 1, minRelevanceScore: 0.5);

await foreach (var memoryResult in memoryResults)
{
    Console.WriteLine($"Result:");
    Console.WriteLine("  URL:     : " + memoryResult.Metadata.Id);
    Console.WriteLine("  Title    : " + memoryResult.Metadata.Description);
    Console.WriteLine("  Relevance: " + memoryResult.Relevance);
}

https://www.cnblogs.com/dudu/p/18034059

posted @ 2024-02-27 16:44  dudu  阅读(26)  评论(0编辑  收藏  举报