|
| 1 | +--- |
| 2 | +title: PartialContextQuestionProcessor |
| 3 | +description: PartialContextQuestionProcessor class enables you to ask questions about a Word document and receive answers based on the most relevant parts of the document content. |
| 4 | +page_title: PartialContextQuestionProcessor |
| 5 | +slug: radwordsprocessing-features-gen-ai-powered-document-insights-partial-context-question-processor |
| 6 | +tags: ai, document, analysis, question, processor, partial, context, embeddings |
| 7 | +published: True |
| 8 | +position: 4 |
| 9 | +--- |
| 10 | +<style> |
| 11 | +table, th, td { |
| 12 | + border: 1px solid; |
| 13 | +} |
| 14 | +table th:first-of-type { |
| 15 | + width: 65%; |
| 16 | +} |
| 17 | +table th:nth-of-type(2) { |
| 18 | + width: 10%; |
| 19 | +} |
| 20 | +table th:nth-of-type(3) { |
| 21 | + width: 25%; |
| 22 | +} |
| 23 | +</style> |
| 24 | + |
| 25 | +# PartialContextQuestionProcessor |
| 26 | + |
| 27 | +The **PartialContextQuestionProcessor** class enables you to ask questions about a Word document and receive answers based on the most relevant parts of the document content. This processor uses embeddings to identify and send only the relevant portions of the document to the AI model, making it more efficient for token usage and more suitable for large documents. This class inherits from the abstract **AIProcessorBase** class, which provides common functionality for all AI processors. |
| 28 | + |
| 29 | +The **PartialContextQuestionProcessor** is ideal for the following scenarios: |
| 30 | + |
| 31 | +1. **Large Documents**: When the document exceeds the token limit of the AI model and cannot be processed in a single call. |
| 32 | +2. **Efficient Token Usage**: When you want to minimize token consumption and optimize costs. |
| 33 | +3. **Specific Questions**: When questions are targeted at specific information within the document rather than requiring complete document understanding. |
| 34 | + |
| 35 | +## Public API and Configuration |
| 36 | + |
| 37 | +|Constructor|Platform|Description| |
| 38 | +|---|---|---| |
| 39 | +|**PartialContextQuestionProcessor(IChatClient chatClient, int modelMaxInputTokenLimit, ISimpleTextDocument document)**|_Specific*_ |Creates an instance with built-in embeddings storage| |
| 40 | +|**PartialContextQuestionProcessor(IChatClient chatClient, IEmbeddingsStorage embeddingsStorage, int modelMaxInputTokenLimit, ISimpleTextDocument document)**|Any|Creates an instance with custom embeddings storage| |
| 41 | + |
| 42 | +> _*Specific_ The .NET {{site.mindotnetversion}}+ (Target OS Windows) + [Packages for .NET Framework and .NET {{site.mindotnetversion}} and .NET {{site.maxdotnetversion}} for Windows]({%slug available-nuget-packages%}#packages-for-net-framework-and-net-{{site.mindotnetversion}}-and-net-{{site.maxdotnetversion}}-for-windows) constructor uses **DefaultEmbeddingsStorage** internally, while the cross-platform constructor requires a custom implementation of **IEmbeddingsStorage** as shown in the [Custom IEmbeddingsStorage Setup]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-partial-context-question-processor%}#implementing-custom-iembeddingsstorage) section. |
| 43 | +
|
| 44 | +### Properties and Methods |
| 45 | + |
| 46 | +|Member|Type|Description| |
| 47 | +|---|---|---| |
| 48 | +|**Settings**|Property|Gets the **PartialContextProcessorSettings** for configuring the AI process| |
| 49 | +|**AnswerQuestion(string question)**|Method|Returns an answer to the question using relevant document context| |
| 50 | + |
| 51 | +>caution **Security Warning:** The output produced by this API is generated by a Large Language Model (LLM). As such, the content should be considered untrusted and may include unexpected or unsafe data. It is strongly recommended to properly sanitize or encode all output before displaying it in a user interface, logging, or using it in any security-sensitive context. |
| 52 | +
|
| 53 | +### PartialContextProcessorSettings |
| 54 | + |
| 55 | +The settings class provides configuration options for the question-answering process: |
| 56 | + |
| 57 | +* **ModelMaxInputTokenLimit**: Maximum input token limit the model allows |
| 58 | +* **TokenizationEncoding**: Tokenization encoding used |
| 59 | +* **ModelId**: ID of the AI model |
| 60 | +* **MaxNumberOfEmbeddingsSent**: Maximum number of context chunks sent (default: 30) |
| 61 | +* **EmbeddingTokenSize**: Size in tokens of each context chunk (default: 300) |
| 62 | + |
| 63 | +## Usage Examples |
| 64 | + |
| 65 | +#### Example 1: Using PartialContextQuestionProcessor with default embeddings storage. |
| 66 | + |
| 67 | +This example demonstrates how to use the **PartialContextQuestionProcessor** with the built-in embeddings storage on .NET {{site.mindotnetversion}}+ (Target OS Windows) + [Packages for .NET Framework and .NET {{site.mindotnetversion}} and .NET {{site.maxdotnetversion}} for Windows]({%slug available-nuget-packages%}#packages-for-net-framework-and-net-{{site.mindotnetversion}}-and-net-{{site.maxdotnetversion}}-for-windows). For setting up the AI client, see the [AI Provider Setup]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-prerequisites%}#ai-provider-setup) section: |
| 68 | + |
| 69 | +<snippet id='libraries-flow-features-gen-ai-ask-questions-using-partial-context'/> |
| 70 | + |
| 71 | +#### Example 2: Using PartialContextQuestionProcessor with Custom Embeddings (.NET Standard/.NET Framework) |
| 72 | + |
| 73 | +This example demonstrates how to use the **PartialContextQuestionProcessor** with a custom embeddings storage implementation as described in the [Custom IEmbeddingsStorage Setup]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-partial-context-question-processor%}#implementing-custom-iembeddingsstorage) section: |
| 74 | + |
| 75 | +<snippet id='libraries-flow-features-gen-ai-ask-questions-using-partial-context-iembeddingsstorage'/> |
| 76 | + |
| 77 | +### Implementing custom IEmbeddingsStorage |
| 78 | + |
| 79 | +A sample custom implementation for the OllamaEmbeddingsStorage is shown in the below code snippet: |
| 80 | + |
| 81 | +>note Requires installing the following NuGet packages: |
| 82 | +> * **LangChain** |
| 83 | +> * **LangChain.Databases.Sqlite** |
| 84 | +> * **Microsoft.Extensions.AI.Ollama** |
| 85 | +> * **Telerik.Windows.Documents.AIConnector** |
| 86 | +> * **Telerik.Windows.Documents.Fixed** |
| 87 | +
|
| 88 | +1. Install Ollama from [ollama.com](https://ollama.com/). |
| 89 | +2. Pull the model you want to use. |
| 90 | +3. Start the Ollama server. |
| 91 | + |
| 92 | +<snippet id='libraries-pdf-features-gen-ai-ask-questions-using-partial-context-ollama-embeddings-storage'/> |
| 93 | + |
| 94 | +#### Example 3: Processing Specific Pages |
| 95 | + |
| 96 | +<snippet id='libraries-flow-features-gen-ai-summarize-process-specific-pages'/> |
| 97 | + |
| 98 | +#### Example 4: Optimizing Embeddings Settings |
| 99 | + |
| 100 | +<snippet id='libraries-flow-features-gen-ai-summarize-optimize-embeddings-storage'/> |
| 101 | + |
| 102 | +## See Also |
| 103 | + |
| 104 | +* [GenAI-powered Document Insights Overview]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-overview%}) |
| 105 | +* [Prerequisites]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-prerequisites%}) |
| 106 | +* [SummarizationProcessor]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-summarization-processor%}) |
| 107 | +* [CompleteContextQuestionProcessor]({%slug radwordsprocessing-features-gen-ai-powered-document-insights-complete-context-question-processor%}) |
0 commit comments