Use ChatGPT
The Documentize PDF Manager for .NET plugin is a powerful tool designed to integrate the ChatGPT API with PDF applications. This plugin allows developers to generate chat responses based on input messages and save the output in PDF format, making it suitable for creating conversational interfaces or analysis reports directly within PDF documents.
Generate Chat Responses
To generate chat responses and save them to a PDF file using the ChatGPT plugin, follow these steps:
- Create an instance of the
ChatGptRequestOptionsclass to configure the request options. - Add input and output PDF files.
- Set the API key and specify parameters such as maximum token count and the query for the ChatGPT model.
- Run the
CreatePdfByChatGptRequestAsyncmethod to generate the chat completion.
1var options = new ChatGptRequestOptions();
2// Set output file path
3options.AddOutput(new FileDataSource("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.Adding System and User Messages
To create a more interactive conversation, you can add both system and user messages. These messages help shape the conversation context.
- Add a system message that sets the context for ChatGPT.
- Add a user message that represents the user’s input for the conversation.
1var options = new ChatGptRequestOptions();
2// Set output file path
3options.AddOutput(new FileDataSource("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 FileDataSource("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.Key Features:
- Chat Completions: Generate responses using the ChatGPT API based on custom input.
- System & User Messages: Provide both system context and user input to create dynamic conversations.
- PDF Output: Save generated chat completions in a structured PDF file for further use.
- Asynchronous Processing: Ensure responsive applications by processing chat completions asynchronously.