# Built-in Services

## Overview

To provide greater flexibility and avoid the over-engineering often associated with framework libraries, along with the confusing and conflicting typical `WithThis()`, `AddThat()` chaining methods, Wisej.AI employs overridable services for higher-level functionalities. This design allows developers to easily customize these functionalities to suit their specific needs.

## Existing Services

Here are the services utilized by various Wisej.AI components, along with their default implementations:

<table><thead><tr><th width="262">Service</th><th width="307">Implementations</th><th width="371">Used by</th></tr></thead><tbody><tr><td><a href="built-in-services/iocrservice">IOCRService</a></td><td>DefaultOCRService</td><td><p>SmartObjectAdapter</p><p>SmartDataEntryAdapter</p></td></tr><tr><td><a href="built-in-services/iloggerservice">ILoggerService</a></td><td>DefaultLoggerService</td><td>Everywhere</td></tr><tr><td><a href="built-in-services/itextsplitterservice">ITextSplitterService</a></td><td>RecursiveCharacterTextSplitterService</td><td>SmartHub</td></tr><tr><td><a href="built-in-services/itokenizerservice">ITokenizerService</a></td><td>DefaultTokenizerService</td><td><p>DatabaseTools</p><p>WebSearchTools</p><p>DocumentSearchTools</p></td></tr><tr><td><a href="built-in-services/ihttpclientservice">IHttpClientService</a></td><td>DefaultHttpClientService</td><td><p>SmartHttpEndpoint</p><p>PineconeEmbeddingStorageService</p><p>AzureAISearchEmbeddingStorageService</p><p>ChromaEmbeddingStorageService</p><p>HuggingFaceEmbeddingGenerationService</p><p>WebSearchTools</p></td></tr><tr><td><a href="built-in-services/iwebsearchservice">IWebSearchService</a></td><td><p>BingWebSearchService</p><p>GoogleWebSearchService</p><p>BraveWebSearchService</p></td><td>WebSearchTools</td></tr><tr><td><a href="built-in-services/isessiontrimmingservice">ISessionTrimmingService</a></td><td>DefaultSessionTrimmingService</td><td>SmartSession</td></tr><tr><td><a href="built-in-services/irerankingservice">IRerankingService</a></td><td>DefaultRerankingService</td><td>DocumentTools<br>DocumentSearchTools</td></tr><tr><td><a href="built-in-services/idocumentconversionservice">IDocumentConversionService</a></td><td>DefaultDocumentConversionService</td><td><p>SmartHub</p><p>SmartReportAdapter</p><p>WebSearchTools</p></td></tr><tr><td><a href="built-in-services/iembeddingstorageservice">IEmbeddingStorageService</a></td><td><p>MemoryEmbeddingStorageService</p><p>FileSystemEmbeddingStorageService</p><p>ChromaEmbeddingStorageService</p><p>PineconeEmbeddingStorageService</p><p>AzureAISearchEmbeddingStorageService</p></td><td><p>SmartHub</p><p>SmartDocumentAdapter<br>DocumentTools</p><p>DocumentSearchTools</p></td></tr><tr><td><a href="built-in-services/iembeddinggenerationservice">IEmbeddingGenerationService</a></td><td>DefaultEmbeddingGenerationService</td><td><p>SmartHub<br>DocumentTools</p><p>DocumentSearchTools</p></td></tr></tbody></table>

{% hint style="warning" %}
Please note that the `IWebSearchService` does not have any default implementation pre-registered by Wisej.AI. If you use the `WebSearchTools` without registering a web search service, it will not return any results.
{% endhint %}

## Replacing Services

To replace any of the built-in services, simply implement the corresponding interface and register your service. The optimal place to perform this registration is in the static constructor of the `Program` class or module in VB.NET.

Alternatively, you can extend any of the built-in service implementations by overriding the virtual properties and methods. In this case, you still need to create your own service class, but instead of implementing the interface from scratch, you extend the existing service implementation and override only the parts you wish to modify.

For instance, if you want to use Aspose PDF for converting PDF documents to text while retaining the default `IDocumentConversionService`, you can follow this approach:

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

```csharp
public class AsposeDocumentConversionService 
	: DefaultDocumentConversionService
{
  static AsposeDocumentConversionService()
  {
    new Aspose.Pdf.License()
      .SetLicense(
        File.OpenRead(Application.MapPath("Aspose.Pdf.lic")));
  }
  
  public string[] Convert(
          Stream stream, 
          string fileType, Metadata metadata = null)
  {
    if (fileType == "pdf")
      return ConvertPdfToText(stream, metadata);

    return base.Convert(stream, fileType);
  }
}
```

{% endtab %}

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

```vbnet
Public Class AsposeDocumentConversionService
  Inherits DefaultDocumentConversionService

  Shared Sub New()
    Dim license As New Aspose.Pdf.License()
      license.SetLicense(File.OpenRead(Application.MapPath("Aspose.Pdf.lic")))
  End Sub

  Public Overloads Function Convert( _
                    stream As Stream, _
                    fileType As String, _
                    Optional metadata As Metadata = Nothing) As String()

    If fileType = "pdf" Then
      Return ConvertPdfToText(stream, metadata)
    End If
        
    Return MyBase.Convert(stream, fileType)
  End Function

End Class
```

{% endtab %}
{% endtabs %}

When the default implementation doesn't offer virtual methods, you can still leverage it by maintaining an instance of the default implementation within your service implementation. Here's how you can do it:

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

```csharp
public class AsposeDocumentConversionService 
	: IDocumentConversionService
{
  private IDocumentConversionService _baseService;
  
  static AsposeDocumentConversionService()
  {
    new Aspose.Pdf.License()
      .SetLicense(
        File.OpenRead(Application.MapPath("Aspose.Pdf.lic")));
  }
  
  public AsposeDocumentConversionService()
  {
    _baseService = new DefaultDocumentConversionService();
  }
  
  public string[] Convert(
          Stream stream, 
          string fileType, Metadata metadata = null)
  {
    if (fileType == "pdf")
      return ConvertPdfToText(stream, metadata);

    return _baseService.Convert(stream, fileType);
  }
}
```

{% endtab %}

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

```vbnet
Public Class AsposeDocumentConversionService
  Inherits DefaultDocumentConversionService

  Private _baseService As IDocumentConversionService

  Shared Sub New()
    Dim license As New Aspose.Pdf.License()
      license.SetLicense(File.OpenRead(Application.MapPath("Aspose.Pdf.lic")))
  End Sub

  Public Sub New()
    _baseService = New DefaultDocumentConversionService()
  End Sub
  
  Public Overloads Function Convert( _
                    stream As Stream, _
                    fileType As String, _
                    Optional metadata As Metadata = Nothing) As String()

    If fileType = "pdf" Then
      Return ConvertPdfToText(stream, metadata)
    End If
        
    Return _baseService.Convert(stream, fileType)
  End Function

End Class
```

{% endtab %}
{% endtabs %}

In place of invoking the `base` or `MyBase` methods, utilize the `_baseService` instance.

## Services Lifetime

You are not required to consistently use the same service once you decide to register your own replacements. Wisej.AI registers the following services with a `Transient` lifetime:

`IOCRService`, `IHttpClientService`, `ITextSplitterService`, `IDocumentConversionService`

This means that each time a service is requested, the consumer receives a new instance of the implementation. This approach allows for flexibility and adaptability, enabling you to customize and replace services as needed without being tied to a single instance.

For example, the SmartHub component requests all the services it utilizes at the time of its creation, as do all the adapters. If you wish to use different services for each instance, ensure that you register the new services before the SmartHub or the adapters are instantiated. This allows you to customize the behavior of each component by providing them with the specific services they require, ensuring that they operate with the desired functionality.

The following services are registered with a `Shared` lifetime, meaning that only a single instance of each service is created and shared across the entire application:

`ILoggerService`, `ITokenizerService`, `IEmbeddingStorageService`, `IEmbeddingGenerationService`, `ISessionTrimmingService`.

{% hint style="info" %}
When registering your own services, you have the flexibility to change the lifetime of any service.
{% endhint %}
