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.cs 7.3 kB

April 2024 Binary Update (#662) * Updated binaries, using [this build](https://github.com/SciSharp/LLamaSharp/actions/runs/8654672719/job/23733195669) for llama.cpp commit `f7001ccc5aa359fcf41bba19d1c99c3d25c9bcc7`. - Added all new functions. - Moved some functions (e.g. `SafeLlamaModelHandle` specific functions) into `SafeLlamaModelHandle.cs` - Exposed tokens on `SafeLlamaModelHandle` and `LLamaWeights` through a `Tokens` property. As new special tokens are added in the future they can be added here. - Changed all token properties to return nullable tokens, to handle some models not having some tokens. - Fixed `DefaultSamplingPipeline` to handle no newline token in some models. * Moved native methods to more specific locations. - Context specific things have been moved into `SafeLLamaContextHandle.cs` and made private - they're exposed through C# properties and methods already. - Checking that GPU layer count is zero if GPU offload is not supported. - Moved methods for creating default structs (`llama_model_quantize_default_params` and `llama_context_default_params`) into relevant structs. * Removed exception if `GpuLayerCount > 0` when GPU is not supported. * - Added low level wrapper methods for new per-sequence state load/save in `SafeLLamaContextHandle` - Added high level wrapper methods (save/load with `State` object or memory mapped file) in `LLamaContext` - Moved native methods for per-sequence state load/save into `SafeLLamaContextHandle` * Added update and defrag methods for KV cache in `SafeLLamaContextHandle` * Updated submodule to `f7001ccc5aa359fcf41bba19d1c99c3d25c9bcc7` * Passing the sequence ID when saving a single sequence state
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using LLama.Common;
  2. using Microsoft.SemanticKernel.Memory;
  3. using LLamaSharp.SemanticKernel.TextEmbedding;
  4. namespace LLama.Examples.Examples
  5. {
  6. public class SemanticKernelMemory
  7. {
  8. private const string MemoryCollectionName = "SKGitHub";
  9. public static async Task Run()
  10. {
  11. string modelPath = UserSettings.GetModelPath();
  12. Console.WriteLine("This example is from: \n" +
  13. "https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs");
  14. var seed = 1337u;
  15. // Load weights into memory
  16. var parameters = new ModelParams(modelPath)
  17. {
  18. Seed = seed,
  19. Embeddings = true
  20. };
  21. using var model = await LLamaWeights.LoadFromFileAsync(parameters);
  22. var embedding = new LLamaEmbedder(model, parameters);
  23. Console.WriteLine("====================================================");
  24. Console.WriteLine("======== Semantic Memory (volatile, in RAM) ========");
  25. Console.WriteLine("====================================================");
  26. /* You can build your own semantic memory combining an Embedding Generator
  27. * with a Memory storage that supports search by similarity (ie semantic search).
  28. *
  29. * In this example we use a volatile memory, a local simulation of a vector DB.
  30. *
  31. * You can replace VolatileMemoryStore with Qdrant (see QdrantMemoryStore connector)
  32. * or implement your connectors for Pinecone, Vespa, Postgres + pgvector, SQLite VSS, etc.
  33. */
  34. var memory = new MemoryBuilder()
  35. .WithTextEmbeddingGeneration(new LLamaSharpEmbeddingGeneration(embedding))
  36. .WithMemoryStore(new VolatileMemoryStore())
  37. .Build();
  38. await RunExampleAsync(memory);
  39. }
  40. private static async Task RunExampleAsync(ISemanticTextMemory memory)
  41. {
  42. await StoreMemoryAsync(memory);
  43. await SearchMemoryAsync(memory, "How do I get started?");
  44. /*
  45. Output:
  46. Query: How do I get started?
  47. Result 1:
  48. URL: : https://github.com/microsoft/semantic-kernel/blob/main/README.md
  49. Title : README: Installation, getting started, and how to contribute
  50. Result 2:
  51. URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb
  52. Title : Jupyter notebook describing how to get started with the Semantic Kernel
  53. */
  54. await SearchMemoryAsync(memory, "Can I build a chat with SK?");
  55. /*
  56. Output:
  57. Query: Can I build a chat with SK?
  58. Result 1:
  59. URL: : https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT
  60. Title : Sample demonstrating how to create a chat skill interfacing with ChatGPT
  61. Result 2:
  62. URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md
  63. Title : README: README associated with a sample chat summary react-based webapp
  64. */
  65. await SearchMemoryAsync(memory, "Jupyter notebook");
  66. await SearchMemoryAsync(memory, "README: README associated with a sample chat summary react-based webapp");
  67. await SearchMemoryAsync(memory, "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function");
  68. }
  69. private static async Task SearchMemoryAsync(ISemanticTextMemory memory, string query)
  70. {
  71. Console.WriteLine("\nQuery: " + query + "\n");
  72. var memories = memory.SearchAsync(MemoryCollectionName, query, limit: 10, minRelevanceScore: 0.5);
  73. int i = 0;
  74. await foreach (MemoryQueryResult result in memories)
  75. {
  76. Console.WriteLine($"Result {++i}:");
  77. Console.WriteLine(" URL: : " + result.Metadata.Id);
  78. Console.WriteLine(" Title : " + result.Metadata.Description);
  79. Console.WriteLine(" Relevance: " + result.Relevance);
  80. Console.WriteLine();
  81. }
  82. Console.WriteLine("----------------------");
  83. }
  84. private static async Task StoreMemoryAsync(ISemanticTextMemory memory)
  85. {
  86. /* Store some data in the semantic memory.
  87. *
  88. * When using Azure Cognitive Search the data is automatically indexed on write.
  89. *
  90. * When using the combination of VolatileStore and Embedding generation, SK takes
  91. * care of creating and storing the index
  92. */
  93. Console.WriteLine("\nAdding some GitHub file URLs and their descriptions to the semantic memory.");
  94. var githubFiles = SampleData();
  95. var i = 0;
  96. foreach (var entry in githubFiles)
  97. {
  98. var result = await memory.SaveReferenceAsync(
  99. collection: MemoryCollectionName,
  100. externalSourceName: "GitHub",
  101. externalId: entry.Key,
  102. description: entry.Value,
  103. text: entry.Value);
  104. Console.WriteLine($"#{++i} saved.");
  105. Console.WriteLine(result);
  106. }
  107. Console.WriteLine("\n----------------------");
  108. }
  109. private static Dictionary<string, string> SampleData()
  110. {
  111. return new Dictionary<string, string>
  112. {
  113. ["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
  114. = "README: Installation, getting started, and how to contribute",
  115. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/02-running-prompts-from-file.ipynb"]
  116. = "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
  117. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks//00-getting-started.ipynb"]
  118. = "Jupyter notebook describing how to get started with the Semantic Kernel",
  119. ["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
  120. = "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
  121. ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs"]
  122. = "C# class that defines a volatile embedding store",
  123. ["https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md"]
  124. = "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4",
  125. ["https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md"]
  126. = "README: README associated with a sample chat summary react-based webapp",
  127. };
  128. }
  129. }
  130. }