mirror of
https://github.com/Karaka-Management/Build.git
synced 2026-01-20 15:48:41 +00:00
improve
This commit is contained in:
parent
f934a76bf9
commit
7a72ac3349
|
|
@ -9,11 +9,11 @@ use phpOMS\Uri\HttpUri;
|
|||
class GptHelper
|
||||
{
|
||||
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 TEMPERATUR = 0.9;
|
||||
private const MAX_TOKENS = 3096;
|
||||
private const MAX_STRLEN = 1500;
|
||||
private const MAX_TOKENS = 1700;
|
||||
private const MAX_STRLEN = 1300;
|
||||
|
||||
private const LIMIT_REQUEST_PER_MINUTE = 3;
|
||||
private const LIMIT_TOKENS_PER_MINUTE = 40000;
|
||||
|
|
@ -53,15 +53,20 @@ class GptHelper
|
|||
$request->setMethod('POST');
|
||||
$request->fromData($requestBody);
|
||||
|
||||
$time = \time();
|
||||
if ($time - self::$lastRun < 61 / self::LIMIT_REQUEST_PER_MINUTE) {
|
||||
\sleep(61 / self::LIMIT_REQUEST_PER_MINUTE - ($time - self::$lastRun));
|
||||
}
|
||||
do {
|
||||
$time = \time();
|
||||
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);
|
||||
self::$lastRun = \time();
|
||||
$response = Rest::request($request);
|
||||
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
|
||||
|
|
@ -76,12 +81,13 @@ class GptHelper
|
|||
if (\strlen($lines) > self::MAX_STRLEN) {
|
||||
$response = self::aiRequest($behavior, $lines);
|
||||
|
||||
if ($response === '') {
|
||||
if ($response === '' || $response === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$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 = '';
|
||||
}
|
||||
|
|
@ -92,22 +98,28 @@ class GptHelper
|
|||
if (\trim($lines) !== '') {
|
||||
$response = self::aiRequest($behavior, $lines);
|
||||
|
||||
if ($response !== '') {
|
||||
if ($response !== '' && $response !== false) {
|
||||
$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') {
|
||||
\rewind($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);
|
||||
|
||||
return $response;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ include __DIR__ . '/../../phpOMS/Autoloader.php';
|
|||
|
||||
// fix docblocks
|
||||
$globs = [
|
||||
__DIR__ . '/../../phpOMS/*.php',
|
||||
__DIR__ . '/../../phpOMS/**/*.php',
|
||||
__DIR__ . '/../../Modules/*/Models/*.php',
|
||||
__DIR__ . '/../../Modules/*/Controller/*.php',
|
||||
];
|
||||
|
|
@ -30,6 +30,7 @@ and for functions:
|
|||
*
|
||||
* @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;
|
||||
|
||||
$fileReader = function ($in, $filename)
|
||||
|
|
@ -38,7 +39,11 @@ $fileReader = function ($in, $filename)
|
|||
$scopeStart = false;
|
||||
$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) {
|
||||
$scopeStart = true;
|
||||
}
|
||||
|
|
@ -55,6 +60,10 @@ $fileReader = function ($in, $filename)
|
|||
$lines .= $line;
|
||||
}
|
||||
|
||||
if ($isFirstLine && empty($lines)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $lines;
|
||||
};
|
||||
|
||||
|
|
@ -72,7 +81,7 @@ foreach ($globs as $glob) {
|
|||
|
||||
// implement test
|
||||
$globs = [
|
||||
__DIR__ . '/../../phpOMS/*.php',
|
||||
__DIR__ . '/../../phpOMS/**.php',
|
||||
__DIR__ . '/../../Modules/*/Models/*.php',
|
||||
];
|
||||
|
||||
|
|
@ -86,7 +95,11 @@ $fileReader = function ($in, $filename)
|
|||
$scopeStart = false;
|
||||
$scopeCounter = 0;
|
||||
|
||||
$isFirstLine = true;
|
||||
|
||||
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
|
||||
$isFirstLine = false;
|
||||
|
||||
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
|
||||
// Check if a test exists for this function
|
||||
$testFile = \str_replace('.php', 'Test.php', $filename);
|
||||
|
|
@ -139,6 +152,10 @@ $fileReader = function ($in, $filename)
|
|||
$lines .= $line;
|
||||
}
|
||||
|
||||
if ($isFirstLine && empty($lines)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $lines;
|
||||
};
|
||||
|
||||
|
|
@ -157,7 +174,7 @@ foreach ($globs as $glob) {
|
|||
|
||||
// complete and improve test
|
||||
$globs = [
|
||||
__DIR__ . '/../../phpOMS/*.php',
|
||||
__DIR__ . '/../../phpOMS/**.php',
|
||||
__DIR__ . '/../../Modules/*/Models/*.php',
|
||||
];
|
||||
|
||||
|
|
@ -171,7 +188,11 @@ $fileReader = function ($in, $filename)
|
|||
$scopeStart = false;
|
||||
$scopeCounter = 0;
|
||||
|
||||
$isFirstLine = true;
|
||||
|
||||
while (($line = \fgets($in)) !== false && ($line !== '' || $scopeCounter > 0)) {
|
||||
$isFirstLine = false;
|
||||
|
||||
if (\stripos($line, 'public function ') !== false || \stripos($line, 'public static function ') !== false) {
|
||||
// Check if a test exists for this function
|
||||
$testFile = \str_replace('.php', 'Test.php', $filename);
|
||||
|
|
@ -226,6 +247,10 @@ $fileReader = function ($in, $filename)
|
|||
$lines .= $line;
|
||||
}
|
||||
|
||||
if ($isFirstLine && empty($lines)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $lines;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user