mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-05 22:38:48 +00:00
Add Model-Specific Size Validation and Update Size Parameter Placeholders for Image Generation (#10923)
* Update size parameter placeholder for image generation Updated the placeholder for the size parameter in the operations.json file to specify valid options based on the selected model (DALL-E 2 or DALL-E 3). - Added size options for DALL-E 2: 256x256, 512x512, 1024x1024 (default: 1024x1024) - Added size options for DALL-E 3: 1024x1024, 1792x1024, 1024x1792 (default: 1024x1024) * Implement model-specific size validation for image generation Added logic to enforce model-specific size validation for DALL-E 2 and DALL-E 3 in the query_operations.ts file. - DALL-E 3 accepts sizes: 1024x1024, 1792x1024, 1024x1792 (default: 1024x1024) - DALL-E 2 accepts sizes: 256x256, 512x512, 1024x1024 (default: 1024x1024) - Introduced a utility function to validate and apply the appropriate size based on the selected model.
This commit is contained in:
parent
0fa84e3fdc
commit
d08cdebbfa
2 changed files with 41 additions and 22 deletions
|
|
@ -276,7 +276,7 @@
|
|||
"key": "size",
|
||||
"type": "codehinter",
|
||||
"description": "Enter image size in pixels (e.g., 1024x1024)",
|
||||
"placeholder": "By default 1024x1024 sized image will be generated",
|
||||
"placeholder": "1024x1024, 1792x1024 or 1024x1792. By default 1024x1024 sized image is generated",
|
||||
"width": "320px",
|
||||
"height": "36px"
|
||||
}
|
||||
|
|
@ -294,7 +294,7 @@
|
|||
"key": "size",
|
||||
"type": "codehinter",
|
||||
"description": "Enter image size in pixels (e.g., 1024x1024)",
|
||||
"placeholder": "By default 1024x1024 sized image will be generated",
|
||||
"placeholder": "256x256, 512x512 or 1024x1024. By default 1024x1024 sized image is generated",
|
||||
"width": "320px",
|
||||
"height": "36px"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,45 @@
|
|||
import OpenAI from 'openai'; // Updated SDK version
|
||||
import { QueryOptions } from './types';
|
||||
|
||||
// Utility function to convert size string to the enum value
|
||||
const getSizeEnum = (size: string | undefined): '256x256' | '512x512' | '1024x1024' => {
|
||||
switch (size) {
|
||||
case '256x256':
|
||||
return '256x256';
|
||||
case '512x512':
|
||||
return '512x512';
|
||||
case '1024x1024':
|
||||
return '1024x1024';
|
||||
default:
|
||||
return '1024x1024'; // Default size
|
||||
// Updated utility function to handle size validation based on model
|
||||
const getSizeEnum = (model: string | undefined, size: string | undefined): '256x256' | '512x512' | '1024x1024' | '1792x1024' | '1024x1792' => {
|
||||
// If the model is DALL-E 3, only allow 1024x1024, 1792x1024, or 1024x1792
|
||||
if (model === 'dall-e-3') {
|
||||
switch (size) {
|
||||
case '1024x1024':
|
||||
return '1024x1024';
|
||||
case '1792x1024':
|
||||
return '1792x1024';
|
||||
case '1024x1792':
|
||||
return '1024x1792';
|
||||
default:
|
||||
return '1024x1024'; // Default size for DALL-E 3
|
||||
}
|
||||
}
|
||||
|
||||
// If the model is DALL-E 2, only allow 1024x1024, 512x512, or 256x256
|
||||
if (model === 'dall-e-2') {
|
||||
switch (size) {
|
||||
case '1024x1024':
|
||||
return '1024x1024';
|
||||
case '512x512':
|
||||
return '512x512';
|
||||
case '256x256':
|
||||
return '256x256';
|
||||
default:
|
||||
return '1024x1024'; // Default size for DALL-E 2
|
||||
}
|
||||
}
|
||||
|
||||
// Default size if model is not recognized
|
||||
return '1024x1024';
|
||||
};
|
||||
|
||||
// Utility function to convert number of images from string or number
|
||||
//const getNumberOfImages = (num_images: number | string | undefined): number => {
|
||||
// const num = typeof num_images === 'string' ? parseInt(num_images) : num_images;
|
||||
// return isNaN(num) ? 1 : Math.max(1, Math.min(10, num)); // Ensure it's between 1 and 10
|
||||
//};
|
||||
//Utility function to convert number of images from string or number
|
||||
/*const getNumberOfImages = (num_images: number | string | undefined): number => {
|
||||
const num = typeof num_images === 'string' ? parseInt(num_images) : num_images;
|
||||
return isNaN(num) ? 1 : Math.max(1, Math.min(10, num)); // Ensure it's between 1 and 10
|
||||
};*/
|
||||
|
||||
export async function getCompletion(
|
||||
openai: OpenAI,
|
||||
|
|
@ -81,15 +101,14 @@ export async function generateImage(
|
|||
openai: OpenAI,
|
||||
options: QueryOptions
|
||||
): Promise<{ status: string; message: string; description?: string; data?: any }> {
|
||||
const { model, prompt,
|
||||
//num_images,
|
||||
size } = options;
|
||||
const { model, prompt, size/* , n */ } = options;
|
||||
|
||||
try {
|
||||
const response = await openai.images.generate({
|
||||
model: model || 'dall-e-3',
|
||||
prompt: prompt || '',
|
||||
size: getSizeEnum(model, size), // Convert and validate image size based on the model
|
||||
//n: getNumberOfImages(num_images), Convert and validate number of images
|
||||
size: getSizeEnum(size), // Convert and validate image size
|
||||
});
|
||||
|
||||
// Return the URL of the first image as a JSON object
|
||||
|
|
|
|||
Loading…
Reference in a new issue