Lint PHP code psr-12, add GH action

This commit is contained in:
Julien Nahum
2024-02-23 11:54:12 +01:00
parent e85e4df7fe
commit 62971a2ef4
226 changed files with 2338 additions and 2144 deletions

View File

@@ -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) {