# SmartExtensions

Namespace: **Wisej.AI**

Assembly: **Wisej.AI** (3.5.0.0)

Provides extension methods for various operations such as calculating cosine similarity, computing clusters, converting strings to camel case, and managing services in a service provider.

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

```csharp
public class SmartExtensions
```

{% endtab %}

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

```visual-basic
Public Class SmartExtensions
```

{% endtab %}
{% endtabs %}

## Methods

### ![](/files/6srAlWaUVvAWWpvmGKFp) AddOrReplaceService\<TService, TImplementation>(services, lifetime)

Adds or replaces a service in the service provider with the specified implementation type.

| Parameter                                     | Type                                                                           | Description                            |
| --------------------------------------------- | ------------------------------------------------------------------------------ | -------------------------------------- |
| **TService**                                  |                                                                                | The type of service to add or replace. |
| **TImplementation**                           |                                                                                | The type of the implementation to use. |
| **services**                                  | [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider) | The service provider to modify.        |
| **lifetime** ![](/files/52BqKf9SltWQ97FbQSBF) | [ServiceLifetime](https://docs.wisej.com/api?q=wisej.services.servicelifetime) | The lifetime of the service.           |

**Returns:** [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider). The modified service provider.

### ![](/files/6srAlWaUVvAWWpvmGKFp) AddOrReplaceService\<TService>(services, implementation, lifetime)

Adds or replaces a service in the service provider with the specified implementation instance.

| Parameter                                     | Type                                                                           | Description                            |
| --------------------------------------------- | ------------------------------------------------------------------------------ | -------------------------------------- |
| **TService**                                  |                                                                                | The type of service to add or replace. |
| **services**                                  | [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider) | The service provider to modify.        |
| **implementation**                            | [TService](/ai/components/api/wisej.ai.smartextensions.md)                     | The implementation instance to use.    |
| **lifetime** ![](/files/52BqKf9SltWQ97FbQSBF) | [ServiceLifetime](https://docs.wisej.com/api?q=wisej.services.servicelifetime) | The lifetime of the service.           |

**Returns:** [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider). The modified service provider.

### ![](/files/6srAlWaUVvAWWpvmGKFp) AddOrReplaceService\<TService>(services, factory, lifetime)

Adds or replaces a service in the service provider using a factory method.

| Parameter                                     | Type                                                                           | Description                                        |
| --------------------------------------------- | ------------------------------------------------------------------------------ | -------------------------------------------------- |
| **TService**                                  |                                                                                | The type of service to add or replace.             |
| **services**                                  | [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider) | The service provider to modify.                    |
| **factory**                                   | [Func\<Type, Object>](https://docs.microsoft.com/dotnet/api/system.func-2)     | The factory method to create the service instance. |
| **lifetime** ![](/files/52BqKf9SltWQ97FbQSBF) | [ServiceLifetime](https://docs.wisej.com/api?q=wisej.services.servicelifetime) | The lifetime of the service.                       |

**Returns:** [ServiceProvider](https://docs.wisej.com/api?q=wisej.services.serviceprovider). The modified service provider.

### ![](/files/6srAlWaUVvAWWpvmGKFp) ComputeClusters(embeddings, count, maxDivergence)

Computes clusters from the given embeddings using the K-Means clustering algorithm.

| Parameter                                          | Type                                                                  | Description                                     |
| -------------------------------------------------- | --------------------------------------------------------------------- | ----------------------------------------------- |
| **embeddings**                                     | [Single\[\]\[\]](https://docs.microsoft.com/dotnet/api/system.single) | The embeddings to cluster.                      |
| **count**                                          | [Int32](https://docs.microsoft.com/dotnet/api/system.int32)           | The number of clusters to create.               |
| **maxDivergence** ![](/files/52BqKf9SltWQ97FbQSBF) | [Double](https://docs.microsoft.com/dotnet/api/system.double)         | The maximum divergence allowed for convergence. |

**Returns:** [ValueTuple\`2\[\]](https://docs.microsoft.com/dotnet/api/system.valuetuple-2). An array of tuples containing the centroid and vectors of each cluster.

K-Means clustering is a method of vector quantization, originally from signal processing, that is popular for cluster analysis in data mining. This method partitions the embeddings into *count* clusters.

### ![](/files/6srAlWaUVvAWWpvmGKFp) CosineSimilarity(vectorA, vectorB)

Calculates the cosine similarity between two vectors.

| Parameter   | Type                                                              | Description        |
| ----------- | ----------------------------------------------------------------- | ------------------ |
| **vectorA** | [Single\[\]](https://docs.microsoft.com/dotnet/api/system.single) | The first vector.  |
| **vectorB** | [Single\[\]](https://docs.microsoft.com/dotnet/api/system.single) | The second vector. |

**Returns:** [Single](https://docs.microsoft.com/dotnet/api/system.single). The cosine similarity between the two vectors.

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space. It is defined as the cosine of the angle between the two vectors.

### ![](/files/6srAlWaUVvAWWpvmGKFp) CosineSimilarity(vectorA, vectorB)

Calculates the cosine similarity between a vector and an array of vectors.

| Parameter   | Type                                                                  | Description                              |
| ----------- | --------------------------------------------------------------------- | ---------------------------------------- |
| **vectorA** | [Single\[\]](https://docs.microsoft.com/dotnet/api/system.single)     | The vector to compare.                   |
| **vectorB** | [Single\[\]\[\]](https://docs.microsoft.com/dotnet/api/system.single) | The array of vectors to compare against. |

**Returns:** [Single\[\]](https://docs.microsoft.com/dotnet/api/system.single). An array of cosine similarity values for each vector in the array.

This method extends the single vector cosine similarity calculation to handle multiple vectors, returning an array of similarity scores.

### ![](/files/6srAlWaUVvAWWpvmGKFp) ToCamelCase(text)

Converts the first character of the string to lowercase, making it camel case.

| Parameter | Type                                                          | Description            |
| --------- | ------------------------------------------------------------- | ---------------------- |
| **text**  | [String](https://docs.microsoft.com/dotnet/api/system.string) | The string to convert. |

**Returns:** [String](https://docs.microsoft.com/dotnet/api/system.string). The camel case version of the string.

This method is useful for converting PascalCase strings to camelCase, which is often used in JSON serialization and other contexts.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.wisej.com/ai/components/api/wisej.ai.smartextensions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
