The LlmSendMessage function provides a simplified interface for
sending a single text prompt to the model and receiving the response
as a string. This function is intended for applications that do not
require structured input or detailed control over message formatting.
This function will not return until the request has completed, an
error has occurred, or the operation has timed out. Applications that
call this function from a user interface thread should provide visual
feedback while waiting for the response, or perform the request on a
background thread to maintain responsiveness.
Applications that accept user input should apply appropriate
validation, filtering, or normalization before sending requests to the
service provider. The LlmValidateText function verifies that
the prompt contains valid message text and the LlmNormalizeText
function can be used to normalize text formatting. These functions
do not perform any semantic analysis of the prompt and do not provide
protection against malicious or intentionally misleading input.
This function does not validate or restrict the content of the input
message. It is the responsibility of the application to ensure that
user-provided input is appropriate for its intended use. This includes
considering scenarios where input may attempt to override instructions,
inject unintended prompts, or otherwise influence the behavior of the
model.
The response is written to the buffer specified by
lpszResponse. If the buffer is not large enough to contain the
complete response, the text will be truncated. The lpdwLength
parameter will always contain the number of characters actually copied
to the buffer.
Line breaks in the response text are normalized before being stored
in the client history. Line feed and carriage return characters are
converted to the standard Windows CRLF end-of-line sequence, ensuring
consistent formatting across different providers and applications.
This also helps ensure compatibility with Windows common controls and
other user interface components.
Some models are capable of performing additional internal reasoning
before generating a response. The amount of reasoning performed and
whether it is exposed by the provider is model-specific. The library
returns the final response intended for the user and does not
currently expose model reasoning or intermediate processing steps.
This behavior ensures consistent results across providers, regardless
of how individual models implement or expose reasoning capabilities.
If the lpdwMessageId parameter is specified, it will receive
a unique identifier for the message within the current client session.
Message identifiers are assigned sequentially, but applications should
treat them as opaque values. They are only guaranteed to be unique
within a single session and should not be compared across different
client sessions.
This function automatically adds the message to the conversation
history for the session. Subsequent calls will include previous messages
as context, subject to any configured history or token limits. To clear
the conversation context, call LlmClearHistory or
LlmResetSession.
Applications that require more advanced features, such as structured
messages, role-based input, or access to detailed response metadata,
should use the LlmSendMessageEx function instead.
#include "cstools12.h"
#include <stdio.h>
int _tmain(void)
{
HCLIENT hClient = INVALID_CLIENT;
DWORD dwError = NO_ERROR;
// Initialize the SocketTools library
if (!LlmInitialize(CSTOOLS12_LICENSE_KEY, NULL))
{
_tprintf(_T("Initialization failed: %lu\n"), LlmGetLastError());
return 1;
}
// Establish a connection to OpenAI using the default endpoint
// and a lightweight model
hClient = LlmConnect(
LLM_PROVIDER_OPENAI,
LLM_OPTION_DEFAULT,
LLM_TIMEOUT,
NULL, // Use default base URL
_T("gpt-5.4-nano"), // Model name
_T("%OPENAI_API_KEY%") // Provider API key
);
if (hClient == INVALID_CLIENT)
{
dwError = LlmGetLastError();
_tprintf(_T("Unable to connect, error %lu\n"), dwError);
LlmUninitialize();
return 1;
}
TCHAR szResponse[4096] = {0};
DWORD dwResponseLength = _countof(szResponse); // number of TCHARs
DWORD dwMessageId = 0;
// Send a simple message to the model
if (LlmSendMessage(hClient,
_T("What is the capital city of Denmark?"),
szResponse,
&dwResponseLength,
&dwMessageId))
{
_tprintf(_T("Response:\n%s\n"), szResponse);
}
else
{
dwError = LlmGetLastError();
_tprintf(_T("Request failed, error %lu\n"), dwError);
}
// Clean up
LlmDisconnect(hClient);
LlmUninitialize();
return 0;
}