From c56788647142424cbea7114b93a9c4a4cfcd2111 Mon Sep 17 00:00:00 2001 From: Torben Evald Hanen Date: Fri, 12 Apr 2024 22:48:19 +0200 Subject: [PATCH] Refactor OpenAI service and error handling --- app/Console/Commands/OpenAIRespond.php | 2 +- app/Services/OpenAIService.php | 22 +++++++--------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/app/Console/Commands/OpenAIRespond.php b/app/Console/Commands/OpenAIRespond.php index 448730da..16ea46e3 100644 --- a/app/Console/Commands/OpenAIRespond.php +++ b/app/Console/Commands/OpenAIRespond.php @@ -93,7 +93,7 @@ private function requestOpenAI(string $question, FilesystemAdapter $testDisk = n $testDisk?->append($testFileName, $binary, ''); } - } catch (GuzzleException $e) { + } catch (\Exception $e) { $this->output->writeln('An error occurred while sending post request to chat gtp'); $this->output->writeln('Code: ' . $e->getCode() . ' Message: ' . $e->getMessage()); exit(CommandAlias::FAILURE); diff --git a/app/Services/OpenAIService.php b/app/Services/OpenAIService.php index 6c441a8a..ca50e8c9 100644 --- a/app/Services/OpenAIService.php +++ b/app/Services/OpenAIService.php @@ -16,21 +16,13 @@ public function __construct() public function ask(string $question): mixed { - return $this->openAiClient->send('POST', 'v1/completions', [ - 'stream' => true, - 'laravel_data' => [ - 'scheme' => 'http', - ], - 'json' => [ - 'model' => 'text-davinci-003', - 'temperature' => 0.7, - 'top_p' => 1, - 'frequency_penalty' => 0, - 'presence_penalty' => 0, - 'max_tokens' => 1700, - 'prompt' => "' . $question . '", - 'stream' => true, - ], + return $this->openAiClient->withOptions([ + 'debug' => true, + ])->asJson()->send('POST', '/v1/chat/completions', [ + 'model' => 'gpt-3.5-turbo', + 'messages' => [['role'=>'user', 'content' => $question ]], + "temperature" => 0.7, + "stream" => true, ]); } }