getVertexAIEmbedding function
Implementation
Future<List<double>> getVertexAIEmbedding(String text) async {
if (_cactusToken == null) {
throw Exception('CactusToken not set. Please call CactusLM.init with cactusToken parameter.');
}
const String projectId = 'cactus-v1-452518';
const String location = 'us-central1';
const String modelId = 'text-embedding-005';
final String endpoint =
'https://$location-aiplatform.googleapis.com/v1/projects/$projectId/locations/$location/publishers/google/models/$modelId:predict';
final Map<String, String> headers = {
'Authorization': 'Bearer $_cactusToken',
'Content-Type': 'application/json',
};
final Map<String, dynamic> requestBody = {
'instances': [
{
'content': text,
'task_type': 'RETRIEVAL_DOCUMENT'
}
]
};
try {
final response = await http.post(
Uri.parse(endpoint),
headers: headers,
body: json.encode(requestBody),
);
if (response.statusCode == 200) {
final Map<String, dynamic> responseBody = json.decode(response.body);
if (responseBody.containsKey('error')) {
throw Exception('API Error: ${responseBody['error']['message']}');
}
final predictions = responseBody['predictions'] as List?;
if (predictions == null || predictions.isEmpty) {
throw Exception('No predictions in response');
}
final embeddings = predictions[0]['embeddings'];
final values = embeddings['values'] as List;
return values.map<double>((v) => v.toDouble()).toList();
} else {
throw Exception(
'Failed to call Vertex AI. Status code: ${response.statusCode}\nBody: ${response.body}');
}
} catch (e) {
throw Exception('An error occurred: $e');
}
}