Using the embedding registry
Embeddings can be saved in a registry to avoid recreating them. The registry also allows us to search for a given number of related embeddings.
First we get a client (see Working with the OpenAI API client):
client := GtOpenAIClient withApiKeyFromFile
We can now try to query the registry.
input := 'A text for an embedding.'. embedding := GtLlmEmbeddingRegistry uniqueInstance at: input
This code will raise an error if the embedding is not found. We can use the client to instead create it if it isn’t found.
embedding := GtLlmEmbeddingRegistry uniqueInstance at: input ifAbsentCreateEmbedding: [ (client generateEmbeddingsWithModel: 'text-embedding-ada-002' andInput: input) items first embedding ]
We can then query the registry for the most related embeddings we can find in the registry. The input itself will not appear in this list.
GtLlmEmbeddingRegistry uniqueInstance findNearest: 3 for: embedding
If we want to reset the registry we can do so.
GtLlmEmbeddingRegistry uniqueInstance reset