-
Notifications
You must be signed in to change notification settings - Fork 868
Update to Microsoft.Extensions.AI.Abstractions 9.9.0 #3960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stephentoub
wants to merge
4
commits into
aws:development
Choose a base branch
from
stephentoub:meai98
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+307
−17
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockImageGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using Amazon.BedrockRuntime.Model; | ||
using Microsoft.Extensions.AI; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Amazon.BedrockRuntime; | ||
|
||
[Experimental("MEAI001")] | ||
internal sealed partial class BedrockImageGenerator : IImageGenerator | ||
{ | ||
/// <summary>The wrapped <see cref="IAmazonBedrockRuntime"/> instance.</summary> | ||
private readonly IAmazonBedrockRuntime _runtime; | ||
/// <summary>Default model ID to use when no model is specified in the request.</summary> | ||
private readonly string? _modelId; | ||
/// <summary>Metadata describing the image generator.</summary> | ||
private readonly ImageGeneratorMetadata _metadata; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BedrockImageGenerator"/> class. | ||
/// </summary> | ||
/// <param name="runtime">The <see cref="IAmazonBedrockRuntime"/> instance to wrap.</param> | ||
/// <param name="defaultModelId">Model ID to use as the default when no model ID is specified in a request.</param> | ||
public BedrockImageGenerator(IAmazonBedrockRuntime runtime, string? defaultModelId) | ||
{ | ||
Debug.Assert(runtime is not null); | ||
|
||
_runtime = runtime!; | ||
_modelId = defaultModelId; | ||
|
||
_metadata = new(AmazonBedrockRuntimeExtensions.ProviderName, defaultModelId: defaultModelId); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
// Do not dispose of _runtime, as this instance doesn't own it. | ||
} | ||
|
||
/// <inheritdoc /> | ||
|
||
/// <inheritdoc /> | ||
public object? GetService(Type serviceType, object? serviceKey) | ||
{ | ||
if (serviceType is null) | ||
{ | ||
throw new ArgumentNullException(nameof(serviceType)); | ||
} | ||
|
||
return | ||
serviceKey is not null ? null : | ||
serviceType == typeof(ImageGeneratorMetadata) ? _metadata : | ||
serviceType.IsInstanceOfType(_runtime) ? _runtime : | ||
serviceType.IsInstanceOfType(this) ? this : | ||
null; | ||
} | ||
|
||
public async Task<ImageGenerationResponse> GenerateAsync( | ||
ImageGenerationRequest request, ImageGenerationOptions? options = null, CancellationToken cancellationToken = default) | ||
{ | ||
if (request is null) | ||
{ | ||
throw new ArgumentNullException(nameof(request)); | ||
} | ||
|
||
int numImages = options?.Count ?? 1; | ||
if (numImages < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(options), "The number of images must be at least 1."); | ||
} | ||
|
||
InvokeModelRequest invokeRequest = options?.RawRepresentationFactory?.Invoke(this) as InvokeModelRequest ?? new(); | ||
invokeRequest.ModelId ??= options?.ModelId ?? _modelId; | ||
invokeRequest.Accept ??= "application/json"; | ||
invokeRequest.ContentType ??= "application/json"; | ||
if (invokeRequest.Body is null) | ||
{ | ||
JsonObject body = new(); | ||
|
||
// Each model has its own way of specifying the prompt and image generation parameters, unfortunately. | ||
// The following logic handles the most common cases today, but may need to be extended for | ||
// future models. | ||
|
||
if (invokeRequest.ModelId?.IndexOf("stability", StringComparison.OrdinalIgnoreCase) >= 0) | ||
{ | ||
// Stability AI models | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-stability-diffusion.html | ||
|
||
if (invokeRequest.ModelId?.IndexOf("stable-diffusion", StringComparison.OrdinalIgnoreCase) >= 0) | ||
{ | ||
JsonArray textPrompts = new(); | ||
for (int i = 0; i < numImages; i++) | ||
{ | ||
textPrompts.Add((JsonNode)new JsonObject { ["text"] = request.Prompt ?? "" }); | ||
} | ||
body["text_prompts"] = textPrompts; | ||
|
||
if (options?.ImageSize?.Width is int width && options.ImageSize?.Height is int height) | ||
{ | ||
body["width"] = width; | ||
body["height"] = height; | ||
} | ||
} | ||
else | ||
{ | ||
body["prompt"] = request.Prompt ?? ""; | ||
} | ||
} | ||
else | ||
{ | ||
// Amazon models (e.g. Titan, Nova Canvas) | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-titan.html | ||
|
||
JsonObject textToImageParams = new() { ["text"] = request.Prompt ?? "" }; | ||
if (request.OriginalImages?.OfType<DataContent>().Where(d => d.HasTopLevelMediaType("image")).FirstOrDefault() is DataContent image) | ||
{ | ||
textToImageParams["conditionImage"] = image.Base64Data.ToString(); | ||
} | ||
|
||
JsonObject imageGenerationConfig = new() | ||
{ | ||
["seed"] = | ||
#if NET | ||
Random.Shared.Next(), | ||
#else | ||
new Random().Next(), | ||
#endif | ||
}; | ||
|
||
if (options?.ImageSize?.Width is int width && options.ImageSize?.Height is int height) | ||
{ | ||
imageGenerationConfig["width"] = width; | ||
imageGenerationConfig["height"] = height; | ||
} | ||
|
||
if (numImages > 1) | ||
{ | ||
imageGenerationConfig["numberOfImages"] = Math.Min(numImages, 5); | ||
} | ||
|
||
body["taskType"] = "TEXT_IMAGE"; | ||
body["textToImageParams"] = textToImageParams; | ||
body["imageGenerationConfig"] = imageGenerationConfig; | ||
} | ||
|
||
invokeRequest.Body = new MemoryStream(JsonSerializer.SerializeToUtf8Bytes(body, BedrockJsonContext.Default.JsonNode)); | ||
} | ||
|
||
InvokeModelResponse rawResponse = await _runtime.InvokeModelAsync(invokeRequest, cancellationToken).ConfigureAwait(false); | ||
|
||
ImageGenerationResponse result = new() { RawRepresentation = rawResponse }; | ||
|
||
using JsonDocument doc = JsonDocument.Parse(rawResponse.Body); | ||
JsonElement root = doc.RootElement; | ||
|
||
const string DefaultGeneratedImageMimeType = "image/png"; | ||
|
||
if (root.TryGetProperty("artifacts", out JsonElement artifactElement) && artifactElement.ValueKind == JsonValueKind.Array) | ||
{ | ||
foreach (var element in artifactElement.EnumerateArray()) | ||
{ | ||
if (element.TryGetProperty("base64", out JsonElement base64Element) && | ||
base64Element.ValueKind == JsonValueKind.String) | ||
{ | ||
result.Contents.Add(new DataContent(Convert.FromBase64String(base64Element.GetString()!), DefaultGeneratedImageMimeType)); | ||
} | ||
} | ||
} | ||
else if (root.TryGetProperty("images", out JsonElement imagesElement) && imagesElement.ValueKind == JsonValueKind.Array) | ||
{ | ||
foreach (var image in imagesElement.EnumerateArray()) | ||
{ | ||
if (image.ValueKind == JsonValueKind.String) | ||
{ | ||
result.Contents.Add(new DataContent(Convert.FromBase64String(image.GetString()!), DefaultGeneratedImageMimeType)); | ||
} | ||
} | ||
} | ||
|
||
if (result.Contents is not { Count: > 0 }) | ||
{ | ||
throw new InvalidOperationException("Image generation did not produce any images."); | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.