Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache models in a class attribute to avoid one network request per provider #133

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
class OpenAiAPIService {
private IClient $client;
private ?array $modelResponseCache = null;

public function __construct(
private LoggerInterface $logger,
Expand Down Expand Up @@ -78,7 +79,14 @@ public function getServiceName(): string {
* @throws Exception
*/
public function getModels(string $userId): array {
$response = $this->request($userId, 'models');
if ($this->modelResponseCache !== null) {
$response = $this->modelResponseCache;
$this->logger->debug('Getting OpenAI models from the memory cache');
} else {
$response = $this->request($userId, 'models');
$this->modelResponseCache = $response;
$this->logger->debug('Actually getting OpenAI models with a network request');
}
if (!isset($response['data'])) {
$this->logger->warning('Error retrieving models: ' . json_encode($response));
throw new Exception($this->l10n->t('Unknown models error'), Http::STATUS_INTERNAL_SERVER_ERROR);
Expand Down
Loading