This commit is contained in:
Dennis Eichhorn 2023-05-24 21:11:27 +00:00
parent f934a76bf9
commit 7a72ac3349
2 changed files with 60 additions and 23 deletions

View File

@ -9,11 +9,11 @@ use phpOMS\Uri\HttpUri;
class GptHelper class GptHelper
{ {
private const ENDPOINT = 'https://api.openai.com/v1/chat/completions'; private const ENDPOINT = 'https://api.openai.com/v1/chat/completions';
private const APIKEY = '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'; private const APIKEY = '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';
private const MODEL = 'gpt-3.5-turbo'; private const MODEL = 'gpt-3.5-turbo';
private const TEMPERATUR = 0.9; private const TEMPERATUR = 0.9;
private const MAX_TOKENS = 3096; private const MAX_TOKENS = 1700;
private const MAX_STRLEN = 1500; private const MAX_STRLEN = 1300;
private const LIMIT_REQUEST_PER_MINUTE = 3; private const LIMIT_REQUEST_PER_MINUTE = 3;
private const LIMIT_TOKENS_PER_MINUTE = 40000; private const LIMIT_TOKENS_PER_MINUTE = 40000;
@ -53,15 +53,20 @@ class GptHelper
$request->setMethod('POST'); $request->setMethod('POST');
$request->fromData($requestBody); $request->fromData($requestBody);
$time = \time(); do {
if ($time - self::$lastRun < 61 / self::LIMIT_REQUEST_PER_MINUTE) { $time = \time();
\sleep(61 / self::LIMIT_REQUEST_PER_MINUTE - ($time - self::$lastRun)); if ($time - self::$lastRun <= (int) (61 / self::LIMIT_REQUEST_PER_MINUTE)) {
} \sleep((int) (61 / self::LIMIT_REQUEST_PER_MINUTE - ($time - self::$lastRun)) + 1);
}
$response = Rest::request($request); $response = Rest::request($request);
self::$lastRun = \time(); self::$lastRun = \time();
} while (\stripos($response->getBody(), '"code":502') !== false
|| \stripos($response->getBody(), '"code": 502') !== false
|| \stripos($response->getBody(), '503 Service') !== false
);
return $response->getBody(); return \str_replace('```', '', $response->getBody());
} }
public static function handleFile(string $inPath, string $outPath, string $behavior, \Closure $fileReader) : string public static function handleFile(string $inPath, string $outPath, string $behavior, \Closure $fileReader) : string
@ -76,12 +81,13 @@ class GptHelper
if (\strlen($lines) > self::MAX_STRLEN) { if (\strlen($lines) > self::MAX_STRLEN) {
$response = self::aiRequest($behavior, $lines); $response = self::aiRequest($behavior, $lines);
if ($response === '') { if ($response === '' || $response === false) {
continue; continue;
} }
$json = \json_decode($response, true); $json = \json_decode($response, true);
\fwrite($out, \str_replace("\n\n", "\n", $json[5][0]['message']['content'])); \fwrite($out, $json[5][0]['message']['content']);
\fwrite($out, "\n");
$lines = ''; $lines = '';
} }
@ -92,22 +98,28 @@ class GptHelper
if (\trim($lines) !== '') { if (\trim($lines) !== '') {
$response = self::aiRequest($behavior, $lines); $response = self::aiRequest($behavior, $lines);
if ($response !== '') { if ($response !== '' && $response !== false) {
$json = \json_decode($response, true); $json = \json_decode($response, true);
\fwrite($out, \str_replace("\n\n", "\n", $json[5][0]['message']['content'])); \fwrite($out, $json[5][0]['message']['content']);
\fwrite($out, "\n");
} }
} }
\fwrite($out, "\n");
\fclose($in);
if ($outPath === 'php://memory') { if ($outPath === 'php://memory') {
\rewind($out); \rewind($out);
$response = \stream_get_contents($out); $response = \stream_get_contents($out);
\fclose($out);
} }
if ($inPath === $outPath) {
if (\ftell($out) / \ftell($in) < 0.9) {
\unlink($outPath . '.out');
} else {
\unlink($outPath);
\rename($outPath . '.out', $outPath);
}
}
\fclose($in);
\fclose($out); \fclose($out);
return $response; return $response;

View File

@ -6,7 +6,7 @@ include __DIR__ . '/../../phpOMS/Autoloader.php';
// fix docblocks // fix docblocks
$globs = [ $globs = [
__DIR__ . '/../../phpOMS/*.php', __DIR__ . '/../../phpOMS/**/*.php',
__DIR__ . '/../../Modules/*/Models/*.php', __DIR__ . '/../../Modules/*/Models/*.php',
__DIR__ . '/../../Modules/*/Controller/*.php', __DIR__ . '/../../Modules/*/Controller/*.php',
]; ];
@ -30,6 +30,7 @@ and for functions:
* *
* @since 1.0.0 * @since 1.0.0
*/ */
Only output the php code, nothing else. Also don't add or change any new php code except the docblocks.
BEHAVIOUR; BEHAVIOUR;
$fileReader = function ($in, $filename) $fileReader = function ($in, $filename)
@ -38,7 +39,11 @@ $fileReader = function ($in, $filename)
$scopeStart = false; $scopeStart = false;
$scopeCounter = 0; $scopeCounter = 0;
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) { $isFirstLine = true;
while (($line = \fgets($in)) !== false && (\trim($line) !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, ' function ') !== false) { if (\stripos($line, ' function ') !== false) {
$scopeStart = true; $scopeStart = true;
} }
@ -55,6 +60,10 @@ $fileReader = function ($in, $filename)
$lines .= $line; $lines .= $line;
} }
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines; return $lines;
}; };
@ -72,7 +81,7 @@ foreach ($globs as $glob) {
// implement test // implement test
$globs = [ $globs = [
__DIR__ . '/../../phpOMS/*.php', __DIR__ . '/../../phpOMS/**.php',
__DIR__ . '/../../Modules/*/Models/*.php', __DIR__ . '/../../Modules/*/Models/*.php',
]; ];
@ -86,7 +95,11 @@ $fileReader = function ($in, $filename)
$scopeStart = false; $scopeStart = false;
$scopeCounter = 0; $scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) { while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) { if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
// Check if a test exists for this function // Check if a test exists for this function
$testFile = \str_replace('.php', 'Test.php', $filename); $testFile = \str_replace('.php', 'Test.php', $filename);
@ -139,6 +152,10 @@ $fileReader = function ($in, $filename)
$lines .= $line; $lines .= $line;
} }
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines; return $lines;
}; };
@ -157,7 +174,7 @@ foreach ($globs as $glob) {
// complete and improve test // complete and improve test
$globs = [ $globs = [
__DIR__ . '/../../phpOMS/*.php', __DIR__ . '/../../phpOMS/**.php',
__DIR__ . '/../../Modules/*/Models/*.php', __DIR__ . '/../../Modules/*/Models/*.php',
]; ];
@ -171,7 +188,11 @@ $fileReader = function ($in, $filename)
$scopeStart = false; $scopeStart = false;
$scopeCounter = 0; $scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) { while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
$isFirstLine = false;
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) { if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
// Check if a test exists for this function // Check if a test exists for this function
$testFile = \str_replace('.php', 'Test.php', $filename); $testFile = \str_replace('.php', 'Test.php', $filename);
@ -226,6 +247,10 @@ $fileReader = function ($in, $filename)
$lines .= $line; $lines .= $line;
} }
if ($isFirstLine && empty($lines)) {
return false;
}
return $lines; return $lines;
}; };