embed method
Generate embeddings for the given input texts
input - List of strings to generate embeddings for
cancelToken - Optional token to cancel the request
Returns a list of embedding vectors or throws an LLMError
Implementation
@override
Future<List<List<double>>> embed(
List<String> input, {
CancelToken? cancelToken,
}) async {
if (config.apiKey.isEmpty) {
throw const AuthError('Missing Google API key');
}
try {
// For single input or small batches, use single endpoint
if (input.length == 1) {
final requestBody = _buildSingleEmbeddingRequest(input.first);
final responseData = await client.postJson(
embeddingEndpoint,
requestBody,
cancelToken: cancelToken,
);
return [_parseSingleEmbeddingResponse(responseData)];
} else {
// For multiple inputs, use batch endpoint
final requestBody = _buildBatchEmbeddingRequest(input);
final responseData = await client.postJson(
batchEmbeddingEndpoint,
requestBody,
cancelToken: cancelToken,
);
return _parseBatchEmbeddingResponse(responseData);
}
} on DioException catch (e) {
throw DioErrorHandler.handleDioError(e, 'Google');
} catch (e) {
throw GenericError('Unexpected error: $e');
}
}