chatWithAgentFuture method

Future<Either<String, LlmChatMessage>> chatWithAgentFuture({
  1. required String model,
  2. required List<LlmChatMessage> messages,
  3. String? sessionId,
  4. String? maxTokens,
  5. double? temperature,
})

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');
  }
}