-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproduction.php
More file actions
183 lines (161 loc) · 7.32 KB
/
production.php
File metadata and controls
183 lines (161 loc) · 7.32 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
require 'vendor/autoload.php';
require 'mindee.php';
use Smalot\PdfParser\Parser;
use Dotenv\Dotenv;
use OpenAI\Client;
// Initialize Dotenv and load .env file
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Retrieve the OPENAI_API_KEY from environment
$openaiKey = $_ENV['OPENAI_API_KEY'];
$mindeeKey = $_ENV['MINDEE_API_KEY'];
header('Content-type: application/json');
function ExtractTextFromCopyablePDF($inputFile)
{
// Initialize the parser
$parser = new Parser();
// Parse the PDF file
$pdf = $parser->parseFile($inputFile);
// Extract the text from the document
$text = $pdf->getText();
// Return the extracted text
return $text;
}
function OpenAIFunctionCalling($extractedText, $openaiKey, $outputFile, $eurovalue = null)
{
error_log("OpenAIFunctionCalling())");
$client = OpenAI::client($openaiKey);
$prompt = "Extract the emitter and recipient company information, invoice number and invoice date are date and order number, currency, and line items.\nSepatrate vat country code if present at the beginning and vat number.\n\nFrom the following invoice text:\n\n" . $extractedText;
$description = 'Extracts emitter and recipient company information, invoice number, date, currency, and line items from an invoice. Sepatrate vat country code if present at the beginning and vat number. Add to the row description the service period, if any. Split emitter address into address, city, zip code and country. Transform country name in international country code. Convert any utf8 charachet to plan and convert any currency symbol to currency abbreviation (EUR, USD, ...). Transform all dated to forma yyyy-mm-dd. BluCloud if present it the the recipient of the invoice.';
$functions = [
[
'name' => 'extract_invoice_data',
'description' => $description,
'parameters' => [
'type' => 'object',
'properties' => [
'emitter' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'address' => ['type' => 'string'],
'city' => ['type' => 'string'],
'zip_code' => ['type' => 'string'],
'country' => ['type' => 'string'],
'vat_country' => ['type' => 'string'],
'vat_number' => ['type' => 'string'],
'contact' => [
'type' => 'object',
'properties' => [
'phone' => ['type' => 'string'],
'email' => ['type' => 'string'],
],
],
],
],
'recipient' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'address' => ['type' => 'string'],
'vat_country' => ['type' => 'string'],
'vat_number' => ['type' => 'string'],
'contact' => [
'type' => 'object',
'properties' => [
'phone' => ['type' => 'string'],
'email' => ['type' => 'string'],
],
],
],
],
'invoice' => [
'type' => 'object',
'properties' => [
'number' => ['type' => 'string'],
'date' => ['type' => 'string', 'format' => 'date', 'description' => 'Invoice date in YYYY-MM-DD format',],
'currency' => ['type' => 'string'],
],
],
'invoice_lines' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'quantity' => ['type' => 'integer'],
'description' => ['type' => 'string'],
'unit_price' => ['type' => 'number'],
'vat_rate' => ['type' => 'number'],
'vat_amount' => ['type' => 'number'],
'total' => ['type' => 'number'],
'period' => [
'type' => 'object',
'properties' => [
'from' => ['type' => 'string', 'format' => 'date-time'],
'to' => ['type' => 'string', 'format' => 'date-time'],
],
],
],
],
],
'totals' => [
'type' => 'object',
'properties' => [
'subtotal' => ['type' => 'number'],
'vat' => ['type' => 'number'],
'total' => ['type' => 'number'],
],
],
'notes' => ['type' => 'string'],
],
],
],
];
$response = $client->chat()->create([
'model' => 'gpt-4',
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => $prompt],
],
'functions' => $functions,
'function_call' => ['name' => 'extract_invoice_data'],
]);
$function_call = $response['choices'][0]['message']['function_call'];
$invoice_data = json_decode($function_call['arguments'], true);
if (isset($invoice_data['invoice']['date'])) {
try {
$date = new DateTime($invoice_data['invoice']['date']);
// Reformat the date to 'Y-m-d' (YYYY-MM-DD)
$invoice_data['invoice']['date'] = $date->format('Y-m-d');
} catch (Exception $e) {
// Handle the exception if the date is invalid
echo 'Invalid date format: ', $e->getMessage();
}
}
file_put_contents($outputFile, json_encode($invoice_data, JSON_PRETTY_PRINT));
//echo 'Generated JSON file successfully.';
echo json_encode($invoice_data, JSON_PRETTY_PRINT);
die();
}
$filecontent = $_POST['file'];
$eurovalue = null;
if (isset($_POST['eurovalue'])){
$eurovalue = $_POST['eurovalue'];
}
$inputFile = "example.pdf";
file_put_contents($inputFile, $filecontent);
$outputFile = "example.json";
$extractedText = ExtractTextFromCopyablePDF($inputFile);
//echo "{$extractedText}\n";
//echo strlen($extractedText) . "\n";
if (strlen($extractedText) > 50) {
//echo "Given PDF file is copyable file.\n";
OpenAIFunctionCalling($extractedText, $openaiKey, $outputFile, $eurovalue);
} else {
//echo "Given PDF file is non-copyable file.\n";
$extractedText = CustomizedMindeeAPI($mindeeKey, $inputFile);
//echo "{$extractedText}\n";
//echo strlen($extractedText) . "\n";
OpenAIFunctionCalling($extractedText, $openaiKey, $outputFile, $eurovalue);
}