Browse Source

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 6c9ff3158c 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
tags/0.11.0
Scott W Harden GitHub 2 years ago
parent
commit
efa49cc8de
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions
  1. +19
    -2
      LLama.Examples/Examples/GetEmbeddings.cs
  2. +2
    -1
      docs/Examples/GetEmbeddings.md

+ 19
- 2
LLama.Examples/Examples/GetEmbeddings.cs View File

@@ -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();
}
}


+ 2
- 1
docs/Examples/GetEmbeddings.md View File

@@ -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)
{


Loading…
Cancel
Save