Skip to content
Open
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 lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ private function buildChatCompletionRequestParams(
}
$content = [];
foreach ($files as $file) {
$content = array_merge($content, $this->openAiFileService->buildFileContentFromFile($file, $service));
$content = array_merge($content, $this->openAiFileService->buildFileContentFromFile($file, $service, $userId));
}
if ($userPrompt !== null) {
$content[] = [
Expand Down
66 changes: 55 additions & 11 deletions lib/Service/OpenAiFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use OCP\TaskProcessing\Exception\ProcessingException;
use OCP\TaskProcessing\Exception\UserFacingProcessingException;
use OCP\TaskProcessing\IManager as ITaskProcessingManager;
use OCP\TaskProcessing\Task;
use OCP\TaskProcessing\TaskTypes\AudioToText;
use Psr\Log\LoggerInterface;

class OpenAiFileService {
Expand Down Expand Up @@ -107,7 +109,7 @@ public function buildFileContentFromId(int $fileId, ?string $userId, ?int $taskI
$userFolder = $this->rootFolder->getUserFolder($userId);
$file = $userFolder->getFirstNodeById($fileId);
}
return $this->buildFileContentFromFile($file, $service);
return $this->buildFileContentFromFile($file, $service, $userId);
}

/**
Expand All @@ -119,7 +121,7 @@ public function buildFileContentFromId(int $fileId, ?string $userId, ?int $taskI
* @throws ProcessingException
* @throws UserFacingProcessingException
*/
public function buildFileContentFromFile(?File $file, ServiceConfig $service): array {
public function buildFileContentFromFile(?File $file, ServiceConfig $service, ?string $userId = null): array {
if (!$file instanceof File || !$file->isReadable()) {
throw new ProcessingException('File is not readable');
}
Expand All @@ -142,7 +144,7 @@ public function buildFileContentFromFile(?File $file, ServiceConfig $service): a
return $this->buildImageContent($file, $fileType, $service);
// OpenAI only supports this for very specific models and support is not that common
} elseif (str_starts_with($fileType, 'audio/')) {
return $this->buildAudioContent($file, $fileType, $service);
return $this->buildAudioContent($file, $fileType, $service, $userId);
// OpenAI does not currently support video attachments
} elseif (str_starts_with($fileType, 'video/')) {
return $this->buildVideoContent($file, $fileType, $service);
Expand Down Expand Up @@ -184,16 +186,58 @@ private function buildImageContent(File $file, string $fileType, ServiceConfig $
}

/**
* @return list<array{type: string, input_audio: array{data: string, format: string}}>
* @return list<array{type: string, input_audio?: array{data: string, format: string}, text?: string}>
*/
private function buildAudioContent(File $file, string $fileType, ServiceConfig $service): array {
private function buildAudioContent(File $file, string $fileType, ServiceConfig $service, ?string $userId): array {
if (!$service->getMultimodalAudioEnabled()) {
throw new UserFacingProcessingException(
'Audio attachments are disabled',
0,
null,
$this->l10n->t('Audio attachments are unsupported.'),
);
if (!array_key_exists(AudioToText::ID, $this->taskProcessingManager->getAvailableTaskTypes())) {
throw new UserFacingProcessingException(
'Audio attachments are disabled',
0,
null,
$this->l10n->t('Audio attachments are unsupported.'),
);
}
$customTaskID = 'multimodal_fallback:' . $file->getId();

$potentialTasks = $this->taskProcessingManager->getTasks($userId, AudioToText::ID, 'integration_openai', $customTaskID, Task::STATUS_SUCCESSFUL);
if (count($potentialTasks) > 0) {
$resultTask = $potentialTasks[0];
} else {
$task = new Task(
AudioToText::ID,
[
'input' => $file->getId(),
],
'integration_openai',
$userId,
$customTaskID,
);
$this->logger->info('Falling back to transcribing the audio for file {file}', ['file' => $file->getName()]);
try {
$resultTask = $this->taskProcessingManager->runTask($task);
} catch (\Throwable $e) {
$this->logger->error('Failed to transcribe audio for file {file}', ['file' => $file->getName(), 'exception' => $e]);
throw new UserFacingProcessingException(
'Failed to transcribe audio',
0,
null,
$this->l10n->t('Failed to transcribe audio.'),
);
}
}
if ($resultTask->getStatus() !== Task::STATUS_SUCCESSFUL) {
throw new UserFacingProcessingException(
'Failed to transcribe audio',
0,
null,
$this->l10n->t('Failed to transcribe audio.'),
);
}
return [[
'type' => 'text',
'text' => 'Filename:' . $file->getName() . "\nTranscription:\n" . $resultTask->getOutput()['output'],

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.

The force push fixed a typo here the \n was missing the n for one of them

]];
}

if (!array_key_exists($fileType, self::SUPPORTED_INPUT_AUDIO_FORMATS)) {
Expand Down
Loading