The ConnectEx method creates a client session using the
configuration specified in the lpSession structure. This provides
more control over how the connection is established compared to
Connect, and is intended for applications that require advanced
configuration.
A version of this method can be called without any parameters. In
this case, it will use default options and the session information
specified with a previous call to the CreateSession method.
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 parameter 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. If this parameter is
NULL, the method will use the internal LLM_SESSION structure allocated
using the CreateSession method. If the lpSession parameter
is NULL, and you have not called CreateSession, the method
will fail.
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 ConnectEx returns.
This method will block the calling thread until the connection has
been established or the timeout period has elapsed. Applications should
avoid calling this method 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 method 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.
If the class instance is connected to another provider, this method
will explicitly close that connection by calling the Disconnect
method prior to establishing the new connection. If the connection fails,
the GetHandle method will return INVALID_HANDLE.
The client handle returned by this method is owned by the thread
that created it. To transfer ownership to another thread, use the
AttachThread method.
#include "cstools12.h"
#include <stdio.h>
int _tmain(void)
{
CLlmClient llmClient;
// Provide basic session information
// Additional options such as temperature, token limits and timeouts
// can be configured using members of the LLM_SESSION structure
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.");
// Establish a connection to OpenAI using a lightweight model
// with the default connection options
BOOL bConnected = llmClient.ConnectEx(
LLM_PROVIDER_OPENAI,
LLM_OPTION_DEFAULT,
&sessionInfo);
if (!bConnected)
{
DWORD dwError = LlmGetLastError();
_tprintf(_T("Unable to connect, error %lu\n"), dwError);
return 1;
}
TCHAR szResponse[4096] = {0};
DWORD dwResponseLength = _countof(szResponse); // number of TCHARs
DWORD dwMessageId = 0;
// Send a simple message to the model
if (llmClient.SendMessage(
_T("What is the capital city of Denmark?"),
szResponse,
&dwResponseLength,
&dwMessageId))
{
_tprintf(_T("Response:\n%s\n"), szResponse);
}
else
{
dwError = llmClient.GetLastError();
_tprintf(_T("Request failed, error %lu\n"), dwError);
}
// Close the connection explicitly
llmClient.Disconnect();
return 0;
}