Fix server crash if multimodal prompt contains no image

This commit is contained in:
Cohee 2023-12-14 21:21:37 +02:00
parent 2045e414d1
commit eec28469f8
2 changed files with 6 additions and 3 deletions

View File

@ -1497,7 +1497,7 @@
</span> </span>
</div> </div>
</div> </div>
<div class="range-block" data-source="openai,openrouter"> <div class="range-block" data-source="openai,openrouter,makersuite">
<label for="openai_image_inlining" class="checkbox_label flexWrap widthFreeExpand"> <label for="openai_image_inlining" class="checkbox_label flexWrap widthFreeExpand">
<input id="openai_image_inlining" type="checkbox" /> <input id="openai_image_inlining" type="checkbox" />
<span data-i18n="Send inline images">Send inline images</span> <span data-i18n="Send inline images">Send inline images</span>

View File

@ -79,6 +79,8 @@ function convertClaudePrompt(messages, addHumanPrefix, addAssistantPostfix, with
* @returns {object[]} Prompt for Google MakerSuite models * @returns {object[]} Prompt for Google MakerSuite models
*/ */
function convertGooglePrompt(messages, model) { function convertGooglePrompt(messages, model) {
// This is a 1x1 transparent PNG
const PNG_PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
const contents = []; const contents = [];
let lastRole = ''; let lastRole = '';
let currentText = ''; let currentText = '';
@ -91,14 +93,15 @@ function convertGooglePrompt(messages, model) {
return role + message.content; return role + message.content;
}).join('\n\n').trim(); }).join('\n\n').trim();
const imageEntry = messages.find((message) => message.content[1]?.image_url); const imageEntry = messages.find((message) => message.content?.[1]?.image_url);
const imageData = imageEntry?.content?.[1]?.image_url?.data ?? PNG_PIXEL;
contents.push({ contents.push({
parts: [ parts: [
{ text: combinedText }, { text: combinedText },
{ {
inlineData: { inlineData: {
mimeType: 'image/png', mimeType: 'image/png',
data: imageEntry.content[1].image_url.url ?? '', data: imageData,
}, },
}, },
], ],