getNewChatCompletion static method
Future<(GroqResponse, GroqUsage, GroqRateLimitInformation)>
getNewChatCompletion({
- required String apiKey,
- required GroqMessage prompt,
- required GroqChat chat,
- required bool expectJSON,
Returns a new chat instance with the given model id
Implementation
static Future<(GroqResponse, GroqUsage, GroqRateLimitInformation)>
getNewChatCompletion({
required String apiKey,
required GroqMessage prompt,
required GroqChat chat,
required bool expectJSON,
}) async {
final Map<String, dynamic> jsonMap = {};
List<Map<String, dynamic>> messages = [];
List<ChatEvent> allMessages = chat.messages;
if (allMessages.length > chat.settings.maxConversationalMemoryLength) {
allMessages.removeRange(
0, allMessages.length - chat.settings.maxConversationalMemoryLength);
}
for (final message in allMessages) {
message.when(
request: (req) => messages.add(req.message.toJson()),
response: (res) =>
messages.add(res.response.choices.first.messageData.toJson()));
// messages.add(message.request.toJson());
// messages.add(message.response!.choices.first.messageData.toJson());
}
messages.add(prompt.toJson());
jsonMap['messages'] = messages;
jsonMap['model'] = chat.model;
if (chat.registeredTools.isNotEmpty) {
jsonMap['tools'] =
chat.registeredTools.map((tool) => tool.toJson()).toList();
}
if (expectJSON) {
jsonMap['response_format'] = {"type": "json_object"};
}
jsonMap.addAll(chat.settings.toJson());
final response = await AuthHttp.post(
url: _chatCompletionUrl, apiKey: apiKey, body: jsonMap);
//Rate Limit information
final rateLimitInfo =
GroqParser.rateLimitInformationFromHeaders(response.headers);
if (response.statusCode < 300) {
final Map<String, dynamic> jsonData =
json.decode(utf8.decode(response.bodyBytes, allowMalformed: true));
final GroqResponse groqResponse =
GroqParser.groqResponseFromJson(jsonData);
final GroqUsage groqUsage =
GroqParser.groqUsageFromJson(jsonData["usage"]);
return (groqResponse, groqUsage, rateLimitInfo);
} else if (response.statusCode == 429) {
throw GroqRateLimitException(
retryAfter: Duration(
seconds: int.tryParse(response.headers['retry-after'] ?? '0') ?? 0,
),
);
} else {
throw GroqException.fromResponse(response);
}
}