Generating embeddings
We can use the OpenAI client to generate embeddings.
First we get a client (see Working with the OpenAI API client):
client := GtOpenAIClient withApiKeyFromFile
We can now use this client to generate an embedding for text.
client generateEmbeddingsWithModel: 'text-embedding-3-small' andInput: 'A text to embed'
We may also generate multiple embeddings at once.
client generateEmbeddingsWithModel: 'text-embedding-3-small' andInput: {'A text to embed'. 'Another text to embed'}
If we want to preserve the input, we can also use the GtLlmEmbedding
utility class for that.
input := 'A text to embed'. embedding := GtLlmEmbedding new input: input; embedding: (client generateEmbeddingsWithModel: 'text-embedding-3-small' andInput: input) items first embedding
We can then for instance apply distance functions on them using GtLlmEmbeddingsUtilities>>#distancesFromEmbeddings:to:
. The default distance metric is cosine
. To explore them, you can look at the distance metrics view on the GtLlmEmbeddingsUtilities
class.

For a local and free option for generating embeddings, see Embeddings in Ollama and Generating embeddings using Ollama models.