# Using SmartEndpoint

## Overview

The `SmartEndpoint` serves as the interface between Wisej.AI and the AI server that hosts the language model. This component is responsible for managing the communication and data exchange with the AI backend. Wisej.AI is designed with broad compatibility in mind and can work with virtually any AI endpoint, enabling seamless integration with [various AI models and servers](https://docs.wisej.com/ai/concepts/supported-providers).

## How to Use

The easiest way to configure an AI endpoint is by selecting it through the Wisej.AI Wizard, which guides you through the setup process with a user-friendly interface. Alternatively, you can directly instantiate and use any of the available `SmartEndpoint` implementations in your code.

{% tabs %}
{% tab title="C#" %}

```csharp
var openAI = new OpenAIEndpoint {
    ApiKey = "...",
    Model = "gpt-41"
};

var prompt = new SmartPrompt("You are a great C# coder");

C.WriteLine(
    (await prompt.AskAsync(openAI, "Write a bubble sort method.")).Text);
```

{% endtab %}

{% tab title="VB.NET" %}

```vbnet
Dim openAI As New OpenAIEndpoint With {
    .ApiKey = "...",
    .Model = "gpt-41"
}

Dim prompt As New SmartPrompt("You are a great C# coder")

C.WriteLine(
    (Await prompt.AskAsync(openAI, "Write a bubble sort method.")).Text)
```

{% endtab %}
{% endtabs %}

### API Key

There are several ways to set the API key on an endpoint:

* **ApiKey Property**: Directly set the API key using the [ApiKey](https://docs.wisej.com/ai/api/smartendpoint#properties) property in your code.
* **ApiKey.json File**: Store the API key in a JSON configuration file named [ApiKey.json](https://docs.wisej.com/ai/concepts/getting-started#apikeys.json-file).
* **Environment Variables**: Set the API key using [environment variables](https://docs.wisej.com/ai/concepts/getting-started#api-keys-in-environment-variables) for more secure and flexible configuration.

However, when an endpoint is used by a service as a default AI provider, it will need to read its API key either from the `ApiKey.json` file or from the environment variables. This is because the service relies on these configurations for default settings. Alternatively, you can re-register the service using an instance of the endpoint created by code and set the `ApiKey` property directly.

{% tabs %}
{% tab title="C#" %}
{% code overflow="wrap" %}

```csharp
static Program() {
  Application.Services
    .AddOrReplaceService<IEmbeddingGenerationService>(
      new DefaultEmbeddingGenerationService(new OpenAIEndpoint {ApiKey = "..."}));
}
```

{% endcode %}
{% endtab %}

{% tab title="VB.NET" %}
{% code overflow="wrap" %}

```vbnet
Shared Sub New()
  Application.Services _
    .AddOrReplaceService(Of IEmbeddingGenerationService)( _
      New DefaultEmbeddingGenerationService(New OpenAIEndpoint With {.ApiKey = "..."}))
End Sub
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Model Options

AI models typically support a variety of configuration options that allow you to tailor their behavior. Some options, such as `temperature`, are commonly found across many models and have standardized meanings. Others may serve similar purposes but can differ slightly in their naming conventions or implementation details, depending on the specific model or provider. Additionally, certain options may be unique to a particular model, offering specialized features or controls that are not available elsewhere. It is important to consult the documentation for each AI model to understand the available options and how they affect the model's output.

The `SmartEndpoint` class exposes a dynamic object called `ModelOptions`, which you can use to configure any option supported by the selected AI model. This flexible approach allows you to set both standard and model-specific options without the need for predefined properties, ensuring compatibility with a wide range of models and features. Simply assign the desired configuration values to the `ModelOptions` object to customize the behavior of the AI model in your application.

{% tabs %}
{% tab title="C#" %}

```csharp
this.opanAIEndpoint1.ModelOptions.temperature = 0.5f;
this.opanAIEndpoint1.ModelOptions.frequency_penalty = 2;
```

{% endtab %}

{% tab title="VB.NET" %}

```vbnet
Me.opanAIEndpoint1.ModelOptions.temperature = 0.5F
Me.opanAIEndpoint1.ModelOptions.frequency_penalty = 2
```

{% endtab %}
{% endtabs %}
