Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@composer bin all install --ansi",
"grep -r 'OCA\\\\OpenAi\\\\Vendor' ./vendor || vendor/bin/php-scoper add-prefix --prefix='OCA\\OpenAi\\Vendor' --output-dir=\".\" --working-dir=\"./vendor/\" -f --config=\"../scoper.inc.php\"",
"composer dump-autoload",
"composer update --no-scripts"
"composer install --no-scripts"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 We should also do this in integration_replicate, I think

]
},
"require-dev": {
Expand Down
35 changes: 22 additions & 13 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,24 +884,37 @@ public function requestImageCreation(
* @return array
*/
public function getImageRequestOptions(?string $userId): array {
$timeout = $this->openAiSettingsService->getRequestTimeout();
$requestOptions = [
'timeout' => $timeout,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timeout should always have some value. When ImageRetrieval isn't authenticated timeout will not be defined.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the last commit.

'timeout' => $this->openAiSettingsService->getRequestTimeout(),
'headers' => [
'User-Agent' => Application::USER_AGENT,
],
];

if ($this->openAiSettingsService->getIsImageRetrievalAuthenticated()) {
$useBasicAuth = $this->openAiSettingsService->getUseBasicAuth();
if ($useBasicAuth) {
if ($this->openAiSettingsService->imageOverrideEnabled()) {
$useBasicAuth = $this->openAiSettingsService->getAdminImageUseBasicAuth();

$apiKey = $this->openAiSettingsService->getAdminImageApiKey();
$basicUser = $this->openAiSettingsService->getAdminImageBasicUser();
$basicPassword = $this->openAiSettingsService->getAdminImageBasicPassword();

$requestOptions['timeout'] = $this->openAiSettingsService->getImageRequestTimeout();
} else {
// image service settings are not overridden
$useBasicAuth = $this->openAiSettingsService->getUseBasicAuth();

// this has no equivalent when the service URL is overridden
// so the user-defined credentials will be ignored
$apiKey = $this->openAiSettingsService->getUserApiKey($userId, true);
$basicUser = $this->openAiSettingsService->getUserBasicUser($userId, true);
$basicPassword = $this->openAiSettingsService->getUserBasicPassword($userId, true);
}
if ($useBasicAuth) {
if ($basicUser !== '' && $basicPassword !== '') {
$requestOptions['headers']['Authorization'] = 'Basic ' . base64_encode($basicUser . ':' . $basicPassword);
}
} else {
$apiKey = $this->openAiSettingsService->getUserApiKey($userId, true);
$requestOptions['headers']['Authorization'] = 'Bearer ' . $apiKey;
}
}
Expand Down Expand Up @@ -1007,15 +1020,11 @@ public function updateExpImgProcessingTime(int $runtime): void {
* @return array decoded request result or error
* @throws Exception
*/
public function request(?string $userId, string $endPoint, array $params = [], string $method = 'GET', ?string $contentType = null, bool $logErrors = true, ?string $serviceType = null): array {
public function request(
?string $userId, string $endPoint, array $params = [], string $method = 'GET',
?string $contentType = null, bool $logErrors = true, ?string $serviceType = null,
): array {
try {
$serviceUrl = '';
$apiKey = '';
$basicUser = '';
$basicPassword = '';
$useBasicAuth = false;
$timeout = 0;

if ($serviceType === Application::SERVICE_TYPE_IMAGE && $this->openAiSettingsService->imageOverrideEnabled()) {
$serviceUrl = $this->openAiSettingsService->getImageServiceUrl();
$apiKey = $this->openAiSettingsService->getAdminImageApiKey();
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/OpenAiSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@ public function getTranslationProviderEnabled(): bool {
*/
public function getIsImageRetrievalAuthenticated(): bool {
$serviceUrl = $this->getServiceUrl();
// the image_request_auth default depends on the service used for image generation
// if we override it, we check the one we are really gonna use
if ($this->imageOverrideEnabled()) {
$serviceUrl = $this->getImageServiceUrl();
}
$isUsingOpenAI = $serviceUrl === '' || $serviceUrl === Application::OPENAI_API_BASE_URL;
$default = $isUsingOpenAI ? '0' : '1';
return $this->appConfig->getValueString(Application::APP_ID, 'image_request_auth', $default, lazy: true) === '1';
Expand Down
Loading