Connect Method  
 

The Connect method establishes a connection to the specified large language model service provider and creates a client session.

Syntax

object.Connect( [Provider], [ModelName], [ApiKey], [BaseUrl], [Timeout], [Options] )

Parameters

Provider
An optional parameter which identifies the service provider. If specified, this parameter must be one of the predefined llmProvider constants and cannot be zero. If the parameter is omitted, the control will use the value of the Provider property to establish the connection. For more information about providers, see the Remarks section below.
ModelName
An optional String that specifies the model name to use for the session. If the parameter is omitted, the value of the ModelName property will be used, if one is defined. If no model name is specified, the default model for the provider will be used. Providers without a default model require an explicit model name. This typically includes models accessed using the Microsoft Azure Foundry or locally hosted inference providers.
ApiKey
An optional String that specifies the API key used to authenticate with the provider. If the parameter is omitted, the value of the ApiKey property will be used. 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.
BaseUrl
An optional String that specifies the base URL for the provider endpoint. If this parameter is omitted, the value of the BaseUrl property will be used. If no base URL is specified, the method will use the default endpoint for the selected provider. Some providers, such as Microsoft Foundry or local inference services, will require you to provide a specific base URL endpoint.
Timeout
An optional Integer parameter which specifies the timeout period, in seconds, for connection and request operations. If this parameter is omitted, the value of the Timeout property will be used.
Options
An optional parameter which specifies one or more option flags that control how the connection is established. If this parameter is omitted, the value of the Options property will be used. This parameter can be zero or a combination of the following values:
Value Description
llmOptionNone A standard connection to the server is established using the default behavior for the selected provider. No additional options are enabled, and the connection will use the provider's default protocol, caching behavior and request headers.
llmOptionNoCache Disables provider and model metadata caching for the client session. When this option is specified, the control will not reuse cached metadata, and provider or model information will be retrieved from the server each time it is required. This may be useful when testing or when current data is required.
llmOptionKeepAlive Enables persistent HTTP connections using the keep-alive mechanism. When enabled, the underlying HTTP connection may be reused for multiple requests, reducing connection overhead and improving performance. If this option is not specified, the connection may be closed after each request.
llmOptionProxy Specifies that the connection should be made using the current Windows proxy server configuration. If the user does not have a proxy server configuration, this option is ignored. This option is typically used in environments where direct internet access is restricted.
llmOptionNoUserAgent Prevents the control from sending a User-Agent header with requests to the provider. By default, a User-Agent string is included to identify the client. This option may be used when a minimal or anonymous request is required.
llmOptionSecure Forces the use of a secure HTTPS connection when communicating with the provider. If the specified base URL or provider default would normally use an insecure protocol, it will be upgraded to a secure connection. Connections to providers such as OpenAI, Google and Anthropic will always be secure.
llmOptionDefault Specifies the default option set, which is equivalent to llmOptionNone. No additional flags are enabled and the connection uses standard behavior.

Return Value

A value of True is returned if the connection was successful. If the method fails, it will return False and the LastError property will contain the error code.

Remarks

The Connect method creates a client session that is used for all subsequent operations with the selected provider. The control maintains the connection settings, authentication information, selected model, and conversation history for the session. If you call this method without specifying any parameters, it will use the control's property values as defaults.

The Provider parameter specifies the service provider that will be used to establish a connection. Provider identifiers are predefined constants built into the control and uniquely identify a specific provider. Applications should use the predefined constants (such as llmProviderOpenAI or llmProviderMicrosoft) 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.

If the service provider requires authentication, the API key can be provided in several formats, allowing applications to provide credentials in a way that balances convenience and security:

  • Literal string
    The API key is provided directly as a string. This is the simplest approach and is convenient for testing or small internal tools, but it is not recommended for production applications. A literal key may be exposed in source code, configuration files, diagnostic logs or the compiled application binary.
  • Environment variable
    The name of an environment variable enclosed in percent symbols, for example "%OPENAI_API_KEY%". The value of the environment variable is used as the API key. This avoids embedding the key directly in the application and is useful for development, scripts and deployment environments. However, environment variables may still be visible to the current user, inherited by child processes or exposed through system configuration, so they should still be protected appropriately.
  • Windows credential
    A credential name enclosed in braces, for example "{MyAppName/OpenAI}". The API key is retrieved from the Windows Credential Manager using the specified name. This is the recommended approach for most Windows applications because the secret is stored outside the application and managed by the operating system. The credential can be created manually using Credential Manager, or programmatically by the application using the Windows Credential Management API. Applications should choose a unique credential name and document it clearly if users or administrators are expected to configure the value themselves.

For production applications, it is recommended that you avoid using literal API key strings and instead use an environment variable or a value stored in the Windows Credential Manager. If you must provide the API key as a literal string, you should take additional steps to protect it, such as storing the key in an encrypted form and limiting access to authorized users or processes. Avoid embedding API keys directly in application source code, configuration files, installers or other content that could be exposed through version control systems, logs or public repositories.

This method will wait until the connection has been established or the timeout period has elapsed. 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. Properties such as KeepAlive and IdleTime can be used to control connection behavior.

If the control is already connected to another provider, this method will explicitly close that connection by calling the Disconnect method prior to establishing the new connection.

Applications which only need to send a single prompt and return a response may find the Ask method easier to use. That method automatically manages the connection and returns the generated response as a String value.

Example

Private Sub cmdConnect_Click()
    LlmClient1.Provider = llmProviderOpenAI
    LlmClient1.ModelName = "gpt-5.4-nano"
    LlmClient1.ApiKey = "%OPENAI_API_KEY%" ' Use an environment variable
    LlmClient1.Timeout = 60
    
    ' Establish a connection to the selected provider
    If LlmClient1.Connect() Then
        MsgBox "Connected to provider successfully", vbInformation
        
        ' Send a simple prompt to the model and update a TextBox
        LlmClient1.Prompt = "What is the capital of Denmark?"

        If LlmClient1.SendMessage() Then
            txtResponse.Text = LlmClient1.Response
        Else
            MsgBox LlmClient1.LastErrorString, vbExclamation
        End If
    Else
        MsgBox LlmClient1.LastErrorString, vbExclamation
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Disconnect the current session when the application exits
    If LlmClient1.IsConnected Then
        LlmClient1.Disconnect
    End If
End Sub

See Also

ApiKey Property, BaseUrl Property, ModelName Property, Options Property, Provider Property, Timeout Property, Ask Method, Disconnect Method, SendMessage Method, OnConnect Event