sendMessage method
Send a message to the chat bot
Implementation
@override
Future<MessageChat> sendMessage({required String message}) async {
late MessageChat chatMessage;
try {
final headers = {
HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
HttpHeaders.contentTypeHeader: 'application/json',
};
var url = Uri.https(_params.domain, _params.endPoint);
var body = json.encode({
'model': _params.modelGpt,
'messages': [
{'role': 'user', 'content': message},
],
'max_tokens': _params.maxToken,
});
var response = await _http
.post(url, body: body, headers: headers)
.timeout(const Duration(seconds: 30));
chatMessage = _handleResponse(response);
} on SocketException {
chatMessage = _handleHttpError('No Internet connection');
} on TimeoutException {
chatMessage = _handleHttpError('Connection timed out');
} on http.ClientException catch (e) {
chatMessage = _handleHttpError('Client exception occurred: ${e.message}');
} catch (e) {
chatMessage = _handleHttpError('Unexpected error occurred: $e');
}
return chatMessage;
}