diff --git a/Helper/GptHelper.php b/Helper/GptHelper.php new file mode 100644 index 0000000..30a9da3 --- /dev/null +++ b/Helper/GptHelper.php @@ -0,0 +1,115 @@ + self::MODEL, + 'max_tokens' => self::MAX_TOKENS, + 'temperature' => self::TEMPERATUR, + 'messages' => [ + [ + 'role' => 'system', + 'content' => $behavior, + ], + [ + 'role' => 'user', + 'content' => $content, + ] + ] + ]; + + $request = new HttpRequest(new HttpUri(self::ENDPOINT)); + $request->header->set('Authorization', 'Bearer ' . self::APIKEY); + $request->header->set('Content-Type', 'application/json'); + $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)); + } + + $response = Rest::request($request); + self::$lastRun = \time(); + + return $response->getBody(); + } + + public static function handleFile(string $inPath, string $outPath, string $behavior, \Closure $fileReader) : string + { + $response = ''; + + $in = \fopen($inPath, 'r'); + $out = \fopen($outPath . ($inPath === $outPath ? '.out' : ''), 'w'); + $lines = ''; + + while (($line = $fileReader($in, $inPath)) !== false) { + if (\strlen($lines) > self::MAX_STRLEN) { + $response = self::aiRequest($behavior, $lines); + + if ($response === '') { + continue; + } + + $json = \json_decode($response, true); + \fwrite($out, \str_replace("\n\n", "\n", $json[5][0]['message']['content'])); + + $lines = ''; + } + + $lines .= $line . "\n"; + } + + if (\trim($lines) !== '') { + $response = self::aiRequest($behavior, $lines); + + if ($response !== '') { + $json = \json_decode($response, true); + \fwrite($out, \str_replace("\n\n", "\n", $json[5][0]['message']['content'])); + } + } + + \fwrite($out, "\n"); + + \fclose($in); + + if ($outPath === 'php://memory') { + \rewind($out); + $response = \stream_get_contents($out); + \fclose($out); + } + + \fclose($out); + + return $response; + } +} diff --git a/Helper/autoGpt.php b/Helper/autoGpt.php new file mode 100644 index 0000000..198b453 --- /dev/null +++ b/Helper/autoGpt.php @@ -0,0 +1,243 @@ + 0)) { + if (\stripos($line, ' function ') !== false) { + $scopeStart = true; + } + + if ($scopeStart) { + $scopeCounter += \substr_count($line, '{'); + $scopeCounter -= \substr_count($line, '}'); + + if ($scopeCounter === 0) { + $scopeStart = false; + } + } + + $lines .= $line; + } + + return $lines; +}; + +foreach ($globs as $glob) { + $files = \glob($glob); + + foreach ($files as $file) { + if (\stripos($file, 'Test.php')) { + continue; + } + + GptHelper::handleFile($file, $file, $behaviour, $fileReader); + } +} + +// implement test +$globs = [ + __DIR__ . '/../../phpOMS/*.php', + __DIR__ . '/../../Modules/*/Models/*.php', +]; + +$behaviour = << 0)) { + 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); + $testFile = \str_replace('/phpOMS/', '/phpOMS/tests/', $filename); + + if (\stripos($testFile, '../Modules/') !== false) { + $subdirectory = "tests/"; + + $position = strrpos($testFile, "/../"); + + if ($position !== false) { + $position = \stripos($testFile, '/', $position + 4) + 1; + $position = \stripos($testFile, '/', $position) + 1; + $testFile = \substr_replace($testFile, $subdirectory, $position, 0); + } + } + + if (\file_exists($testFile)) { + $scopeStart = true; + + $testContent = \file_get_contents($testFile); + + // Parse functio name + $pattern = '/\bfunction\b\s+(\w+)\s*\(/'; + \preg_match($pattern, $line, $matches); + + if (\stripos($testContent, $matches[1] ?? '~~~') !== false) { + // Test already exists + $scopeStart = false; + } + } + + $scopeStart = true; + } + + // Not in a function / not relevant + if (!$scopeStart) { + continue; + } + + if ($scopeStart) { + $scopeCounter += \substr_count($line, '{'); + $scopeCounter -= \substr_count($line, '}'); + + if ($scopeCounter === 0) { + $scopeStart = false; + } + } + + $lines .= $line; + } + + return $lines; +}; + +foreach ($globs as $glob) { + $files = \glob($glob); + + foreach ($files as $file) { + if (\stripos($file, 'Test.php')) { + continue; + } + + $response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader); + echo $response , \PHP_EOL , \PHP_EOL; + } +} + +// complete and improve test +$globs = [ + __DIR__ . '/../../phpOMS/*.php', + __DIR__ . '/../../Modules/*/Models/*.php', +]; + +$behaviour = << 0)) { + 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); + $testFile = \str_replace('/phpOMS/', '/phpOMS/tests/', $filename); + + if (\stripos($testFile, '../Modules/') !== false) { + $subdirectory = "tests/"; + + $position = strrpos($testFile, "/../"); + + if ($position !== false) { + $position = \stripos($testFile, '/', $position + 4) + 1; + $position = \stripos($testFile, '/', $position) + 1; + $testFile = \substr_replace($testFile, $subdirectory, $position, 0); + } + } + + if (\file_exists($testFile)) { + $scopeStart = true; + + $testContent = \file_get_contents($testFile); + + // Parse functio name + $pattern = '/\bfunction\b\s+(\w+)\s*\(/'; + \preg_match($pattern, $line, $matches); + + if (\stripos($testContent, $matches[1] ?? '~~~') !== false) { + // Test already exists + $scopeStart = false; + } + + $scopeStart = true; + } else { + $scopeStart = false; + } + } + + // Not in a function / not relevant + if (!$scopeStart) { + continue; + } + + if ($scopeStart) { + $scopeCounter += \substr_count($line, '{'); + $scopeCounter -= \substr_count($line, '}'); + + if ($scopeCounter === 0) { + $scopeStart = false; + } + } + + $lines .= $line; + } + + return $lines; +}; + +foreach ($globs as $glob) { + $files = \glob($glob); + + foreach ($files as $file) { + if (\stripos($file, 'Test.php')) { + continue; + } + + $response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader); + echo $response , \PHP_EOL , \PHP_EOL; + } +} \ No newline at end of file