try to fix image gen

This commit is contained in:
songtianlun 2025-08-31 01:40:52 +08:00
parent 4a7149b4bb
commit a881be2b78
2 changed files with 44 additions and 24 deletions

View File

@ -37,20 +37,30 @@ const IMAGE_MODEL_ADAPTERS: Record<string, ModelAdapter> = {
name: 'Gemini 2.5 Flash Image Preview',
prepareRequest: (userInput: string, promptContent: string, params: Record<string, unknown>) => ({
model: 'google/gemini-2.5-flash-image-preview',
contents: [{
parts: [{
text: `${promptContent}\n\nUser input: ${userInput}`
}]
messages: [{
role: 'user',
content: `${promptContent}\n\nUser input: ${userInput}`
}],
generationConfig: {
temperature: params.temperature || 0.7,
maxOutputTokens: params.maxTokens || 1024
}
...(params.maxTokens ? { max_tokens: params.maxTokens } : {}),
...(params.topP ? { top_p: params.topP } : {}),
...(params.frequencyPenalty ? { frequency_penalty: params.frequencyPenalty } : {}),
...(params.presencePenalty ? { presence_penalty: params.presencePenalty } : {})
}),
parseResponse: (response: Record<string, unknown>) => ({
content: (response as { candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>; generated_image_url?: string }).candidates?.[0]?.content?.parts?.[0]?.text || (response as { generated_image_url?: string }).generated_image_url || 'Image generated successfully',
parseResponse: (response: Record<string, unknown>) => {
// 尝试从不同的响应格式中提取内容
const choices = (response as { choices?: Array<{ message?: { content?: string } }> }).choices
const content = choices?.[0]?.message?.content || ''
// 从内容中提取图像URL
const urlMatch = content.match(/https?:\/\/[^\s<>"']*\.(?:png|jpg|jpeg|gif|webp|bmp|svg)(?:\?[^\s<>"']*)?/i)
const imageUrl = urlMatch?.[0] || ''
return {
content: imageUrl || content || 'Image generation completed',
outputType: 'image'
})
}
}
}
};

View File

@ -33,20 +33,30 @@ const IMAGE_MODEL_ADAPTERS: Record<string, ModelAdapter> = {
name: 'Gemini 2.5 Flash Image Preview',
prepareRequest: (userInput: string, promptContent: string, params: Record<string, unknown>) => ({
model: 'google/gemini-2.5-flash-image-preview',
contents: [{
parts: [{
text: `${promptContent}\n\nUser input: ${userInput}`
}]
messages: [{
role: 'user',
content: `${promptContent}\n\nUser input: ${userInput}`
}],
generationConfig: {
temperature: params.temperature || 0.7,
maxOutputTokens: params.maxTokens || 1024
}
...(params.maxTokens ? { max_tokens: params.maxTokens } : {}),
...(params.topP ? { top_p: params.topP } : {}),
...(params.frequencyPenalty ? { frequency_penalty: params.frequencyPenalty } : {}),
...(params.presencePenalty ? { presence_penalty: params.presencePenalty } : {})
}),
parseResponse: (response: Record<string, unknown>) => ({
content: (response as { candidates?: Array<{ content?: { parts?: Array<{ text?: string }> } }>; generated_image_url?: string }).candidates?.[0]?.content?.parts?.[0]?.text || (response as { generated_image_url?: string }).generated_image_url || 'Image generated successfully',
parseResponse: (response: Record<string, unknown>) => {
// 尝试从不同的响应格式中提取内容
const choices = (response as { choices?: Array<{ message?: { content?: string } }> }).choices
const content = choices?.[0]?.message?.content || ''
// 从内容中提取图像URL
const urlMatch = content.match(/https?:\/\/[^\s<>"']*\.(?:png|jpg|jpeg|gif|webp|bmp|svg)(?:\?[^\s<>"']*)?/i)
const imageUrl = urlMatch?.[0] || ''
return {
content: imageUrl || content || 'Image generation completed',
outputType: 'image'
})
}
}
}
};