-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
86 lines (67 loc) · 2.4 KB
/
index.js
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
const fs = require("fs");
const PDFParser = require("pdf2json");
const pdfParser = new PDFParser(this,1);
const { Configuration, OpenAIApi } = require("openai");
const myFunction = async (input) => {
const output = {};
let pdf = input.pdf;
let key = input.key;
let model = input.model;
let prompt = input.prompt;
let completion = input.completion;
let temperature = input.temperature;
let max_tokens = input.max_tokens;
let top_p = input.top_p;
let frequency_penalty = input.frequency_penalty;
let presence_penalty = input.presence_penalty;
async function coupa(input) {
pdfParser.on("pdfParser_dataError", errData => console.error(errData.parserError) );
pdfParser.loadPDF(pdf);
let patient = await new Promise(async (resolve, reject) => {
pdfParser.on("pdfParser_dataReady", pdfData => {
resolve( pdfParser.getRawTextContent().trim());
});
});
const configuration = new Configuration({
apiKey: key,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: model,
prompt: prompt + "\n\n" + patient + "\n\n" + completion,
temperature: temperature,
max_tokens: max_tokens,
top_p: top_p,
frequency_penalty: frequency_penalty,
presence_penalty: presence_penalty,
});
console.log(response.data.choices[0].text)
return(response.data.choices[0].text)
}
output.response = await coupa(input);
return output;
}
exports.main = async (input) => {
return await myFunction(input);
}
const myFunction1 = async () => {
const input = {};
let fileInputName = 'invoice1.pdf';
let pdf = fs.readFileSync(fileInputName);
const buff = Buffer.from(pdf);
const base64 = buff.toString('base64');
input.pdf = base64;
input.key = 'OPENAI_KEY';
input.model = 'text-davinci-002';
input.prompt = 'Extract invoice number, invoice date, total amount in sgd:';
input.completion = 'invoice number, invoice date, total amount in sgd:';
input.temperature = 0.7;
input.max_tokens = 453;
input.top_p = 1;
input.frequency_penalty = 0;
input.presence_penalty = 0;
var res = await myFunction(input);
console.log("Result:")
console.log(res)
};
myFunction1();