Skip to content
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

[Azure OpenAI] Image generation support from 2023-06-01-preview service API #37317

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -8,6 +8,7 @@ public static partial class AzureOpenAIModelFactory
public static Azure.AI.OpenAI.EmbeddingItem EmbeddingItem(System.Collections.Generic.IEnumerable<float> embedding = null, int index = 0) { throw null; }
public static Azure.AI.OpenAI.Embeddings Embeddings(System.Collections.Generic.IEnumerable<Azure.AI.OpenAI.EmbeddingItem> data = null, Azure.AI.OpenAI.EmbeddingsUsage usage = null) { throw null; }
public static Azure.AI.OpenAI.EmbeddingsUsage EmbeddingsUsage(int promptTokens = 0, int totalTokens = 0) { throw null; }
public static Azure.AI.OpenAI.ImageLocation ImageLocation(System.Uri url = null) { throw null; }
}
public partial class ChatChoice
{
Expand Down Expand Up @@ -158,6 +159,79 @@ internal EmbeddingsUsage() { }
public int PromptTokens { get { throw null; } }
public int TotalTokens { get { throw null; } }
}
public partial class ImageGenerationOptions
{
public ImageGenerationOptions() { }
public ImageGenerationOptions(string prompt) { }
public int? ImageCount { get { throw null; } set { } }
public string Prompt { get { throw null; } set { } }
public Azure.AI.OpenAI.ImageGenerationResponseFormat? ResponseFormat { get { throw null; } set { } }
public Azure.AI.OpenAI.ImageSize? Size { get { throw null; } set { } }
public string User { get { throw null; } set { } }
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct ImageGenerationResponseFormat : System.IEquatable<Azure.AI.OpenAI.ImageGenerationResponseFormat>
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public ImageGenerationResponseFormat(string value) { throw null; }
public static Azure.AI.OpenAI.ImageGenerationResponseFormat Base64 { get { throw null; } }
public static Azure.AI.OpenAI.ImageGenerationResponseFormat Url { get { throw null; } }
public bool Equals(Azure.AI.OpenAI.ImageGenerationResponseFormat other) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object obj) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.AI.OpenAI.ImageGenerationResponseFormat left, Azure.AI.OpenAI.ImageGenerationResponseFormat right) { throw null; }
public static implicit operator Azure.AI.OpenAI.ImageGenerationResponseFormat (string value) { throw null; }
public static bool operator !=(Azure.AI.OpenAI.ImageGenerationResponseFormat left, Azure.AI.OpenAI.ImageGenerationResponseFormat right) { throw null; }
public override string ToString() { throw null; }
}
public partial class ImageGenerations
{
internal ImageGenerations() { }
public System.DateTimeOffset Created { get { throw null; } }
public System.Collections.Generic.IReadOnlyList<Azure.AI.OpenAI.ImageResponseItem> Data { get { throw null; } }
}
public partial class ImageLocation : Azure.AI.OpenAI.ImageResponseItem
{
internal ImageLocation() { }
public System.Uri Url { get { throw null; } }
public override System.IO.Stream GetStream(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class ImagePayload : Azure.AI.OpenAI.ImageResponseItem
{
internal ImagePayload() { }
public System.ReadOnlyMemory<char> Base64Data { get { throw null; } }
public override System.IO.Stream GetStream(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public override System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public abstract partial class ImageResponseItem
{
protected ImageResponseItem() { }
public abstract System.IO.Stream GetStream(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
public abstract System.Threading.Tasks.Task<System.IO.Stream> GetStreamAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public readonly partial struct ImageSize : System.IEquatable<Azure.AI.OpenAI.ImageSize>
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public ImageSize(string value) { throw null; }
public static Azure.AI.OpenAI.ImageSize Size1024x1024 { get { throw null; } }
public static Azure.AI.OpenAI.ImageSize Size256x256 { get { throw null; } }
public static Azure.AI.OpenAI.ImageSize Size512x512 { get { throw null; } }
public bool Equals(Azure.AI.OpenAI.ImageSize other) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object obj) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.AI.OpenAI.ImageSize left, Azure.AI.OpenAI.ImageSize right) { throw null; }
public static implicit operator Azure.AI.OpenAI.ImageSize (string value) { throw null; }
public static bool operator !=(Azure.AI.OpenAI.ImageSize left, Azure.AI.OpenAI.ImageSize right) { throw null; }
public override string ToString() { throw null; }
}
public partial class OpenAIClient
{
protected OpenAIClient() { }
Expand All @@ -180,14 +254,17 @@ public OpenAIClient(System.Uri endpoint, Azure.Core.TokenCredential tokenCredent
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.StreamingCompletions>> GetCompletionsStreamingAsync(string deploymentOrModelName, Azure.AI.OpenAI.CompletionsOptions completionsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.AI.OpenAI.Embeddings> GetEmbeddings(string deploymentOrModelName, Azure.AI.OpenAI.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.Embeddings>> GetEmbeddingsAsync(string deploymentOrModelName, Azure.AI.OpenAI.EmbeddingsOptions embeddingsOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.AI.OpenAI.ImageGenerations> GetImageGenerations(Azure.AI.OpenAI.ImageGenerationOptions imageGenerationOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.OpenAI.ImageGenerations>> GetImageGenerationsAsync(Azure.AI.OpenAI.ImageGenerationOptions imageGenerationOptions, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
}
public partial class OpenAIClientOptions : Azure.Core.ClientOptions
{
public OpenAIClientOptions(Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion version = Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion.V2023_03_15_Preview) { }
public OpenAIClientOptions(Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion version = Azure.AI.OpenAI.OpenAIClientOptions.ServiceVersion.V2023_06_01_Preview) { }
public enum ServiceVersion
{
V2022_12_01 = 1,
V2023_03_15_Preview = 2,
V2023_05_15 = 2,
V2023_06_01_Preview = 3,
}
}
public partial class StreamingChatChoice
Expand Down
27 changes: 27 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/src/Custom/ImageGenerationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

namespace Azure.AI.OpenAI
{
/// <summary> Represents the request data used to generate images. </summary>
public partial class ImageGenerationOptions
{
/// <summary> Initializes a new instance of ImageGenerationOptions. </summary>
public ImageGenerationOptions()
{
}

/// <summary>
/// Gets or sets the description used to influence the generation of requested images.
/// </summary>
/// <remarks>
/// For best results, ensure that the prompt is specific and sufficiently rich in details about
/// desired topical content.
/// </remarks>
public string Prompt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Collections.Generic;
using System.Text.Json;
using Azure;
using Azure.Core;

namespace Azure.AI.OpenAI
{
public partial class ImageGenerations
{
internal static ImageGenerations DeserializeImageGenerations(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
long created = default;
IReadOnlyList<ImageResponseItem> data = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("created"u8))
{
created = property.Value.GetInt64();
continue;
}
if (property.NameEquals("data"u8))
{
// CUSTOM CODE NOTE: "data" is replaced with a custom union type (via abstract class and
// inheritance) and this modification appropriately deserializes that.
List<ImageResponseItem> array = new List<ImageResponseItem>();
foreach (var item in property.Value.EnumerateArray())
{
array.Add(ImageResponseItem.DeserializeImageResponseItem(item));
}
data = array;
continue;
}
}
return new ImageGenerations(created, data);
}
}
}
41 changes: 41 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/src/Custom/ImageGenerations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Collections.Generic;

namespace Azure.AI.OpenAI
{
/// <summary> The result of an image generation operation. </summary>
public partial class ImageGenerations
{
/// <summary>
/// Gets a list of generated image items in the format specified for the request.
/// </summary>
public IReadOnlyList<ImageResponseItem> Data { get; }

/// <summary>
/// Gets the timestamp for when this <see cref="ImageGenerations"/> response was originated.
/// </summary>
public DateTimeOffset Created => DateTimeOffset.FromUnixTimeSeconds(InternalCreatedSecondsAfterUnixEpoch);

// CUSTOM CODE NOTE: Pending code generation directly deserializing to DateTimeOffset, that conversion is
// manual using an internal field.
internal long InternalCreatedSecondsAfterUnixEpoch { get; }

// CUSTOM CODE NOTE: .NET currently emits the .tsp union of 'ImageLocation | ImagePayload' only as the former
// item. To work around this for the moment, we'll temporary demote the emitted member to
// internal visibility and replace it with an appropriately typed custom counterpart.
internal IReadOnlyList<ImageLocation> InternalEmittedImageResponseItems { get; }

internal ImageGenerations(long created, IReadOnlyList<ImageResponseItem> data)
{
InternalCreatedSecondsAfterUnixEpoch = created;
Data = data;
}
}
}
82 changes: 82 additions & 0 deletions sdk/openai/Azure.AI.OpenAI/src/Custom/ImageLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.AI.OpenAI
{
/// <summary>
/// An individual image result, represented as a URL at which the image may be retrieved.
/// </summary>
public partial class ImageLocation : ImageResponseItem
{
// CUSTOM CODE NOTE: we augment the ImageLocation model with a reference to the client's pipeline to facilitate
// the GetStreamAsync() member function helper.
internal HttpPipeline ClientPipeline { get; set; }

/// <summary>
/// Gets a stream that encapsulates the image data located at this <see cref="ImageLocation"/>'s
/// <see cref="Url"/>. Uses the <see cref="HttpPipeline"/> from the originating client and inherits any
/// custom pipeline policy that was applied.
/// </summary>
/// <param name="cancellationToken">
/// A cancellation token that may be used to abort the streaming data retrieval.
/// </param>
/// <returns> A stream pointing to the image data at <see cref="Url"/>. </returns>
public override async Task<Stream> GetStreamAsync(CancellationToken cancellationToken = default)
{
HttpMessage message = ClientPipeline.CreateMessage();
message.BufferResponse = false;

var requestUri = new RequestUriBuilder();
requestUri.Reset(Url);

message.Request.Method = RequestMethod.Get;
message.Request.Uri = requestUri;
message.SetProperty(SkippableBearerTokenAuthenticationPolicy.SkipPropertyKey, true);

Response response = await ClientPipeline.ProcessMessageAsync(
message,
requestContext: null,
cancellationToken)
.ConfigureAwait(false);

return response.ContentStream;
}

/// <summary>
/// Gets a stream that encapsulates the image data located at this <see cref="ImageLocation"/>'s
/// <see cref="Url"/>. Uses the <see cref="HttpPipeline"/> from the originating client and inherits any
/// custom pipeline policy that was applied.
/// </summary>
/// <param name="cancellationToken">
/// A cancellation token that may be used to abort the streaming data retrieval.
/// </param>
/// <returns> A stream pointing to the image data at <see cref="Url"/>. </returns>
public override Stream GetStream(CancellationToken cancellationToken = default)
{
HttpMessage message = ClientPipeline.CreateMessage();
message.BufferResponse = false;

var requestUri = new RequestUriBuilder();
requestUri.Reset(Url);

message.Request.Method = RequestMethod.Get;
message.Request.Uri = requestUri;
message.SetProperty(SkippableBearerTokenAuthenticationPolicy.SkipPropertyKey, true);

RequestContext context = OpenAIClient.FromCancellationToken(cancellationToken);
Response response = ClientPipeline.ProcessMessage(message, context, cancellationToken);

return response.ContentStream;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Text.Json;

// CUSTOM CODE NOTE: The .NET emitter currently skips the second half of the 'ImageLocation | ImagePayload' union
// that should generate this class. It's implemented in custom code pending generation adjustment.

namespace Azure.AI.OpenAI
{
public partial class ImagePayload : ImageResponseItem
{
internal static ImagePayload DeserializeImagePayload(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
}
string base64JsonValue = default;
foreach (var property in element.EnumerateObject())
{
if (property.NameEquals("b64_json"u8))
{
base64JsonValue = property.Value.GetString();
continue;
}
}
return new ImagePayload(base64JsonValue);
}
}
}
Loading