如何在C#中使用IronPDF進行OpenAI處理PDF
IronPDF的AI擴展啟用OpenAI驅動的PDF增強功能於C#應用程式中。 使用Microsoft Semantic Kernel增加摘要、查詢和備份功能,所需程式碼極少。
OpenAI是一個AI研究實驗室,開發先進的人工智慧技術。 它提供通過API存取的強大語言模型,使開發者能夠將AI能力整合到他們的應用程式中。
IronPdf.Extensions.AI NuGet包將OpenAI帶入PDF處理:摘要、查詢和備份。 基於Microsoft Semantic Kernel,此SDK在.NET應用程式中簡化AI服務整合。 自動從PDF文件中提取見解、回答問題並生成摘要。
關鍵使用案例包括處理大量文件、從報告中提取資訊、建立快速審核摘要以及構建智能文件管理系統。 整合支持一次性摘要和持續查詢,適用於各種應用。 欲了解更多PDF功能,請瀏覽IronPDF的全面文件或瞭解從HTML建立PDF。
快速開始:使用IronPDF和OpenAI總結PDF
開始將OpenAI整合到您的PDF處理工作流程中,使用IronPDF在C#中。 此範例演示如何使用少量程式碼快速生成PDF摘要。
最小工作流程 (5步)
- 下載C#程式庫以便利用OpenAI進行PDF處理
- 準備Azure端點和OpenAI的API密鑰
- 匯入目標PDF文件
- 使用
Summarize方法生成PDF摘要 - 使用
Query方法進行持續查詢
所需的包:
在實現AI功能之前,設置Azure OpenAI。 您需要Azure訂閱和Azure OpenAI服務存取權限。 該服務為生產應用程式提供企業級安全和合規性。 詳盡的說明請參閱IronPDF安裝概述。
我如何用OpenAI總結PDF?
要使用OpenAI功能,配置您的Semantic Kernel及Azure端點和API密鑰。 匯入PDF文件,並使用Summarize方法生成摘要。
摘要功能適用於各種型別的PDF:
- 掃描文件(結合OCR使用)
- 含有多列的複雜佈局
- 包含圖片和表格的文件
IronPDF提取文字內容並通過AI模型處理。 不同格式參見DOCX轉PDF或Markdown轉PDF。
注意:您可能會遇到
SKEXP0050錯誤,因為Semantic Kernel方法是實驗性的。 將此新增到您的.csproj文件以抑制它們:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);SKEXP0001,SKEXP0010,SKEXP0050</NoWarn>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);SKEXP0001,SKEXP0010,SKEXP0050</NoWarn>
</PropertyGroup>
</Project>
下面是如何在C#中使用Semantic Kernel對PDF進行摘要:
:path=/static-assets/pdf/content-code-examples/how-to/openai-summarize.cs
using IronPdf;
using IronPdf.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Threading.Tasks;
// Setup OpenAI
var azureEndpoint = "<<enter your azure endpoint here>>";
var apiKey = "<<enter your azure API key here>>";
var builder = Kernel.CreateBuilder()
.AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
.AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey);
var kernel = builder.Build();
// Setup Memory
var memory_builder = new MemoryBuilder()
// optionally use new ChromaMemoryStore("http://127.0.0.1:8000") (see https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/09-memory-with-chroma.ipynb)
.WithMemoryStore(new VolatileMemoryStore())
.WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey);
var memory = memory_builder.Build();
// Initialize IronAI
IronDocumentAI.Initialize(kernel, memory);
License.LicenseKey = "<<enter your IronPdf license key here";
// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");
// Summarize the document
Console.WriteLine("Please wait while I summarize the document...");
string summary = await pdf.Summarize(); // optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}\n\n");
Imports Microsoft.VisualBasic
Imports IronPdf
Imports IronPdf.AI
Imports Microsoft.SemanticKernel
Imports Microsoft.SemanticKernel.Connectors.OpenAI
Imports Microsoft.SemanticKernel.Memory
Imports System
Imports System.Threading.Tasks
' Setup OpenAI
Private azureEndpoint = "<<enter your azure endpoint here>>"
Private apiKey = "<<enter your azure API key here>>"
Private builder = Kernel.CreateBuilder().AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey).AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey)
Private kernel = builder.Build()
' Setup Memory
Private memory_builder = (New MemoryBuilder()).WithMemoryStore(New VolatileMemoryStore()).WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
Private memory = memory_builder.Build()
' Initialize IronAI
IronDocumentAI.Initialize(kernel, memory)
License.LicenseKey = "<<enter your IronPdf license key here"
' Import PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile("wikipedia.pdf")
' Summarize the document
Console.WriteLine("Please wait while I summarize the document...")
Dim summary As String = Await pdf.Summarize() ' optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}" & vbLf & vbLf)
程式碼初始化Semantic Kernel和記憶儲存。 記憶儲存在持續查詢中維持上下文。 選擇如下:
VolatileMemoryStore:用於開發和測試的記憶體儲存ChromaMemoryStore:用於生產的持久矢量資料庫- 其他儲存:Azure認知搜索、Qdrant等
用於生產時,實現錯誤處理,並新增自定義日誌記錄來追蹤AI操作。 探索異步和多執行緒來同時處理多個文件。
摘要輸出看起來如何?
摘要提供了簡明的文件概覽,提取了主要主題、重要事實和相關細節。 AI模型識別並優先考慮重要內容,使您快速理解冗長文件。
我如何持續查詢PDF?
單次查詢不適合所有情境。 IronPdf.Extensions.AI套件提供Query方法以進行持續查詢。 構建會話介面、研究工具或文件分析應用,其中使用者可對同一文件提出多次問題。
持續查詢保持會話上下文,允許後續問題和澄清。 理想用於:
- 客戶支持系統參照文件
- 法律文件分析需要條款解釋
- 教育應用程式用於研究複雜材料
- 研究工具提取特定資訊
為增強處理,考慮分別提取文字和圖片或實施PDF壓縮,以優化大型文件在AI處理前的狀態。
:path=/static-assets/pdf/content-code-examples/how-to/openai-query.cs
using IronPdf;
using IronPdf.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Threading.Tasks;
// Setup OpenAI
var azureEndpoint = "<<enter your azure endpoint here>>";
var apiKey = "<<enter your azure API key here>>";
var builder = Kernel.CreateBuilder()
.AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
.AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey);
var kernel = builder.Build();
// Setup Memory
var memory_builder = new MemoryBuilder()
// optionally use new ChromaMemoryStore("http://127.0.0.1:8000") (see https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/09-memory-with-chroma.ipynb)
.WithMemoryStore(new VolatileMemoryStore())
.WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey);
var memory = memory_builder.Build();
// Initialize IronAI
IronDocumentAI.Initialize(kernel, memory);
License.LicenseKey = "<<enter your IronPdf license key here";
// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");
// Continuous query
while (true)
{
Console.Write("User Input: ");
var response = await pdf.Query(Console.ReadLine());
Console.WriteLine($"\n{response}");
}
Imports Microsoft.VisualBasic
Imports IronPdf
Imports IronPdf.AI
Imports Microsoft.SemanticKernel
Imports Microsoft.SemanticKernel.Connectors.OpenAI
Imports Microsoft.SemanticKernel.Memory
Imports System
Imports System.Threading.Tasks
' Setup OpenAI
Private azureEndpoint = "<<enter your azure endpoint here>>"
Private apiKey = "<<enter your azure API key here>>"
Private builder = Kernel.CreateBuilder().AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey).AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey)
Private kernel = builder.Build()
' Setup Memory
Private memory_builder = (New MemoryBuilder()).WithMemoryStore(New VolatileMemoryStore()).WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
Private memory = memory_builder.Build()
' Initialize IronAI
IronDocumentAI.Initialize(kernel, memory)
License.LicenseKey = "<<enter your IronPdf license key here"
' Import PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile("wikipedia.pdf")
' Continuous query
Do
Console.Write("User Input: ")
Dim response = Await pdf.Query(Console.ReadLine())
Console.WriteLine($vbLf & "{response}")
Loop
持續查詢系統使用嵌入技術理解問題語義,提供準確、有上下文的回應。 每次查詢都針對文件內容進行處理,由AI保持會話歷史,以提供日益相關的答案。
為了在處理大型文件或同時出現多個使用者時獲得最佳性能,實施快取策略,並探索IronPDF的性能優化技術。 考慮速率限制以及適當的授權金鑰管理,以進行生產部署。
處理敏感文件時,實施適當的安全措施。 在AI處理前後,IronPDF提供各種安全和加密選項以保護PDF。
常見問題
PDF 處理的 AI 擴展的目的是什麼?
IronPdf.Extensions.AI NuGet 套件使得在 C# 應用中啟用 OpenAI 驅動的 PDF 增強。它允許您使用 Microsoft Semantic Kernel 為您的 PDF 新增摘要、查詢和記憶功能,使用最少的程式碼,自動從文件中提取見解並回答問題。
AI 驅動的 PDF 處理的主要使用案例是什麼?
IronPDF 的 AI 擴展非常適合處理大量文件、從報告中提取資訊、建立快速審核摘要和建立智能的文件管理系統。整合支持單次摘要和連續查詢以用於各種應用。
如何快速使用 OpenAI 摘要 PDF?
using IronPDF 的 AI 擴展,您可以僅用一行程式碼就能摘要任何 PDF:await IronPdf.AI.PdfAIEngine.Summarize("input.pdf", "summary.txt", azureEndpoint, azureApiKey)。這種簡單的實現使得從 PDF 文件生成摘要變得容易。
我需要安裝哪些套件來進行 AI PDF 處理?
要實現 IronPDF 的 AI 功能,您需要三個套件:IronPdf(主要的 PDF 程式庫)、IronPdf.Extensions.AI(AI 擴展)和 Microsoft.SemanticKernel.Plugins.Memory(用於語義 kernel 功能)。
using OpenAI 與 PDF 之前的先決條件是什麼?
在使用 IronPDF 實現 AI 功能之前,您需要通過具有 Azure OpenAI 服務存取權限的 Azure 訂閱設置 Azure OpenAI。該服務提供企業級的安全性和生產應用的合規性,並需要 Azure 結點和 API 金鑰。
AI PDF 處理的最小工作流程是什麼?
using IronPDF 的最小工作流程由 5 個步驟組成:1)下載 C# 程式庫,2)準備 Azure 結點和 API 金鑰,3)匯入目標 PDF 文件,4)使用 Summarize 方法生成摘要,5)使用 Query 方法進行連續查詢。
AI 擴展如何與 Microsoft Semantic Kernel 整合?
IronPDF 的 AI 擴展基於 Microsoft Semantic Kernel 構建,其簡化了 .NET 應用中的 AI 服務整合。該 SDK 處理與 OpenAI 服務連接的複雜性,並提供針對 PDF 的 AI 操作的簡單 API。

