Generating embeddings
We can use the OpenAI client to generate embeddings.
First we get a client (see Working directly with the OpenAI API client):
client := GtLOpenAIClient 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 GtLEmbedding
utility class for that.
input := 'A text to embed'. embedding := GtLEmbedding 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 GtLEmbeddingsUtilities>>#distancesFromEmbeddings:to:
. The default distance metric is cosine. To explore them, you can look at the distance metrics view on the GtLEmbeddingsUtilities
class.
For a local and free option for generating embeddings, see Generating embeddings using Ollama models.