sendMessage method
Sends message
to the model as a continuation of the chat history.
Prepends the history to the request and uses the provided model to generate new content.
When there are no candidates in the response, the message
and response
are ignored and will not be recorded in the history.
Waits for any ongoing or pending requests to sendMessage or sendMessageStream to complete before generating new content. Successful messages and responses for ongoing or pending requests will be reflected in the history sent for this message.
Implementation
Future<GenerateContentResponse> sendMessage(Content message) async {
final lock = await _mutex.acquire();
try {
final response = await _generateContent(_history.followedBy([message]),
safetySettings: _safetySettings, generationConfig: _generationConfig);
if (response.candidates case [final candidate, ...]) {
_history.add(message);
final normalizedContent = candidate.content.role == null
? Content('model', candidate.content.parts)
: candidate.content;
_history.add(normalizedContent);
}
return response;
} finally {
lock.release();
}
}