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.

BuilderExtensions.cs 5.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.KernelMemory;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using LLama;
  8. using LLama.Common;
  9. using Microsoft.KernelMemory.AI;
  10. using Microsoft.SemanticKernel.AI.Embeddings;
  11. using LLama.Native;
  12. namespace LLamaSharp.KernelMemory
  13. {
  14. /// <summary>
  15. /// Provides extension methods for the KernelMemoryBuilder class.
  16. /// </summary>
  17. public static class BuilderExtensions
  18. {
  19. /// <summary>
  20. /// Adds LLamaSharpTextEmbeddingGeneration to the KernelMemoryBuilder.
  21. /// </summary>
  22. /// <param name="builder">The KernelMemoryBuilder instance.</param>
  23. /// <param name="config">The LLamaSharpConfig instance.</param>
  24. /// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration added.</returns>
  25. public static IKernelMemoryBuilder WithLLamaSharpTextEmbeddingGeneration(this IKernelMemoryBuilder builder, LLamaSharpConfig config)
  26. {
  27. var generator = new LLamaSharpTextEmbeddingGenerator(config);
  28. builder.AddSingleton<ITextEmbeddingGenerator>(generator);
  29. builder.AddIngestionEmbeddingGenerator(generator);
  30. return builder;
  31. }
  32. /// <summary>
  33. /// Adds LLamaSharpTextEmbeddingGeneration to the KernelMemoryBuilder.
  34. /// </summary>
  35. /// <param name="builder">The KernelMemoryBuilder instance.</param>
  36. /// <param name="textEmbeddingGenerator">The LLamaSharpTextEmbeddingGeneration instance.</param>
  37. /// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration added.</returns>
  38. public static IKernelMemoryBuilder WithLLamaSharpTextEmbeddingGeneration(this IKernelMemoryBuilder builder, LLamaSharpTextEmbeddingGenerator textEmbeddingGenerator)
  39. {
  40. builder.AddSingleton<ITextEmbeddingGenerator>(textEmbeddingGenerator);
  41. builder.AddIngestionEmbeddingGenerator(textEmbeddingGenerator);
  42. return builder;
  43. }
  44. /// <summary>
  45. /// Adds LLamaSharpTextGeneration to the KernelMemoryBuilder.
  46. /// </summary>
  47. /// <param name="builder">The KernelMemoryBuilder instance.</param>
  48. /// <param name="config">The LLamaSharpConfig instance.</param>
  49. /// <returns>The KernelMemoryBuilder instance with LLamaSharpTextGeneration added.</returns>
  50. public static IKernelMemoryBuilder WithLLamaSharpTextGeneration(this IKernelMemoryBuilder builder, LLamaSharpConfig config)
  51. {
  52. builder.AddSingleton<ITextGenerator>(new LlamaSharpTextGenerator(config));
  53. return builder;
  54. }
  55. /// <summary>
  56. /// Adds LLamaSharpTextGeneration to the KernelMemoryBuilder.
  57. /// </summary>
  58. /// <param name="builder">The KernelMemoryBuilder instance.</param>
  59. /// <param name="textGenerator">The LlamaSharpTextGeneration instance.</param>
  60. /// <returns>The KernelMemoryBuilder instance with LLamaSharpTextGeneration added.</returns>
  61. public static IKernelMemoryBuilder WithLLamaSharpTextGeneration(this IKernelMemoryBuilder builder, LlamaSharpTextGenerator textGenerator)
  62. {
  63. builder.AddSingleton<ITextGenerator>(textGenerator);
  64. return builder;
  65. }
  66. /// <summary>
  67. /// Adds LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration to the KernelMemoryBuilder.
  68. /// </summary>
  69. /// <param name="builder">The KernelMemoryBuilder instance.</param>
  70. /// <param name="config">The LLamaSharpConfig instance.</param>
  71. /// <param name="weights"></param>
  72. /// <param name="context"></param>
  73. /// <returns>The KernelMemoryBuilder instance with LLamaSharpTextEmbeddingGeneration and LLamaSharpTextGeneration added.</returns>
  74. public static IKernelMemoryBuilder WithLLamaSharpDefaults(this IKernelMemoryBuilder builder, LLamaSharpConfig config, LLamaWeights? weights=null, LLamaContext? context=null)
  75. {
  76. var parameters = new ModelParams(config.ModelPath)
  77. {
  78. ContextSize = config?.ContextSize ?? 2048,
  79. Seed = config?.Seed ?? 0,
  80. GpuLayerCount = config?.GpuLayerCount ?? 20,
  81. Embeddings = true,
  82. MainGpu = config?.MainGpu ?? 0,
  83. SplitMode = config?.SplitMode ?? GPUSplitMode.None,
  84. };
  85. if (weights == null)
  86. {
  87. weights = LLamaWeights.LoadFromFile(parameters);
  88. context = weights.CreateContext(parameters);
  89. }
  90. var executor = new StatelessExecutor(weights, parameters);
  91. var embedder = new LLamaEmbedder(weights, parameters);
  92. builder.WithLLamaSharpTextEmbeddingGeneration(new LLamaSharpTextEmbeddingGenerator(embedder));
  93. builder.WithLLamaSharpTextGeneration(new LlamaSharpTextGenerator(weights, context, executor, config?.DefaultInferenceParams));
  94. return builder;
  95. }
  96. }
  97. }