chatWithAgentFuture method
Implementation
Future<Either<String, LlmChatMessage>> chatWithAgentFuture({
required String model,
required List<LlmChatMessage> messages,
String? sessionId,
String? maxTokens,
double? temperature,
}) async {
try {
final body = {
'messages': messages
.map((e) => {'role': e.type, 'content': e.message})
.toList(),
'model': model,
};
if (maxTokens != null) {
body['maxTokens'] = maxTokens;
}
if (sessionId != null) {
body['sessionId'] = sessionId;
}
if (temperature != null) {
body['temperature'] = temperature;
}
final response = await post(
Uri.parse('$baseUrl/v1/chat/completions'),
headers: {
'Content-Type': 'application/json',
'apikey': apiKey,
},
body: json.encode(body),
);
final parsedResponse =
ChatCompletionResponse.fromJson(json.decode(response.body));
final message = LlmChatMessage(
time: DateTime.now().millisecondsSinceEpoch,
message: parsedResponse.choices.first.message.content,
type: parsedResponse.choices.first.message.role);
return Right(message);
} catch (e) {
debugPrint('$tag chatWithAgentFuture Exception: $e');
return Left('Exception occurred: $e');
}
}