Audio inlining for OpenAI and Custom-compatible (#4964)
* Audio inlining for OpenAI and Custom-compatible * Add context sizes * chatgpt-image-latest * Add quality control for gpt-image
This commit is contained in:
+1
-1
@@ -2024,7 +2024,7 @@
|
||||
<i class="icon-supported fa-solid fa-film" title="Supported by the current model" data-i18n="[title]Supported by the current model"></i>
|
||||
<i class="icon-unsupported fa-solid fa-film" title="Unsupported by the current model" data-i18n="[title]Unsupported by the current model"></i>
|
||||
</div>
|
||||
<div id="openai_audio_inlining_supported" data-source="makersuite,vertexai,openrouter">
|
||||
<div id="openai_audio_inlining_supported" data-source="makersuite,vertexai,openrouter,openai,custom">
|
||||
<i class="icon-supported fa-solid fa-music" title="Supported by the current model" data-i18n="[title]Supported by the current model"></i>
|
||||
<i class="icon-unsupported fa-solid fa-music" title="Unsupported by the current model" data-i18n="[title]Unsupported by the current model"></i>
|
||||
</div>
|
||||
|
||||
@@ -320,6 +320,7 @@ const defaultSettings = {
|
||||
// OpenAI settings
|
||||
openai_style: 'vivid',
|
||||
openai_quality: 'standard',
|
||||
openai_quality_gpt: 'auto',
|
||||
openai_duration: '8',
|
||||
|
||||
style: 'Default',
|
||||
@@ -528,6 +529,7 @@ async function loadSettings() {
|
||||
$('#sd_interactive_mode').prop('checked', extension_settings.sd.interactive_mode);
|
||||
$('#sd_openai_style').val(extension_settings.sd.openai_style);
|
||||
$('#sd_openai_quality').val(extension_settings.sd.openai_quality);
|
||||
$('#sd_openai_quality_gpt').val(extension_settings.sd.openai_quality_gpt);
|
||||
$('#sd_openai_duration').val(extension_settings.sd.openai_duration);
|
||||
$('#sd_comfy_type').val(extension_settings.sd.comfy_type);
|
||||
$('#sd_comfy_url').val(extension_settings.sd.comfy_url);
|
||||
@@ -1850,7 +1852,7 @@ function switchModelSpecificControls(modelId) {
|
||||
|
||||
modelControls.each(function () {
|
||||
const models = String($(this).attr('data-sd-model') || '').split(',').map(m => m.trim());
|
||||
$(this).toggle(models.includes(modelId));
|
||||
$(this).toggle(models.some(m => modelId.includes(m)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2169,6 +2171,7 @@ async function loadOpenAiModels() {
|
||||
{ value: 'gpt-image-1.5', text: 'gpt-image-1.5' },
|
||||
{ value: 'gpt-image-1-mini', text: 'gpt-image-1-mini' },
|
||||
{ value: 'gpt-image-1', text: 'gpt-image-1' },
|
||||
{ value: 'chatgpt-image-latest', text: 'chatgpt-image-latest' },
|
||||
{ value: 'dall-e-3', text: 'dall-e-3' },
|
||||
{ value: 'dall-e-2', text: 'dall-e-2' },
|
||||
{ value: 'sora-2', text: 'sora-2' },
|
||||
@@ -3697,7 +3700,7 @@ async function generateOpenAiImage(prompt, signal) {
|
||||
|
||||
const isDalle2 = /dall-e-2/.test(extension_settings.sd.model);
|
||||
const isDalle3 = /dall-e-3/.test(extension_settings.sd.model);
|
||||
const isGptImg = /gpt-image-1/.test(extension_settings.sd.model);
|
||||
const isGptImg = /gpt-image-(1|latest)/.test(extension_settings.sd.model);
|
||||
const isSora2 = /sora-2/.test(extension_settings.sd.model);
|
||||
|
||||
if (isDalle2 && prompt.length > dalle2PromptLimit) {
|
||||
@@ -3770,7 +3773,7 @@ async function generateOpenAiImage(prompt, signal) {
|
||||
model: extension_settings.sd.model,
|
||||
size: `${width}x${height}`,
|
||||
n: 1,
|
||||
quality: isDalle3 ? extension_settings.sd.openai_quality : undefined,
|
||||
quality: isDalle3 ? extension_settings.sd.openai_quality : (isGptImg ? extension_settings.sd.openai_quality_gpt : undefined),
|
||||
style: isDalle3 ? extension_settings.sd.openai_style : undefined,
|
||||
response_format: isDalle2 || isDalle3 ? 'b64_json' : undefined,
|
||||
moderation: isGptImg ? 'low' : undefined,
|
||||
@@ -5309,6 +5312,10 @@ jQuery(async () => {
|
||||
extension_settings.sd.electronhub_quality = String($(this).val());
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
$('#sd_openai_quality_gpt').on('input', function () {
|
||||
extension_settings.sd.openai_quality_gpt = String($(this).val());
|
||||
saveSettingsDebounced();
|
||||
});
|
||||
|
||||
if (!CSS.supports('field-sizing', 'content')) {
|
||||
$('.sd_settings .inline-drawer-toggle').on('click', function () {
|
||||
|
||||
@@ -178,7 +178,16 @@
|
||||
<option value="natural">Natural</option>
|
||||
</select>
|
||||
</div>
|
||||
<div data-sd-model="dall-e-3,cogview-4-250304" class="flex1">
|
||||
<div data-sd-model="gpt-image" class="flex1">
|
||||
<label for="sd_openai_quality_gpt" data-i18n="Image Quality">Image Quality</label>
|
||||
<select id="sd_openai_quality_gpt">
|
||||
<option value="auto" data-i18n="Auto">Auto</option>
|
||||
<option value="low" data-i18n="Low">Low</option>
|
||||
<option value="medium" data-i18n="Medium">Medium</option>
|
||||
<option value="high" data-i18n="High">High</option>
|
||||
</select>
|
||||
</div>
|
||||
<div data-sd-model="dall-e-3,cogview-4" class="flex1">
|
||||
<label for="sd_openai_quality" data-i18n="Image Quality">Image Quality</label>
|
||||
<select id="sd_openai_quality">
|
||||
<option value="standard" data-i18n="Standard">Standard</option>
|
||||
|
||||
@@ -4689,6 +4689,9 @@ function getMaxContextOpenAI(value) {
|
||||
else if (value.includes('gpt-4.1')) {
|
||||
return max_1mil;
|
||||
}
|
||||
else if (value.includes('gpt-audio')) {
|
||||
return max_128k;
|
||||
}
|
||||
else if (value.startsWith('o1')) {
|
||||
return max_128k;
|
||||
}
|
||||
@@ -4707,6 +4710,9 @@ function getMaxContextOpenAI(value) {
|
||||
else if (['gpt-4-32k', 'gpt-4-32k-0314', 'gpt-4-32k-0613'].includes(value)) {
|
||||
return max_32k;
|
||||
}
|
||||
else if (value.includes('gpt-realtime')) {
|
||||
return max_32k;
|
||||
}
|
||||
else if (['gpt-3.5-turbo-16k', 'gpt-3.5-turbo-16k-0613'].includes(value)) {
|
||||
return max_16k;
|
||||
}
|
||||
@@ -5946,21 +5952,30 @@ export function isAudioInliningSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only Gemini models support audio for now
|
||||
const audioSupportedModels = [
|
||||
'gemini-2.0',
|
||||
'gemini-2.5',
|
||||
'gemini-3',
|
||||
'gemini-exp-1206',
|
||||
'gpt-4o-audio',
|
||||
'gpt-4o-realtime',
|
||||
'gpt-4o-mini-audio',
|
||||
'gpt-4o-mini-realtime',
|
||||
'gpt-audio',
|
||||
'gpt-realtime',
|
||||
];
|
||||
|
||||
switch (oai_settings.chat_completion_source) {
|
||||
case chat_completion_sources.OPENAI:
|
||||
return audioSupportedModels.some(model => oai_settings.openai_model.includes(model));
|
||||
case chat_completion_sources.MAKERSUITE:
|
||||
return audioSupportedModels.some(model => oai_settings.google_model.includes(model));
|
||||
case chat_completion_sources.VERTEXAI:
|
||||
return audioSupportedModels.some(model => oai_settings.vertexai_model.includes(model));
|
||||
case chat_completion_sources.OPENROUTER:
|
||||
return (Array.isArray(model_list) && model_list.find(m => m.id === oai_settings.openrouter_model)?.architecture?.input_modalities?.includes('audio'));
|
||||
case chat_completion_sources.CUSTOM:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2063,6 +2063,8 @@ router.post('/generate', async function (request, response) {
|
||||
if (getConfigValue('openai.randomizeUserId', false, 'boolean')) {
|
||||
bodyParams['user'] = uuidv4();
|
||||
}
|
||||
|
||||
embedOpenRouterMedia(request.body.messages, { audio: true, video: false });
|
||||
} else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.OPENROUTER) {
|
||||
apiUrl = 'https://openrouter.ai/api/v1';
|
||||
apiKey = readSecret(request.user.directories, SECRET_KEYS.OPENROUTER);
|
||||
@@ -2122,7 +2124,7 @@ router.post('/generate', async function (request, response) {
|
||||
const enableGeminiSystemPromptCache = getConfigValue('gemini.enableSystemPromptCache', false, 'boolean');
|
||||
|
||||
if (Array.isArray(request.body.messages)) {
|
||||
embedOpenRouterMedia(request.body.messages);
|
||||
embedOpenRouterMedia(request.body.messages, { audio: true, video: true });
|
||||
addOpenRouterSignatures(request.body.messages, request.body.model);
|
||||
|
||||
if (isClaude) {
|
||||
@@ -2160,6 +2162,7 @@ router.post('/generate', async function (request, response) {
|
||||
|
||||
mergeObjectWithYaml(bodyParams, request.body.custom_include_body);
|
||||
mergeObjectWithYaml(headers, request.body.custom_include_headers);
|
||||
embedOpenRouterMedia(request.body.messages, { audio: true, video: false });
|
||||
} else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.PERPLEXITY) {
|
||||
apiUrl = API_PERPLEXITY;
|
||||
apiKey = readSecret(request.user.directories, SECRET_KEYS.PERPLEXITY);
|
||||
|
||||
@@ -1296,10 +1296,14 @@ export function calculateGoogleBudgetTokens(maxTokens, reasoningEffort, model) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Embed media content in OpenRouter messages.
|
||||
* Embed media content in OpenRouter messages (OpenAI-compatible).
|
||||
* @param {object[]} messages Array of messages
|
||||
* @param {object} options Options for embedding
|
||||
* @param {boolean} [options.audio] Enable audio embedding (default: true)
|
||||
* @param {boolean} [options.video] Enable video embedding (default: true)
|
||||
* @returns {void}
|
||||
*/
|
||||
export function embedOpenRouterMedia(messages) {
|
||||
export function embedOpenRouterMedia(messages, { audio = true, video = true } = { audio: true, video: true }) {
|
||||
if (!Array.isArray(messages)) {
|
||||
return;
|
||||
}
|
||||
@@ -1310,11 +1314,11 @@ export function embedOpenRouterMedia(messages) {
|
||||
}
|
||||
|
||||
for (const contentPart of message.content) {
|
||||
if (contentPart?.type === 'video_url' && contentPart.video_url?.url?.startsWith('data:')) {
|
||||
if (video && contentPart?.type === 'video_url' && contentPart.video_url?.url?.startsWith('data:')) {
|
||||
contentPart.type = 'input_video';
|
||||
}
|
||||
|
||||
if (contentPart?.type === 'audio_url' && contentPart.audio_url?.url?.startsWith('data:')) {
|
||||
if (audio && contentPart?.type === 'audio_url' && contentPart.audio_url?.url?.startsWith('data:')) {
|
||||
const formatMap = {
|
||||
'audio/mpeg': 'mp3',
|
||||
'audio/wav': 'wav',
|
||||
|
||||
Reference in New Issue
Block a user