Move endpoint version to conifg. Refactor ugli model lists

61c7f53d22ce351e3d7fcbf4c3a3a0922de4acdc

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +30 -23Showing whitespace changes
default/config.yaml+4 -0
@@ -234,6 +234,10 @@ claude:
234 # should be ideal for most use cases.234 # should be ideal for most use cases.
235 # Any value other than a non-negative integer will be ignored and caching at depth will not be enabled.235 # Any value other than a non-negative integer will be ignored and caching at depth will not be enabled.
236 cachingAtDepth: -1236 cachingAtDepth: -1
237# -- GOOGLE GEMINI API CONFIGURATION --
238gemini:
239 # API endpoint version ("v1beta" or "v1alpha")
240 apiVersion: 'v1beta'
237# -- SERVER PLUGIN CONFIGURATION --241# -- SERVER PLUGIN CONFIGURATION --
238enableServerPlugins: false242enableServerPlugins: false
239# Attempt to automatically update server plugins on startup243# Attempt to automatically update server plugins on startup
src/endpoints/backends/chat-completions.js+26 -23
@@ -343,7 +343,6 @@ async function sendMakerSuiteRequest(request, response) {
343 const enableWebSearch = Boolean(request.body.enable_web_search);343 const enableWebSearch = Boolean(request.body.enable_web_search);
344 const requestImages = Boolean(request.body.request_images);344 const requestImages = Boolean(request.body.request_images);
345 const reasoningEffort = String(request.body.reasoning_effort);345 const reasoningEffort = String(request.body.reasoning_effort);
346 const isThinking = model.includes('thinking');
347 const isGemma = model.includes('gemma');346 const isGemma = model.includes('gemma');
348 const isLearnLM = model.includes('learnlm');347 const isLearnLM = model.includes('learnlm');
349348
@@ -359,43 +358,52 @@ async function sendMakerSuiteRequest(request, response) {
359 };358 };
360359
361 function getGeminiBody() {360 function getGeminiBody() {
361 // #region UGLY MODEL LISTS AREA
362 const imageGenerationModels = [
363 'gemini-2.0-flash-exp',
364 'gemini-2.0-flash-exp-image-generation',
365 ];
366
367 const blockNoneModels = [
368 'gemini-1.5-pro-001',
369 'gemini-1.5-flash-001',
370 'gemini-1.5-flash-8b-exp-0827',
371 'gemini-1.5-flash-8b-exp-0924',
372 ];
373
374 const thinkingBudgetModels = [
375 'gemini-2.5-flash-preview-04-17',
376 ];
377 // #endregion
378
362 if (!Array.isArray(generationConfig.stopSequences) || !generationConfig.stopSequences.length) {379 if (!Array.isArray(generationConfig.stopSequences) || !generationConfig.stopSequences.length) {
363 delete generationConfig.stopSequences;380 delete generationConfig.stopSequences;
364 }381 }
365382
366 const useMultiModal = requestImages && ['gemini-2.0-flash-exp', 'gemini-2.0-flash-exp-image-generation'].includes(model);383 const enableImageModality = requestImages && imageGenerationModels.includes(model);
367 if (useMultiModal) {384 if (enableImageModality) {
368 generationConfig.responseModalities = ['text', 'image'];385 generationConfig.responseModalities = ['text', 'image'];
369 }386 }
370387
371 const useSystemPrompt = !useMultiModal && (388 const useSystemPrompt = !enableImageModality && !isGemma && request.body.use_makersuite_sysprompt;
372 model.includes('gemini-2.5-pro') ||
373 model.includes('gemini-2.5-flash') ||
374 model.includes('gemini-2.0-pro') ||
375 model.includes('gemini-2.0-flash') ||
376 model.includes('gemini-1.5-pro') ||
377 model.includes('gemini-1.5-flash') ||
378 model.includes('learnlm') ||
379 model.startsWith('gemini-exp')
380 ) && request.body.use_makersuite_sysprompt;
381389
382 const tools = [];390 const tools = [];
383 const prompt = convertGooglePrompt(request.body.messages, model, useSystemPrompt, getPromptNames(request));391 const prompt = convertGooglePrompt(request.body.messages, model, useSystemPrompt, getPromptNames(request));
384 let safetySettings = GEMINI_SAFETY;392 let safetySettings = GEMINI_SAFETY;
385393
386 // These models do not support setting the threshold to OFF at all.394 // These models do not support setting the threshold to OFF at all.
387 if (['gemini-1.5-pro-001', 'gemini-1.5-flash-001', 'gemini-1.5-flash-8b-exp-0827', 'gemini-1.5-flash-8b-exp-0924'].includes(model)) {395 if (blockNoneModels.includes(model)) {
388 safetySettings = GEMINI_SAFETY.map(setting => ({ ...setting, threshold: 'BLOCK_NONE' }));396 safetySettings = GEMINI_SAFETY.map(setting => ({ ...setting, threshold: 'BLOCK_NONE' }));
389 }397 }
390398
391 if (enableWebSearch && !useMultiModal && !isGemma && !isLearnLM && !model.includes('gemini-2.0-flash-lite')) {399 if (enableWebSearch && !enableImageModality && !isGemma && !isLearnLM && !model.includes('gemini-2.0-flash-lite')) {
392 const searchTool = model.includes('1.5') || model.includes('1.0')400 const searchTool = model.includes('1.5')
393 ? ({ google_search_retrieval: {} })401 ? ({ google_search_retrieval: {} })
394 : ({ google_search: {} });402 : ({ google_search: {} });
395 tools.push(searchTool);403 tools.push(searchTool);
396 }404 }
397405
398 if (Array.isArray(request.body.tools) && request.body.tools.length > 0 && !useMultiModal && !isGemma) {406 if (Array.isArray(request.body.tools) && request.body.tools.length > 0 && !enableImageModality && !isGemma) {
399 const functionDeclarations = [];407 const functionDeclarations = [];
400 for (const tool of request.body.tools) {408 for (const tool of request.body.tools) {
401 if (tool.type === 'function') {409 if (tool.type === 'function') {
@@ -411,11 +419,6 @@ async function sendMakerSuiteRequest(request, response) {
411 tools.push({ function_declarations: functionDeclarations });419 tools.push({ function_declarations: functionDeclarations });
412 }420 }
413421
414 // One more models list to maintain, yay
415 const thinkingBudgetModels = [
416 'gemini-2.5-flash-preview-04-17',
417 ];
418
419 if (thinkingBudgetModels.includes(model)) {422 if (thinkingBudgetModels.includes(model)) {
420 const thinkingBudget = calculateGoogleBudgetTokens(generationConfig.maxOutputTokens, reasoningEffort);423 const thinkingBudget = calculateGoogleBudgetTokens(generationConfig.maxOutputTokens, reasoningEffort);
421424
@@ -451,7 +454,7 @@ async function sendMakerSuiteRequest(request, response) {
451 controller.abort();454 controller.abort();
452 });455 });
453456
454 const apiVersion = isThinking ? 'v1alpha' : 'v1beta';457 const apiVersion = getConfigValue('gemini.apiVersion', 'v1beta');
455 const responseType = (stream ? 'streamGenerateContent' : 'generateContent');458 const responseType = (stream ? 'streamGenerateContent' : 'generateContent');
456459
457 const generateResponse = await fetch(`${apiUrl.toString().replace(/\/$/, '')}/${apiVersion}/models/${model}:${responseType}?key=${apiKey}${stream ? '&alt=sse' : ''}`, {460 const generateResponse = await fetch(`${apiUrl.toString().replace(/\/$/, '')}/${apiVersion}/models/${model}:${responseType}?key=${apiKey}${stream ? '&alt=sse' : ''}`, {