-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathImageGeneratingChatClient.cs
More file actions
516 lines (441 loc) · 21.8 KB
/
Copy pathImageGeneratingChatClient.cs
File metadata and controls
516 lines (441 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI;
/// <summary>A delegating chat client that enables image generation capabilities by converting <see cref="HostedImageGenerationTool"/> instances to function tools.</summary>
/// <remarks>
/// <para>
/// The provided implementation of <see cref="IChatClient"/> is thread-safe for concurrent use so long as the
/// <see cref="IImageGenerator"/> employed is also thread-safe for concurrent use.
/// </para>
/// <para>
/// This client automatically detects <see cref="HostedImageGenerationTool"/> instances in the <see cref="ChatOptions.Tools"/> collection
/// and replaces them with equivalent function tools that the chat client can invoke to perform image generation and editing operations.
/// </para>
/// </remarks>
[Experimental(DiagnosticIds.Experiments.AIImageGeneration, UrlFormat = DiagnosticIds.UrlFormat)]
public sealed class ImageGeneratingChatClient : DelegatingChatClient
{
/// <summary>
/// Specifies how image and other data content is handled when passing data to an inner client.
/// </summary>
/// <remarks>
/// Use this enumeration to control whether images in the data content are passed as-is, replaced
/// with unique identifiers, or only generated images are replaced. This setting affects how downstream clients
/// receive and process image data.
/// Reducing what's passed downstream can help manage the context window.
/// </remarks>
public enum DataContentHandling
{
/// <summary>Pass all DataContent to inner client.</summary>
None,
/// <summary>Replace all images with unique identifiers when passing to inner client.</summary>
AllImages,
/// <summary>Replace only images that were produced by past image generation requests with unique identifiers when passing to inner client.</summary>
GeneratedImages
}
private const string ImageKey = "meai_image";
private readonly IImageGenerator _imageGenerator;
private readonly DataContentHandling _dataContentHandling;
/// <summary>Initializes a new instance of the <see cref="ImageGeneratingChatClient"/> class.</summary>
/// <param name="innerClient">The underlying <see cref="IChatClient"/>.</param>
/// <param name="imageGenerator">An <see cref="IImageGenerator"/> instance that will be used for image generation operations.</param>
/// <param name="dataContentHandling">Specifies how to handle <see cref="DataContent"/> instances when passing messages to the inner client.
/// The default is <see cref="DataContentHandling.AllImages"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="innerClient"/> or <paramref name="imageGenerator"/> is <see langword="null"/>.</exception>
public ImageGeneratingChatClient(IChatClient innerClient, IImageGenerator imageGenerator, DataContentHandling dataContentHandling = DataContentHandling.AllImages)
: base(innerClient)
{
_imageGenerator = Throw.IfNull(imageGenerator);
_dataContentHandling = dataContentHandling;
}
/// <inheritdoc/>
public override async Task<ChatResponse> GetResponseAsync(
IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
{
_ = Throw.IfNull(messages);
var requestState = new RequestState(_imageGenerator, _dataContentHandling);
// Process the chat options to replace HostedImageGenerationTool with functions
var processedOptions = requestState.ProcessChatOptions(options);
var processedMessages = requestState.ProcessChatMessages(messages);
// Get response from base implementation
var response = await base.GetResponseAsync(processedMessages, processedOptions, cancellationToken);
// Replace FunctionResultContent instances with generated image content
foreach (var message in response.Messages)
{
message.Contents = requestState.ReplaceImageGenerationFunctionResults(message.Contents);
}
return response;
}
/// <inheritdoc/>
public override async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<ChatMessage> messages, ChatOptions? options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
_ = Throw.IfNull(messages);
var requestState = new RequestState(_imageGenerator, _dataContentHandling);
// Process the chat options to replace HostedImageGenerationTool with functions
var processedOptions = requestState.ProcessChatOptions(options);
var processedMessages = requestState.ProcessChatMessages(messages);
await foreach (var update in base.GetStreamingResponseAsync(processedMessages, processedOptions, cancellationToken))
{
// Replace any FunctionResultContent instances with generated image content
var newContents = requestState.ReplaceImageGenerationFunctionResults(update.Contents);
if (!ReferenceEquals(newContents, update.Contents))
{
// Create a new update instance with modified contents
var modifiedUpdate = update.Clone();
modifiedUpdate.Contents = newContents;
yield return modifiedUpdate;
}
else
{
yield return update;
}
}
}
/// <summary>Provides a mechanism for releasing unmanaged resources.</summary>
/// <param name="disposing"><see langword="true"/> to dispose managed resources; otherwise, <see langword="false"/>.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
_imageGenerator.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Contains all the per-request state and methods for handling image generation requests.
/// This class is created fresh for each request to ensure thread safety.
/// This class is not exposed publicly and does not own any of it's resources.
/// </summary>
private sealed class RequestState
{
private readonly IImageGenerator _imageGenerator;
private readonly DataContentHandling _dataContentHandling;
private readonly HashSet<string> _toolNames = new(StringComparer.Ordinal);
private readonly Dictionary<string, List<AIContent>> _imageContentByCallId = [];
private readonly Dictionary<string, AIContent> _imageContentById = new(StringComparer.OrdinalIgnoreCase);
private ImageGenerationOptions? _imageGenerationOptions;
public RequestState(IImageGenerator imageGenerator, DataContentHandling dataContentHandling)
{
_imageGenerator = imageGenerator;
_dataContentHandling = dataContentHandling;
}
/// <summary>
/// Processes the chat messages to replace images in data content with unique identifiers as needed.
/// All images will be stored for later retrieval during image editing operations.
/// See <see cref="DataContentHandling"/> for details on image replacement behavior.
/// </summary>
/// <param name="messages">Messages to process.</param>
/// <returns>Processed messages, or the original messages if no changes were made.</returns>
public IEnumerable<ChatMessage> ProcessChatMessages(IEnumerable<ChatMessage> messages)
{
List<ChatMessage>? newMessages = null;
int messageIndex = 0;
foreach (var message in messages)
{
List<AIContent>? newContents = null;
for (int contentIndex = 0; contentIndex < message.Contents.Count; contentIndex++)
{
var content = message.Contents[contentIndex];
void ReplaceImage(string imageId, DataContent dataContent)
{
// Replace image with a placeholder text content, to give an indication to the model of its placement in the context
newContents ??= CopyList(message.Contents, contentIndex);
newContents.Add(new TextContent($"[{ImageKey}:{imageId}] available for edit.")
{
Annotations = dataContent.Annotations,
AdditionalProperties = dataContent.AdditionalProperties
});
}
if (content is DataContent dataContent && dataContent.HasTopLevelMediaType("image"))
{
// Store the image to make available for edit
var imageId = StoreImage(dataContent);
if (_dataContentHandling == DataContentHandling.AllImages)
{
ReplaceImage(imageId, dataContent);
continue; // Skip adding the original content
}
}
else if (content is ImageGenerationToolResultContent toolResultContent)
{
foreach (var output in toolResultContent.Outputs ?? [])
{
if (output is DataContent generatedDataContent && generatedDataContent.HasTopLevelMediaType("image"))
{
// Store the image to make available for edit
var imageId = StoreImage(generatedDataContent, isGenerated: true);
if (_dataContentHandling == DataContentHandling.AllImages ||
_dataContentHandling == DataContentHandling.GeneratedImages)
{
ReplaceImage(imageId, generatedDataContent);
}
}
}
if (_dataContentHandling == DataContentHandling.AllImages ||
_dataContentHandling == DataContentHandling.GeneratedImages)
{
// skip adding the generated content
continue;
}
}
// Add the original content if no replacement was made
newContents?.Add(content);
}
if (newContents != null)
{
newMessages ??= [.. messages.Take(messageIndex)];
var newMessage = message.Clone();
newMessage.Contents = newContents;
newMessages.Add(newMessage);
}
else
{
newMessages?.Add(message);
}
messageIndex++;
}
return newMessages ?? messages;
}
public ChatOptions? ProcessChatOptions(ChatOptions? options)
{
if (options?.Tools is null || options.Tools.Count == 0)
{
return options;
}
List<AITool>? newTools = null;
var tools = options.Tools;
for (int i = 0; i < tools.Count; i++)
{
var tool = tools[i];
// remove all instances of HostedImageGenerationTool and store the options from the last one
if (tool is HostedImageGenerationTool imageGenerationTool)
{
_imageGenerationOptions = imageGenerationTool.Options;
// for the first image generation tool, clone the options and insert our function tools
// remove any subsequent image generation tools
newTools ??= InitializeTools(tools, i);
}
else
{
newTools?.Add(tool);
}
}
if (newTools is not null)
{
var newOptions = options.Clone();
newOptions.Tools = newTools;
return newOptions;
}
return options;
List<AITool> InitializeTools(IList<AITool> existingTools, int toOffsetExclusive)
{
#if NET
ReadOnlySpan<AITool> tools =
#else
AITool[] tools =
#endif
[
AIFunctionFactory.Create(GenerateImageAsync),
AIFunctionFactory.Create(EditImageAsync),
AIFunctionFactory.Create(GetImagesForEdit)
];
foreach (var tool in tools)
{
_toolNames.Add(tool.Name);
}
var result = CopyList(existingTools, toOffsetExclusive, tools.Length);
result.AddRange(tools);
return result;
}
}
/// <summary>
/// Replaces FunctionResultContent instances for image generation functions with actual generated image content.
/// We will have two messages
/// 1. Role: Assistant, FunctionCall
/// 2. Role: Tool, FunctionResult
/// We need to replace content from both but we shouldn't remove the messages.
/// If we do not then ChatClient's may not accept our altered history.
///
/// When interating with a HostedImageGenerationTool we will have typically only see a single Message with
/// Role: Assistant that contains the DataContent (or a provider specific content, that's exposed as DataContent).
/// </summary>
/// <param name="contents">The list of AI content to process.</param>
public IList<AIContent> ReplaceImageGenerationFunctionResults(IList<AIContent> contents)
{
List<AIContent>? newContents = null;
// Replace image-generation FunctionCallContent with ImageGenerationToolCallContent, and FunctionResultContent with generated image content
for (int i = 0; i < contents.Count; i++)
{
var content = contents[i];
// We must lookup by name because in the streaming case we have not yet been called to record the CallId.
if (content is FunctionCallContent functionCall &&
_toolNames.Contains(functionCall.Name))
{
// create a new list copying all items before this one, and omit the FunctionCallContent
newContents ??= CopyList(contents, i);
if (functionCall.Name != nameof(GetImagesForEdit))
{
newContents.Add(new ImageGenerationToolCallContent(functionCall.CallId));
}
}
else if (content is FunctionResultContent functionResult &&
_imageContentByCallId.TryGetValue(functionResult.CallId, out var imageContents))
{
// create a new list copying all items before this one
newContents ??= CopyList(contents, i);
if (imageContents.Any())
{
// Insert ImageGenerationToolResultContent in its place, do not preserve the FunctionResultContent
newContents.Add(new ImageGenerationToolResultContent(functionResult.CallId)
{
Outputs = imageContents
});
}
// Remove the mapping as it's no longer needed
_ = _imageContentByCallId.Remove(functionResult.CallId);
}
else
{
// keep the existing content if we have a new list
newContents?.Add(content);
}
}
return newContents ?? contents;
}
[Description("Generates images based on a text description.")]
public async Task<string> GenerateImageAsync(
[Description("A detailed description of the image to generate")] string prompt,
CancellationToken cancellationToken = default)
{
// Get the call ID from the current function invocation context
var callId = FunctionInvokingChatClient.CurrentContext?.CallContent.CallId;
if (callId == null)
{
return "No call ID available for image generation.";
}
var request = new ImageGenerationRequest(prompt);
var options = _imageGenerationOptions ?? new ImageGenerationOptions();
options.Count ??= 1;
var response = await _imageGenerator.GenerateAsync(request, options, cancellationToken);
if (response.Contents.Count == 0)
{
return "No image was generated.";
}
List<string> imageIds = [];
List<AIContent> imageContents = _imageContentByCallId[callId] = [];
foreach (var content in response.Contents)
{
if (content is DataContent imageContent && imageContent.MediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
imageContents.Add(imageContent);
imageIds.Add(StoreImage(imageContent, true));
}
}
return "Generated image successfully.";
}
[Description("Lists the identifiers of all images available for edit.")]
public IEnumerable<string> GetImagesForEdit()
{
// Get the call ID from the current function invocation context
var callId = FunctionInvokingChatClient.CurrentContext?.CallContent.CallId;
if (callId == null)
{
return ["No call ID available for image editing."];
}
_imageContentByCallId[callId] = [];
return _imageContentById.Keys.AsEnumerable();
}
[Description("Edits an existing image based on a text description.")]
public async Task<string> EditImageAsync(
[Description("A detailed description of the image to generate")] string prompt,
[Description($"The image to edit from one of the available image identifiers returned by {nameof(GetImagesForEdit)}")] string imageId,
CancellationToken cancellationToken = default)
{
// Get the call ID from the current function invocation context
var callId = FunctionInvokingChatClient.CurrentContext?.CallContent.CallId;
if (callId == null)
{
return "No call ID available for image editing.";
}
if (string.IsNullOrEmpty(imageId))
{
return "No imageId provided";
}
try
{
var originalImage = RetrieveImageContent(imageId);
if (originalImage == null)
{
return $"No image found with: {imageId}";
}
var request = new ImageGenerationRequest(prompt, [originalImage]);
var response = await _imageGenerator.GenerateAsync(request, _imageGenerationOptions, cancellationToken);
if (response.Contents.Count == 0)
{
return "No edited image was generated.";
}
List<string> imageIds = [];
List<AIContent> imageContents = _imageContentByCallId[callId] = [];
foreach (var content in response.Contents)
{
if (content is DataContent imageContent && imageContent.MediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
imageContents.Add(imageContent);
imageIds.Add(StoreImage(imageContent, true));
}
}
return "Edited image successfully.";
}
catch (FormatException)
{
return "Invalid image data format. Please provide a valid base64-encoded image.";
}
}
private static List<T> CopyList<T>(IList<T> original, int toOffsetExclusive, int additionalCapacity = 0)
{
var newList = new List<T>(original.Count + additionalCapacity);
// Copy all items up to and excluding the current index
for (int j = 0; j < toOffsetExclusive; j++)
{
newList.Add(original[j]);
}
return newList;
}
private DataContent? RetrieveImageContent(string imageId)
{
if (_imageContentById.TryGetValue(imageId, out var imageContent))
{
return imageContent as DataContent;
}
return null;
}
private string StoreImage(DataContent imageContent, bool isGenerated = false)
{
// Generate a unique ID for the image if it doesn't have one
string? imageId = null;
if (imageContent.AdditionalProperties?.TryGetValue(ImageKey, out imageId) is false || imageId is null)
{
imageId = imageContent.Name ?? Guid.NewGuid().ToString();
}
if (isGenerated)
{
imageContent.AdditionalProperties ??= [];
imageContent.AdditionalProperties[ImageKey] = imageId;
}
// Store the image content for later retrieval
_imageContentById[imageId] = imageContent;
return imageId;
}
}
}