Skip to content

Commit f2016d9

Browse files
authored
Merge pull request #289 from aspose-pdf/PDFNET-58347_AI_Copilot
AI Copilot (PDFNET-58347)
2 parents c3de976 + f4d53c9 commit f2016d9

File tree

2 files changed

+199
-1
lines changed

2 files changed

+199
-1
lines changed

net/advanced-operations/_index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ type: docs
55
weight: 90
66
url: /net/advanced-operations/
77
description: Aspose.PDF can perform not only simple and easy tasks but also cope with more complex goals. Check the next section for advanced users and developers.
8-
lastmod: "2022-02-17"
8+
lastmod: "2024-10-22"
99
sitemap:
1010
changefreq: "weekly"
1111
priority: 0.7
@@ -90,6 +90,7 @@ You'll learn different ways to:
9090
- [Working with XML](/pdf/net/working-with-xml) - construct PDF documents based on the XML structure
9191
- [Compare PDF documents](/pdf/net/compare-pdf-documents/) - possible to compare PDF documents content
9292
- [Navigation and Interaction](/pdf/net/navigation-and-interaction/) - deal with actions, bookmarks, navigate pages
93+
- [AI Copilot](/pdf/net/ai-copilot/) - allow to process PDF documents using LLMs from different providers
9394
- [Annotations](/pdf/net/annotations/) - Annotations allow users to add custom content on PDF pages. You can add, delete and modify the annotation from the PDF documents.
9495
- [Artifacts](/pdf/net/artifacts/) - deal with watermarks and other special objects in PDF
9596
- [Accessibility. Tagged PDF](/pdf/net/accessibility-tagged-pdf/) - Tagging is essential for PDF accessibility. Aspose.PDF allows to add tags into PDF and establish logical reading order and to provide a means for indicating structure and type.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
---
2+
title: Working with PDF Documents using AI Copilot
3+
linktitle: Working with PDF Documents using AI Copilot
4+
type: docs
5+
weight: 10
6+
url: /net/ai-copilot/
7+
description: This article describes how AI Copilot can be used to process the PDF document with Aspose.PDF library.
8+
sitemap:
9+
changefreq: "monthly"
10+
priority: 0.8
11+
lastmod: "2024-10-22"
12+
---
13+
{{% alert color="primary" %}}
14+
**Aspose.PDF AI Copilot API** designed to allows users to process PDF documents using LLMs from different providers. This [API](https://reference.aspose.com/pdf/net/aspose.pdf/) will help users in building chatbot applications and integrating PDF solutions with LLMs.
15+
{{% /alert %}}
16+
17+
## Key Features:
18+
19+
* Document summary.
20+
* Chat with documents.
21+
* Get images from documents and provide descriptions.
22+
23+
## Examples:
24+
25+
Currently, the following copilots available:
26+
27+
**OpenAI Summary** allows users to generate summaries from documents. It provides a convenient way to create summaries by configuring options such as the model, temperature, number of tokens, model instructions, document attachments and others. The copilot can asynchronously generate summaries as text, documents and save the summaries in various formats. The provided demo code showcases the creation of an OpenAI client, the configuration of copilot options and the usage of the SummaryCopilot to generate and save summaries.
28+
29+
```cs
30+
// Create AI client.
31+
var openAiClient = OpenAIClient
32+
.CreateWithApiKey(ApiKey) // Create OpenAI client with the API key.
33+
.WithProject("proj_123") // Configure optional parameters.
34+
.Build();
35+
36+
// Create copilot options.
37+
var options = OpenAISummaryCopilotOptions
38+
.Create() // Create options like this, or...
39+
//.Create(options => { options.Model = OpenAIModels.Gpt35Turbo; }) // ...create using delegate.
40+
.WithTemperature(0.5) // Configure other optional parameters.
41+
.WithDocument("DocumentInputPath") // .WithDocument methods allows to add text, pdf and paths to documents.
42+
.WithDocuments(new List<TextDocument>()); // .WithDocuments methods allows to add text, pdf and path collections.
43+
44+
// Create summary copilot.
45+
var summaryCopilot = AICopilotFactory.CreateSummaryCopilot(openAiClient, options);
46+
47+
// Get summary text.
48+
string summaryText = await summaryCopilot.GetSummaryAsync();
49+
50+
// Get summary document.
51+
Document summaryDocument = await summaryCopilot.GetSummaryDocumentAsync();
52+
53+
// Get summary document with page info.
54+
Document summaryDocumentWithPageInfo = await summaryCopilot.GetSummaryDocumentAsync(new PageInfo());
55+
56+
// Save summary as PDF document.
57+
await summaryCopilot.SaveSummaryAsync("outputPath");
58+
```
59+
60+
**OpenAI Chat** is an AI copilot designed for chat interactions with documents. It facilitates generating responses to user queries and managing context. Users can configure the copilot options, such as the model, temperature, number of tokens, model instructions, document attachments and others. The copilot can provide responses to single or multiple queries, save responses in various formats, save and delete the context.
61+
62+
The provided code demonstrates the creation of an OpenAI client, configuration of ChatCopilot options and usage of the ChatCopilot to interact with user queries and manage context.
63+
64+
```cs
65+
// Create AI client.
66+
var openAiClient = OpenAIClient
67+
.CreateWithApiKey(ApiKey) // Create OpenAI client with the API key.
68+
.WithProject("proj_123") // Configure optional parameters.
69+
.WithOrganization("org_123")
70+
.Build(); // Build.
71+
72+
// Create copilot options.
73+
var options = OpenAIChatCopilotOptions
74+
.Create() // Create options like this, or...
75+
//.Create(options => { options.Model = OpenAIModels.Gpt35Turbo; }) // ...create using delegate.
76+
.WithModel(OpenAIModels.Gpt35Turbo) // Configure other optional parameters.
77+
.WithTemperature(0.5)
78+
.WithTopP(1)
79+
.WithDocument("DocumentInputPath") // Attach documents using .WithDocument(s) methods allows to add text, pdf and paths to documents.
80+
.WithContextBackupJsonPath("PathToContextBackup") // Supply context backup to resume the conversation session.
81+
.WithRestoreContextFromBackup(true); // If set to true, the context
82+
83+
// Create summary copilot.
84+
var chatCopilot = AICopilotFactory.CreateChatCopilot(openAiClient, options);
85+
86+
// Get response on a user query.
87+
string copilotResponse1 = await chatCopilot.GetResponseAsync("user message");
88+
89+
// Get response on a list of queries.
90+
string copilotResponse2 = await chatCopilot
91+
.GetResponseAsync(new List<string>
92+
{
93+
"message1",
94+
"message2"
95+
});
96+
97+
// Save summary as PDF document.
98+
await chatCopilot.SaveResponseAsync("message1", "outputPath");
99+
100+
// Save summary as PDF document.
101+
await chatCopilot
102+
.SaveResponseAsync(new List<string>
103+
{
104+
"message1",
105+
"message2"
106+
},
107+
"outputPath");
108+
```
109+
110+
**OpenAI Image Description** is an AI copilot designed for generating image descriptions of images inside PDF documents as well as separate image files. Users can configure the copilot options, such as the model, temperature, number of tokens, model instructions, document attachments and others. The copilot provides the ability to get image descriptions for all attached documents at once.
111+
112+
The provided code snippet demonstrates the creation of an OpenAI client, configuration of ImageDescriptionCopilot options and usage of the copilot to obtain image descriptions for attached documents. Additionally, there is an extension method that allows adding image descriptions to images in the attached documents and saving new documents in the provided directory.
113+
114+
```cs
115+
// Create AI client.
116+
var openAiClient = OpenAIClient
117+
.CreateWithApiKey(ApiKey) // Create OpenAI client with the API key.
118+
.WithProject("proj_123") // Configure optional parameters.
119+
.WithOrganization("org_123")
120+
.Build(); // Build.
121+
122+
// Create copilot options.
123+
var options = OpenAIImageDescriptionCopilotOptions
124+
.Create() // Create options like this, or...
125+
//.Create(options => { options.Model = OpenAIModels.Gpt35Turbo; }) // ...create using delegate.
126+
.WithModel(OpenAIModels.Gpt35Turbo) // Configure other optional parameters.
127+
.WithTemperature(0.5)
128+
.WithTopP(1)
129+
.WithDocument(new PdfDocument // Attach documents.
130+
{
131+
Name = "Another_Pdf_with_images",
132+
Document = new Document(GetInputPath("Pdf_with_images_low_res_bw.pdf"))
133+
})
134+
.WithDocument(GetInputPath("Mona_liza.jpg")) // Attach images
135+
.WithDocument(GetInputPath("Pdf_with_images.pdf")); // Attach document paths.
136+
137+
// Create copilot.
138+
var copilot = AICopilotFactory.CreateImageDescriptionCopilot(openAiClient, options);
139+
140+
// Get Image descriptions.
141+
List<ImageDescriptionResult> imageDescriptions = await copilot.GetImageDescriptionsAsync();
142+
143+
// Use extension method to add image descriptions to attached documents.
144+
await copilot.AddPdfImageDescriptionsAsync("DocumentsOutputDirectory");
145+
```
146+
147+
**Llama Chat** allows the creation of a client to send requests to the Llama chat completion API.
148+
149+
```cs
150+
var llamaClient = LlamaClient
151+
.CreateWithApiKey(ApiKey) // Create Llama client with the API key.
152+
.Build();
153+
154+
var result = await llamaClient.CreateCompletionAsync(new LlamaChatCompletionRequest
155+
{
156+
Messages = new List<ChatMessage>
157+
{
158+
ChatMessage.FromUser("Hello!")
159+
}
160+
});
161+
162+
var response = result.Choices[0].Message.Content; // Hello! How can I assist you today?
163+
```
164+
165+
**Llama Summary** allows client can be used to create the Summary Copilot.
166+
167+
```cs
168+
var llamaClient = LlamaClient
169+
.CreateWithApiKey(ApiKey) // Create Llama client with the API key.
170+
.Build();
171+
172+
// Create copilot options.
173+
var options = LlamaSummaryCopilotOptions
174+
.Create() // Create options like this, or...
175+
//.Create(options => { options.Model = LlamaModels.Llama13BChat; }) // ...create using delegate.
176+
.WithTemperature(0.5) // Configure other optional parameters.
177+
.WithDocument("DocumentInputPath") // .WithDocument methods allow to add text, pdf, and paths to documents.
178+
.WithDocuments(new List<TextDocument>()); // .WithDocuments methods allow to add text, pdf and path collections.
179+
180+
// Create summary copilot.
181+
var summaryCopilot = AICopilotFactory.CreateSummaryCopilot(llamaClient, options);
182+
183+
// Get summary text.
184+
string summaryText = await summaryCopilot.GetSummaryAsync();
185+
186+
// Get summary document.
187+
Document summaryDocument = await summaryCopilot.GetSummaryDocumentAsync();
188+
189+
// Get the summary document with page info.
190+
Document summaryDocumentWithPageInfo = await summaryCopilot.GetSummaryDocumentAsync(new PageInfo());
191+
192+
// Save the summary as a PDF document.
193+
await summaryCopilot.SaveSummaryAsync("outputPath");
194+
195+
// Save summary with specified format.
196+
await summaryCopilot.SaveSummaryAsync("outputPath", SaveFormat.DocX);
197+
```

0 commit comments

Comments
 (0)