PHP Classes

File: backend/runPredict.php

Recommend this page to a friend!
  Classes of Rodrigo Faustino   Modelo LLM PHP   backend/runPredict.php   Download  
File: backend/runPredict.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Modelo LLM PHP
Large language model to implement a chat bot
Author: By
Last change:
Date: Yesterday
Size: 1,087 bytes
 

Contents

Class file image Download
<?php
ini_set
('memory_limit', '14096M');

require
'vendor/autoload.php';

use
Chat\X\Utils\NextWordPredictor;

$modelDir = 'model';
$modelPath = $modelDir . '/naive_bayes_model.phpml';
$vectorizerPath = $modelDir . '/vectorizer.phpml';
$featureSelectorPath = $modelDir . '/feature_selector.phpml';
$initialContext = 'o presidente lula';
$desiredLength = 10;

$predictor = new NextWordPredictor('');
$predictor->loadModel($modelPath, $vectorizerPath, $featureSelectorPath);

$phrase = explode(' ', $initialContext);

// Gerar palavras até alcançar o comprimento desejado
while (count($phrase) < $desiredLength) {
   
$contextWords = array_slice($phrase, -3, 1);
   
$context = implode(' ', $contextWords);
   
   
$nextWord = $predictor->predict($context);
   
    if (
$nextWord === null) {
        echo
"Nenhuma previsão disponível para o contexto: '{$context}'.\n";
        break;
    }
   
   
$phrase[] = $nextWord;
   
//echo "Contexto: '{$context}' => Próxima palavra prevista: '{$nextWord}'\n";
   
$finalPhrase = implode(' ', $phrase);
    echo
$finalPhrase . "\n";
}

?>