如何在 C# IronPDF 中使用 OpenAI for PDF.
IronPDF 的 AI 扩展可在 C# 应用程序中实现 OpenAI 驱动的 PDF 增强功能。 使用 Microsoft Semantic Kernel 以最少的代码添加总结、查询和记忆功能。
OpenAI 是一家人工智能研究实验室,致力于开发先进的人工智能技术。 它提供了可通过 API 访问的强大语言模型,使开发人员能够将人工智能功能集成到他们的应用程序中。
IronPdf.Extensions.AI NuGet包将OpenAI引入PDF处理中:摘要、查询和记忆。 该 SDK 基于 Microsoft Semantic Kernel 开发,可简化 .NET 应用程序中的人工智能服务集成。 从 PDF 文档中自动提取见解、回答问题并生成摘要。
主要用例包括处理大量文件、从报告中提取信息、创建快速审阅摘要以及构建智能文件管理系统。 该集成支持各种应用的一次性汇总和连续查询。 如需了解更多 PDF 功能,请浏览IronPDF 的全面文档或了解从 HTML 创建 PDF 。
快速入门:使用IronPDF和 OpenAI 生成 PDF 摘要
开始使用 IronPDF in C# 将 OpenAI 集成到您的 PDF 处理工作流程中。 本示例演示了只需几行代码就能快速总结 PDF 内容。
最小工作流程(5 个步骤)
- 下载 C# 库以利用 OpenAI for PDF 。
- 准备 OpenAI 的 Azure 端点和 API 密钥
- 导入目标 PDF 文档
- 使用
Summarize方法生成 PDF 摘要 - 使用
Query方法进行连续查询
所需软件包:
在实施人工智能功能之前,请设置 Azure OpenAI。 您需要拥有 Azure OpenAI 服务访问权限的 Azure 订阅。 该服务可为生产应用程序提供企业级安全性和合规性。 详细说明请参见 IronPDF 安装概述。
如何使用 OpenAI 总结 PDF?
要使用 OpenAI 功能,请使用您的 Azure 端点和 API 密钥配置语义内核。 导入PDF文档并使用Summarize方法生成总结。
摘要功能适用于各种 PDF 类型:
- 扫描文件(与 OCR 结合使用时)
- 多列复杂布局
- 包含图片和表格的文件
IronPDF 提取文本内容并通过人工智能模型进行处理。 有关不同格式,请参阅 将 DOCX 转换为 PDF 或 将 Markdown 转换为 PDF 。
[{我:(
注意:您可能会遇到SKEXP0050错误,因为语义内核方法是实验性的。 将其添加到您的.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)
代码初始化语义内核和内存存储。 在连续查询过程中,内存存储要保持上下文一致。 请选择
VolatileMemoryStore:用于开发和测试的内存存储ChromaMemoryStore:用于生产的持久性矢量数据库- 其他存储:Azure认知搜索、Qdrant等
对于生产,要实现错误处理和自定义日志以跟踪 IronPDF 操作。 探索 async 和多线程,以便同时处理多个文档。
摘要输出是什么样的?
摘要提供了简明的文档概述,提取了主要话题、重要事实和相关细节。 人工智能模型可以识别并优先处理重要内容,从而快速理解冗长的文档。
如何连续查询 PDF?
单一查询并不适合所有情况。 Query方法。 构建对话界面、研究工具或文档分析应用程序,用户可就同一文档提出多个问题。
持续查询可保持对话语境,允许后续问题和澄清。 适用于:
- 客户支持系统参考文档
- 需要解释条款的法律文件分析
- 学习复杂材料的教育应用程序
- 提取特定信息的研究工具
为增强处理能力,请考虑分别提取文本和图像,或实施PDF压缩,以便在进行 IronPDF 处理之前优化大型文档。
: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)
连续查询系统使用嵌入式技术来理解问题的语义,从而提供准确、符合上下文的回复。 每次查询都会针对文档内容进行处理,人工智能会维护对话历史记录,以提供相关性越来越高的答案。
为实现大型文档或并发用户的最佳性能,请实施缓存策略并探索 IronPDF的性能优化技术。 考虑对生产部署进行速率限制和适当的许可证密钥管理。
在处理敏感文件时,应采取适当的安全措施。 IronPDF 提供各种安全和加密选项,可在 AI 处理前后保护 PDF。
常见问题解答
AI 扩展用于 PDF 处理的目的是什么?
IronPdf.Extensions.AI NuGet 软件包可在 C# 应用程序中实现 OpenAI 驱动的 PDF 增强功能。它允许您使用微软语义内核(Microsoft Semantic Kernel)以最少的代码为您的 PDF 添加摘要、查询和记忆功能,帮助自动从文档中提取见解和回答问题。
人工智能驱动的 PDF 处理有哪些关键用例?
IronPDF 的人工智能扩展非常适合处理大量文档、从报告中提取信息、创建快速审阅摘要以及构建智能文档管理系统。该集成支持各种应用的一次性汇总和连续查询。
如何使用 OpenAI 快速总结 PDF?
有了 IronPDF 的 AI 扩展,您只需一行代码就能汇总任何 PDF:await IronPdf.AI.PdfAIEngine.Summarize("input.pdf", "summary.txt", azureEndpoint, azureApiKey)。这种简单的实现方式可轻松从 PDF 文档生成摘要。
AI PDF 处理需要安装哪些软件包?
要使用 IronPDF 实现人工智能功能,您需要三个软件包:IronPDF(PDF主库)、IronPdf.Extensions.AI(AI扩展)和Microsoft.SemanticKernel.Plugins.Memory(用于语义内核功能)。
将 OpenAI 与 PDF 结合使用的前提条件是什么?
在使用 IronPDF 实现人工智能功能之前,需要使用具有 Azure OpenAI 服务访问权限的 Azure 订阅设置 Azure OpenAI。该服务为生产应用程序提供企业级安全性和合规性,需要一个 Azure 端点和 API 密钥。
AI PDF 处理的最基本工作流程是什么?
IronPDF 的最小工作流程包括 5 个步骤:1)下载 C# 库;2)准备 Azure 端点和 API 密钥;3)导入目标 PDF 文档;4)使用 Summarize 方法生成摘要;5)使用 Query 方法进行连续查询。
人工智能扩展如何与 Microsoft Semantic Kernel 集成?
IronPDF for .NET 的人工智能扩展基于微软语义内核(Microsoft Semantic Kernel)构建,简化了.NET 应用程序中的人工智能服务集成。该 SDK 可处理连接 OpenAI 服务的复杂性,并为特定于 PDF 的 AI 操作提供直接的 API。

