Skip to content

Commit 0457279

Browse files
committed
fix local image gpt-image-1 issue
1 parent 7340e90 commit 0457279

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

src/BE/Chats.BE.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<UserSecretsId>d4aa34e2-6c5f-41b9-b61b-c1a48b1d1b44</UserSecretsId>
8-
<Version>1.5.3</Version>
8+
<Version>1.5.4</Version>
99
</PropertyGroup>
1010

1111
<ItemGroup>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ private static async Task<MessageLiteDto[]> FillContents(LinkedList<MessageLiteD
349349
Dictionary<long, MessageContent[]> contents = await db.MessageContents
350350
.Where(x => messageIds.Contains(x.MessageId))
351351
.Include(x => x.MessageContentBlob)
352-
.Include(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileService)
353-
.Include(x => x.MessageContentFile).ThenInclude(x => x!.File).ThenInclude(x => x.FileImageInfo)
352+
.Include(x => x.MessageContentFile).ThenInclude(x => x!.File.FileService)
353+
.Include(x => x.MessageContentFile).ThenInclude(x => x!.File.FileImageInfo)
354+
.Include(x => x.MessageContentFile).ThenInclude(x => x!.File.FileContentType)
354355
.Include(x => x.MessageContentText)
355356
.GroupBy(x => x.MessageId)
356357
.ToDictionaryAsync(k => k.Key, v => v.ToArray(), cancellationToken);

src/BE/Services/FileServices/DBFileDef.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ public record DBFileDef(byte[] Bytes, string ContentType, string? SuggestedFileN
44
{
55
public string FileName => SuggestedFileName ?? MakeFileNameByContentType(ContentType);
66

7-
protected static string MakeFileNameByContentType(string contentType)
7+
internal static string MakeFileNameByContentType(string contentType)
88
{
99
return contentType switch
1010
{
1111
"image/jpeg" => "image.jpg",
1212
"image/png" => "image.png",
1313
"image/gif" => "image.gif",
14+
"image/webp" => "image.webp",
15+
"image/svg+xml" => "image.svg",
1416
_ => "image"
1517
};
1618
}

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Chats.BE.DB.Enums;
88
using System.Text.Json.Nodes;
99
using System.ClientModel.Primitives;
10+
using Chats.BE.Services.FileServices;
1011

1112
namespace Chats.BE.Services.Models.ChatServices.OpenAI.Special;
1213

@@ -70,24 +71,33 @@ public override async Task<ChatSegment> Chat(IReadOnlyList<ChatMessage> messages
7071
// EndUserId = options.EndUserId,
7172
// }, cancellationToken);
7273
MultiPartFormDataBinaryContent form = new();
73-
(Uri url, HttpResponseMessage resp)[] downloadedImages = await Task.WhenAll(images.GroupBy(x => x.ImageUri).Select(async image =>
74+
Dictionary<Uri, HttpResponseMessage> downloadedFiles = (await Task.WhenAll(images
75+
.Where(x => x.ImageUri != null)
76+
.GroupBy(x => x.ImageUri).Select(async image =>
7477
{
7578
HttpResponseMessage resp = await http.GetAsync(image.Key, cancellationToken);
7679
return (url: image.Key, resp);
77-
}));
78-
Dictionary<Uri, HttpResponseMessage> files = downloadedImages.ToDictionary(k => k.url, v => v.resp);
80+
})))
81+
.ToDictionary(k => k.url, v => v.resp);
7982

8083
foreach (ChatMessageContentPart image in images)
8184
{
82-
HttpResponseMessage file = files[image.ImageUri];
83-
string fileName = Path.GetFileName(image.ImageUri.LocalPath);
84-
if (fileName.Contains("mask.png"))
85+
if (image.ImageUri != null)
8586
{
86-
form.Add(await file.Content.ReadAsStreamAsync(cancellationToken), "mask", fileName, file.Content.Headers.ContentType?.ToString());
87+
HttpResponseMessage file = downloadedFiles[image.ImageUri];
88+
string fileName = Path.GetFileName(image.ImageUri.LocalPath);
89+
if (fileName.Contains("mask.png"))
90+
{
91+
form.Add(await file.Content.ReadAsStreamAsync(cancellationToken), "mask", fileName, file.Content.Headers.ContentType?.ToString());
92+
}
93+
else
94+
{
95+
form.Add(await file.Content.ReadAsStreamAsync(cancellationToken), "image[]", fileName, file.Content.Headers.ContentType?.ToString());
96+
}
8797
}
8898
else
8999
{
90-
form.Add(await file.Content.ReadAsStreamAsync(cancellationToken), "image[]", fileName, file.Content.Headers.ContentType?.ToString());
100+
form.Add(image.ImageBytes, "image[]", image.Filename ?? DBFileDef.MakeFileNameByContentType(image.ImageBytesMediaType), image.ImageBytesMediaType);
91101
}
92102
}
93103
form.Add(prompt, "prompt");

0 commit comments

Comments
 (0)