From efa49cc8de28002d345776ef7f79702526af7d9e Mon Sep 17 00:00:00 2001 From: Scott W Harden Date: Tue, 20 Feb 2024 10:34:20 -0500 Subject: [PATCH] Improve "embeddings" example (#525) * Embeddings example: set EmbeddingMode true prevents an exception from being thrown when GetEmbeddings() is called * Embeddings example: improve documentation and styling * docs: improve GetEmbeddings page If EmbeddingMode is not set to true, GetEmbeddings() throws an exception * docs: improve GetEmbeddings page The previous commit 6c9ff3158c108f4a5decdaaa6c595dda675ee3e4 was inaccurate * Embeddings example: improve styling displays the example description after the model is loaded to ensure the text is on the screen at the time the prompt is first requested --- LLama.Examples/Examples/GetEmbeddings.cs | 21 +++++++++++++++++++-- docs/Examples/GetEmbeddings.md | 3 ++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/LLama.Examples/Examples/GetEmbeddings.cs b/LLama.Examples/Examples/GetEmbeddings.cs index 35de82eb..9eab7c07 100644 --- a/LLama.Examples/Examples/GetEmbeddings.cs +++ b/LLama.Examples/Examples/GetEmbeddings.cs @@ -6,21 +6,38 @@ namespace LLama.Examples.Examples { public static void Run() { + Console.ForegroundColor = ConsoleColor.White; Console.Write("Please input your model path: "); var modelPath = Console.ReadLine(); - var @params = new ModelParams(modelPath); + Console.ForegroundColor = ConsoleColor.DarkGray; + var @params = new ModelParams(modelPath) { EmbeddingMode = true }; using var weights = LLamaWeights.LoadFromFile(@params); var embedder = new LLamaEmbedder(weights, @params); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine( + """ + This example displays embeddings from a text prompt. + Embeddings are numerical codes that represent information like words, images, or concepts. + These codes capture important relationships between those objects, + like how similar words are in meaning or how close images are visually. + This allows machine learning models to efficiently understand and process complex data. + Embeddings of a text in LLM is sometimes useful, for example, to train other MLP models. + """); // NOTE: this description was AI generated + while (true) { + Console.ForegroundColor = ConsoleColor.White; Console.Write("Please input your text: "); Console.ForegroundColor = ConsoleColor.Green; var text = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.White; - Console.WriteLine(string.Join(", ", embedder.GetEmbeddings(text))); + float[] embeddings = embedder.GetEmbeddings(text).Result; + Console.WriteLine($"Embeddings contain {embeddings.Length:N0} floating point values:"); + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.WriteLine(string.Join(", ", embeddings.Take(20)) + ", ..."); Console.WriteLine(); } } diff --git a/docs/Examples/GetEmbeddings.md b/docs/Examples/GetEmbeddings.md index e827271f..56c0b995 100644 --- a/docs/Examples/GetEmbeddings.md +++ b/docs/Examples/GetEmbeddings.md @@ -14,7 +14,8 @@ public class GetEmbeddings { Console.Write("Please input your model path: "); string modelPath = Console.ReadLine(); - var embedder = new LLamaEmbedder(new ModelParams(modelPath)); + var modelParams = new ModelParams(modelPath) { EmbeddingMode = true }; + var embedder = new LLamaEmbedder(modelParams); while (true) {