-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (131 loc) · 4.72 KB
/
Copy pathindex.js
File metadata and controls
160 lines (131 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require('dotenv').config()
const Groq = require("groq-sdk");
const pdf = require('pdf-parse');
const prompts = require('prompts');
const fs = require('fs/promises');
const path = require('path');
prompts.override(require('yargs').argv);
// Check if ENV key is set
if (!process.env.GROQ_API_KEY) {
console.log('No Groq API key found.')
return
}
// Initialize the Groq client
const groq = new Groq({
apiKey: process.env.GROQ_API_KEY,
});
// Run the script in an async function
(async () => {
// Groq's available models you can add as much model as you want and test them
//https://console.groq.com/docs/models
const models = [
{ title: 'llama-3.3-70b-versatile', value: 'llama-3.3-70b-versatile' },
{ title: 'LLaMA2 70B (4K context)', value: 'llama2-70b-4096' }
];
// Lets the user pick a model
const modelSelected = await prompts([
{
type: 'select',
name: 'model',
message: 'Pick a model from Groq',
choices: models,
initial: 0
}
]);
// If no model is selected, throw an error
if (!modelSelected.model) {
console.log('No model selected. Please try again.')
return
}
// Get all the files in the files folder
let allFiles = await fs.readdir('./files')
let pdfs = allFiles.filter(file => path.extname(file).toLowerCase() === '.pdf');
// Map all the pdfs to an object with a title and value
let choices = pdfs.map(pdf => {
return { title: pdf, value: `${pdf}` }
})
// If the folder files has no pdf file extensions, throw an error
if (choices.length === 0) {
console.log('No PDFs found in ./files folder. Please add some PDFs and try again.')
return
}
// Lets the user pick a PDF to summarize
const pdfSelected = await prompts([
{
type: 'select',
name: 'filename',
message: 'Pick a PDF to summarize',
choices,
}
]);
// If no pdf selected, throw an error
if (!pdfSelected.filename) {
console.log('No PDF selected. Please try again.')
return
}
// Add the path to the pdfSelected object
pdfSelected.path = `./files/${pdfSelected.filename}`
// The dataBuffer is a buffer instance, so we need to convert it to a string
const dataBuffer = await fs.readFile(pdfSelected.path);
// The pdf function returns a promise, so we need to await it
pdfSelected.text = await pdf(dataBuffer).then(data => data.text)
// Separate pdfSelected.text into 500 character chunks
pdfSelected.chunks = []
const chunkSize = 500 * 4;
// Loop through the PDF text and add each chunk to the chunks array
for (let i = 0; i < pdfSelected.text.length; i += chunkSize) {
pdfSelected.chunks.push(pdfSelected.text.slice(i, i + chunkSize));
}
console.log (`${pdfSelected.filename} has ${pdfSelected.chunks.length} pages and ${pdfSelected.text.length} characteres. This is around ${pdfSelected.text.length/4} tokens.`)
// Create an array to hold all the AI responses
pdfSelected.summaries = []
// Loop through each chunk and perform an AI lookup
for (let i = 0; i < pdfSelected.chunks.length; i++) {
const completion = await groq.chat.completions.create({
messages: [
{
role: "system",
content: "You are a tool that Summarizes PDF. This tool is a application script that converts inputs PDF content and outputs a list the main points. Do not communicate with the user directly."
},
{
role: "user",
content: `PDF content: ${pdfSelected.chunks[i]}`
}
],
model: modelSelected.model,
max_tokens: 500,
temperature: 0.1,
top_p: 0.9
});
// Add the AI response to the responses array
pdfSelected.summaries.push(completion.choices[0].message.content)
}
// combine the summaries array into one string then export
let summary = pdfSelected.summaries.join('\n\n')
// Summary of the summary if the summary is too long (over 500 characters)
if (summary.length > 500) {
const completion = await groq.chat.completions.create({
messages: [
{
role: "system",
content: "You are a tool that Summarizes PDF. This tool is a application script that converts inputs PDF content and outputs a list the main points. Do not communicate with the user directly."
},
{
role: "user",
content: `PDF content: ${summary}`
}
],
model: modelSelected.model,
max_tokens: 500,
temperature: 0.1,
top_p: 0.9
});
// If response is received, update summary with new summary
if(completion.choices[0].message.content){
summary = completion.choices[0].message.content
}
}
// using fs export the summaries to a file
fs.writeFile(`./files/${pdfSelected.filename.replace('.pdf', '')}.txt`, summary);
console.log(`Summary saved to ./files/${pdfSelected.filename.replace('.pdf', '')}.txt`)
})();