-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve error logger & get models from api
- Loading branch information
Showing
8 changed files
with
570 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Datlechin\FlarumChatGPT\Api\Controller; | ||
|
||
use Datlechin\FlarumChatGPT\OpenAIClient; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Contracts\Cache\Store; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class ShowOpenAiModelsController implements RequestHandlerInterface | ||
{ | ||
public function __construct( | ||
protected OpenAIClient $client, | ||
protected Store $cache, | ||
protected SettingsRepositoryInterface $settings | ||
) { | ||
} | ||
|
||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$data = $this->cache->get('datlechin-chatgpt.models') ?: []; | ||
|
||
if (empty($data) && $this->settings->get('datlechin-chatgpt.api_key')) { | ||
$data = $this->client->models()->list()->data; | ||
$this->cache->put('datlechin-chatgpt.models', $data, 60 * 60); | ||
} | ||
|
||
return new JsonResponse($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Datlechin\FlarumChatGPT; | ||
|
||
use Exception; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use OpenAI; | ||
use OpenAI\Client; | ||
use OpenAI\Resources\Models; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class OpenAIClient | ||
{ | ||
public ?Client $client = null; | ||
|
||
public function __construct(protected SettingsRepositoryInterface $settings, protected LoggerInterface $logger) | ||
{ | ||
$apiKey = $this->settings->get('datlechin-chatgpt.api_key'); | ||
|
||
if (empty($apiKey)) { | ||
$this->logger->error('OpenAI API key is not set.'); | ||
return; | ||
} | ||
|
||
$this->client = OpenAI::client($apiKey); | ||
} | ||
|
||
public function completions(string $content = null): ?string | ||
{ | ||
try { | ||
$result = $this->client->completions()->create([ | ||
'model' => $this->settings->get('datlechin-chatgpt.model', 'text-davinci-003'), | ||
'prompt' => $content, | ||
'max_tokens' => (int) $this->settings->get('datlechin-chatgpt.max_tokens', 100), | ||
]); | ||
} catch (Exception $e) { | ||
$this->logger->error($e->getMessage()); | ||
|
||
return null; | ||
} | ||
|
||
return $result->choices[0]->text; | ||
} | ||
|
||
public function models(): Models | ||
{ | ||
return $this->client->models(); | ||
} | ||
} |