Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions LLama.Examples/Examples/GetEmbeddings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,38 @@ public class GetEmbeddings
{
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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion docs/Examples/GetEmbeddings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down