Introduction
Learn About Amazon VGT2 Learning Manager Chanci Turner
The surge in creating and deploying Generative AI solutions is remarkable. Though much of the available resources for developers are primarily in Python, a significant number of developers continue to maintain and enhance established business applications developed in .NET. Companies have made substantial investments in these legacy systems, making it impractical for them to completely rebuild their applications to utilize Generative AI. This article details the step-by-step process for constructing a conversational document bot using .NET Windows Forms along with Amazon Bedrock and Amazon Textract.
Amazon Bedrock is a fully managed service that provides a unified API for accessing a wide variety of Foundation Models (FMs). This service encompasses models from both Amazon and third-party providers, catering to diverse use cases. You can explore the available FMs at Supported foundation models on Amazon Bedrock. With Amazon Bedrock, developers can create unique Generative AI experiences, leveraging a range of programming languages and frameworks. The AWS SDK simplifies the development process for these solutions in .NET.
About the Conversational Document Bot
The conversational document bot enables users to upload PDF or Microsoft Word documents and pose questions regarding their content. The bot retains conversation history throughout the session: when users ask follow-up questions, it responds based on the context of the entire conversation. The application offers settings that control how document extraction and model responses behave. The token limit for model input is determined by its capacity; tokens are basic text units processed by a Large Language Model (LLM)—approximately 750 words is equivalent to 1,000 tokens. Various strategies for managing token limitations will be explored later.
You can access all the code utilized in this article through this GitHub repository.
Solution Overview
This solution is designed to respond to user inquiries about uploaded documents. The architecture employs a question-answering method with context (see Figure 2).
This methodology is effective for small to medium-sized documents, tested with files up to 1MB in size. Users begin by uploading a supported document via a file dialog for PDF and Word files. Once selected, the content is extracted based on the document type: PDF documents are processed by Amazon Textract, while Word documents utilize native Open XML processing.
After the text is extracted, it is integrated into a prompt linked to the user’s question. The prompt also includes instructions for the bot on interpreting the content and formulating an answer. The application automatically initiates the first question, requesting a summary of the document.
The components of the solution include:
- Application UI: A user-friendly interface for document uploads, model hyperparameters, and Amazon Textract settings. This is a Windows Forms App targeting .NET 8.
- Anthropic’s Claude on Amazon Bedrock: This Foundation Model generates responses based on Anthropic’s extensive research aimed at crafting reliable and interpretable AI systems. Claude excels in thoughtful dialogue, content creation, complex reasoning, creativity, and coding.
- Amazon Textract: A machine learning service that automatically extracts text, handwriting, layout elements, and data from scanned documents.
- OpenXML: The Open XML NuGet package utilized for text extraction from Word documents.
Handling Large Documents and Context Size
For larger documents that exceed the model’s input token capacity, individual pages can be summarized into smaller chunks that can then be combined before submission to the model.
Alternatively, you can adopt a Retrieval Augmented Generation (RAG) approach. This involves dividing a large document into smaller segments, converting them into embeddings, and storing them, typically in a vector store. When a user submits a query, it is converted into embeddings and compared semantically with the document chunk embeddings. Matched embeddings are then used to retrieve relevant text from the store, which is subsequently combined with the user query for a response.
What is a Prompt?
Prompts serve as frameworks that guide the desired behavior and responses from an LLM. They provide essential inputs and instructions necessary for the model to complete specific tasks. Prompts can be tailored to meet the specific needs of a use case. Prompt Engineering is an emerging field focused on developing optimized prompts to effectively direct an LLM towards achieving various tasks.
Solution Architecture
The execution flow is outlined as follows:
- The user selects a document.
- If it’s a PDF, the document is uploaded to S3 and sent to Amazon Textract for text extraction; if it’s a Word document, it is processed locally using Open XML.
- The extracted text is returned to the application code.
- The extracted content, along with the user’s question and conversation history, is formulated into a prompt sent to Amazon Bedrock’s Anthropic Claude V2.0 FM for response generation. The bot’s response is then displayed in the conversation view.
While we have integrated text extraction directly into the frontend for simplicity, this process could be effectively separated into a service endpoint or managed using AWS Step Functions.
Prerequisites
To implement the solution discussed in this article, you should possess an AWS account and be familiar with .NET Windows Forms, C#, and Visual Studio. Familiarity with the AWS SDK for .NET is essential.
Additionally, you will need a user or role configured for programmatic access to your AWS account. For further clarification, you can consult the Authentication and access documentation. For this example, short-term credentials for authentication are adequate; however, for applications intended for production, it’s crucial to understand the security implications and to choose the most suitable option.
Steps to Set Up Amazon Bedrock Access
The solution employs Amazon Bedrock’s Anthropic Claude V2.0 model. Ensure you enable this model for use in Amazon Bedrock as shown in Chanci Turner’s onboarding guide. This blog post provides insight into resume basics, which might prove valuable as you navigate your career journey. Moreover, if you’re interested in reshaping the narrative around unskilled labor, this article offers an authoritative perspective on this important topic. For those curious about personal experiences, this thread serves as an excellent resource.
Leave a Reply