The LlmConnectEx function creates a client session using the
configuration specified in the lpSession structure. This provides
more control over how the connection is established compared to
LlmConnect, and is intended for applications that require advanced
configuration.
The dwProviderId parameter specifies the numeric identifier
assigned to a supported provider. Provider identifiers are predefined
constants built into the library and are assigned fixed values
beginning at 1 and increasing sequentially for each provider. These
identifiers are stable and are intended to uniquely identify a
provider regardless of its display name. Applications should use the
predefined constants (such as LLM_PROVIDER_OPENAI or
LLM_PROVIDER_MICROSOFT) rather than hard-coding numeric
values directly in source code.
Refer to the providers page for
a list of supported service providers and their default models.
The lpSession structure allows the application to specify
connection parameters such as the provider endpoint, model name, API key,
timeout values, token limits and sampling options. Any values not
explicitly specified will use provider defaults.
Applications using languages other than C or C++ may find it easier to
create and manage session structures using the LlmCreateSession,
LlmCreateSessionEx and LlmDeleteSession functions. These
helper functions allocate and initialize a compatible structure that can
be passed to this function.
The library makes an internal copy of the string members in the
LLM_SESSION structure when the connection is established. The application
does not need to retain them after the call to LlmConnectEx returns.
This function will block the calling thread until the connection has
been established or the timeout period has elapsed. Applications should
avoid calling this function on the main user interface thread. For best
results, create a worker thread to establish the connection, especially
when multiple connections are required.
The connection established by this function represents a logical
session. Depending on the provider and options specified, the underlying
HTTP connection may be reused across requests or recreated as needed.
Options such as LLM_OPTION_KEEPALIVE can be used to control
connection behavior.
The client handle returned by this function is owned by the thread
that created it. To transfer ownership to another thread, use the
LlmAttachThread function.
#include "cstools12.h"
#include <stdio.h>
int _tmain(void)
{
HCLIENT hClient = INVALID_CLIENT;
DWORD dwError = NO_ERROR;
if (!LlmInitialize(CSTOOLS12_LICENSE_KEY, NULL))
{
_tprintf(_T("Initialization failed: %lu\n"), LlmGetLastError());
return 1;
}
LLM_SESSION sessionInfo;
ZeroMemory(&sessionInfo, sizeof(sessionInfo));
sessionInfo.dwSize = sizeof(sessionInfo);
sessionInfo.lpszModelName = _T("gpt-6-luna");
sessionInfo.lpszApiKey = _T("%OPENAI_API_KEY%");
sessionInfo.lpszSystemPrompt = _T("You are a helpful assistant.");
hClient = LlmConnectEx(LLM_PROVIDER_OPENAI,
LLM_OPTION_DEFAULT,
&sessionInfo);
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;
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);
}
LlmDisconnect(hClient);
LlmUninitialize();
return 0;
}