Building an Azure OpenAI-Powered PDF Question-Answering System in .NET
Chander D.
CEO of Cazton, Author, Microsoft AI MVP, Microsoft RD & Google Developer Expert Award
Introduction: With the increasing amount of information available in PDF documents, it has become essential to find ways to extract meaningful insights quickly and efficiently. One way to achieve this is by leveraging artificial intelligence and natural language processing techniques to create powerful question-answering systems.
In this blog post, we will guide you through the process of building a natural language question-answering system using OpenAI and Azure in .NET. This system will allow you to ask questions about a PDF document in natural language, and the AI will provide relevant answers based on the document's content.
We will cover every necessary step and concept required to make this work, including setting up the environment, configuring the OpenAI API client, loading and extracting text from a PDF document, and asking questions to receive answers.
Prerequisites: Before we start, ensure that you have the following installed and set up on your machine:
Step 1: Set Up the Environment
First, create a new .NET Console app and install the required NuGet packages:
dotnet new console -o OpenAI_PDF_QA_Azure
cd OpenAI_PDF_QA_Azure
dotnet add package PdfPig
dotnet add package Azure.AI.OpenAI
Next, replace the content of the Program.cs file with the following C# code:
using System;
using System.IO;
using System.Text;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using OpenAI;
using Azure;
using Azure.AI.OpenAI;
using static System.Environment;
Step 2: Configure the OpenAI API Client
To configure the OpenAI API client, you will need to provide your Azure OpenAI API Key, endpoint, and deployment name. Replace the placeholders in the following code snippet with your respective values:
// Load environment variables
string key = "<Add your key>";
string endpoint = "<Add your endpoint>"; (Looks like: "https://Cazton.openai.azure.com/" )
string engine = "<Add your deployment name>";
// Configure OpenAI API client
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));?
Step 3: Load the PDF File and Extract Text
In this step, we will use the PdfPig library to load a PDF file and extract its text content. Replace the pdfFilePath variable's value with the path to your PDF file:
// Load PDF file and extract text
string pdfFilePath = "gpt.pdf";
StringBuilder fullText = new StringBuilder();?
using (PdfDocument pdf = PdfDocument.Open(pdfFilePath))
{
???for (int i = 0; i < pdf.NumberOfPages; i++)
???{
???????Page page = pdf.GetPage(i + 1);
???????fullText.Append(page.Text);
???}
}
Step 4: Set Up the GPT-3 Model and Prompt
Now that we have extracted the text from the PDF, we will set up the GPT-3 model and the initial prompt. The prompt will include the full text from the PDF document, providing GPT-3 with the necessary context to answer the questions.
Step 5: Ask Questions and Get Answers
In this step, we will define an array of questions to ask about the PDF document. Then, we will iterate through these questions, sending them to the GPT-3 model and receiving relevant answers. Finally, we will print the answers to the console:
// Ask questions and get answers
string[] questions = { "What is the document about?", "Who wrote the document?", "What is the main idea of the document?" };
?foreach (string question in questions)
{
???string fullPrompt = question;
???CompletionsOptions completionsOptions = new()
???{
???????MaxTokens = 100
???};
???Response<Completions> completionsResponse = await client.GetCompletionsAsync(deploymentOrModelName: modelEngine, prompt: fullPrompt);?
???string answer = completionsResponse.Value.Choices[0].Text;
???Console.WriteLine($"{question}\n{answer}\n");
}
Now, combine all the code snippets into the Main method in the Program.cs file:
using System;
using System.IO;
using System.Text;
using UglyToad.PdfPig;
using UglyToad.PdfPig.Content;
using OpenAI;
using Azure;
using Azure.AI.OpenAI;
using static System.Environment;
?
namespace OpenAI_PDF_QA_Azure
{
???class Program
领英推荐
???{
???????static async Task Main(string[] args)
???????{
???????????// Load environment variables
???????????string key = "<Add your key>";
???????????string endpoint = "<Add your endpoint>";
???????????string engine = "<Add your deployment name>";
?
???????????// Configure OpenAI API client
???????????OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
?
???????????// Load PDF file and extract text
???????????string pdfFilePath = "gpt.pdf";
???????????StringBuilder fullText = new StringBuilder();
?
???????????using (PdfDocument pdf = PdfDocument.Open(pdfFilePath))
???????????{
???????????????for (int i = 0; i < pdf.NumberOfPages; i++)
???????????????{
???????????????????Page page = pdf.GetPage(i + 1);
???????????????????fullText.Append(page.Text);
???????????????}
???????????}
?
???????????// Set up GPT-3 model and prompt
???????????string modelEngine = "CaztonDavinci3";
???????????string prompt = $"What is the answer to the following question regarding the PDF document?\n\n{fullText}\n\n";
?
???????????// Ask questions and get answers
???????????string[] questions = { "What is the document about?", "Who wrote the document?", "What is the main idea of the document?" };
?
???????????foreach (string question in questions)
???????????{
???????????????string fullPrompt = question;
???????????????CompletionsOptions completionsOptions = new()
???????????????{
???????????????????MaxTokens = 100
???????????????};
???????????????Response<Completions> completionsResponse = await client.GetCompletionsAsync(deploymentOrModelName: modelEngine, prompt: fullPrompt);
?
???????????????string answer = completionsResponse.Value.Choices[0].Text;
???????????????Console.WriteLine($"{question}\n{answer}\n");
???????????}
???????}
???}
}
Finally, run the console app with the following command:
dotnet run
Conclusion: In this blog post, we have demonstrated how to build a natural language question-answering system using Azure OpenAI in .NET. This system allows you to ask questions about a PDF document in natural language, and the AI will provide relevant answers based on the document's content.
We covered setting up the environment, configuring the OpenAI API client, loading and extracting text from a PDF document, and asking questions to receive answers.
GPT-3 models, various PDF documents, or even other types of documents. It can be further extended to support additional natural language processing tasks such as summarization, translation, or sentiment analysis.
By leveraging the power of OpenAI and Azure in .NET, you can create intelligent applications that can understand and process human language, unlocking new possibilities and improving efficiency in various domains like education, research, customer support, and more.
In the future, you can explore incorporating this question-answering system into a web application, chatbot, or voice assistant, providing users with an interactive interface to extract information from documents seamlessly.
Improvements to Consider:
Mastering OpenAI GPT-4 and Azure OpenAI using Python and .NET
We are thrilled to offer a comprehensive training program for individuals who are eager to expand their knowledge and master the art of PDF document questioning. Through hands-on exercises, real-world examples, and personalized guidance, participants will learn how to implement efficient chunking, apply advanced retrieval algorithms, utilize embeddings and vector databases, and build an enhanced query interface. They will also gain insights into optimizing model performance, handling different document types, and overcoming common challenges encountered during the document questioning process.?
Conclusion:
By incorporating these improvements and participating in our training program, you will unlock the full potential of OpenAI GPT-3 for PDF document questioning. With efficient chunking, better retrieval algorithms, embeddings and vector databases, and an enhanced query interface, you will be able to extract valuable information, answer complex queries, and gain deeper insights from PDF documents.
Are you ready to take your skills to the next level? Join our training program and discover the limitless possibilities of OpenAI. Sign up for our online training program and take advantage of a special $1,000 discount by using the code "Cazton" during registration. Mastering OpenAI GPT-4 and Azure OpenAI using Python and .NET Tickets, Thu, Jun 22, 2023 at 9:00 AM | Eventbrite
Mastering OpenAI GPT-4 and Azure OpenAI using Python and .NET
Training Agenda:
Setting Up the Environment and Working with APIs
Module 1: Setting up Python and .NET Environments for GPT-4
Module 2: Mastering the Art of Prompt Engineering
Enterprise development with OpenAI: APIs, Text Generation, and GPT-4 App Development
Module 3: Understanding and Working with OpenAI APIs
Module 4: Text Generation and Completion
Module 5: Building a GPT-4 Powered PDF Query App
Advanced Projects and Techniques
Module 6: Chatbots and Chat Completion
Module 7: GPT-4 Chat Bot for Your Website
Module 8: GPT-4 with Private Data
Module 9: GPT-4 with Live Internet Data
Module 10: Vector Databases
For detailed agenda, click here: Mastering OpenAI GPT-4 and Azure OpenAI using Python and .NET Tickets, Thu, Jun 22, 2023 at 9:00 AM | Eventbrite
Happy coding!
Application System Analyst at Harris County Central Technology Services (CTS)
1 年thanks, it is better than ms own document.