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

Choose stt model in admin settings #154

Merged
merged 5 commits into from
Nov 18, 2024
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
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
string $appName,
IRequest $request,
private OpenAiSettingsService $openAiSettingsService,
private ?string $userId
private ?string $userId,
) {
parent::__construct($appName, $request);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/OldProcessing/Translation/TranslationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public function translate(?string $fromLanguage, string $toLanguage, string $tex

$toLanguage = $coreLanguages[$toLanguage];
if ($fromLanguage !== null) {
$this->logger->debug('OpenAI translation FROM['.$fromLanguage.'] TO['.$toLanguage.']', ['app' => Application::APP_ID]);
$this->logger->debug('OpenAI translation FROM[' . $fromLanguage . '] TO[' . $toLanguage . ']', ['app' => Application::APP_ID]);
$fromLanguage = $coreLanguages[$fromLanguage] ?? $fromLanguage;
$prompt = 'Translate from ' . $fromLanguage . ' to ' . $toLanguage . ': ' . $text;
} else {
$this->logger->debug('OpenAI translation TO['.$toLanguage.']', ['app' => Application::APP_ID]);
$this->logger->debug('OpenAI translation TO[' . $toLanguage . ']', ['app' => Application::APP_ID]);
$prompt = 'Translate to ' . $toLanguage . ': ' . $text;
}
$adminModel = $this->config->getAppValue(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID;
Expand Down
16 changes: 10 additions & 6 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
private ICacheFactory $cacheFactory,
private QuotaUsageMapper $quotaUsageMapper,
private OpenAiSettingsService $openAiSettingsService,
IClientService $clientService
IClientService $clientService,
) {
$this->client = $clientService->newClient();
}
Expand Down Expand Up @@ -470,7 +470,7 @@ public function transcribeBase64Mp3(
?string $userId,
string $audioBase64,
bool $translate = true,
string $model = Application::DEFAULT_TRANSCRIPTION_MODEL_ID
string $model = Application::DEFAULT_MODEL_ID,
): string {
return $this->transcribe(
$userId,
Expand All @@ -491,7 +491,7 @@ public function transcribeFile(
?string $userId,
File $file,
bool $translate = false,
string $model = Application::DEFAULT_TRANSCRIPTION_MODEL_ID
string $model = Application::DEFAULT_MODEL_ID,
): string {
try {
$transcriptionResponse = $this->transcribe($userId, $file->getContent(), $translate, $model);
Expand All @@ -515,14 +515,18 @@ public function transcribe(
?string $userId,
string $audioFileContent,
bool $translate = true,
string $model = Application::DEFAULT_TRANSCRIPTION_MODEL_ID
string $model = Application::DEFAULT_MODEL_ID,
): string {
if ($this->isQuotaExceeded($userId, Application::QUOTA_TYPE_TRANSCRIPTION)) {
throw new Exception($this->l10n->t('Audio transcription quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
}
// enforce whisper for OpenAI
if ($this->isUsingOpenAi()) {
$model = Application::DEFAULT_TRANSCRIPTION_MODEL_ID;
}

$params = [
'model' => $model,
'model' => $model === Application::DEFAULT_MODEL_ID ? Application::DEFAULT_TRANSCRIPTION_MODEL_ID : $model,
'file' => $audioFileContent,
'response_format' => 'verbose_json',
// Verbose needed for extraction of audio duration
Expand Down Expand Up @@ -560,7 +564,7 @@ public function transcribe(
* @throws Exception
*/
public function requestImageCreation(
?string $userId, string $prompt, string $model, int $n = 1, string $size = Application::DEFAULT_DEFAULT_IMAGE_SIZE
?string $userId, string $prompt, string $model, int $n = 1, string $size = Application::DEFAULT_DEFAULT_IMAGE_SIZE,
): array {
if ($this->isQuotaExceeded($userId, Application::QUOTA_TYPE_IMAGE)) {
throw new Exception($this->l10n->t('Image generation quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
Expand Down
21 changes: 21 additions & 0 deletions lib/Service/OpenAiSettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OpenAiSettingsService {
'service_name' => 'string',
'api_key' => 'string',
'default_completion_model_id' => 'string',
'default_stt_model_id' => 'string',
'default_image_model_id' => 'string',
'default_image_size' => 'string',
'max_tokens' => 'integer',
Expand Down Expand Up @@ -109,6 +110,13 @@ public function getAdminDefaultCompletionModelId(): string {
return $this->appConfig->getValueString(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID;
}

/**
* @return string
*/
public function getAdminDefaultSttModelId(): string {
return $this->appConfig->getValueString(Application::APP_ID, 'default_stt_model_id') ?: Application::DEFAULT_MODEL_ID;
}

/**
* @return string
*/
Expand Down Expand Up @@ -263,6 +271,7 @@ public function getAdminConfig(): array {
'service_name' => $this->getServiceName(),
'api_key' => $this->getAdminApiKey(),
'default_completion_model_id' => $this->getAdminDefaultCompletionModelId(),
'default_stt_model_id' => $this->getAdminDefaultSttModelId(),
'default_image_model_id' => $this->getAdminDefaultImageModelId(),
'default_image_size' => $this->getAdminDefaultImageSize(),
'max_tokens' => $this->getMaxTokens(),
Expand Down Expand Up @@ -389,6 +398,15 @@ public function setAdminDefaultCompletionModelId(string $defaultCompletionModelI
$this->appConfig->setValueString(Application::APP_ID, 'default_completion_model_id', $defaultCompletionModelId);
}

/**
* @param string $defaultSttModelId
* @return void
*/
public function setAdminDefaultSttModelId(string $defaultSttModelId): void {
// No need to validate. As long as it's a string, we're happy campers
$this->appConfig->setValueString(Application::APP_ID, 'default_stt_model_id', $defaultSttModelId);
}

/**
* @param string $defaultImageModelId
* @return void
Expand Down Expand Up @@ -561,6 +579,9 @@ public function setAdminConfig(array $adminConfig): void {
if (isset($adminConfig['default_completion_model_id'])) {
$this->setAdminDefaultCompletionModelId($adminConfig['default_completion_model_id']);
}
if (isset($adminConfig['default_stt_model_id'])) {
$this->setAdminDefaultSttModelId($adminConfig['default_stt_model_id']);
}
if (isset($adminConfig['default_image_model_id'])) {
$this->setAdminDefaultImageModelId($adminConfig['default_image_model_id']);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Personal implements ISettings {
public function __construct(
private IInitialState $initialStateService,
private OpenAiSettingsService $openAiSettingsService,
private ?string $userId
private ?string $userId,
) {
}

Expand Down
6 changes: 5 additions & 1 deletion lib/TaskProcessing/AudioToTextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OCA\OpenAi\AppInfo\Application;
use OCA\OpenAi\Service\OpenAiAPIService;
use OCP\Files\File;
use OCP\IAppConfig;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\TaskTypes\AudioToText;
use Psr\Log\LoggerInterface;
Expand All @@ -18,6 +19,7 @@ class AudioToTextProvider implements ISynchronousProvider {
public function __construct(
private OpenAiAPIService $openAiAPIService,
private LoggerInterface $logger,
private IAppConfig $appConfig,
) {
}

Expand Down Expand Up @@ -75,8 +77,10 @@ public function process(?string $userId, array $input, callable $reportProgress)
}
$inputFile = $input['input'];

$model = $this->appConfig->getValueString(Application::APP_ID, 'default_stt_model_id', Application::DEFAULT_MODEL_ID) ?: Application::DEFAULT_MODEL_ID;

try {
$transcription = $this->openAiAPIService->transcribeFile($userId, $inputFile);
$transcription = $this->openAiAPIService->transcribeFile($userId, $inputFile, false, $model);
return ['output' => $transcription];
} catch (Exception $e) {
$this->logger->warning('OpenAI\'s Whisper transcription failed with: ' . $e->getMessage(), ['exception' => $e]);
Expand Down
2 changes: 1 addition & 1 deletion lib/TaskProcessing/TranslateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
$this->logger->debug('OpenAI translation FROM[' . $fromLanguage . '] TO[' . $toLanguage . ']', ['app' => Application::APP_ID]);
$prompt = 'Translate from ' . $fromLanguage . ' to ' . $toLanguage . ': ' . $inputText;
} else {
$this->logger->debug('OpenAI translation TO['.$toLanguage.']', ['app' => Application::APP_ID]);
$this->logger->debug('OpenAI translation TO[' . $toLanguage . ']', ['app' => Application::APP_ID]);
$prompt = 'Translate to ' . $toLanguage . ': ' . $inputText;
}

Expand Down
Loading