使用 ChatGPT

Documentize PDF Manager for .NET 插件是一个强大的工具,旨在将 ChatGPT API 与 PDF 应用程序集成。该插件允许开发者基于输入消息生成聊天回复,并将输出保存为 PDF 格式,适用于在 PDF 文档中直接创建对话界面或分析报告。

生成聊天回复

要使用 ChatGPT 插件生成聊天回复并将其保存为 PDF 文件,请按照以下步骤操作:

  1. 创建 ChatGptRequestOptions 类的实例,以配置请求选项。
  2. 添加输入和输出 PDF 文件。
  3. 设置 API 密钥并指定参数,如最大 token 数量和 ChatGPT 模型的查询。
  4. 运行 CreatePdfByChatGptRequestAsync 方法生成聊天完成内容。
 1var options = new ChatGptRequestOptions();
 2// Set output file path
 3options.AddOutput(new FileData("path_to_result_pdf_file.pdf")); 
 4options.ApiKey = "Your API key."; // You need to provide the key to access the API.
 5options.MaxTokens = 1000; // The maximum number of tokens to generate in the chat completion.
 6
 7// Add the request messages.
 8options.Messages.Add(new Message
 9{
10    Content = "You are a helpful assistant.",
11    Role = Role.System
12});
13options.Messages.Add(new Message
14{
15    Content = "What is the biggest pizza diameter ever made?",
16    Role = Role.User
17});
18
19// Process the request.
20var result = await PdfManager.CreatePdfByChatGptRequestAsync(options);
21
22var fileResultPath = result.ResultCollection[0].Data;
23var chatCompletionObject = result.ResultCollection[1].Data as ChatCompletion; // The ChatGPT API chat completion object.

添加系统消息和用户消息

为了创建更具交互性的对话,您可以同时添加系统消息和用户消息。这些消息有助于塑造对话上下文。

  1. 添加系统消息以设定 ChatGPT 的上下文。
  2. 添加用户消息,代表用户在对话中的输入。
 1var options = new ChatGptRequestOptions();
 2// Set output file path
 3options.AddOutput(new FileData("path_to_result_pdf_file.pdf"));
 4
 5// Add the PDF text source.
 6// In case of multiple sources, the text from each document will be added to the request message collection
 7// as a separate message with the role "user".
 8options.AddInput(new FileData("TextSource.pdf"));
 9
10options.ApiKey = "Your API key."; // You need to provide the key to access the API.
11options.MaxTokens = 1000; // The maximum number of tokens to generate in the chat completion.
12
13// Add the request message.
14// In this case, the system message with Content = "You are a helpful assistant." is added by default.
15// The role of the query message is "user" by default.
16options.Query = "How many letters in the provided text?";
17
18// Process the request.
19var result = await PdfManager.CreatePdfByChatGptRequestAsync(options);
20
21var fileResultPath = result.ResultCollection[0].Data;
22var chatCompletionObject = result.ResultCollection[1].Data as ChatCompletion; // The ChatGPT API chat completion object.

关键特性

  • Chat 完成:基于自定义输入使用 ChatGPT API 生成回复。
  • 系统 & 用户消息:同时提供系统上下文和用户输入,以创建动态对话。
  • PDF 输出:将生成的聊天完成内容保存为结构化的 PDF 文件,以便进一步使用。
  • 异步处理:通过异步方式处理聊天完成,确保应用程序响应流畅。
 中文