sendMessage method
Future<(GroqResponse, GroqUsage)>
sendMessage(
- String prompt, {
- GroqMessageRole role = GroqMessageRole.user,
- String? username,
- bool expectJSON = false,
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)
expectJSON
whether to expect a JSON response or not. You need to explain the JSON structure
in the prompt for this feature
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,
bool expectJSON = false,
}) async {
final request =
GroqMessage(content: prompt, role: role, username: username);
_streamController.add(RequestChatEvent(request));
_chatItems.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,
expectJSON: expectJSON,
);
} catch (e) {
_streamController.addError(e);
rethrow;
}
_rateLimitInfo = rateLimitInfo;
item.setResponse(response, usage);
_chatItems.add(ResponseChatEvent(response, usage));
_conversationItems.add(item);
_streamController.add(ResponseChatEvent(response, usage));
return (response, usage);
}