docs/development/basic/add-new-image-model.mdx
Learn more about the AI image generation modal design in the AI Image Generation Modal Design Discussion
All image generation models must use the standard parameters defined in packages/model-bank/src/standard-parameters/index.ts. This ensures parameter consistency across different Providers, creating a more unified user experience.
Supported Standard Parameters:
prompt (required): Text prompt for image generationaspectRatio: Aspect ratio (e.g., "16:9", "1:1")width / height: Image dimensionssize: Preset dimensions (e.g., "1024x1024")seed: Random seedsteps: Generation stepscfg: Guidance scaleThese models can be requested using the OpenAI SDK, with request parameters and return values consistent with DALL-E and GPT-Image-X series.
Taking Zhipu's CogView-4 as an example, which is an OpenAI-compatible model, you can add it by adding the model configuration in the corresponding AI models file packages/model-bank/src/aiModels/zhipu.ts:
const zhipuImageModels: AIImageModelCard[] = [
// Add model configuration
// https://bigmodel.cn/dev/howuse/image-generation-model/cogview-4
{
description:
'CogView-4 is the first open-source text-to-image model from Zhipu that supports Chinese character generation, with comprehensive improvements in semantic understanding, image generation quality, and Chinese-English text generation capabilities.',
displayName: 'CogView-4',
enabled: true,
id: 'cogview-4',
parameters: {
prompt: {
default: '',
},
size: {
default: '1024x1024',
enum: ['1024x1024', '768x1344', '864x1152', '1344x768', '1152x864', '1440x720', '720x1440'],
},
},
releasedAt: '2025-03-04',
type: 'image',
},
];
For image generation models that are not compatible with OpenAI format, you need to implement a custom createImage method. There are two main implementation approaches:
Most Providers use openaiCompatibleFactory for OpenAI compatibility. You can pass in a custom createImage function (reference PR #8534).
Implementation Steps:
Read Provider documentation and standard parameter definitions
packages/model-bank/src/standard-parameters/index.ts to understand supported parametersImplement custom createImage method
Add tests
Code Example:
// packages/model-runtime/src/providers/<provider-name>/createImage.ts
export const createProviderImage = async (
payload: ImageGenerationPayload,
options: any,
): Promise<ImageGenerationResponse> => {
const { model, prompt, ...params } = payload;
// Call Provider's native API
const result = await callProviderAPI({
model,
prompt,
// Convert parameter format
custom_param: params.width,
// ...
});
// Return unified format
return {
created: Date.now(),
data: [{ url: result.imageUrl }],
};
};
// packages/model-runtime/src/providers/<provider-name>/index.ts
export const LobeProviderAI = openaiCompatibleFactory({
constructorOptions: {
// ... other configurations
},
createImage: createProviderImage, // Pass custom implementation
provider: ModelProvider.ProviderName,
});
If your Provider has an independent class implementation, you can directly add the createImage method in the class (reference PR #8503).
Implementation Steps:
Read Provider documentation and standard parameter definitions
packages/model-bank/src/standard-parameters/index.tsImplement createImage method in Provider class
createImage method directly in the classAdd tests
Code Example:
// packages/model-runtime/src/providers/<provider-name>/index.ts
export class LobeProviderAI {
async createImage(
payload: ImageGenerationPayload,
options?: ChatStreamCallbacks,
): Promise<ImageGenerationResponse> {
const { model, prompt, ...params } = payload;
// Call native API and handle response
const result = await this.client.generateImage({
model,
prompt,
// Parameter conversion
});
return {
created: Date.now(),
data: [{ url: result.url }],
};
}
}
AgentRuntimeError consistently for error wrapping to maintain error message consistency