Skip to content

Commit d73bcdc

Browse files
committed
tested FE confirmed streamed ImageGeneration service worked
1 parent 40c0239 commit d73bcdc

7 files changed

Lines changed: 127 additions & 88 deletions

File tree

src/BE/Controllers/Chats/Chats/ChatController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ async Task<Step> RunOne(List<OpenAIChatMessage> messageToSend)
677677
{
678678
writer.TryWrite(new ToolCompletedLine(chatSpan.SpanId, toolCallResponse.IsSuccess, toolCallResponse.ToolCallId!, toolCallResponse.Response!));
679679
}
680+
else if (item is Base64PreviewImage preview)
681+
{
682+
writer.TryWrite(new ImageGeneratingLine(chatSpan.SpanId, preview.ToTempFileDto()));
683+
}
680684
else if (item is ImageChatSegment imgSeg)
681685
{
682686
imageFileCache[imgSeg] = new TaskCompletionSource<DB.File>();
@@ -842,7 +846,7 @@ public IActionResult StopChat(string stopId)
842846

843847
private static string GenerateAlphaFirstToken(int length)
844848
{
845-
if (length <= 0) throw new ArgumentOutOfRangeException(nameof(length));
849+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length);
846850
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
847851
const string alphanum = letters + "0123456789";
848852

src/BE/DB/Extensions/StepContent.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ public static IEnumerable<StepContent> FromFullResponse(InternalChatSegment last
9191
yield return FromError(errorText);
9292
}
9393
// lastSegment.Items is merged now
94-
foreach (StepContent item in lastSegment.Items.Select(x =>
94+
foreach (StepContent? item in lastSegment.Items.Select(x =>
9595
{
9696
return x switch
9797
{
98+
Base64PreviewImage => null, // skip preview images
9899
TextChatSegment text => FromText(text.Text),
99100
ThinkChatSegment think => FromThink(think.Think),
100101
ImageChatSegment image => FromFile(imageMcCache[image].Task.GetAwaiter().GetResult()),
@@ -104,7 +105,10 @@ public static IEnumerable<StepContent> FromFullResponse(InternalChatSegment last
104105
};
105106
}))
106107
{
107-
yield return item;
108+
if (item is not null)
109+
{
110+
yield return item;
111+
}
108112
}
109113
}
110114
}

src/BE/Services/Models/ChatServices/ImageChatSegment.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Chats.BE.Services.FileServices;
1+
using Chats.BE.Controllers.Chats.Messages.Dtos;
2+
using Chats.BE.Services.FileServices;
23
using Chats.BE.Services.Models.Dtos;
34
using System.Text.Json.Serialization;
45

@@ -11,9 +12,9 @@ public abstract record ImageChatSegment : ChatSegmentItem
1112
{
1213
public abstract Task<DBFileDef> Download(CancellationToken cancellationToken = default);
1314

14-
public abstract string ToTempUrl();
15+
protected abstract string ToTempUrl();
1516

16-
public abstract string ToContentType();
17+
protected abstract string ToContentType();
1718
}
1819

1920
public record Base64Image : ImageChatSegment
@@ -32,9 +33,22 @@ public override Task<DBFileDef> Download(CancellationToken cancellationToken = d
3233
return Task.FromResult(new DBFileDef(bytes, ContentType, null));
3334
}
3435

35-
public override string ToTempUrl() => $"data:{ContentType};base64,{Base64}";
36+
protected override string ToTempUrl() => $"data:{ContentType};base64,{Base64}";
3637

37-
public override string ToContentType() => ContentType;
38+
protected override string ToContentType() => ContentType;
39+
}
40+
41+
public record Base64PreviewImage : Base64Image
42+
{
43+
public FileDto ToTempFileDto()
44+
{
45+
return new FileDto()
46+
{
47+
Id = Guid.NewGuid().ToString(),
48+
ContentType = ContentType,
49+
Url = ToTempUrl(),
50+
};
51+
}
3852
}
3953

4054
public record UrlImage : ImageChatSegment
@@ -54,9 +68,9 @@ public override async Task<DBFileDef> Download(CancellationToken cancellationTok
5468
return new DBFileDef(bytes, contentType, fileName);
5569
}
5670

57-
public override string ToTempUrl() => Url;
71+
protected override string ToTempUrl() => Url;
5872

59-
public override string ToContentType() => Url switch
73+
protected override string ToContentType() => Url switch
6074
{
6175
var x when x.Contains(".png", StringComparison.OrdinalIgnoreCase) => "image/png",
6276
var x when x.Contains(".jpg", StringComparison.OrdinalIgnoreCase) => "image/jpeg",

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<C
104104

105105
if (!message.Response.IsError)
106106
{
107-
await foreach (ChatSegment segment in ProcessImageStreamResponseAsync(message.Response, sw, "image_gen_sse", cancellationToken))
107+
await foreach (ChatSegment segment in ProcessImageStreamResponseAsync(message.Response, sw, cancellationToken))
108108
{
109109
yield return segment;
110110
}
@@ -140,7 +140,7 @@ public override async IAsyncEnumerable<ChatSegment> ChatStreamed(IReadOnlyList<C
140140

141141
if (!message.Response.IsError)
142142
{
143-
await foreach (ChatSegment segment in ProcessImageStreamResponseAsync(message.Response, sw, "image_edit_sse", cancellationToken))
143+
await foreach (ChatSegment segment in ProcessImageStreamResponseAsync(message.Response, sw, cancellationToken))
144144
{
145145
yield return segment;
146146
}
@@ -336,7 +336,6 @@ private async Task<MultiPartFormDataBinaryContent> BuildImageEditFormAsync(
336336
private static async IAsyncEnumerable<ChatSegment> ProcessImageStreamResponseAsync(
337337
PipelineResponse response,
338338
Stopwatch sw,
339-
string logPrefix,
340339
[EnumeratorCancellation] CancellationToken cancellationToken)
341340
{
342341
if (response.ContentStream == null)
@@ -378,7 +377,7 @@ private static async IAsyncEnumerable<ChatSegment> ProcessImageStreamResponseAsy
378377
yield return new ChatSegment()
379378
{
380379
FinishReason = null,
381-
Items = [ChatSegmentItem.FromBase64Image(b64Json, contentType)],
380+
Items = [ChatSegmentItem.FromBase64PreviewImage(b64Json, contentType)],
382381
Usage = null,
383382
};
384383
}

src/BE/Services/Models/Dtos/ChatSegmentItem.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ public static ImageChatSegment FromBase64Image(string base64, string contentType
3737
};
3838
}
3939

40+
public static ImageChatSegment FromBase64PreviewImage(string base64, string contentType)
41+
{
42+
return new Base64PreviewImage
43+
{
44+
Base64 = base64,
45+
ContentType = contentType
46+
};
47+
}
48+
4049
public static ImageChatSegment FromBinaryData(BinaryData binaryData, string contentType)
4150
{
4251
return new Base64Image

0 commit comments

Comments
 (0)