|
7 | 7 | using Chats.BE.DB.Enums; |
8 | 8 | using System.Text.Json.Nodes; |
9 | 9 | using System.ClientModel.Primitives; |
| 10 | +using Chats.BE.Services.FileServices; |
10 | 11 |
|
11 | 12 | namespace Chats.BE.Services.Models.ChatServices.OpenAI.Special; |
12 | 13 |
|
@@ -70,24 +71,33 @@ public override async Task<ChatSegment> Chat(IReadOnlyList<ChatMessage> messages |
70 | 71 | // EndUserId = options.EndUserId, |
71 | 72 | // }, cancellationToken); |
72 | 73 | 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 => |
74 | 77 | { |
75 | 78 | HttpResponseMessage resp = await http.GetAsync(image.Key, cancellationToken); |
76 | 79 | 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); |
79 | 82 |
|
80 | 83 | foreach (ChatMessageContentPart image in images) |
81 | 84 | { |
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) |
85 | 86 | { |
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 | + } |
87 | 97 | } |
88 | 98 | else |
89 | 99 | { |
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); |
91 | 101 | } |
92 | 102 | } |
93 | 103 | form.Add(prompt, "prompt"); |
|
0 commit comments