Lint PHP code psr-12, add GH action
This commit is contained in:
@@ -14,15 +14,20 @@ use OpenAI\Exceptions\ErrorException;
|
||||
*/
|
||||
class GptCompleter
|
||||
{
|
||||
const AI_MODEL = 'gpt-4-turbo-preview';
|
||||
public const AI_MODEL = 'gpt-4-turbo-preview';
|
||||
|
||||
protected Client $openAi;
|
||||
|
||||
protected mixed $result;
|
||||
|
||||
protected array $completionInput;
|
||||
|
||||
protected ?string $systemMessage;
|
||||
|
||||
protected bool $expectsJson = false;
|
||||
|
||||
protected int $tokenUsed = 0;
|
||||
|
||||
protected bool $useStreaming = false;
|
||||
|
||||
public function __construct(string $apiKey, protected int $retries = 2, protected string $model = self::AI_MODEL)
|
||||
@@ -33,36 +38,41 @@ class GptCompleter
|
||||
public function setAiModel(string $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setSystemMessage(string $systemMessage): self
|
||||
{
|
||||
$this->systemMessage = $systemMessage;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function useStreaming(): self
|
||||
{
|
||||
$this->useStreaming = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function expectsJson(): self
|
||||
{
|
||||
$this->expectsJson = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function doesNotExpectJson(): self
|
||||
{
|
||||
$this->expectsJson = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function completeChat(array $messages, int $maxTokens = 4096, float $temperature = 0.81, ?bool $exceptJson = null): self
|
||||
{
|
||||
if (!is_null($exceptJson)) {
|
||||
if (! is_null($exceptJson)) {
|
||||
$this->expectsJson = $exceptJson;
|
||||
}
|
||||
$this->computeChatCompletion($messages, $maxTokens, $temperature)
|
||||
@@ -89,18 +99,19 @@ class GptCompleter
|
||||
$payload = Str::of($this->result)->trim();
|
||||
if ($payload->contains('```json')) {
|
||||
$payload = $payload->after('```json')->before('```');
|
||||
} else if ($payload->contains('```')) {
|
||||
} elseif ($payload->contains('```')) {
|
||||
$payload = $payload->after('```')->before('```');
|
||||
}
|
||||
$payload = $payload->toString();
|
||||
$exception = null;
|
||||
|
||||
try {
|
||||
$newPayload = (new JsonFixer)->fix($payload);
|
||||
$newPayload = (new JsonFixer())->fix($payload);
|
||||
|
||||
return json_decode($newPayload, true);
|
||||
} catch (\Aws\Exception\InvalidJsonException $e) {
|
||||
$exception = $e;
|
||||
Log::warning("Invalid JSON, retrying:");
|
||||
Log::warning('Invalid JSON, retrying:');
|
||||
Log::warning($payload);
|
||||
$this->queryCompletion();
|
||||
}
|
||||
@@ -113,9 +124,10 @@ class GptCompleter
|
||||
$payload = Str::of($this->result)->trim();
|
||||
if ($payload->contains('```html')) {
|
||||
$payload = $payload->after('```html')->before('```');
|
||||
} else if ($payload->contains('```')) {
|
||||
} elseif ($payload->contains('```')) {
|
||||
$payload = $payload->after('```')->before('```');
|
||||
}
|
||||
|
||||
return $payload->toString();
|
||||
}
|
||||
|
||||
@@ -134,7 +146,7 @@ class GptCompleter
|
||||
if (isset($this->systemMessage) && $messages[0]['role'] !== 'system') {
|
||||
$messages = array_merge([[
|
||||
'role' => 'system',
|
||||
'content' => $this->systemMessage
|
||||
'content' => $this->systemMessage,
|
||||
]], $messages);
|
||||
}
|
||||
|
||||
@@ -142,16 +154,17 @@ class GptCompleter
|
||||
'model' => $this->model,
|
||||
'messages' => $messages,
|
||||
'max_tokens' => $maxTokens,
|
||||
'temperature' => $temperature
|
||||
'temperature' => $temperature,
|
||||
];
|
||||
|
||||
if ($this->expectsJson) {
|
||||
$completionInput['response_format'] = [
|
||||
'type' => 'json_object'
|
||||
'type' => 'json_object',
|
||||
];
|
||||
}
|
||||
|
||||
$this->completionInput = $completionInput;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -162,7 +175,7 @@ class GptCompleter
|
||||
}
|
||||
|
||||
try {
|
||||
Log::debug("Open AI query: " . json_encode($this->completionInput));
|
||||
Log::debug('Open AI query: '.json_encode($this->completionInput));
|
||||
$response = $this->openAi->chat()->create($this->completionInput);
|
||||
} catch (ErrorException $errorException) {
|
||||
// Retry once
|
||||
@@ -171,12 +184,13 @@ class GptCompleter
|
||||
}
|
||||
$this->tokenUsed += $response->usage->totalTokens;
|
||||
$this->result = $response->choices[0]->message->content;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function queryStreamedCompletion(): self
|
||||
{
|
||||
Log::debug("Open AI query: " . json_encode($this->completionInput));
|
||||
Log::debug('Open AI query: '.json_encode($this->completionInput));
|
||||
$this->result = '';
|
||||
$response = $this->openAi->chat()->createStreamed($this->completionInput);
|
||||
foreach ($response as $chunk) {
|
||||
|
||||
@@ -53,13 +53,12 @@ class JsonFixer
|
||||
/**
|
||||
* Set/unset silent mode.
|
||||
*
|
||||
* @param bool $silent
|
||||
*
|
||||
* @param bool $silent
|
||||
* @return $this
|
||||
*/
|
||||
public function silent($silent = true)
|
||||
{
|
||||
$this->silent = (bool)$silent;
|
||||
$this->silent = (bool) $silent;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -67,8 +66,7 @@ class JsonFixer
|
||||
/**
|
||||
* Set missing value.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function missingValue($value)
|
||||
@@ -87,16 +85,15 @@ class JsonFixer
|
||||
/**
|
||||
* Fix the truncated JSON.
|
||||
*
|
||||
* @param string $json The JSON string to fix.
|
||||
*
|
||||
* @param string $json The JSON string to fix.
|
||||
* @return string Fixed JSON. If failed with silent then original JSON.
|
||||
* @throws InvalidJsonException When fixing fails.
|
||||
*
|
||||
* @throws InvalidJsonException When fixing fails.
|
||||
*/
|
||||
public function fix($json)
|
||||
{
|
||||
$json = preg_replace('/(?<!\\\\)(?:\\\\{2})*\p{C}+/u', '', $json);
|
||||
list($head, $json, $tail) = $this->trim($json);
|
||||
[$head, $json, $tail] = $this->trim($json);
|
||||
|
||||
if (empty($json) || $this->isValid($json)) {
|
||||
return $json;
|
||||
@@ -108,7 +105,7 @@ class JsonFixer
|
||||
|
||||
$this->reset();
|
||||
|
||||
return $head . $this->doFix($json) . $tail;
|
||||
return $head.$this->doFix($json).$tail;
|
||||
}
|
||||
|
||||
protected function trim($json)
|
||||
@@ -125,15 +122,15 @@ class JsonFixer
|
||||
|
||||
protected function isValid($json)
|
||||
{
|
||||
\json_decode($json,true,512,JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
\json_decode($json, true, 512, JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
|
||||
return \JSON_ERROR_NONE === \json_last_error();
|
||||
return \json_last_error() === \JSON_ERROR_NONE;
|
||||
}
|
||||
|
||||
protected function quickFix($json)
|
||||
{
|
||||
if (\strlen($json) === 1 && isset($this->pairs[$json])) {
|
||||
return $json . $this->pairs[$json];
|
||||
return $json.$this->pairs[$json];
|
||||
}
|
||||
|
||||
if ($json[0] !== '"') {
|
||||
@@ -153,7 +150,7 @@ class JsonFixer
|
||||
|
||||
protected function maybeLiteral($json)
|
||||
{
|
||||
if (!\in_array($json[0], ['t', 'f', 'n'])) {
|
||||
if (! \in_array($json[0], ['t', 'f', 'n'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -170,14 +167,14 @@ class JsonFixer
|
||||
|
||||
protected function doFix($json)
|
||||
{
|
||||
list($index, $char) = [-1, ''];
|
||||
[$index, $char] = [-1, ''];
|
||||
|
||||
while (isset($json[++$index])) {
|
||||
list($prev, $char) = [$char, $json[$index]];
|
||||
[$prev, $char] = [$char, $json[$index]];
|
||||
|
||||
$next = isset($json[$index + 1]) ? $json[$index + 1] : '';
|
||||
|
||||
if (!\in_array($char, [' ', "\n", "\r"])) {
|
||||
if (! \in_array($char, [' ', "\n", "\r"])) {
|
||||
$this->stack($prev, $char, $index, $next);
|
||||
}
|
||||
}
|
||||
@@ -212,7 +209,7 @@ class JsonFixer
|
||||
protected function popToken($token = null)
|
||||
{
|
||||
// Last one
|
||||
if (null === $token) {
|
||||
if ($token === null) {
|
||||
return \array_pop($this->stack);
|
||||
}
|
||||
|
||||
@@ -228,7 +225,7 @@ class JsonFixer
|
||||
protected function maybeStr($prev, $char, $index)
|
||||
{
|
||||
if ($prev !== '\\' && $char === '"') {
|
||||
$this->inStr = !$this->inStr;
|
||||
$this->inStr = ! $this->inStr;
|
||||
}
|
||||
|
||||
if ($this->inStr && $this->lastToken() !== '"') {
|
||||
@@ -267,7 +264,7 @@ class JsonFixer
|
||||
}
|
||||
|
||||
\Log::debug('Broken json received: ', [
|
||||
'json' => $json
|
||||
'json' => $json,
|
||||
]);
|
||||
|
||||
throw new InvalidJsonException(
|
||||
|
||||
@@ -16,7 +16,7 @@ trait PadsJson
|
||||
{
|
||||
public function pad($tmpJson)
|
||||
{
|
||||
if (!$this->inStr) {
|
||||
if (! $this->inStr) {
|
||||
$tmpJson = \rtrim($tmpJson, ',');
|
||||
while ($this->lastToken() === ',') {
|
||||
$this->popToken();
|
||||
@@ -37,11 +37,11 @@ trait PadsJson
|
||||
|
||||
$match = \preg_match('/(tr?u?e?|fa?l?s?e?|nu?l?l?)$/', $tmpJson, $matches);
|
||||
|
||||
if (!$match || null === $literal = $this->maybeLiteral($matches[1])) {
|
||||
if (! $match || null === $literal = $this->maybeLiteral($matches[1])) {
|
||||
return $tmpJson;
|
||||
}
|
||||
|
||||
return \substr($tmpJson, 0, 0 - \strlen($matches[1])) . $literal;
|
||||
return \substr($tmpJson, 0, 0 - \strlen($matches[1])).$literal;
|
||||
}
|
||||
|
||||
protected function padStack($tmpJson)
|
||||
@@ -57,7 +57,7 @@ trait PadsJson
|
||||
|
||||
protected function padObject($tmpJson)
|
||||
{
|
||||
if (!$this->objectNeedsPadding($tmpJson)) {
|
||||
if (! $this->objectNeedsPadding($tmpJson)) {
|
||||
return $tmpJson;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ trait PadsJson
|
||||
}
|
||||
|
||||
$tmpJson = $this->padIf($tmpJson, ':');
|
||||
$tmpJson = $tmpJson . $this->missingValue;
|
||||
$tmpJson = $tmpJson.$this->missingValue;
|
||||
|
||||
if ($this->lastToken() === '"') {
|
||||
$this->popToken();
|
||||
@@ -82,19 +82,19 @@ trait PadsJson
|
||||
|
||||
protected function objectNeedsPadding($tmpJson)
|
||||
{
|
||||
$last = \substr($tmpJson, -1);
|
||||
$empty = $last === '{' && !$this->inStr;
|
||||
$last = \substr($tmpJson, -1);
|
||||
$empty = $last === '{' && ! $this->inStr;
|
||||
|
||||
return !$empty && $this->arrayPos < $this->objectPos;
|
||||
return ! $empty && $this->arrayPos < $this->objectPos;
|
||||
}
|
||||
|
||||
protected function padString($string)
|
||||
{
|
||||
$last = \substr($string, -1);
|
||||
$last = \substr($string, -1);
|
||||
$last2 = \substr($string, -2);
|
||||
|
||||
if ($last2 === '\"' || $last !== '"') {
|
||||
return $string . '"';
|
||||
return $string.'"';
|
||||
}
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
@@ -105,7 +105,7 @@ trait PadsJson
|
||||
protected function padIf($string, $substr)
|
||||
{
|
||||
if (\substr($string, 0 - \strlen($substr)) !== $substr) {
|
||||
return $string . $substr;
|
||||
return $string.$substr;
|
||||
}
|
||||
|
||||
return $string;
|
||||
|
||||
Reference in New Issue
Block a user