Package com.tnsai.llm.cache
Interface EmbeddingProvider
- All Known Implementing Classes:
HashEmbeddingProvider,OllamaEmbeddingProvider
public interface EmbeddingProvider
Provides embeddings for text to enable semantic similarity search.
EmbeddingProvider converts text into vector embeddings that can be compared using cosine similarity or other distance metrics.
Implementations
OllamaEmbeddingProvider- Ollama/api/embed; keyless local or cloudHashEmbeddingProvider- Simple hash-based (for testing)
-
Method Summary
Modifier and TypeMethodDescriptionstatic doublecosineSimilarity(float[] a, float[] b) Calculates cosine similarity between two embeddings.float[]Generates an embedding vector for the given text.default float[]embed(String text, EmbeddingTask task) Generates an embedding fortextconditioned ontask.default List<float[]> embedBatch(List<String> texts) Generates embeddings for multiple texts in batch.default List<float[]> embedBatch(List<String> texts, EmbeddingTask task) Batch counterpart ofembed(String, EmbeddingTask).intReturns the dimension of the embedding vectors.Returns the name of the embedding model.
-
Method Details
-
embed
Generates an embedding vector for the given text.- Parameters:
text- The text to embed- Returns:
- A float array representing the embedding vector
-
embedBatch
Generates embeddings for multiple texts in batch.- Parameters:
texts- The texts to embed- Returns:
- List of embedding vectors
-
embed
Generates an embedding fortextconditioned ontask.Instruction-tuned models (e.g.
nomic-embed-text) embed the same text differently per task; supplying the right task is what separates related from unrelated text. The default ignorestask— correct for providers whose model has no task convention (hash-based, OpenAItext-embedding-3).- See Also:
-
embedBatch
Batch counterpart ofembed(String, EmbeddingTask). The default ignorestask; task-aware providers override this. -
getDimension
int getDimension()Returns the dimension of the embedding vectors. -
getModelName
String getModelName()Returns the name of the embedding model. -
cosineSimilarity
static double cosineSimilarity(float[] a, float[] b) Calculates cosine similarity between two embeddings.- Parameters:
a- First embeddingb- Second embedding- Returns:
- Similarity score between -1 and 1 (1 = identical)
-