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.

GetEmbeddings.cs 1.9 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
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using LLama.Common;
  2. namespace LLama.Examples.Examples
  3. {
  4. public class GetEmbeddings
  5. {
  6. public static void Run()
  7. {
  8. string modelPath = UserSettings.GetModelPath();
  9. Console.ForegroundColor = ConsoleColor.DarkGray;
  10. var @params = new ModelParams(modelPath) { Embeddings = true };
  11. using var weights = LLamaWeights.LoadFromFile(@params);
  12. var embedder = new LLamaEmbedder(weights, @params);
  13. Console.ForegroundColor = ConsoleColor.Yellow;
  14. Console.WriteLine(
  15. """
  16. This example displays embeddings from a text prompt.
  17. Embeddings are numerical codes that represent information like words, images, or concepts.
  18. These codes capture important relationships between those objects,
  19. like how similar words are in meaning or how close images are visually.
  20. This allows machine learning models to efficiently understand and process complex data.
  21. Embeddings of a text in LLM is sometimes useful, for example, to train other MLP models.
  22. """); // NOTE: this description was AI generated
  23. while (true)
  24. {
  25. Console.ForegroundColor = ConsoleColor.White;
  26. Console.Write("Please input your text: ");
  27. Console.ForegroundColor = ConsoleColor.Green;
  28. var text = Console.ReadLine();
  29. Console.ForegroundColor = ConsoleColor.White;
  30. float[] embeddings = embedder.GetEmbeddings(text).Result;
  31. Console.WriteLine($"Embeddings contain {embeddings.Length:N0} floating point values:");
  32. Console.ForegroundColor = ConsoleColor.DarkGray;
  33. Console.WriteLine(string.Join(", ", embeddings.Take(20)) + ", ...");
  34. Console.WriteLine();
  35. }
  36. }
  37. }
  38. }