Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ffe9a92
Prototype of using ImageGenerationTool
ericstj Aug 5, 2025
e5edc77
Handle DataContent returned from ImageGen
ericstj Aug 8, 2025
2d19cce
React to rename and improve metadata
ericstj Aug 9, 2025
5eef474
Handle image_generation tool content from streaming
ericstj Aug 20, 2025
ff80804
Add handling for combining updates with images
ericstj Aug 25, 2025
1725ce1
Add tests for new ChatResponseUpdateExtensions
ericstj Aug 26, 2025
c44f5fb
Merge branch 'main' of https://github.com/dotnet/extensions into Imag…
ericstj Sep 19, 2025
b4fe94b
Rename ImageGenerationTool to HostedImageGenerationTool
ericstj Sep 20, 2025
06bfa30
Remove ChatResponseUpdateCoalescingOptions
ericstj Sep 20, 2025
ca8b15d
Add ImageGeneratingChatClient
ericstj Sep 23, 2025
62e0ac5
Fix namespace of tool
ericstj Sep 26, 2025
81e6e5a
Replace traces of function calling
ericstj Sep 26, 2025
6559a66
More namepsace fix
ericstj Sep 26, 2025
398bbdb
Enable editing
ericstj Sep 30, 2025
ac2de35
Merge branch 'main' of https://github.com/dotnet/extensions into Imag…
ericstj Sep 30, 2025
1d96532
Update to preview OpenAI with image tool support
ericstj Oct 1, 2025
6a6ffa2
Temporary OpenAI feed
ericstj Oct 3, 2025
94ceab2
Fix tests
ericstj Oct 3, 2025
96e9747
Add integration tests for ImageGeneratingChatClient
ericstj Oct 3, 2025
9ddc91a
Remove ChatRole.Tool -> Assistant workaround
ericstj Oct 4, 2025
3b589ac
Remove use of private reflection for Image results
ericstj Oct 6, 2025
20919ab
Add ChatResponseUpdate.Clone
ericstj Oct 6, 2025
e5f68a6
Move all mutable state into RequestState object
ericstj Oct 7, 2025
9f9a430
Adjust prompt to improve integration test reliability
ericstj Oct 7, 2025
799a72e
Refactor tool initialization
ericstj Oct 7, 2025
6029b01
Add integration tests for streaming
ericstj Oct 7, 2025
173352a
Merge remote-tracking branch 'upstream/main' into ImageGenerationTool
ericstj Oct 27, 2025
69d2d98
React to changes and fix tests
ericstj Oct 27, 2025
86363f8
Address feedback
ericstj Oct 28, 2025
94cffbd
Fix SkipTestException from ConditionalTheory
ericstj Oct 30, 2025
67089ab
Merge branch 'main' of https://github.com/dotnet/extensions into Imag…
ericstj Oct 30, 2025
56cf3b4
Fix formatting
ericstj Oct 30, 2025
ad2b953
Add back image replacement coalescing (removed in merge)
ericstj Oct 30, 2025
e20b768
Fix template tests and use new OpenAI
ericstj Oct 30, 2025
b392df3
Merge branch 'main' of https://github.com/dotnet/extensions into Imag…
ericstj Nov 3, 2025
a38fd6c
Remove use of temporary staging nuget feed
ericstj Nov 3, 2025
cfa3f16
Address feedback
ericstj Nov 4, 2025
dcdebef
Make ImageGeneratingChatClient use ImageGenerationTool*Content
ericstj Nov 4, 2025
a60ddd2
Remove ApplyUpdates and Coalesce ImageResults instead of DataContent.
ericstj Nov 5, 2025
50db985
Workaround OpenAI issue where image data is not read for partial images.
ericstj Nov 5, 2025
10467a5
Improved workaround
ericstj Nov 5, 2025
8cff0ae
Return ImageGenerationToolCallContent from OpenAI
ericstj Nov 5, 2025
7780595
Add OpenAI image tool tests with representation of real traffic
ericstj Nov 6, 2025
9b94d68
Correct the event sequence for streaming single image
ericstj Nov 6, 2025
87dfaa4
Fix some docs and refactor for clarity
ericstj Nov 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,95 @@ static async Task AddMessagesAsync(
list.AddMessages(await updates.ToChatResponseAsync(cancellationToken).ConfigureAwait(false));
}

/// <summary>Applies a <see cref="ChatResponseUpdate"/> to an existing <see cref="ChatResponse"/>.</summary>
/// <param name="response">The response to which the update should be applied.</param>
/// <param name="update">The update to apply to the response.</param>
/// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="update"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This method modifies the existing <paramref name="response"/> by incorporating the content and metadata
/// from the <paramref name="update"/>. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
/// message boundaries, as well as coalescing contiguous <see cref="AIContent"/> items where applicable, e.g. multiple
/// <see cref="TextContent"/> instances in a row may be combined into a single <see cref="TextContent"/>.
/// </remarks>
[Experimental("MEAI0001")]
public static void ApplyUpdate(this ChatResponse response, ChatResponseUpdate update)
Comment thread
ericstj marked this conversation as resolved.
Outdated
{
_ = Throw.IfNull(response);
_ = Throw.IfNull(update);

ProcessUpdate(update, response);
FinalizeResponse(response);
}

/// <summary>Applies <see cref="ChatResponseUpdate"/> instances to an existing <see cref="ChatResponse"/>.</summary>
/// <param name="response">The response to which the updates should be applied.</param>
/// <param name="updates">The updates to apply to the response.</param>
/// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="updates"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This method modifies the existing <paramref name="response"/> by incorporating the content and metadata
/// from the <paramref name="updates"/>. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
/// message boundaries, as well as coalescing contiguous <see cref="AIContent"/> items where applicable, e.g. multiple
/// <see cref="TextContent"/> instances in a row may be combined into a single <see cref="TextContent"/>.
/// </remarks>
[Experimental("MEAI0001")]
public static void ApplyUpdates(this ChatResponse response, IEnumerable<ChatResponseUpdate> updates)
{
_ = Throw.IfNull(response);
_ = Throw.IfNull(updates);

if (updates is ICollection<ChatResponseUpdate> { Count: 0 })
Comment thread
ericstj marked this conversation as resolved.
Outdated
{
return;
}

foreach (var update in updates)
{
ProcessUpdate(update, response);
}

FinalizeResponse(response);
}

/// <summary>Applies <see cref="ChatResponseUpdate"/> instances to an existing <see cref="ChatResponse"/> asynchronously.</summary>
/// <param name="response">The response to which the updates should be applied.</param>
/// <param name="updates">The updates to apply to the response.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task"/> representing the completion of the operation.</returns>
/// <exception cref="ArgumentNullException"><paramref name="response"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="updates"/> is <see langword="null"/>.</exception>
/// <remarks>
/// This method modifies the existing <paramref name="response"/> by incorporating the content and metadata
/// from the <paramref name="updates"/>. This includes using <see cref="ChatResponseUpdate.MessageId"/> to determine
/// message boundaries, as well as coalescing contiguous <see cref="AIContent"/> items where applicable, e.g. multiple
/// <see cref="TextContent"/> instances in a row may be combined into a single <see cref="TextContent"/>.
/// </remarks>
[Experimental("MEAI0001")]
public static Task ApplyUpdatesAsync(
this ChatResponse response,
IAsyncEnumerable<ChatResponseUpdate> updates,
CancellationToken cancellationToken = default)
{
_ = Throw.IfNull(response);
_ = Throw.IfNull(updates);

return ApplyUpdatesAsync(response, updates, cancellationToken);

static async Task ApplyUpdatesAsync(
ChatResponse response,
IAsyncEnumerable<ChatResponseUpdate> updates,
CancellationToken cancellationToken)
{
await foreach (var update in updates.WithCancellation(cancellationToken).ConfigureAwait(false))
{
ProcessUpdate(update, response);
}

FinalizeResponse(response);
}
}

/// <summary>Combines <see cref="ChatResponseUpdate"/> instances into a single <see cref="ChatResponse"/>.</summary>
/// <param name="updates">The updates to be combined.</param>
/// <returns>The combined <see cref="ChatResponse"/>.</returns>
Expand Down Expand Up @@ -375,6 +464,29 @@ private static void ProcessUpdate(ChatResponseUpdate update, ChatResponse respon
(response.Usage ??= new()).Add(usage.Details);
break;

case DataContent dataContent when
!string.IsNullOrEmpty(dataContent.Name):
// Check if there's an existing DataContent with the same name to replace
for (int i = 0; i < message.Contents.Count; i++)
Comment thread
ericstj marked this conversation as resolved.
Outdated
{
if (message.Contents[i] is DataContent existingDataContent &&
string.Equals(existingDataContent.Name, dataContent.Name, StringComparison.Ordinal))
{
// Replace the existing DataContent
message.Contents[i] = dataContent;
dataContent = null!;
break;
}
}

if (dataContent is not null)
{
// No existing DataContent with the same name, add it normally
message.Contents.Add(dataContent);
}

break;

default:
message.Contents.Add(content);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.AI;
/// </para>
/// <para>
/// The relationship between <see cref="ChatResponse"/> and <see cref="ChatResponseUpdate"/> is
/// codified in the <see cref="ChatResponseExtensions.ToChatResponseAsync"/> and
/// codified in the <see cref="ChatResponseExtensions.ToChatResponseAsync(IAsyncEnumerable{ChatResponseUpdate}, System.Threading.CancellationToken)"/> and
/// <see cref="ChatResponse.ToChatResponseUpdates"/>, which enable bidirectional conversions
/// between the two. Note, however, that the provided conversions may be lossy, for example if multiple
/// updates all have different <see cref="RawRepresentation"/> objects whereas there's only one slot for
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.AI;

/// <summary>Represents a hosted tool that can be specified to an AI service to enable it to perform image generation.</summary>
/// <remarks>
/// This tool does not itself implement image generation. It is a marker that can be used to inform a service
/// that the service is allowed to perform image generation if the service is capable of doing so.
/// </remarks>
[Experimental("MEAI001")]
public class HostedImageGenerationTool : AITool
{
/// <summary>
/// Initializes a new instance of the <see cref="HostedImageGenerationTool"/> class with the specified options.
/// </summary>
public HostedImageGenerationTool()
: base()
Comment thread
ericstj marked this conversation as resolved.
Outdated
{
}

/// <summary>
/// Gets or sets the options used to configure image generation.
/// </summary>
public ImageGenerationOptions? Options { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace Microsoft.Extensions.AI;
WriteIndented = true)]
[JsonSerializable(typeof(OpenAIClientExtensions.ToolJson))]
[JsonSerializable(typeof(IDictionary<string, object?>))]
[JsonSerializable(typeof(IDictionary<string, string?>))]
[JsonSerializable(typeof(IReadOnlyDictionary<string, object?>))]
[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(IEnumerable<string>))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(int))]
Comment thread
ericstj marked this conversation as resolved.
Outdated
internal sealed partial class OpenAIJsonContext : JsonSerializerContext;
Loading
Loading