# GenAI-powered Document Insights Prerequisites

This article explains the requirements for using the GenAI-powered Document Insights feature in the [RadWordsProcessing library](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/overview).

## Required References

In addition to the [standard RadWordsProcessing references](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/getting-started#package-references), add the following references to use the GenAI-powered Document Insights features:

| .NET Framework                             | .NET Standard-compatible           |
| ------------------------------------------ | ---------------------------------- |
| `Telerik.Windows.Documents.AIConnector` \* | `Telerik.Documents.AIConnector` \* |

> **\*** The `Documents.AIConnector` NuGet package internally depends on:
>
> * `Telerik.Documents.AI.Core`
> * `Telerik.Documents.AI.RAG`
>   * `Microsoft.Extensions.AI.Abstractions`
>   * `SharpToken`
>
> `Microsoft.Extensions.AI.Abstractions` is currently available only in **preview** version.
> If you reference an assembly/dll of `Documents.AIConnector` instead of a NuGet package, you must manually add the `SharpToken` NuGet package.

## NuGet Packages

You also need to install a package for your specific AI provider:

| Package                                              | Use Case                            |
| ---------------------------------------------------- | ----------------------------------- |
| `Microsoft.Extensions.AI.OpenAI` + `Azure.AI.OpenAI` | For using Azure OpenAI.             |
| `Microsoft.Extensions.AI.OpenAI` + `OpenAI`          | For using OpenAI.                   |
| `Microsoft.Extensions.AI.Ollama`                     | For using Ollama (local AI models). |

## AI Provider Setup

Before using the GenAI-powered Document Insights feature, you need to set up an AI provider.

| [Azure OpenAI Setup](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/prerequisites#azure-openai-setup) | [OpenAI Setup](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/prerequisites#openai-setup) | [Ollama Setup (Local AI)](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/prerequisites#ollama-setup-local-ai) |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Uses the Azure OpenAI service, which provides enterprise-grade security, compliance, and regional availability for OpenAI models.                                                                | Uses direct access to OpenAI models through the API service. Suitable for general development scenarios.                                                                             | Runs AI models locally on your machine. Useful for development or for working with sensitive data where privacy is important.                                                                            |

### Azure OpenAI Setup

1. Create an Azure OpenAI resource in the Azure portal.
2. Deploy a model in your Azure OpenAI resource.
3. Get your Azure OpenAI endpoint and key.

> caution The following code snippet is valid for Microsoft.Extensions.AI.OpenAI 10.3. The specific `IChatClient` initialization may differ depending on the version.

**Example 1: Create an AzureOpenAIClient, get a chat client for a deployment, and cast it to IChatClient**

```C#

// using Microsoft.Extensions.AI;
// using Azure.AI.OpenAI;

// Set up Azure OpenAI client
string key = "your-azure-openai-key";
string endpoint = "https://your-resource-name.openai.azure.com/";
string deploymentName = "your-deployment-name";

AzureOpenAIClient azureClient = new(
    new Uri(endpoint),
    new Azure.AzureKeyCredential(key),
    new AzureOpenAIClientOptions());
ChatClient chatClient = azureClient.GetChatClient(deploymentName);

IChatClient iChatClient = (IChatClient)chatClient;
int maxTokenLimit = 128000; // Adjust based on your model

```

### OpenAI Setup

1. Create an OpenAI account.
2. Get your API key from the OpenAI dashboard.

**Example 2: Create an OpenAI client, get a chat client for a model, and cast it to IChatClient**

```C#

//using Microsoft.Extensions.AI;
//using OpenAI;

// Set up OpenAI client
string key = "your-openai-api-key";
string modelId = "gpt-4o-mini";

OpenAI.OpenAIClient openAIClient = new OpenAI.OpenAIClient(key);
OpenAI.Chat.ChatClient chatClient = openAIClient.GetChatClient(modelId);
IChatClient client = (IChatClient)chatClient;
int maxTokenLimit = 128000; // Adjust based on your model

```

### Ollama Setup (Local AI)

Ollama allows you to run AI models locally on your machine. This is useful for development or when dealing with sensitive data.

1. Install Ollama from [ollama.com](https://ollama.com/).
2. Pull the model you want to use.
3. Start the Ollama server.

**Example 3: Connect to a local Ollama server with an OllamaChatClient for the llama3 model**

```C#

//using Microsoft.Extensions.AI;

// Install and run Ollama:
// 1. Install Ollama: https://ollama.com/
// 2. Pull the model: ollama pull llama3
// 3. Ensure Ollama is running: ollama serve

// Set up Ollama client
IChatClient iChatClient = new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3");
int maxTokenLimit = 4096; // Adjust based on your model

```

## See Also

* [GenAI-powered Document Insights Overview](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/overview)
* [SummarizationProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/summarization-processor)
* [PartialContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/partial-context-question-processor)
* [CompleteContextQuestionProcessor](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/editing/gen-ai-powered-document-insights/complete-context-question-processor)
