The LlmExportHistory function exports the current conversation
history associated with the client session to a
JSON-formatted file.
The exported history includes both client messages and model responses
stored in the current session history.
This function only exports conversation history currently stored
in memory for the client session. It cannot be used if the
conversation history has been disabled by setting the history
size to zero.
If the lpszFileName parameter specifies a directory or
device name, the function will fail. It is permitted to use
environment variables in the file name by surrounding the variable
name with the percent symbol. If the file name contains any leading or
trailing space characters, they will be removed. If the file already
exists, the current process must have permission to overwrite the
file. If the file has the read-only attribute set, the function will
fail and the last error code will be set to ST_ERROR_ACCESS_DENIED.
If the nMessageCount parameter is non-zero, the function exports
only the most recent messages in the conversation history. Older messages
are skipped. If the specified count exceeds the number of available
messages, all available messages are exported.
The exported file uses UTF-8 encoded JSON text and is intended to be
human-readable. Applications should not depend on the exact formatting
or internal structure of the file because future versions of the library
may include additional metadata or fields. To help prevent data loss,
the function writes the exported history to a temporary file and only
replaces the destination file after the export operation has completed
successfully. If the export fails, any existing file specified by the
lpszFileName parameter is left unchanged.
The exported file is intended to provide a simple mechanism for
persisting conversation state between application sessions. A previously
exported history file can later be restored using the LlmImportHistory
function.
Important: The conversation history may contain sensitive or
private information. Applications should take reasonable precautions to
protect exported history files, such as limiting access permissions or
encrypting the exported file. Applications which allow users to export
conversation history should consider informing users that the exported
file may contain private information.
LONG nExported = 0;
// Export the complete conversation history to a JSON file
if (LlmExportHistory(hClient,
_T("%USERPROFILE%\\Documents\\conversation.json"),
0,
&nExported))
{
_tprintf(_T("Exported %ld messages\n"), nExported);
}
else
{
DWORD dwError = LlmGetLastError();
_tprintf(_T("Unable to export conversation history, error 0x%08lX\n"), dwError);
}