# WebSearchTools

## Overview

`WebSearchTools` class offers a set of functions to search and retrieve content from the web. It utilizes the [`IWebSearchService`](/ai/components/built-in-services/iwebsearchservice.md) to interact with various web search providers.

It can download HTML pages as well as any document format supported by the current [`IDocumentConversionService`](/ai/components/built-in-services/idocumentconversionservice.md). Additionally, it handles redirects and deflated (compressed) responses.

{% hint style="danger" %}
Please note that executing JavaScript and loading images on web pages is not supported.
{% endhint %}

The search functionality is achieved by offering two methods to the AI. The `search` method executes a search query and returns relevant URLs along with page content snippets. The `read` method extracts content from the list of URLs selected by the AI.

When the tool downloads content, it does not employ vectorization to condense the text into relevant segments. Instead, it returns the full text content, truncated to the `MaxContextToken` property value (default is 4096 tokens). This limit applies to each page being read. Therefore, if the AI uses the tool to read multiple pages and the `MaxContextToken` is set too high, you may exceed the `ContextWindow` of the current endpoint.

## Using WebSearchTools

To enable the use of [WebSearchTools ](/ai/components/api/tools/wisej.ai.tools.websearchtools.md) simply add it to a SmartHub, SmartAdapter, or SmartPrompt.

```csharp
this.smartDataEntryAdapter1
    .UseTools(
        new WebSearchTools());
```

You can add WebSearchTools to any adapter or prompt to enhance their core functionality in conjunction with other tools. For instance, when paired with the [SmartObjectAdapter](/ai/components/adapters/smartobjectadapter.md), it empowers the AI to fill in missing data by conducting web searches.

## Properties

<table><thead><tr><th width="189" valign="top">Name</th><th>Description</th></tr></thead><tbody><tr><td valign="top">MaxSites</td><td>Read-write and overridable. Default is 5.<br>The <code>MaxSites</code> property limits the number of sites returned by the search engine.</td></tr><tr><td valign="top">MaxContextTokens</td><td>Read-write and overridable. Default is 4096.<br>The <code>MaxContextTokens</code> property is responsible for truncating the content of a single web page when it is converted to text. This truncation is necessary to create the Retrieval-Augmented Generation (RAG) string efficiently.</td></tr><tr><td valign="top">UserAgent</td><td>Read-write and overridable. Default is <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36</code>.<br>The tool is designed to mimic a desktop browser when accessing websites to prevent being blocked or throttled.</td></tr><tr><td valign="top">Timeout</td><td>Read-write and overridable. Default is 30000.<br>The <code>Timeout</code> property specifies the maximum duration, in milliseconds, that the system will wait to read the content of a web page before aborting the request.</td></tr></tbody></table>

## Services

There is no default `IWebSearchService` installed. To use the `WebSearchTools` object, you must register a web search service. The currently available built-in services are: `BingWebSearchService`, `GoogleWebSearchService`, and `BraveWebSearchService`. Each requires an API key, and for `GoogleWebSearchService`, an engine ID is also needed (please refer to Google's documentation for more details).

Here is how to register the `BingWebSearchService` during startup:

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

```csharp
internal static class Program
{
  static Program()
  {
    Application.Services
      .AddOrReplaceService<IWebSearchService, BingWebSearchService>();
  }
}
```

{% endtab %}

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

```vbnet
Module Program
    Shared Sub New()
        Application.Services.AddOrReplaceService( _
            Of IWebSearchService, BingWebSearchService)()
    End Sub
End Class
```

{% endtab %}
{% endtabs %}

If you prefer to specify the API key directly in the code, instead of using the [`ApiKeys.json`](https://docs.wisej.com/ai/components/built-in-smarttools/pages/ZKRA5LS0XtSW01P9zy6k#apikeys.json-file) file or environment variables, use the following code:

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

```csharp
internal static class Program
{
  static Program()
  {
    Application.Services
      .AddOrReplaceService<IWebSearchService>(new BingWebSearchService {
          ApiKey = "..."
    })
  }
}
```

{% endtab %}

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

```vbnet
Module Program
    Shared Sub New()
        Application.Services.AddOrReplaceService( _
            Of IWebSearchService, New BingWebSearchService With {
                .ApiKey = "..."
            })()
    End Sub
End Class
```

{% endtab %}
{% endtabs %}

Refer to [`IWebSearchService`](/ai/components/built-in-services/iwebsearchservice.md) for more information on how to implement and use a custom web search provider.

The tool also utilizes the `IDocumentConversionService` to convert HTML content into plain text. The default implementation provided by Wisej.AI leverages the HtmlAgilityPack, a library designed for parsing HTML content. It processes HTML by breaking it down and subsequently reassembling it into a coherent and usable text format.


---

# 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/built-in-smarttools/websearchtools.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.
