您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GetEmbeddings.md 2.0 kB

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