如何在 .NET 中使用 ChatGPT 处理 PDF 文本
这篇文章展示了如何在 .NET 中将 ChatGPT 与 PDF 工作流集成,使用 Aspose.PDF 聊天插件. 您将学习如何从 PDF 中提取文本,通过 Chat GPT 中处理它,并对新或现有 PDF 的回复写作 - 理想用于文档总结、自动评论或人工智能驱动的内容丰富。
现实世界问题
手动从 PDF 文件中提取有意义的信息、总结或答案需要时间,开发人员需要一个简化的方式来将PDF 内容与 ChatGPT 连接到自动处理和反馈,节省时间和提高生产力。
解决方案概述
Aspose.PDF ChatGPT Plugin for .NET 允许您将 PDF 内容发送到 Chat GPT,接收完成或总结,并将答案保存为新的 PDF - 所有具有最小代码。
原則
- Visual Studio 2019 或以后
- .NET 6.0 或更高
- Aspose.PDF for .NET 通过 NuGet 安装
- OpenAI API 为 ChatGPT 密钥
PM> Install-Package Aspose.PDF
步骤实施
步骤 1: 安装和设置 Aspose.PDF
添加所需名称空间:
using Aspose.Pdf.Plugins;
using System.IO;
using System.Threading.Tasks;
步骤2:编写 PDF 文本或文件
指定您的输入PDF和所需输出PDF文件:
string inputPdfPath = @"C:\Samples\source.pdf";
string outputPdfPath = @"C:\Samples\ChatGPT_output.pdf";
步骤3:设置 ChatGPT 请求选项
设置您的 API 密钥、快点和输出路径. 您可以手动从 PDF 中提取文本,或者让插件使用整个 PDF 文件作为输入:
using (var plugin = new PdfChatGpt())
{
var options = new PdfChatGptRequestOptions();
options.AddInput(new FileDataSource(inputPdfPath)); // Use full PDF text as message
options.AddOutput(new FileDataSource(outputPdfPath)); // Path for the output PDF
options.ApiKey = "Your-OpenAI-API-Key"; // REQUIRED: Your API key for ChatGPT
options.MaxTokens = 1000; // Limit response size
options.Query = "Summarize the contents of this document."; // Or ask any question about the PDF
您还可以添加自定义对话消息(系统/用户角色):
options.Messages.Add(new Message
{
Content = "You are a document assistant. Summarize the provided PDF text.",
Role = Role.System
});
options.Messages.Add(new Message
{
Content = "What are the main topics covered in this PDF?",
Role = Role.User
});
步骤4:向 ChatGPT 发送请求并保存结果
无同步处理请求,接收新的 PDF 文件路径和 ChatGPT 回复:
// Process the request and await the result
var result = await plugin.ProcessAsync(options);
var fileResultPath = result.ResultCollection[0].Data; // Path to the output PDF
var chatCompletion = result.ResultCollection[1].Data as ChatCompletion; // ChatGPT API object
// Access the generated response text if needed:
var firstChoice = chatCompletion?.Choices?.FirstOrDefault();
var responseText = firstChoice?.Message?.Content;
Console.WriteLine($"PDF generated at: {fileResultPath}");
Console.WriteLine("ChatGPT response:");
Console.WriteLine(responseText);
}
步骤5:错误处理和Async使用
始终插入 async 通话并处理 API / 网络错误:
try
{
// (Code above)
}
catch (Exception ex)
{
Console.WriteLine($"Error during ChatGPT PDF processing: {ex.Message}");
}
完整实施例子
using Aspose.Pdf.Plugins;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
string inputPdfPath = @"C:\Samples\source.pdf";
string outputPdfPath = @"C:\Samples\ChatGPT_output.pdf";
using (var plugin = new PdfChatGpt())
{
var options = new PdfChatGptRequestOptions();
options.AddInput(new FileDataSource(inputPdfPath));
options.AddOutput(new FileDataSource(outputPdfPath));
options.ApiKey = "Your-OpenAI-API-Key";
options.MaxTokens = 1000;
options.Query = "Summarize the content of this PDF document.";
try
{
var result = await plugin.ProcessAsync(options);
var fileResultPath = result.ResultCollection[0].Data;
var chatCompletion = result.ResultCollection[1].Data as ChatCompletion;
var firstChoice = chatCompletion?.Choices?.FirstOrDefault();
var responseText = firstChoice?.Message?.Content;
Console.WriteLine($"PDF generated at: {fileResultPath}");
Console.WriteLine("ChatGPT response:");
Console.WriteLine(responseText);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
使用案例和应用程序
PDF 摘要和 AI 发明评论
使用 ChatGPT 自动总结合同、报告或研究文件,并将回复保存到 PDF 中。
自动 Q&A 或 Insights 提取
向 ChatGPT 发送自定义提示,以便从 PDF 文件中提取答案、表或关键数据。
Batch 文档丰富
将其集成到工作流中,以处理许多PDF文件,产生基于聊天的回复或自动通知。
共同挑战与解决方案
挑战: API 限制或响应曲线
解決方案: 調整 MaxTokens
和 Query
为最佳结果;如果需要,将大型PDF分成碎片。
挑战:安全 API 关键管理
解决方案: 安全存储 API 密钥(环境变量、漏洞)并避免在生产中硬编码。
绩效考虑
- Batch PDF 输入和促销,以尽量减少 API 通话。
- 使用 async 工作流以保持您的应用程序响应性。
- Tune token 限制来管理 API 成本。
最佳实践
- 总是检查 PDF 输出和 ChatGPT 回复的准确性。
- 将促销和消息角色定制为目标结果。
- 安全管理 API 认证。
- 在 async 操作中记录和轻松处理错误。
先进的场景
- 使用多个 PDF 或即时变量在一个圆圈。
- 组合系统/用户信息为复杂的背景或任务。
- 为下流处理或工作流提供 PDF 输出。
结论
Aspose.PDF ChatGPT Plugin for .NET 允许开发人员自动化文档分析、总结和互动的 PDF 处理,使用 ChattgPT 的功率 - 直接在他们的 .net 应用程序中。