embed abstract method
Generates embeddings for the given texts.
Embeddings are vector representations of text that can be used for semantic search, similarity comparison, and other machine learning tasks.
Parameters:
model- The embedding model to use (e.g., 'text-embedding-3-small', 'nomic-embed-text'). Must be a model that supports embeddings.messages- The texts to embed. Each string will be converted to an embedding vector. Must not be empty.options- Additional model-specific options. Format depends on the backend:- Ollama: Options are passed directly to the API
- ChatGPT: Currently unused (OpenAI API doesn't support additional options)
- llama.cpp: Currently unused
Returns: A Future<List<LLMEmbedding>> containing one embedding per input message. Each embedding contains:
embedding- The embedding vector as a list of doublesmodel- The model that generated the embeddingpromptEvalCount- Number of tokens in the input text
Example:
final embeddings = await repo.embed(
model: 'text-embedding-3-small',
messages: ['Hello world', 'Goodbye world'],
);
print('Embedding dimension: ${embeddings[0].embedding.length}');
print('First embedding: ${embeddings[0].embedding.take(5).toList()}');
Throws:
- LLMApiException if the API request fails
- UnsupportedError if embeddings are not supported by the backend
Implementation
Future<List<LLMEmbedding>> embed({
required String model,
required List<String> messages,
Map<String, dynamic> options = const {},
});