Created
September 22, 2023 22:20
-
-
Save cosmeoes/b10e42b401bcde82f9d2b8cf71bba42a to your computer and use it in GitHub Desktop.
Union's open AI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands\OneTime; | |
use Exception; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Cache; | |
use OpenAI; | |
class GetUnionCatalogData extends Command | |
{ | |
protected $client; | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'get-union:catalog'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = "Attempts to get union's catalog data using the OpenAI api"; | |
public function handle() | |
{ | |
$buscabotApiKey = '...'; // OpenAI api key | |
$handle = fopen("catalogo-union.csv", "r"); // Union Catalog as csv | |
$results = json_decode(file_get_contents('union_results.json')); | |
$lastRow = Cache::get("union_current_row", 4); | |
$currentRow = 1; | |
$errors = 0; | |
$this->client = OpenAI::client($buscabotApiKey); | |
while (true) { | |
try { | |
fgetcsv($handle); // skipp headers | |
while (($row = fgetcsv($handle)) !== FALSE) { | |
if ($currentRow <= $lastRow) { | |
$currentRow++; | |
continue; | |
} | |
$response = $this->getResponseFromOpenAI([$row]); | |
$responseArray = json_decode($response[0]); | |
if (is_array($responseArray)) { | |
$this->info("Response succesful merging with prev results. Done with: " . $currentRow); | |
$results = array_merge($results, $responseArray); | |
file_put_contents("union_results.json", json_encode($results, JSON_PRETTY_PRINT)); | |
Cache::put('union_current_row', $currentRow); | |
$errors = 0; | |
} else { | |
throw new Exception("not array"); | |
} | |
$currentRow++; | |
} | |
break; | |
} catch (Exception $e) { | |
$errors++; | |
// save results to json file | |
file_put_contents("union_results.json", json_encode($results, JSON_PRETTY_PRINT)); | |
// save current row to cache | |
Cache::put('union_current_row', max($currentRow, $lastRow)); | |
// print error | |
$this->error($e->getMessage() . ' ' . $e->getTraceAsString()); | |
if ($errors > 5) { | |
$this->info("5 consecutive errors stoping."); | |
return Command::FAILURE; | |
} | |
$time = $errors * 3; | |
if (str_starts_with($e->getMessage(), 'Rate limit reached for')) { | |
$time = 60; | |
} | |
$this->info("Sleeping for {$time} seconds"); | |
sleep($time); | |
} | |
} | |
return Command::SUCCESS; | |
} | |
public function getResponseFromOpenAI($list) | |
{ | |
$messages = []; | |
foreach($list as $toRequest) { | |
$messages[] = $this->getMessage(implode(',', $toRequest)); | |
} | |
$response = $this->client->chat()->create([ | |
'model' => 'gpt-3.5-turbo', | |
'messages' => $messages | |
]); | |
$results = []; | |
foreach ($response['choices'] as $choice) { | |
$results[] = $choice['message']['content']; | |
} | |
return $results; | |
} | |
public function getMessage($text) { | |
return [ | |
'role' => 'system', | |
'content' => | |
<<<EOD | |
RESPONDE EN FORMATO JSON SIN NADA DE TEXTO ADICIONAL. | |
De los sigientes datos: | |
MARCA,# PARTE,DESCRIPCION | |
AUTOPAR,A39428X,ABRAZADERA SERIE 1480 1550 FORD F450 6.4 LTS 6.7 LTS 6.8 LTS 4X2 1999/16 | |
SYD,93277197,ABRAZADERA VARILLA ESTABILIZADORA GM CAMIONETA CHEVY 98/02 | |
FRAM,SAF0W20N5,ACEITE SINTETICO 0W20 MOTORES NUEVOS GASOLINA CHEVROLET 5 LTS OEM CAJA C/4 PZ TARIMA C/160 PZ ESPECIFICACIONES DEXOS APPROVED GEN2 ILSAC GF5 | |
Se obtiene la signuente informacion | |
[{"part_number":"A39428X","brand":"AUTOPAR","name":"ABRAZADERA SERIE 1480 1550","cars":[{"make":"Ford","model":"F450","engine":"6.4 LTS","start_year": 1999,"end_year": 2016},{"make":"Ford","model": "F450","engine": "6.7 LTS","start_year": 1999,"end_year": 2016},{"make":"Ford","model":"F450","engine":"6.8 LTS","start_year":1999,"end_year":2016}]},{"part_number":"SAF0W20N5","brand":"FRAM","name":"ACEITE SINTETICO 0W20","cars":[{"make":"GM","model":"Camioneta Chevy","engine":null,"start_year":1998,"end_year":2002}]},{"part_number":"93277197","brand":"SYD","name":"ABRAZADERA VARILLA ESTABILIZADORA","cars": []}] | |
Si alguna de la informacion no esta presente, pon los valores como null. Cada objeto representa una fila de los datos. Si hay varios modelos para la misma marca de auto, crea un objeto de "car" separado. Conserva el orden y no omitas ninguna fila. Si todos los campos de carro son null no lo incluyas. | |
Regresa los datos para los sigientes registros: | |
EOD . $text, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment