Skip to content
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
39 changes: 29 additions & 10 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ private function isModelListValid($models): bool {
}

/**
* @param string $userId
* @param ?string $userId
* @return array|string[]
* @throws Exception
*/
public function getModels(string $userId): array {
public function getModels(?string $userId): array {
// caching against 'getModelEnumValues' calls from all the providers
if ($this->areCredsValid === false) {
$this->logger->info('Cannot get OpenAI models without an API key');
Expand All @@ -118,11 +118,33 @@ public function getModels(string $userId): array {
return $this->modelsMemoryCache;
}

$cacheKey = Application::MODELS_CACHE_KEY;
$userCacheKey = Application::MODELS_CACHE_KEY . '_' . ($userId ?? '');
$adminCacheKey = Application::MODELS_CACHE_KEY . '-main';
$cache = $this->cacheFactory->createDistributed(Application::APP_ID);
if ($cachedModels = $cache->get($cacheKey)) {
$this->logger->debug('Getting OpenAI models from distributed cache');
return $cachedModels;

// try to get models from the user cache first
if ($userId !== null) {
$userCachedModels = $cache->get($userCacheKey);
if ($userCachedModels) {
$this->logger->debug('Getting OpenAI models from user cache for user ' . $userId);
return $userCachedModels;
}
}

// if the user has an API key or uses basic auth, skip the admin cache
if (!(
$this->openAiSettingsService->getUserApiKey($userId, false) !== ''
|| (
$this->openAiSettingsService->getUseBasicAuth()
&& $this->openAiSettingsService->getUserBasicUser($userId) !== ''
&& $this->openAiSettingsService->getUserBasicPassword($userId) !== ''
)
)) {
// if no user cache or userId is null, try to get from the admin cache
if ($adminCachedModels = $cache->get($adminCacheKey)) {
$this->logger->debug('Getting OpenAI models from the main distributed cache');
return $adminCachedModels;
}
}

try {
Expand All @@ -149,7 +171,7 @@ public function getModels(string $userId): array {
throw new Exception($this->l10n->t('Invalid models response received'), Http::STATUS_INTERNAL_SERVER_ERROR);
}

$cache->set($cacheKey, $modelsResponse, Application::MODELS_CACHE_TTL);
$cache->set($userId !== null ? $userCacheKey : $adminCacheKey, $modelsResponse, Application::MODELS_CACHE_TTL);
$this->modelsMemoryCache = $modelsResponse;
$this->areCredsValid = true;
return $modelsResponse;
Expand All @@ -175,9 +197,6 @@ private function hasOwnOpenAiApiKey(string $userId): bool {
* @return array
*/
public function getModelEnumValues(?string $userId): array {
if ($userId === null) {
return [];
}
try {
$modelResponse = $this->getModels($userId);
$modelEnumValues = array_map(function (array $model) {
Expand Down
Loading