update auto gpt

This commit is contained in:
Dennis Eichhorn 2023-05-25 08:07:41 +00:00
parent 7a72ac3349
commit 185d5fcf1e
5 changed files with 274 additions and 12 deletions

View File

@ -1,6 +1,6 @@
<?php
namespace Build\Helper;
namespace Build\Tools\AutoGpt;
use phpOMS\Message\Http\HttpRequest;
use phpOMS\Message\Http\Rest;
@ -9,7 +9,7 @@ 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 = '';
private const MODEL = 'gpt-3.5-turbo';
private const TEMPERATUR = 0.9;
private const MAX_TOKENS = 1700;
@ -69,7 +69,7 @@ class GptHelper
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, bool $bulk = true) : string
{
$response = '';
@ -78,7 +78,16 @@ class GptHelper
$lines = '';
while (($line = $fileReader($in, $inPath)) !== false) {
if (\strlen($lines) > self::MAX_STRLEN) {
$line = \str_replace(' ', "\t", $line);
if (\strlen($line) > self::MAX_STRLEN) {
continue;
}
if (!$bulk) {
$lines = $line;
}
if (!$bulk || \strlen($lines) + \strlen($line) > self::MAX_STRLEN) {
$response = self::aiRequest($behavior, $lines);
if ($response === '' || $response === false) {
@ -92,10 +101,12 @@ class GptHelper
$lines = '';
}
$lines .= $line . "\n";
if ($bulk) {
$lines .= $line . "\n";
}
}
if (\trim($lines) !== '') {
if (\trim($lines) !== '' && \strlen($lines) <= self::MAX_STRLEN) {
$response = self::aiRequest($behavior, $lines);
if ($response !== '' && $response !== false) {
@ -111,7 +122,7 @@ class GptHelper
}
if ($inPath === $outPath) {
if (\ftell($out) / \ftell($in) < 0.9) {
if (\ftell($in) === 0 || \ftell($out) / \ftell($in) < 0.9) {
\unlink($outPath . '.out');
} else {
\unlink($outPath);

View File

@ -0,0 +1,67 @@
<?php
use Build\Tools\AutoGpt\GptHelper;
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
You are a developer and want to improve the docblocks of member variables and functions.
This is a docblock for class member variables:
/**
* Default delivery address.
*
* @var null|Address
* @since 1.0.0
*/
This is a docblock for functions:
/**
* Order contact elements
*
* @param ContactElement \$ele Element
* @param bool \$isRequired Is required
*
* @return int
*
* @since 1.0.0
*/
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
if (\filesize($filename) > 1300) {
return false;
}
$lines = '';
$isFirstLine = true;
while (($line = \fgets($in)) !== false) {
$isFirstLine = false;
$lines .= $line;
}
if ($isFirstLine && empty($lines)) {
return false;
}
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);
}
}

View File

@ -2,13 +2,13 @@
use Build\Helper\GptHelper;
include __DIR__ . '/../../phpOMS/Autoloader.php';
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../phpOMS/**/*.php',
__DIR__ . '/../../Modules/*/Models/*.php',
__DIR__ . '/../../Modules/*/Controller/*.php',
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
@ -167,7 +167,7 @@ foreach ($globs as $glob) {
continue;
}
$response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader);
$response = GptHelper::handleFile($file, 'php://memory', $behaviour, $fileReader, false);
echo $response , \PHP_EOL , \PHP_EOL;
}
}

View File

@ -0,0 +1,76 @@
<?php
use Build\Tools\AutoGpt\GptHelper;
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
You are a developer.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
$lines = '';
$scopeStart = false;
$scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($isFirstLine || $scopeStart)) {
if (\stripos($line, ' function ') !== false) {
$scopeStart = true;
}
// Not in a function / not relevant
if (!$scopeStart) {
continue;
}
$isFirstLine = false;
$scopeChange = \substr_count($line, '{') - \substr_count($line, '}');
if ($scopeCounter !== 0 && $scopeCounter + $scopeChange === 0) {
$scopeStart = false;
}
$scopeCounter += $scopeChange;
$lines .= $line;
}
// No content or too short
if (($isFirstLine && empty(\trim($lines))) || \strlen($lines) < 550) {
return false;
}
return "Improve the performance and security of the following function without chaning the behaviour. Don't give me any explanations:\n" . $lines;
};
$output = __DIR__ . '/../../../improvements.log';
$log = \fopen($output, 'w');
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, false);
if (\trim($response) !== '') {
\fwrite($log, $file . ":\n");
\fwrite($log, $response);
\fwrite($log, "\n\n");
}
}
}
\fclose($log);

View File

@ -0,0 +1,108 @@
<?php
use Build\Tools\AutoGpt\GptHelper;
include __DIR__ . '/../../../phpOMS/Autoloader.php';
// fix docblocks
$globs = [
__DIR__ . '/../../../phpOMS/**/*.php',
__DIR__ . '/../../../Modules/*/Models/*.php',
__DIR__ . '/../../../Modules/*/Controller/*.php',
];
$behaviour = <<<BEHAVIOUR
You are a developer.
BEHAVIOUR;
$fileReader = function ($in, $filename)
{
$lines = '';
$scopeStart = false;
$scopeCounter = 0;
$isFirstLine = true;
while (($line = \fgets($in)) !== false && ($isFirstLine || $scopeStart)) {
if (\stripos($line, ' public function ') !== false
|| \stripos($line, ' public static function ') !== false
) {
$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
continue;
}
}
$scopeStart = true;
}
// Not in a function / not relevant
if (!$scopeStart) {
continue;
}
$isFirstLine = false;
$scopeChange = \substr_count($line, '{') - \substr_count($line, '}');
if ($scopeCounter !== 0 && $scopeCounter + $scopeChange === 0) {
$scopeStart = false;
}
$scopeCounter += $scopeChange;
$lines .= $line;
}
// No content or too short
if (($isFirstLine && empty(\trim($lines))) || \strlen($lines) < 550) {
return false;
}
return "Implement a PHPUnit test function for the following function:\n" . $lines;
};
$output = __DIR__ . '/../../../improvements.log';
$log = \fopen($output, 'w');
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, false);
if (\trim($response) !== '') {
\fwrite($log, $file . ":\n");
\fwrite($log, $response);
\fwrite($log, "\n\n");
}
}
}
\fclose($log);