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
31 changes: 20 additions & 11 deletions lib/Service/OpenAiAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading