Generating embeddings using Ollama models

We can use the Ollama client to generate embeddings.

Using the client initialization code from Working with the Ollama API client:

client := GtOllamaClient new
  

We can now use this client to generate an embedding for text.

client generateEmbeddingsWithModel: 'llama3.1' andInput: 'A text to embed'
  

We may also generate multiple embeddings at once.

client
	generateEmbeddingsWithModel: 'llama3.1'
	andInput: {'A text to embed'. 'Another text to embed'}
  

If we want to preserve the input, we can also use the GtLlmEmbedding Object << #GtLlmEmbedding slots: { #input . #embedding }; package: 'Gt4Llm' utility class for that.

input := 'A text to embed'.

embedding := GtLlmEmbedding new
		input: input;
		embedding: (client generateEmbeddingsWithModel: 'llama3.1' andInput: 'A text to embed')
				items first embedding
  

We can then for instance apply distance functions on them using GtLlmEmbeddingsUtilities>>#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 GtLlmEmbeddingsUtilities Object << #GtLlmEmbeddingsUtilities slots: {}; package: 'Gt4Llm' class.

For a OpenAI-compliant option for generating embeddings, see Embeddings in OpenAI and Generating embeddings. For an in-image embedding registry that can act as an ad-hoc vector database, see Using the embedding registry.