-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
188 lines (161 loc) · 6.95 KB
/
index.php
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
184
185
186
187
188
<?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'];
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)
{
$client = OpenAI::client($openaiKey);
$prompt = "Extract the emitter and recipient company information, invoice number, date, currency, and line items from the following invoice text:\n\n" . $extractedText;
$functions = [
[
'name' => 'extract_invoice_data',
'description' => 'Extracts emitter and recipient company information, invoice number, date, currency, and line items from an invoice.',
'parameters' => [
'type' => 'object',
'properties' => [
'emitter' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'address' => ['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_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'],
'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'],
]);
if (isset($response['usage'])) {
$totalTokens = $response['usage']['total_tokens'];
$promptTokens = $response['usage']['prompt_tokens'];
$completionTokens = $response['usage']['completion_tokens'];
echo "Total Tokens: $totalTokens\n";
echo "Prompt Tokens: $promptTokens\n";
echo "Completion Tokens: $completionTokens\n";
// Save token usage to a file
$fileContent = "Total Tokens: $totalTokens\n";
$fileContent .= "Prompt Tokens: $promptTokens\n";
$fileContent .= "Completion Tokens: $completionTokens\n";
file_put_contents("usedToken.txt", $fileContent);
echo "Token usage has been saved to usedToken.txt.\n";
} else {
echo "Usage data not available.";
}
$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.';
}
$inputFile = "2.pdf";
$outputFile = "2.json";
$extractedText = ExtractTextFromCopyablePDF($inputFile);
echo "{$extractedText}\n";
echo strlen($extractedText) . "\n";
if (strlen($extractedText) > 50) {
echo "Given PDF file is copyable file.\n";
file_put_contents("extractedText.txt", $extractedText);
OpenAIFunctionCalling($extractedText, $openaiKey, $outputFile);
} else {
echo "Given PDF file is non-copyable file.\n";
$extractedText = CustomizedMindeeAPI($mindeeKey, $inputFile);
file_put_contents("extractedText.txt", $extractedText);
echo "{$extractedText}\n";
echo strlen($extractedText) . "\n";
OpenAIFunctionCalling($extractedText, $openaiKey, $outputFile);
}