embed method
Generate embeddings for the given input texts
input
- List of strings to generate embeddings for
Returns a list of embedding vectors or throws an LLMError
Implementation
@override
Future<List<List<double>>> embed(List<String> input) async {
final requestBody = {
'model': config.model,
'input': input,
'encoding_format': config.embeddingEncodingFormat ?? 'float',
if (config.embeddingDimensions != null)
'dimensions': config.embeddingDimensions,
};
final responseData = await client.postJson('embeddings', requestBody);
final data = responseData['data'] as List?;
if (data == null) {
throw ResponseFormatError(
'Invalid embedding response format: missing data field',
responseData.toString(),
);
}
try {
final embeddings = data.map((item) {
if (item is! Map<String, dynamic>) {
throw ResponseFormatError(
'Invalid embedding item format: expected Map<String, dynamic>',
item.toString(),
);
}
final embedding = item['embedding'];
if (embedding is! List) {
throw ResponseFormatError(
'Invalid embedding format: expected List',
embedding.toString(),
);
}
return embedding.cast<double>();
}).toList();
return embeddings;
} catch (e) {
if (e is LLMError) rethrow;
throw ResponseFormatError(
'Failed to parse embedding response: $e',
responseData.toString(),
);
}
}