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 Object << #GtLEmbedding slots: { #input . #embedding }; package: 'Gt4Llm-VectorDb' 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: distancesFromEmbeddings: listOfEmbeddings to: anEmbedding ^ self distancesFromEmbeddings: listOfEmbeddings to: anEmbedding usingMetric: self defaultMetric . The default distance metric is cosine. To explore them, you can look at the distance metrics view on the GtLEmbeddingsUtilities Object << #GtLEmbeddingsUtilities slots: {}; tag: 'Model'; package: 'Gt4Llm-VectorDb' class.

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