diff --git a/lib/Service/OpenAiAPIService.php b/lib/Service/OpenAiAPIService.php index 1c4bede7..14208d9d 100644 --- a/lib/Service/OpenAiAPIService.php +++ b/lib/Service/OpenAiAPIService.php @@ -496,24 +496,33 @@ public function createChatCompletion( $messages[] = $message; } } - if ($userPrompt !== null || $userAudioPromptBase64 !== null) { - $message = ['role' => 'user', 'content' => []]; + if ($userAudioPromptBase64 !== null) { + // if there is audio, use the new message format (content is a list of objects) + $message = [ + 'role' => 'user', + 'content' => [ + [ + 'type' => 'input_audio', + 'input_audio' => [ + 'data' => $userAudioPromptBase64, + 'format' => 'mp3', + ], + ], + ], + ]; if ($userPrompt !== null) { $message['content'][] = [ 'type' => 'text', 'text' => $userPrompt, ]; } - if ($userAudioPromptBase64 !== null) { - $message['content'][] = [ - 'type' => 'input_audio', - 'input_audio' => [ - 'data' => $userAudioPromptBase64, - 'format' => 'mp3', - ], - ]; - } $messages[] = $message; + } elseif ($userPrompt !== null) { + // if there is only text, use the old message format (content is a string) + $messages[] = [ + 'role' => 'user', + 'content' => $userPrompt, + ]; } if ($toolMessage !== null) { $msgs = json_decode($toolMessage, true); diff --git a/tests/unit/Providers/OpenAiProviderTest.php b/tests/unit/Providers/OpenAiProviderTest.php index fe0d2bd5..1d9d694b 100644 --- a/tests/unit/Providers/OpenAiProviderTest.php +++ b/tests/unit/Providers/OpenAiProviderTest.php @@ -140,7 +140,7 @@ public function testFreePromptProvider(): void { $options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']]; $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, - 'messages' => [['role' => 'user', 'content' => [['type' => 'text', 'text' => $prompt]]]], + 'messages' => [['role' => 'user', 'content' => $prompt]], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'user' => self::TEST_USER1, @@ -204,7 +204,7 @@ public function testEmojiProvider(): void { $message = 'Give me an emoji for the following text. Output only the emoji without any other characters.' . "\n\n" . $prompt; $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, - 'messages' => [['role' => 'user', 'content' => [['type' => 'text', 'text' => $message]]]], + 'messages' => [['role' => 'user', 'content' => $message]], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'user' => self::TEST_USER1, @@ -269,7 +269,7 @@ public function testHeadlineProvider(): void { $message = 'Give me the headline of the following text in its original language. Do not output the language. Output only the headline without any quotes or additional punctuation.' . "\n\n" . $prompt; $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, - 'messages' => [['role' => 'user', 'content' => [['type' => 'text', 'text' => $message]]]], + 'messages' => [['role' => 'user', 'content' => $message]], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'user' => self::TEST_USER1, @@ -334,7 +334,7 @@ public function testChangeToneProvider(): void { $message = "Reformulate the following text in a $toneInput tone in its original language. Output only the reformulation. Here is the text:" . "\n\n" . $textInput . "\n\n" . 'Do not mention the used language in your reformulation. Here is your reformulation in the same language:'; $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, - 'messages' => [['role' => 'user', 'content' => [['type' => 'text', 'text' => $message]]]], + 'messages' => [['role' => 'user', 'content' => $message]], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'user' => self::TEST_USER1, @@ -402,7 +402,7 @@ public function testSummaryProvider(): void { 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [ ['role' => 'system', 'content' => $systemPrompt], - ['role' => 'user', 'content' => [['type' => 'text', 'text' => $prompt]]], + ['role' => 'user', 'content' => $prompt], ], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, @@ -469,7 +469,7 @@ public function testProofreadProvider(): void { 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [ ['role' => 'system', 'content' => $systemPrompt], - ['role' => 'user', 'content' => [['type' => 'text', 'text' => $prompt]]], + ['role' => 'user', 'content' => $prompt], ], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, @@ -539,7 +539,7 @@ public function testTranslationProvider(): void { $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [ - ['role' => 'user', 'content' => [['type' => 'text', 'text' => 'Translate from ' . $fromLang . ' to English (US): ' . $inputText]]], + ['role' => 'user', 'content' => 'Translate from ' . $fromLang . ' to English (US): ' . $inputText], ], 'n' => $n, 'max_completion_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,