Skip to content

Commit 42b1773

Browse files
committed
auto route image generation to non streaming if n>1 to fix stream mode not support multiple image
1 parent 60df748 commit 42b1773

2 files changed

Lines changed: 14 additions & 25 deletions

File tree

src/BE/DB/Enums/DBReasoningEffort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class DBReasoningEffortExtensions
3030
DBReasoningEffort.Minimal => GeneratedImageQuality.Low, // treating minimal as low for image quality
3131
DBReasoningEffort.Low => GeneratedImageQuality.Low,
3232
DBReasoningEffort.Medium => GeneratedImageQuality.Medium,
33-
DBReasoningEffort.High => GeneratedImageQuality.High,
33+
DBReasoningEffort.High => new GeneratedImageQuality("high"),
3434
_ => throw new Exception($"Unknown DBReasoningEffort value: {effort}"),
3535
};
3636

src/BE/Services/Models/ChatServices/OpenAI/Special/ImageGenerationService.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<C
3737
string prompt = GetPromptStatic(messages);
3838
ChatMessageContentPart[] images = GetImagesStatic(messages);
3939

40+
// 兼容 n>1 时不走 stream
41+
int n = options.MaxOutputTokenCount ?? 1;
42+
if (n > 1)
43+
{
44+
// fallback 到 Chat 方法
45+
Console.WriteLine("ImageGenerationService.ChatStreamed: n > 1, fallback to non-streaming Chat method.");
46+
ChatSegment result = await Chat(messages, options, cancellationToken);
47+
yield return result;
48+
yield break;
49+
}
50+
4051
ClientPipeline pipeline = imageClient.Pipeline;
4152

4253
if (images.Length == 0)
@@ -46,7 +57,7 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<C
4657
{
4758
["prompt"] = prompt,
4859
["model"] = Model.ApiModelId,
49-
["n"] = options.MaxOutputTokenCount ?? 1,
60+
["n"] = n,
5061
["stream"] = true,
5162
["partial_images"] = 3,
5263
["moderation"] = "low"
@@ -67,17 +78,6 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<C
6778
_ => throw new NotSupportedException($"Unsupported image size: {_imageSize}"),
6879
};
6980
}
70-
else
71-
{
72-
if (prompt.Contains("3:2"))
73-
{
74-
requestBody["size"] = "1536x1024";
75-
}
76-
else if (prompt.Contains("2:3"))
77-
{
78-
requestBody["size"] = "1024x1536";
79-
}
80-
}
8181

8282
if (options.EndUserId != null)
8383
{
@@ -170,7 +170,7 @@ public override async Task<ChatSegment> Chat(IReadOnlyList<ChatMessage> messages
170170
Quality = _reasoningEffort.ToGeneratedImageQuality(),
171171
Size = _imageSize switch
172172
{
173-
DBKnownImageSize.Default => prompt.Contains("3:2") ? GeneratedImageSize.W1536xH1024 : prompt.Contains("2:3") ? GeneratedImageSize.W1024xH1536 : null,
173+
DBKnownImageSize.Default => null,
174174
DBKnownImageSize.W1024xH1024 => GeneratedImageSize.W1024xH1024,
175175
DBKnownImageSize.W1536xH1024 => GeneratedImageSize.W1536xH1024,
176176
DBKnownImageSize.W1024xH1536 => GeneratedImageSize.W1024xH1536,
@@ -297,17 +297,6 @@ private async Task<MultiPartFormDataBinaryContent> BuildImageEditFormAsync(
297297
_ => throw new NotSupportedException($"Unsupported image size: {_imageSize}"),
298298
}, "size");
299299
}
300-
else
301-
{
302-
if (prompt.Contains("3:2"))
303-
{
304-
form.Add("1536x1024", "size");
305-
}
306-
else if (prompt.Contains("2:3"))
307-
{
308-
form.Add("1024x1536", "size");
309-
}
310-
}
311300

312301
if (options.EndUserId != null)
313302
{

0 commit comments

Comments
 (0)