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
4 changes: 3 additions & 1 deletion lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ public function createCompletion(
* @param string|null $toolMessage JSON string with role, content, tool_call_id
* @param array|null $tools
* @param string|null $userAudioPromptBase64
* @param string|null $userAudioPromptFormat
* @return array{messages: array<string>, tool_calls: array<string>, audio_messages: list<array<string, mixed>>}
* @throws Exception
*/
Expand All @@ -505,6 +506,7 @@ public function createChatCompletion(
?string $toolMessage = null,
?array $tools = null,
?string $userAudioPromptBase64 = null,
?string $userAudioPromptFormat = null,
): array {
if ($this->isQuotaExceeded($userId, Application::QUOTA_TYPE_TEXT)) {
throw new Exception($this->l10n->t('Text generation quota exceeded'), Http::STATUS_TOO_MANY_REQUESTS);
Expand Down Expand Up @@ -560,7 +562,7 @@ public function createChatCompletion(
'type' => 'input_audio',
'input_audio' => [
'data' => $userAudioPromptBase64,
'format' => 'wav',
'format' => $userAudioPromptFormat ?? 'wav',
],
],
],
Expand Down
13 changes: 12 additions & 1 deletion lib/TaskProcessing/AudioToAudioChatProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

class AudioToAudioChatProvider implements ISynchronousProvider {

// OpenAI supports wav and mp3
// https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages
private const SUPPORTED_INPUT_AUDIO_FORMATS = [
'audio/mp3' => 'mp3',
'audio/mpeg' => 'mp3',
'audio/wav' => 'wav',
'audio/x-wav' => 'wav',
];

public function __construct(
private OpenAiAPIService $openAiAPIService,
private IL10N $l,
Expand Down Expand Up @@ -213,6 +222,8 @@ private function oneStep(
string $sttModel, string $llmModel, string $ttsModel, float $speed, string $serviceName,
): array {
$result = [];
$audioInputMimetype = mime_content_type($inputFile->fopen('rb'));
$audioInputFormat = self::SUPPORTED_INPUT_AUDIO_FORMATS[$audioInputMimetype] ?? 'wav';
$b64Audio = base64_encode($inputFile->getContent());
$extraParams = [
'modalities' => ['text', 'audio'],
Expand All @@ -221,7 +232,7 @@ private function oneStep(
$systemPrompt .= ' Producing text responses will break the user interface. Important: You have multimodal voice capability, and you use voice exclusively to respond.';
$completion = $this->openAiAPIService->createChatCompletion(
$userId, $llmModel, null, $systemPrompt, $history, 1, 1000,
$extraParams, null, null, $b64Audio,
$extraParams, null, null, $b64Audio, $audioInputFormat
);
$message = array_pop($completion['audio_messages']);
// TODO find a way to force the model to answer with audio when there is only text in the history
Expand Down
Loading