sendMessage method
Future<(GroqResponse, GroqUsage)>
sendMessage(
- String prompt, {
- GroqMessageRole role = GroqMessageRole.user,
- String? username,
Sends a new request message to the chat
prompt
the message content
role
the message role DO NOT USE assistant, it is reserved for the AI
username
the username of the message sender (optional)
Returns a tuple of the response and the resource usage
Example:
final (response, usage) = await chat.sendMessage('Explain the concept of a chatbot');
print(response.choices.first.message); //prints the response message
print(usage.totalTokens); //prints the total tokens used in the response
Implementation
Future<(GroqResponse, GroqUsage)> sendMessage(
String prompt, {
GroqMessageRole role = GroqMessageRole.user,
String? username,
}) async {
final request =
GroqMessage(content: prompt, role: role, username: username);
_streamController.add(RequestChatEvent(request));
final item = GroqConversationItem(_model, request);
GroqResponse response;
GroqUsage usage;
GroqRateLimitInformation rateLimitInfo;
try {
(response, usage, rateLimitInfo) = await GroqApi.getNewChatCompletion(
apiKey: _apiKey,
prompt: request,
chat: this,
);
} catch (e) {
_streamController.addError(e);
rethrow;
}
_rateLimitInfo = rateLimitInfo;
item.setResponse(response, usage);
_conversationItems.add(item);
_streamController.add(ResponseChatEvent(response, usage));
return (response, usage);
}