Closed
Description
Description
Laravel Framework 11.29.0
`public function extractDataFromPDF($pdfPath)
{
// Extrair o texto do PDF usando o PdfParser
$parser = new Parser();
$pdf = $parser->parseFile($pdfPath);
$pdfText = $pdf->getText();
// Dividir o texto em partes menores, se necessário, para evitar o limite de tokens
$chunks = str_split($pdfText, 4000); // Ajuste o tamanho do chunk conforme necessário
$extractedData = [];
foreach ($chunks as $chunk) {
$result = $this->client->completions()->create([
'model' => 'gpt-4',
'prompt' => "Aqui está uma parte do conteúdo do PDF:\n\n" . $chunk . "\n\nExtraia os dados estruturados de forma adequada para alimentar o banco de dados.",
'max_tokens' => 1500,
]);
$extractedData[] = $result['choices'][0]['text'];
}
// Combinar os dados extraídos de cada parte
$finalData = implode(" ", $extractedData);
// Tentar decodificar para JSON, caso o ChatGPT tenha retornado em JSON
$structuredData = json_decode($finalData, true);
// Retorna os dados estruturados ou o texto completo
return $structuredData ?: $finalData;
}`
Steps To Reproduce
Uploading a file
Extract the text from the PDF using PdfParser
Split the text into smaller parts, if necessary, to avoid the token limit
OpenAI PHP Client Version
^0.10.2
PHP Version
PHP 8.3.12 (cli) (built: Sep 27 2024 03:53:05) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.12, Copyright (c) Zend Technologies with Zend OPcache v8.3.12, Copyright (c), by Zend Technologies with Xdebug v3.3.2, Copyright (c) 2002-2024, by Derick Rethans
Notes
Can someone help me!
Metadata
Metadata
Assignees
Labels
Something isn't workingSomething isn't working