You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SemanticKernelMemory.md 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Semantic-kernel - with kernel-memory
  2. Semantic Memory allows to store your data like traditional DBs, adding the ability to query it using natural language.
  3. ```cs
  4. using LLama.Common;
  5. using Microsoft.SemanticKernel.Memory;
  6. using LLamaSharp.SemanticKernel.TextEmbedding;
  7. namespace LLama.Examples.Examples
  8. {
  9. public class SemanticKernelMemory
  10. {
  11. private const string MemoryCollectionName = "SKGitHub";
  12. public static async Task Run()
  13. {
  14. string modelPath = UserSettings.GetModelPath();
  15. Console.WriteLine("This example is from: \n" +
  16. "https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs");
  17. var seed = 1337u;
  18. // Load weights into memory
  19. var parameters = new ModelParams(modelPath)
  20. {
  21. Seed = seed,
  22. EmbeddingMode = true
  23. };
  24. using var model = LLamaWeights.LoadFromFile(parameters);
  25. var embedding = new LLamaEmbedder(model, parameters);
  26. Console.WriteLine("====================================================");
  27. Console.WriteLine("======== Semantic Memory (volatile, in RAM) ========");
  28. Console.WriteLine("====================================================");
  29. /* You can build your own semantic memory combining an Embedding Generator
  30. * with a Memory storage that supports search by similarity (ie semantic search).
  31. *
  32. * In this example we use a volatile memory, a local simulation of a vector DB.
  33. *
  34. * You can replace VolatileMemoryStore with Qdrant (see QdrantMemoryStore connector)
  35. * or implement your connectors for Pinecone, Vespa, Postgres + pgvector, SQLite VSS, etc.
  36. */
  37. var memory = new MemoryBuilder()
  38. .WithTextEmbeddingGeneration(new LLamaSharpEmbeddingGeneration(embedding))
  39. .WithMemoryStore(new VolatileMemoryStore())
  40. .Build();
  41. await RunExampleAsync(memory);
  42. }
  43. private static async Task RunExampleAsync(ISemanticTextMemory memory)
  44. {
  45. await StoreMemoryAsync(memory);
  46. await SearchMemoryAsync(memory, "How do I get started?");
  47. /*
  48. Output:
  49. Query: How do I get started?
  50. Result 1:
  51. URL: : https://github.com/microsoft/semantic-kernel/blob/main/README.md
  52. Title : README: Installation, getting started, and how to contribute
  53. Result 2:
  54. URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb
  55. Title : Jupyter notebook describing how to get started with the Semantic Kernel
  56. */
  57. await SearchMemoryAsync(memory, "Can I build a chat with SK?");
  58. /*
  59. Output:
  60. Query: Can I build a chat with SK?
  61. Result 1:
  62. URL: : https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT
  63. Title : Sample demonstrating how to create a chat skill interfacing with ChatGPT
  64. Result 2:
  65. URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md
  66. Title : README: README associated with a sample chat summary react-based webapp
  67. */
  68. await SearchMemoryAsync(memory, "Jupyter notebook");
  69. await SearchMemoryAsync(memory, "README: README associated with a sample chat summary react-based webapp");
  70. await SearchMemoryAsync(memory, "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function");
  71. }
  72. private static async Task SearchMemoryAsync(ISemanticTextMemory memory, string query)
  73. {
  74. Console.WriteLine("\nQuery: " + query + "\n");
  75. var memories = memory.SearchAsync(MemoryCollectionName, query, limit: 10, minRelevanceScore: 0.5);
  76. int i = 0;
  77. await foreach (MemoryQueryResult result in memories)
  78. {
  79. Console.WriteLine($"Result {++i}:");
  80. Console.WriteLine(" URL: : " + result.Metadata.Id);
  81. Console.WriteLine(" Title : " + result.Metadata.Description);
  82. Console.WriteLine(" Relevance: " + result.Relevance);
  83. Console.WriteLine();
  84. }
  85. Console.WriteLine("----------------------");
  86. }
  87. private static async Task StoreMemoryAsync(ISemanticTextMemory memory)
  88. {
  89. /* Store some data in the semantic memory.
  90. *
  91. * When using Azure Cognitive Search the data is automatically indexed on write.
  92. *
  93. * When using the combination of VolatileStore and Embedding generation, SK takes
  94. * care of creating and storing the index
  95. */
  96. Console.WriteLine("\nAdding some GitHub file URLs and their descriptions to the semantic memory.");
  97. var githubFiles = SampleData();
  98. var i = 0;
  99. foreach (var entry in githubFiles)
  100. {
  101. var result = await memory.SaveReferenceAsync(
  102. collection: MemoryCollectionName,
  103. externalSourceName: "GitHub",
  104. externalId: entry.Key,
  105. description: entry.Value,
  106. text: entry.Value);
  107. Console.WriteLine($"#{++i} saved.");
  108. Console.WriteLine(result);
  109. }
  110. Console.WriteLine("\n----------------------");
  111. }
  112. private static Dictionary<string, string> SampleData()
  113. {
  114. return new Dictionary<string, string>
  115. {
  116. ["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
  117. = "README: Installation, getting started, and how to contribute",
  118. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/02-running-prompts-from-file.ipynb"]
  119. = "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
  120. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks//00-getting-started.ipynb"]
  121. = "Jupyter notebook describing how to get started with the Semantic Kernel",
  122. ["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
  123. = "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
  124. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs"]
  125. = "C# class that defines a volatile embedding store",
  126. ["https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md"]
  127. = "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4",
  128. ["https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md"]
  129. = "README: README associated with a sample chat summary react-based webapp",
  130. };
  131. }
  132. }
  133. }
  134. ```