feat: 优化阿里生图模型错误提示,使用后台配置的模型名称
- 错误提示中显示后台配置的模型显示名称 - 添加 modelDisplayName 参数到 doGenerate 接口 - 当用户只发文字时,提示需要上传图片
This commit is contained in:
parent
864057c9cd
commit
e5b3088671
@ -897,6 +897,10 @@ export async function POST(request: Request) {
|
||||
console.log("[Chat API] Using Alibaba Image generation");
|
||||
console.log("[Chat API] Image params:", requestBody.imageParams);
|
||||
|
||||
// 🆕 获取模型显示名称,用于错误提示
|
||||
const alibabaModelConfig = await getAiModelById({ id: finalModelId });
|
||||
const modelDisplayName = alibabaModelConfig?.displayName || actualModelId;
|
||||
|
||||
const alibabaStream = createUIMessageStream({
|
||||
execute: async ({ writer }) => {
|
||||
try {
|
||||
@ -908,10 +912,11 @@ export async function POST(request: Request) {
|
||||
// 转换消息(阿里生图支持 URL,会自动上传 base64)
|
||||
const modelMessages = await convertMessagesWithImages(messagesForAlibaba, "alibaba-image");
|
||||
|
||||
// 调用 doGenerate 获取图片
|
||||
// 调用 doGenerate 获取图片,传递模型显示名称
|
||||
const result = await (languageModel as any).doGenerate({
|
||||
messages: modelMessages,
|
||||
imageParams: requestBody.imageParams || { aspectRatio: "auto", imageSize: "2K", imageCount: 1 },
|
||||
modelDisplayName, // 🆕 传递模型显示名称
|
||||
});
|
||||
|
||||
const imageMarkdown = result.text || "图片生成失败";
|
||||
|
||||
@ -286,6 +286,7 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
|
||||
doGenerate: async (options: {
|
||||
messages: Array<{ role: string; content: string | Array<{ type: string; text?: string; image?: string }> }>;
|
||||
imageParams?: AlibabaImageParams & { aspectRatio?: string; imageSize?: string; imageCount?: number };
|
||||
modelDisplayName?: string; // 🆕 模型显示名称,用于错误提示
|
||||
}) => {
|
||||
console.log("[Alibaba Image] doGenerate called");
|
||||
console.log("[Alibaba Image] Model:", modelId);
|
||||
@ -344,6 +345,12 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
|
||||
throw new Error("No prompt found in message");
|
||||
}
|
||||
|
||||
// 验证:qwen-image-edit 系列模型必须有输入图片
|
||||
if (imageUrls.length === 0) {
|
||||
const modelName = options.modelDisplayName || modelId;
|
||||
throw new Error(`「${modelName}」是图片编辑模型,需要至少上传一张图片才能进行编辑。如需纯文生图,请使用其他生图模型。`);
|
||||
}
|
||||
|
||||
// 构建参数
|
||||
const params: AlibabaImageParams = {
|
||||
n: options.imageParams?.imageCount || options.imageParams?.n || 1,
|
||||
@ -366,7 +373,10 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
|
||||
params.size = sizeMap[options.imageParams.imageSize] || "1024*1024";
|
||||
}
|
||||
|
||||
console.log("[Alibaba Image] Prompt:", prompt.substring(0, 100) + "...");
|
||||
// 根据图片数量确定编辑模式
|
||||
const editMode = imageUrls.length === 1 ? "单图编辑" : `多图融合 (${imageUrls.length}张)`;
|
||||
console.log("[Alibaba Image] 编辑模式:", editMode);
|
||||
console.log("[Alibaba Image] Prompt:", prompt.substring(0, 100) + (prompt.length > 100 ? "..." : ""));
|
||||
console.log("[Alibaba Image] Input images:", imageUrls.length);
|
||||
console.log("[Alibaba Image] Final params:", params);
|
||||
|
||||
@ -403,6 +413,7 @@ export function createAlibabaImage({ apiKey, baseURL }: AlibabaImageConfig) {
|
||||
doStream: async function* (options: {
|
||||
messages: Array<{ role: string; content: string | Array<{ type: string; text?: string; image?: string }> }>;
|
||||
imageParams?: AlibabaImageParams & { aspectRatio?: string; imageSize?: string; imageCount?: number };
|
||||
modelDisplayName?: string;
|
||||
}) {
|
||||
console.log("[Alibaba Image] doStream called (using doGenerate internally)");
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user