GroqChat.fromJson constructor
GroqChat.fromJson()
Creates a GroqChat instance from a JSON object.
This factory constructor initializes a GroqChat object by deserializing
the provided JSON data. The function functionNameToFunction is used to
map string identifiers in the JSON to their corresponding function objects.
The parameter given in the functionNameToFunction is the function name, which is used in the GroqToolItem.
Enabling the proper deserialization of registered tools.
Example:
var groqChat = GroqChat.fromJson(json, (name) {
if (name == 'exampleFunction') return exampleFunction;
if (name == 'anotherFunction') return anotherFunction;
throw ArgumentError('Unknown function name: $name');
});
Implementation
GroqChat.fromJson(Map<String, dynamic> json,
Function(Map<String, dynamic>) Function(String) functionNameToFunction) {
_model = json['model'] as String;
_apiKey = json['apiKey'] as String;
_settings = GroqParser.settignsFromJson(json['settings']);
_chatItems = (json['chatItems'] as List<dynamic>)
.map((item) =>
GroqParser.chatEventFromJson(item as Map<String, dynamic>))
.toList();
for (final item in _chatItems) {
if (item is RequestChatEvent) {
_conversationItems.add(GroqConversationItem(_model, item.message));
} else if (item is ResponseChatEvent) {
_conversationItems.last.setResponse(item.response, item.usage);
}
}
if (json['rateLimitInfo'] != null) {
_rateLimitInfo =
GroqParser.rateLimitInformationFromJson(json['rateLimitInfo']);
}
_registeredTools = (json['registeredTools'] as List)
.map((item) =>
GroqParser.groqToolItemFromChatJson(item, functionNameToFunction))
.toList();
}