| |
| BOOL CreateSession( |
| |
DWORD dwProviderId, |
|
| |
LPCTSTR lpszBaseUrl, |
|
| |
LPCTSTR lpszModelName, |
|
| |
LPCTSTR lpszApiKey, |
|
| |
LPCTSTR lpszApiVersion |
|
| ); |
| BOOL CreateSession( |
| |
DWORD dwProviderId, |
|
| |
LPCTSTR lpszBaseUrl, |
|
| |
LPCTSTR lpszModelName, |
|
| |
LPCTSTR lpszApiKey, |
|
| |
LPCTSTR lpszApiVersion, |
|
| |
LPCTSTR lpszSystemPrompt, |
|
| |
LONG nMaxTokens, |
|
| |
FLOAT fTemperature, |
|
| |
FLOAT fTopP |
|
| ); |
The CreateSession method allocates and initializes an
internal
LLM_SESSION structure using the specified connection
parameters.
Parameters
- dwProviderId
- Specifies the service provider. This parameter must be one of the
LLM_PROVIDER constants. Refer to the
providers page for a list of supported
providers and their default models.
- lpszBaseUrl
- A pointer to a null-terminated string that specifies the base URL
for the provider endpoint. This parameter may be NULL to use the
default endpoint for the selected provider. Some providers, such as
Microsoft Foundry or local services, require a specific base URL.
- lpszModelName
- A pointer to a null-terminated string that specifies the model name
to use for the session. This parameter may be NULL to use the
provider default model, if one is defined. Providers without a default
model require an explicit model name.
- lpszApiKey
- A pointer to a null-terminated string that specifies the API key
used to authenticate with the provider. The API key may be provided
using a literal string, an environment variable, or a value stored in
the Windows Credential Manager. Using an environment variable or
secure credential storage is recommended for production applications
to avoid exposing sensitive information in source code or application
binaries.
- lpszApiVersion
- A pointer to a null-terminated string that specifies the API version
required by the provider. This parameter may be NULL if the provider does
not require an explicit version. Some providers, such as Anthropic or
Microsoft, may require a version string when establishing a connection.
- lpszSystemPrompt
- A pointer to a null-terminated string that specifies the system prompt
used to provide instructions to the model. This parameter may be NULL if
no system prompt is required.
- nMaxTokens
- The maximum number of tokens that may be generated in the response.
Specify zero to use the provider or model default.
- fTemperature
- The temperature value used to control response variability. Lower
values produce more predictable responses, while higher values allow
more variation. Specify
LLM_TEMPERATURE_DEFAULT to use the
default value.
- fTopP
- The nucleus sampling value used to limit token selection based on
probability. Specify
LLM_TOP_P_DEFAULT to use the default
behavior. Most applications should leave this value at the default
unless they specifically need to tune sampling behavior.
- lppSession
- A pointer to a variable that receives a pointer to an allocated
LLM_SESSION structure. On successful return, this parameter
will contain a pointer to a structure that can be passed to
ConnectEx. The structure must be released using the
DeleteSession method when it is no longer needed.
Return Value
If the method succeeds, the return value is non-zero. If the method fails, the return value is
zero. To get extended
error information, call GetLastError.
Remarks
The CreateSession method allocates and initializes an
internal LLM_SESSION structure for the class instance using
the specified parameters. This method is used in conjunction with
the ConnectEx method when an application requires extended
session configuration.
The memory allocated for the session structure will be released
when the class destructor is invoked. The DeleteSession method
can be used to immediately free this memory after a connection has
been established, but this is not required.
It is important to note that the class owns the memory allocated
for this structure, and there can only be one session structure
allocated for each class instance. Your application should
never attempt to explicitly free the pointer returned by this method
and you should not retain a copy of the pointer. The GetSession
method can be used to retrieve a pointer to the current session
structure if one has been allocated.
Example
#include "cstools12.h"
#include <stdio.h>
int _tmain(void)
{
CLlmClient llmClient;
DWORD dwError = NO_ERROR;
if (!llmClient.CreateSession(
LLM_PROVIDER_OPENAI,
NULL,
_T("gpt-6-luna"),
_T("%OPENAI_API_KEY%"))
{
dwError = llmClient.GetLastError();
_tprintf(_T("Unable to create session, error %lu\n"), dwError);
return 1;
}
if (!llmClient.ConnectEx())
{
dwError = llmClient.GetLastError();
_tprintf(_T("Unable to connect, error %lu\n"), dwError);
return 1;
}
TCHAR szResponse[4096] = {0};
DWORD dwResponseLength = _countof(szResponse);
DWORD dwMessageId = 0;
if (llmClient.SendMessage(
_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);
}
llmClient.Disconnect();
return 0;
}
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csllmv12.lib
Unicode: Implemented as Unicode and ANSI versions
See Also
|
|