diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceClientBuilderExtensions.cs b/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceClientBuilderExtensions.cs index 5ef054431019a..3348bd6a13c20 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceClientBuilderExtensions.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceClientBuilderExtensions.cs @@ -12,62 +12,35 @@ namespace Microsoft.Extensions.Azure { - /// Extension methods to add , to client builder. + /// Extension methods to add to client builder. public static partial class AIInferenceClientBuilderExtensions { /// Registers a instance. /// The builder to register with. /// Service host. /// A credential used to authenticate to an Azure Service. - public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, Uri endpoint, AzureKeyCredential credential) + public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, Uri endpoint, AzureKeyCredential credential) where TBuilder : IAzureClientFactoryBuilder { - return builder.RegisterClientFactory((options) => new ChatCompletionsClient(endpoint, credential, options)); + return builder.RegisterClientFactory((options) => new ChatCompletionsClient(endpoint, credential, options)); } /// Registers a instance. /// The builder to register with. /// Service host. - public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, Uri endpoint) + public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, Uri endpoint) where TBuilder : IAzureClientFactoryBuilderWithCredential { - return builder.RegisterClientFactory((options, cred) => new ChatCompletionsClient(endpoint, cred, options)); - } - - /// Registers a instance. - /// The builder to register with. - /// Service host. - /// A credential used to authenticate to an Azure Service. - public static IAzureClientBuilder AddEmbeddingsClient(this TBuilder builder, Uri endpoint, AzureKeyCredential credential) - where TBuilder : IAzureClientFactoryBuilder - { - return builder.RegisterClientFactory((options) => new EmbeddingsClient(endpoint, credential, options)); - } - - /// Registers a instance. - /// The builder to register with. - /// Service host. - public static IAzureClientBuilder AddEmbeddingsClient(this TBuilder builder, Uri endpoint) - where TBuilder : IAzureClientFactoryBuilderWithCredential - { - return builder.RegisterClientFactory((options, cred) => new EmbeddingsClient(endpoint, cred, options)); + return builder.RegisterClientFactory((options, cred) => new ChatCompletionsClient(endpoint, cred, options)); } /// Registers a instance. /// The builder to register with. /// The configuration values. - public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, TConfiguration configuration) - where TBuilder : IAzureClientFactoryBuilderWithConfiguration - { - return builder.RegisterClientFactory(configuration); - } - /// Registers a instance. - /// The builder to register with. - /// The configuration values. - public static IAzureClientBuilder AddEmbeddingsClient(this TBuilder builder, TConfiguration configuration) + public static IAzureClientBuilder AddChatCompletionsClient(this TBuilder builder, TConfiguration configuration) where TBuilder : IAzureClientFactoryBuilderWithConfiguration { - return builder.RegisterClientFactory(configuration); + return builder.RegisterClientFactory(configuration); } } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceModelFactory.cs b/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceModelFactory.cs index b69b4ab3c432c..947d086078dbd 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceModelFactory.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/AIInferenceModelFactory.cs @@ -30,6 +30,14 @@ public static ChatMessageTextContentItem ChatMessageTextContentItem(string text return new ChatMessageTextContentItem("text", serializedAdditionalRawData: null, text); } + /// Initializes a new instance of . + /// The details of the input audio. + /// A new instance for mocking. + public static ChatMessageAudioContentItem ChatMessageAudioContentItem(ChatMessageInputAudio inputAudio = null) + { + return new ChatMessageAudioContentItem("input_audio", serializedAdditionalRawData: null, inputAudio); + } + /// Initializes a new instance of . /// The ID of the tool call. /// The type of tool call. Currently, only `function` is supported. @@ -67,16 +75,6 @@ public static ChatCompletionsNamedToolChoice ChatCompletionsNamedToolChoice(Chat return new ChatCompletionsNamedToolChoice(type, function, serializedAdditionalRawData: null); } - /// Initializes a new instance of . - /// The number of tokens generated across all completions emissions. - /// The number of tokens in the provided prompts for the completions request. - /// The total number of tokens processed for the completions request and response. - /// A new instance for mocking. - public static CompletionsUsage CompletionsUsage(int completionTokens = default, int promptTokens = default, int totalTokens = default) - { - return new CompletionsUsage(completionTokens, promptTokens, totalTokens, serializedAdditionalRawData: null); - } - /// Initializes a new instance of . /// The ordered index associated with this chat completions choice. /// The reason that this chat completions choice completed its generated. @@ -102,6 +100,16 @@ public static ChatResponseMessage ChatResponseMessage(ChatRole role = default, s return new ChatResponseMessage(role, content, toolCalls?.ToList(), serializedAdditionalRawData: null); } + /// Initializes a new instance of . + /// The number of tokens generated across all completions emissions. + /// The number of tokens in the provided prompts for the completions request. + /// The total number of tokens processed for the completions request and response. + /// A new instance for mocking. + public static CompletionsUsage CompletionsUsage(int completionTokens = default, int promptTokens = default, int totalTokens = default) + { + return new CompletionsUsage(completionTokens, promptTokens, totalTokens, serializedAdditionalRawData: null); + } + /// Initializes a new instance of . /// The name of the AI model. For example: `Phi21`. /// The type of the AI model. A Unique identifier for the profile. @@ -112,43 +120,6 @@ public static ModelInfo ModelInfo(string modelName = null, ModelType modelType = return new ModelInfo(modelName, modelType, modelProviderName, serializedAdditionalRawData: null); } - /// Initializes a new instance of . - /// Unique identifier for the embeddings result. - /// Embedding values for the prompts submitted in the request. - /// Usage counts for tokens input using the embeddings API. - /// The model ID used to generate this result. - /// A new instance for mocking. - public static EmbeddingsResult EmbeddingsResult(string id = null, IEnumerable data = null, EmbeddingsUsage usage = null, string model = null) - { - data ??= new List(); - - return new EmbeddingsResult(id, data?.ToList(), usage, model, serializedAdditionalRawData: null); - } - - /// Initializes a new instance of . - /// - /// List of embedding values for the input prompt. These represent a measurement of the - /// vector-based relatedness of the provided input. Or a base64 encoded string of the embedding vector. - /// - /// Index of the prompt to which the EmbeddingItem corresponds. - /// A new instance for mocking. - public static EmbeddingItem EmbeddingItem(BinaryData embedding = null, int index = default) - { - return new EmbeddingItem(embedding, index, serializedAdditionalRawData: null); - } - - /// Initializes a new instance of . - /// Number of tokens in the request. - /// - /// Total number of tokens transacted in this request/response. Should equal the - /// number of tokens in the request. - /// - /// A new instance for mocking. - public static EmbeddingsUsage EmbeddingsUsage(int promptTokens = default, int totalTokens = default) - { - return new EmbeddingsUsage(promptTokens, totalTokens, serializedAdditionalRawData: null); - } - /// Initializes a new instance of . /// A unique identifier associated with this chat completions response. /// @@ -156,14 +127,14 @@ public static EmbeddingsUsage EmbeddingsUsage(int promptTokens = default, int to /// represented as seconds since the beginning of the Unix epoch of 00:00 on 1 Jan 1970. /// /// The model used for the chat completion. - /// Usage information for tokens processed and generated as part of this completions operation. /// /// An update to the collection of completion choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// + /// Usage information for tokens processed and generated as part of this completions operation. /// A new instance for mocking. - public static StreamingChatCompletionsUpdate StreamingChatCompletionsUpdate(string id = null, DateTimeOffset created = default, string model = null, CompletionsUsage usage = null, IEnumerable choices = null) + public static StreamingChatCompletionsUpdate StreamingChatCompletionsUpdate(string id = null, DateTimeOffset created = default, string model = null, IEnumerable choices = null, CompletionsUsage usage = null) { choices ??= new List(); @@ -171,8 +142,8 @@ public static StreamingChatCompletionsUpdate StreamingChatCompletionsUpdate(stri id, created, model, - usage, choices?.ToList(), + usage, serializedAdditionalRawData: null); } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/AudioContentFormat.cs b/sdk/ai/Azure.AI.Inference/src/Generated/AudioContentFormat.cs new file mode 100644 index 0000000000000..3723b8aa68c8b --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/AudioContentFormat.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ComponentModel; + +namespace Azure.AI.Inference +{ + /// A representation of the possible audio formats for audio. + public readonly partial struct AudioContentFormat : IEquatable + { + private readonly string _value; + + /// Initializes a new instance of . + /// is null. + public AudioContentFormat(string value) + { + _value = value ?? throw new ArgumentNullException(nameof(value)); + } + + private const string WavValue = "wav"; + private const string Mp3Value = "mp3"; + + /// Specifies audio in WAV format. + public static AudioContentFormat Wav { get; } = new AudioContentFormat(WavValue); + /// Specifies audio in MP3 format. + public static AudioContentFormat Mp3 { get; } = new AudioContentFormat(Mp3Value); + /// Determines if two values are the same. + public static bool operator ==(AudioContentFormat left, AudioContentFormat right) => left.Equals(right); + /// Determines if two values are not the same. + public static bool operator !=(AudioContentFormat left, AudioContentFormat right) => !left.Equals(right); + /// Converts a to a . + public static implicit operator AudioContentFormat(string value) => new AudioContentFormat(value); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override bool Equals(object obj) => obj is AudioContentFormat other && Equals(other); + /// + public bool Equals(AudioContentFormat other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); + + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public override int GetHashCode() => _value != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(_value) : 0; + /// + public override string ToString() => _value; + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.Serialization.cs index 1fd5ab2dd912a..8ad5ae184edae 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.Serialization.cs @@ -40,8 +40,6 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit writer.WriteNumberValue(Created, "U"); writer.WritePropertyName("model"u8); writer.WriteStringValue(Model); - writer.WritePropertyName("usage"u8); - writer.WriteObjectValue(Usage, options); writer.WritePropertyName("choices"u8); writer.WriteStartArray(); foreach (var item in Choices) @@ -49,6 +47,8 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit writer.WriteObjectValue(item, options); } writer.WriteEndArray(); + writer.WritePropertyName("usage"u8); + writer.WriteObjectValue(Usage, options); if (options.Format != "W" && _serializedAdditionalRawData != null) { foreach (var item in _serializedAdditionalRawData) @@ -89,8 +89,8 @@ internal static ChatCompletions DeserializeChatCompletions(JsonElement element, string id = default; DateTimeOffset created = default; string model = default; - CompletionsUsage usage = default; IReadOnlyList choices = default; + CompletionsUsage usage = default; IDictionary serializedAdditionalRawData = default; Dictionary rawDataDictionary = new Dictionary(); foreach (var property in element.EnumerateObject()) @@ -110,11 +110,6 @@ internal static ChatCompletions DeserializeChatCompletions(JsonElement element, model = property.Value.GetString(); continue; } - if (property.NameEquals("usage"u8)) - { - usage = CompletionsUsage.DeserializeCompletionsUsage(property.Value, options); - continue; - } if (property.NameEquals("choices"u8)) { List array = new List(); @@ -125,6 +120,11 @@ internal static ChatCompletions DeserializeChatCompletions(JsonElement element, choices = array; continue; } + if (property.NameEquals("usage"u8)) + { + usage = CompletionsUsage.DeserializeCompletionsUsage(property.Value, options); + continue; + } if (options.Format != "W") { rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); @@ -135,8 +135,8 @@ internal static ChatCompletions DeserializeChatCompletions(JsonElement element, id, created, model, - usage, choices, + usage, serializedAdditionalRawData); } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.cs index 1861a3df65f8c..7af202a833113 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletions.cs @@ -57,25 +57,25 @@ public partial class ChatCompletions /// represented as seconds since the beginning of the Unix epoch of 00:00 on 1 Jan 1970. /// /// The model used for the chat completion. - /// Usage information for tokens processed and generated as part of this completions operation. /// /// The collection of completions choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// - /// , , or is null. - internal ChatCompletions(string id, DateTimeOffset created, string model, CompletionsUsage usage, IEnumerable choices) + /// Usage information for tokens processed and generated as part of this completions operation. + /// , , or is null. + internal ChatCompletions(string id, DateTimeOffset created, string model, IEnumerable choices, CompletionsUsage usage) { Argument.AssertNotNull(id, nameof(id)); Argument.AssertNotNull(model, nameof(model)); - Argument.AssertNotNull(usage, nameof(usage)); Argument.AssertNotNull(choices, nameof(choices)); + Argument.AssertNotNull(usage, nameof(usage)); Id = id; Created = created; Model = model; - Usage = usage; Choices = choices.ToList(); + Usage = usage; } /// Initializes a new instance of . @@ -85,20 +85,20 @@ internal ChatCompletions(string id, DateTimeOffset created, string model, Comple /// represented as seconds since the beginning of the Unix epoch of 00:00 on 1 Jan 1970. /// /// The model used for the chat completion. - /// Usage information for tokens processed and generated as part of this completions operation. /// /// The collection of completions choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// + /// Usage information for tokens processed and generated as part of this completions operation. /// Keeps track of any properties unknown to the library. - internal ChatCompletions(string id, DateTimeOffset created, string model, CompletionsUsage usage, IReadOnlyList choices, IDictionary serializedAdditionalRawData) + internal ChatCompletions(string id, DateTimeOffset created, string model, IReadOnlyList choices, CompletionsUsage usage, IDictionary serializedAdditionalRawData) { Id = id; Created = created; Model = model; - Usage = usage; Choices = choices; + Usage = usage; _serializedAdditionalRawData = serializedAdditionalRawData; } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClient.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClient.cs index 89ce6729a8932..1d0c1bab7768c 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClient.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClient.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Azure.Core; @@ -18,9 +19,8 @@ namespace Azure.AI.Inference /// The ChatCompletions service client. public partial class ChatCompletionsClient { - private const string AuthorizationHeader = "Authorization"; + private const string AuthorizationHeader = "api-key"; private readonly AzureKeyCredential _keyCredential; - private const string AuthorizationApiKeyPrefix = "Bearer"; private static readonly string[] AuthorizationScopes = new string[] { "https://ml.azure.com/.default" }; private readonly TokenCredential _tokenCredential; private readonly HttpPipeline _pipeline; @@ -42,7 +42,7 @@ protected ChatCompletionsClient() /// Service host. /// A credential used to authenticate to an Azure Service. /// or is null. - public ChatCompletionsClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new AzureAIInferenceClientOptions()) + public ChatCompletionsClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new ChatCompletionsClientOptions()) { } @@ -50,7 +50,7 @@ protected ChatCompletionsClient() /// Service host. /// A credential used to authenticate to an Azure Service. /// or is null. - public ChatCompletionsClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new AzureAIInferenceClientOptions()) + public ChatCompletionsClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new ChatCompletionsClientOptions()) { } @@ -59,11 +59,29 @@ protected ChatCompletionsClient() /// A credential used to authenticate to an Azure Service. /// The options for configuring the client. /// or is null. - public ChatCompletionsClient(Uri endpoint, TokenCredential credential, AzureAIInferenceClientOptions options) + public ChatCompletionsClient(Uri endpoint, AzureKeyCredential credential, ChatCompletionsClientOptions options) { Argument.AssertNotNull(endpoint, nameof(endpoint)); Argument.AssertNotNull(credential, nameof(credential)); - options ??= new AzureAIInferenceClientOptions(); + options ??= new ChatCompletionsClientOptions(); + + ClientDiagnostics = new ClientDiagnostics(options, true); + _keyCredential = credential; + _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { new AzureKeyCredentialPolicy(_keyCredential, AuthorizationHeader) }, new ResponseClassifier()); + _endpoint = endpoint; + _apiVersion = options.Version; + } + + /// Initializes a new instance of ChatCompletionsClient. + /// Service host. + /// A credential used to authenticate to an Azure Service. + /// The options for configuring the client. + /// or is null. + public ChatCompletionsClient(Uri endpoint, TokenCredential credential, ChatCompletionsClientOptions options) + { + Argument.AssertNotNull(endpoint, nameof(endpoint)); + Argument.AssertNotNull(credential, nameof(credential)); + options ??= new ChatCompletionsClientOptions(); ClientDiagnostics = new ClientDiagnostics(options, true); _tokenCredential = credential; @@ -72,6 +90,198 @@ public ChatCompletionsClient(Uri endpoint, TokenCredential credential, AzureAIIn _apiVersion = options.Version; } + /// + /// Gets chat completions for the provided chat messages. + /// Completions support a wide variety of tasks and generate text that continues from or "completes" + /// provided prompt data. The method makes a REST API call to the `/chat/completions` route + /// on the given endpoint. + /// + /// + /// The collection of context messages associated with this chat completions request. + /// Typical usage begins with a chat message for the System role that provides instructions for + /// the behavior of the assistant, followed by alternating messages between the User and + /// Assistant roles. + /// + /// + /// A value that influences the probability of generated tokens appearing based on their cumulative + /// frequency in generated text. + /// Positive values will make tokens less likely to appear as their frequency increases and + /// decrease the likelihood of the model repeating the same statements verbatim. + /// Supported range is [-2, 2]. + /// + /// A value indicating whether chat completions should be streamed for this request. + /// + /// A value that influences the probability of generated tokens appearing based on their existing + /// presence in generated text. + /// Positive values will make tokens less likely to appear when they already exist and increase the + /// model's likelihood to output new topics. + /// Supported range is [-2, 2]. + /// + /// + /// The sampling temperature to use that controls the apparent creativity of generated completions. + /// Higher values will make output more random while lower values will make results more focused + /// and deterministic. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// + /// An alternative to sampling with temperature called nucleus sampling. This value causes the + /// model to consider the results of tokens with the provided probability mass. As an example, a + /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be + /// considered. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// The maximum number of tokens to generate. + /// + /// An object specifying the format that the model must output. + /// + /// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. + /// + /// Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + /// + /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + /// + /// A collection of textual sequences that will end completions generation. + /// + /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model + /// may response with a function call request and provide the input arguments in JSON format for that function. + /// + /// If specified, the model will configure which of the provided tools it can use for the chat completions response. + /// + /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the + /// same seed and parameters should return the same result. Determinism is not guaranteed. + /// + /// ID of the specific AI model to use, if more than one model is available on the endpoint. + /// + /// Controls what happens if extra parameters, undefined by the REST API, + /// are passed in the JSON request payload. + /// This sets the HTTP request header `extra-parameters`. + /// + /// The cancellation token to use. + /// is null. + internal virtual async Task> CompleteAsync(IEnumerable messages, float? frequencyPenalty = null, bool? internalShouldStreamResponse = null, float? presencePenalty = null, float? temperature = null, float? nucleusSamplingFactor = null, int? maxTokens = null, ChatCompletionsResponseFormat responseFormat = null, IEnumerable stopSequences = null, IEnumerable tools = null, BinaryData toolChoice = null, long? seed = null, string model = null, ExtraParameters? extraParams = null, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(messages, nameof(messages)); + + CompleteRequest completeRequest = new CompleteRequest( + messages.ToList(), + frequencyPenalty, + internalShouldStreamResponse, + presencePenalty, + temperature, + nucleusSamplingFactor, + maxTokens, + responseFormat, + stopSequences?.ToList() as IReadOnlyList ?? new ChangeTrackingList(), + tools?.ToList() as IReadOnlyList ?? new ChangeTrackingList(), + toolChoice, + seed, + model, + null); + RequestContext context = FromCancellationToken(cancellationToken); + Response response = await CompleteAsync(completeRequest.ToRequestContent(), extraParams?.ToString(), context).ConfigureAwait(false); + return Response.FromValue(ChatCompletions.FromResponse(response), response); + } + + /// + /// Gets chat completions for the provided chat messages. + /// Completions support a wide variety of tasks and generate text that continues from or "completes" + /// provided prompt data. The method makes a REST API call to the `/chat/completions` route + /// on the given endpoint. + /// + /// + /// The collection of context messages associated with this chat completions request. + /// Typical usage begins with a chat message for the System role that provides instructions for + /// the behavior of the assistant, followed by alternating messages between the User and + /// Assistant roles. + /// + /// + /// A value that influences the probability of generated tokens appearing based on their cumulative + /// frequency in generated text. + /// Positive values will make tokens less likely to appear as their frequency increases and + /// decrease the likelihood of the model repeating the same statements verbatim. + /// Supported range is [-2, 2]. + /// + /// A value indicating whether chat completions should be streamed for this request. + /// + /// A value that influences the probability of generated tokens appearing based on their existing + /// presence in generated text. + /// Positive values will make tokens less likely to appear when they already exist and increase the + /// model's likelihood to output new topics. + /// Supported range is [-2, 2]. + /// + /// + /// The sampling temperature to use that controls the apparent creativity of generated completions. + /// Higher values will make output more random while lower values will make results more focused + /// and deterministic. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// + /// An alternative to sampling with temperature called nucleus sampling. This value causes the + /// model to consider the results of tokens with the provided probability mass. As an example, a + /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be + /// considered. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// The maximum number of tokens to generate. + /// + /// An object specifying the format that the model must output. + /// + /// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. + /// + /// Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + /// + /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + /// + /// A collection of textual sequences that will end completions generation. + /// + /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model + /// may response with a function call request and provide the input arguments in JSON format for that function. + /// + /// If specified, the model will configure which of the provided tools it can use for the chat completions response. + /// + /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the + /// same seed and parameters should return the same result. Determinism is not guaranteed. + /// + /// ID of the specific AI model to use, if more than one model is available on the endpoint. + /// + /// Controls what happens if extra parameters, undefined by the REST API, + /// are passed in the JSON request payload. + /// This sets the HTTP request header `extra-parameters`. + /// + /// The cancellation token to use. + /// is null. + internal virtual Response Complete(IEnumerable messages, float? frequencyPenalty = null, bool? internalShouldStreamResponse = null, float? presencePenalty = null, float? temperature = null, float? nucleusSamplingFactor = null, int? maxTokens = null, ChatCompletionsResponseFormat responseFormat = null, IEnumerable stopSequences = null, IEnumerable tools = null, BinaryData toolChoice = null, long? seed = null, string model = null, ExtraParameters? extraParams = null, CancellationToken cancellationToken = default) + { + Argument.AssertNotNull(messages, nameof(messages)); + + CompleteRequest completeRequest = new CompleteRequest( + messages.ToList(), + frequencyPenalty, + internalShouldStreamResponse, + presencePenalty, + temperature, + nucleusSamplingFactor, + maxTokens, + responseFormat, + stopSequences?.ToList() as IReadOnlyList ?? new ChangeTrackingList(), + tools?.ToList() as IReadOnlyList ?? new ChangeTrackingList(), + toolChoice, + seed, + model, + null); + RequestContext context = FromCancellationToken(cancellationToken); + Response response = Complete(completeRequest.ToRequestContent(), extraParams?.ToString(), context); + return Response.FromValue(ChatCompletions.FromResponse(response), response); + } + /// /// Returns information about the AI model. /// The method makes a REST API call to the `/info` route on the given endpoint. diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/AzureAIInferenceClientOptions.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClientOptions.cs similarity index 70% rename from sdk/ai/Azure.AI.Inference/src/Generated/AzureAIInferenceClientOptions.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClientOptions.cs index 46dcb4716cad0..e68f1c4ec7fb8 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/AzureAIInferenceClientOptions.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsClientOptions.cs @@ -10,8 +10,8 @@ namespace Azure.AI.Inference { - /// Client options for Azure.AI.Inference library clients. - public partial class AzureAIInferenceClientOptions : ClientOptions + /// Client options for ChatCompletionsClient. + public partial class ChatCompletionsClientOptions : ClientOptions { private const ServiceVersion LatestVersion = ServiceVersion.V2024_05_01_Preview; @@ -24,8 +24,8 @@ public enum ServiceVersion internal string Version { get; } - /// Initializes new instance of AzureAIInferenceClientOptions. - public AzureAIInferenceClientOptions(ServiceVersion version = LatestVersion) + /// Initializes new instance of ChatCompletionsClientOptions. + public ChatCompletionsClientOptions(ServiceVersion version = LatestVersion) { Version = version switch { diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.Serialization.cs deleted file mode 100644 index 3d996776fc760..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.Serialization.cs +++ /dev/null @@ -1,356 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Inference -{ - public partial class ChatCompletionsOptions : IUtf8JsonSerializable, IJsonModel - { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - - /// The JSON writer. - /// The client options for reading and writing models. - protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(ChatCompletionsOptions)} does not support writing '{format}' format."); - } - - writer.WritePropertyName("messages"u8); - writer.WriteStartArray(); - foreach (var item in Messages) - { - writer.WriteObjectValue(item, options); - } - writer.WriteEndArray(); - if (Optional.IsDefined(FrequencyPenalty)) - { - writer.WritePropertyName("frequency_penalty"u8); - writer.WriteNumberValue(FrequencyPenalty.Value); - } - if (Optional.IsDefined(InternalShouldStreamResponse)) - { - writer.WritePropertyName("stream"u8); - writer.WriteBooleanValue(InternalShouldStreamResponse.Value); - } - if (Optional.IsDefined(PresencePenalty)) - { - writer.WritePropertyName("presence_penalty"u8); - writer.WriteNumberValue(PresencePenalty.Value); - } - if (Optional.IsDefined(Temperature)) - { - writer.WritePropertyName("temperature"u8); - writer.WriteNumberValue(Temperature.Value); - } - if (Optional.IsDefined(NucleusSamplingFactor)) - { - writer.WritePropertyName("top_p"u8); - writer.WriteNumberValue(NucleusSamplingFactor.Value); - } - if (Optional.IsDefined(MaxTokens)) - { - writer.WritePropertyName("max_tokens"u8); - writer.WriteNumberValue(MaxTokens.Value); - } - if (Optional.IsDefined(ResponseFormat)) - { - writer.WritePropertyName("response_format"u8); - writer.WriteObjectValue(ResponseFormat, options); - } - if (Optional.IsCollectionDefined(StopSequences)) - { - writer.WritePropertyName("stop"u8); - writer.WriteStartArray(); - foreach (var item in StopSequences) - { - writer.WriteStringValue(item); - } - writer.WriteEndArray(); - } - if (Optional.IsCollectionDefined(Tools)) - { - writer.WritePropertyName("tools"u8); - writer.WriteStartArray(); - foreach (var item in Tools) - { - writer.WriteObjectValue(item, options); - } - writer.WriteEndArray(); - } - if (Optional.IsDefined(InternalSuppressedToolChoice)) - { - writer.WritePropertyName("tool_choice"u8); -#if NET6_0_OR_GREATER - writer.WriteRawValue(InternalSuppressedToolChoice); -#else - using (JsonDocument document = JsonDocument.Parse(InternalSuppressedToolChoice)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - if (Optional.IsDefined(Seed)) - { - writer.WritePropertyName("seed"u8); - writer.WriteNumberValue(Seed.Value); - } - if (Optional.IsDefined(Model)) - { - writer.WritePropertyName("model"u8); - writer.WriteStringValue(Model); - } - foreach (var item in AdditionalProperties) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - - ChatCompletionsOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(ChatCompletionsOptions)} does not support reading '{format}' format."); - } - - using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeChatCompletionsOptions(document.RootElement, options); - } - - internal static ChatCompletionsOptions DeserializeChatCompletionsOptions(JsonElement element, ModelReaderWriterOptions options = null) - { - options ??= ModelSerializationExtensions.WireOptions; - - if (element.ValueKind == JsonValueKind.Null) - { - return null; - } - IList messages = default; - float? frequencyPenalty = default; - bool? stream = default; - float? presencePenalty = default; - float? temperature = default; - float? topP = default; - int? maxTokens = default; - ChatCompletionsResponseFormat responseFormat = default; - IList stop = default; - IList tools = default; - BinaryData toolChoice = default; - long? seed = default; - string model = default; - IDictionary additionalProperties = default; - Dictionary additionalPropertiesDictionary = new Dictionary(); - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("messages"u8)) - { - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(ChatRequestMessage.DeserializeChatRequestMessage(item, options)); - } - messages = array; - continue; - } - if (property.NameEquals("frequency_penalty"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - frequencyPenalty = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("stream"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - stream = property.Value.GetBoolean(); - continue; - } - if (property.NameEquals("presence_penalty"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - presencePenalty = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("temperature"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - temperature = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("top_p"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - topP = property.Value.GetSingle(); - continue; - } - if (property.NameEquals("max_tokens"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - maxTokens = property.Value.GetInt32(); - continue; - } - if (property.NameEquals("response_format"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - responseFormat = ChatCompletionsResponseFormat.DeserializeChatCompletionsResponseFormat(property.Value, options); - continue; - } - if (property.NameEquals("stop"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(item.GetString()); - } - stop = array; - continue; - } - if (property.NameEquals("tools"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(ChatCompletionsToolDefinition.DeserializeChatCompletionsToolDefinition(item, options)); - } - tools = array; - continue; - } - if (property.NameEquals("tool_choice"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - toolChoice = BinaryData.FromString(property.Value.GetRawText()); - continue; - } - if (property.NameEquals("seed"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - seed = property.Value.GetInt64(); - continue; - } - if (property.NameEquals("model"u8)) - { - model = property.Value.GetString(); - continue; - } - additionalPropertiesDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); - } - additionalProperties = additionalPropertiesDictionary; - return new ChatCompletionsOptions( - messages, - frequencyPenalty, - stream, - presencePenalty, - temperature, - topP, - maxTokens, - responseFormat, - stop ?? new ChangeTrackingList(), - tools ?? new ChangeTrackingList(), - toolChoice, - seed, - model, - additionalProperties); - } - - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - return ModelReaderWriter.Write(this, options); - default: - throw new FormatException($"The model {nameof(ChatCompletionsOptions)} does not support writing '{options.Format}' format."); - } - } - - ChatCompletionsOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - { - using JsonDocument document = JsonDocument.Parse(data); - return DeserializeChatCompletionsOptions(document.RootElement, options); - } - default: - throw new FormatException($"The model {nameof(ChatCompletionsOptions)} does not support reading '{options.Format}' format."); - } - } - - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; - - /// Deserializes the model from a raw response. - /// The response to deserialize the model from. - internal static ChatCompletionsOptions FromResponse(Response response) - { - using var document = JsonDocument.Parse(response.Content); - return DeserializeChatCompletionsOptions(document.RootElement); - } - - /// Convert into a . - internal virtual RequestContent ToRequestContent() - { - var content = new Utf8JsonRequestContent(); - content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); - return content; - } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.cs deleted file mode 100644 index 55b2bf833d24c..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsOptions.cs +++ /dev/null @@ -1,210 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Azure.AI.Inference -{ - /// - /// The configuration information for a chat completions request. - /// Completions support a wide variety of tasks and generate text that continues from or "completes" - /// provided prompt data. - /// - public partial class ChatCompletionsOptions - { - /// Initializes a new instance of . - /// - /// The collection of context messages associated with this chat completions request. - /// Typical usage begins with a chat message for the System role that provides instructions for - /// the behavior of the assistant, followed by alternating messages between the User and - /// Assistant roles. - /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include , , and . - /// - /// is null. - public ChatCompletionsOptions(IEnumerable messages) - { - Argument.AssertNotNull(messages, nameof(messages)); - - Messages = messages.ToList(); - StopSequences = new ChangeTrackingList(); - Tools = new ChangeTrackingList(); - AdditionalProperties = new ChangeTrackingDictionary(); - } - - /// Initializes a new instance of . - /// - /// The collection of context messages associated with this chat completions request. - /// Typical usage begins with a chat message for the System role that provides instructions for - /// the behavior of the assistant, followed by alternating messages between the User and - /// Assistant roles. - /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include , , and . - /// - /// - /// A value that influences the probability of generated tokens appearing based on their cumulative - /// frequency in generated text. - /// Positive values will make tokens less likely to appear as their frequency increases and - /// decrease the likelihood of the model repeating the same statements verbatim. - /// Supported range is [-2, 2]. - /// - /// A value indicating whether chat completions should be streamed for this request. - /// - /// A value that influences the probability of generated tokens appearing based on their existing - /// presence in generated text. - /// Positive values will make tokens less likely to appear when they already exist and increase the - /// model's likelihood to output new topics. - /// Supported range is [-2, 2]. - /// - /// - /// The sampling temperature to use that controls the apparent creativity of generated completions. - /// Higher values will make output more random while lower values will make results more focused - /// and deterministic. - /// It is not recommended to modify temperature and top_p for the same completions request as the - /// interaction of these two settings is difficult to predict. - /// Supported range is [0, 1]. - /// - /// - /// An alternative to sampling with temperature called nucleus sampling. This value causes the - /// model to consider the results of tokens with the provided probability mass. As an example, a - /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be - /// considered. - /// It is not recommended to modify temperature and top_p for the same completions request as the - /// interaction of these two settings is difficult to predict. - /// Supported range is [0, 1]. - /// - /// The maximum number of tokens to generate. - /// - /// The format that the model must output. Use this to enable JSON mode instead of the default text mode. - /// Note that to enable JSON mode, some AI models may also require you to instruct the model to produce JSON - /// via a system or user message. - /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include and . - /// - /// A collection of textual sequences that will end completions generation. - /// - /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model - /// may response with a function call request and provide the input arguments in JSON format for that function. - /// - /// If specified, the model will configure which of the provided tools it can use for the chat completions response. - /// - /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the - /// same seed and parameters should return the same result. Determinism is not guaranteed. - /// - /// ID of the specific AI model to use, if more than one model is available on the endpoint. - /// Additional Properties. - internal ChatCompletionsOptions(IList messages, float? frequencyPenalty, bool? internalShouldStreamResponse, float? presencePenalty, float? temperature, float? nucleusSamplingFactor, int? maxTokens, ChatCompletionsResponseFormat responseFormat, IList stopSequences, IList tools, BinaryData internalSuppressedToolChoice, long? seed, string model, IDictionary additionalProperties) - { - Messages = messages; - FrequencyPenalty = frequencyPenalty; - InternalShouldStreamResponse = internalShouldStreamResponse; - PresencePenalty = presencePenalty; - Temperature = temperature; - NucleusSamplingFactor = nucleusSamplingFactor; - MaxTokens = maxTokens; - ResponseFormat = responseFormat; - StopSequences = stopSequences; - Tools = tools; - InternalSuppressedToolChoice = internalSuppressedToolChoice; - Seed = seed; - Model = model; - AdditionalProperties = additionalProperties; - } - /// - /// A value that influences the probability of generated tokens appearing based on their cumulative - /// frequency in generated text. - /// Positive values will make tokens less likely to appear as their frequency increases and - /// decrease the likelihood of the model repeating the same statements verbatim. - /// Supported range is [-2, 2]. - /// - public float? FrequencyPenalty { get; set; } - /// - /// A value that influences the probability of generated tokens appearing based on their existing - /// presence in generated text. - /// Positive values will make tokens less likely to appear when they already exist and increase the - /// model's likelihood to output new topics. - /// Supported range is [-2, 2]. - /// - public float? PresencePenalty { get; set; } - /// - /// The sampling temperature to use that controls the apparent creativity of generated completions. - /// Higher values will make output more random while lower values will make results more focused - /// and deterministic. - /// It is not recommended to modify temperature and top_p for the same completions request as the - /// interaction of these two settings is difficult to predict. - /// Supported range is [0, 1]. - /// - public float? Temperature { get; set; } - /// - /// An alternative to sampling with temperature called nucleus sampling. This value causes the - /// model to consider the results of tokens with the provided probability mass. As an example, a - /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be - /// considered. - /// It is not recommended to modify temperature and top_p for the same completions request as the - /// interaction of these two settings is difficult to predict. - /// Supported range is [0, 1]. - /// - public float? NucleusSamplingFactor { get; set; } - /// The maximum number of tokens to generate. - public int? MaxTokens { get; set; } - /// - /// The format that the model must output. Use this to enable JSON mode instead of the default text mode. - /// Note that to enable JSON mode, some AI models may also require you to instruct the model to produce JSON - /// via a system or user message. - /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include and . - /// - public ChatCompletionsResponseFormat ResponseFormat { get; set; } - /// A collection of textual sequences that will end completions generation. - public IList StopSequences { get; } - /// - /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model - /// may response with a function call request and provide the input arguments in JSON format for that function. - /// - public IList Tools { get; } - /// - /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the - /// same seed and parameters should return the same result. Determinism is not guaranteed. - /// - public long? Seed { get; set; } - /// ID of the specific AI model to use, if more than one model is available on the endpoint. - public string Model { get; set; } - /// - /// Additional Properties - /// - /// To assign an object to the value of this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - public IDictionary AdditionalProperties { get; } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.Serialization.cs index 19320a2d360fd..76c5cc54cbe83 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.Serialization.cs @@ -13,7 +13,7 @@ namespace Azure.AI.Inference { [PersistableModelProxy(typeof(UnknownChatCompletionsResponseFormat))] - public partial class ChatCompletionsResponseFormat : IUtf8JsonSerializable, IJsonModel + internal partial class ChatCompletionsResponseFormat : IUtf8JsonSerializable, IJsonModel { void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); @@ -77,7 +77,8 @@ internal static ChatCompletionsResponseFormat DeserializeChatCompletionsResponse { switch (discriminator.GetString()) { - case "json_object": return ChatCompletionsResponseFormatJSON.DeserializeChatCompletionsResponseFormatJSON(element, options); + case "json_object": return ChatCompletionsResponseFormatJsonObject.DeserializeChatCompletionsResponseFormatJsonObject(element, options); + case "json_schema": return ChatCompletionsResponseFormatJsonSchema.DeserializeChatCompletionsResponseFormatJsonSchema(element, options); case "text": return ChatCompletionsResponseFormatText.DeserializeChatCompletionsResponseFormatText(element, options); } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.cs index 93d9fa9b6c0dc..65eee831814dd 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormat.cs @@ -15,9 +15,9 @@ namespace Azure.AI.Inference /// Note that to enable JSON mode, some AI models may also require you to instruct the model to produce JSON /// via a system or user message. /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include and . + /// The available derived classes include , and . /// - public abstract partial class ChatCompletionsResponseFormat + internal abstract partial class ChatCompletionsResponseFormat { /// /// Keeps track of any properties unknown to the library. diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.Serialization.cs similarity index 61% rename from sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.Serialization.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.Serialization.cs index c86d5a9969bdc..d2148969d2278 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.Serialization.cs @@ -13,11 +13,11 @@ namespace Azure.AI.Inference { - public partial class ChatCompletionsResponseFormatJSON : IUtf8JsonSerializable, IJsonModel + internal partial class ChatCompletionsResponseFormatJsonObject : IUtf8JsonSerializable, IJsonModel { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { writer.WriteStartObject(); JsonModelWriteCore(writer, options); @@ -28,28 +28,28 @@ void IJsonModel.Write(Utf8JsonWriter writer, /// The client options for reading and writing models. protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJSON)} does not support writing '{format}' format."); + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonObject)} does not support writing '{format}' format."); } base.JsonModelWriteCore(writer, options); } - ChatCompletionsResponseFormatJSON IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + ChatCompletionsResponseFormatJsonObject IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJSON)} does not support reading '{format}' format."); + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonObject)} does not support reading '{format}' format."); } using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeChatCompletionsResponseFormatJSON(document.RootElement, options); + return DeserializeChatCompletionsResponseFormatJsonObject(document.RootElement, options); } - internal static ChatCompletionsResponseFormatJSON DeserializeChatCompletionsResponseFormatJSON(JsonElement element, ModelReaderWriterOptions options = null) + internal static ChatCompletionsResponseFormatJsonObject DeserializeChatCompletionsResponseFormatJsonObject(JsonElement element, ModelReaderWriterOptions options = null) { options ??= ModelSerializationExtensions.WireOptions; @@ -73,46 +73,46 @@ internal static ChatCompletionsResponseFormatJSON DeserializeChatCompletionsResp } } serializedAdditionalRawData = rawDataDictionary; - return new ChatCompletionsResponseFormatJSON(type, serializedAdditionalRawData); + return new ChatCompletionsResponseFormatJsonObject(type, serializedAdditionalRawData); } - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": return ModelReaderWriter.Write(this, options); default: - throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJSON)} does not support writing '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonObject)} does not support writing '{options.Format}' format."); } } - ChatCompletionsResponseFormatJSON IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + ChatCompletionsResponseFormatJsonObject IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": { using JsonDocument document = JsonDocument.Parse(data); - return DeserializeChatCompletionsResponseFormatJSON(document.RootElement, options); + return DeserializeChatCompletionsResponseFormatJsonObject(document.RootElement, options); } default: - throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJSON)} does not support reading '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonObject)} does not support reading '{options.Format}' format."); } } - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; /// Deserializes the model from a raw response. /// The response to deserialize the model from. - internal static new ChatCompletionsResponseFormatJSON FromResponse(Response response) + internal static new ChatCompletionsResponseFormatJsonObject FromResponse(Response response) { using var document = JsonDocument.Parse(response.Content); - return DeserializeChatCompletionsResponseFormatJSON(document.RootElement); + return DeserializeChatCompletionsResponseFormatJsonObject(document.RootElement); } /// Convert into a . diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.cs similarity index 68% rename from sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.cs index e52f9c3529f03..21669dc2fb873 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJSON.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonObject.cs @@ -15,18 +15,18 @@ namespace Azure.AI.Inference /// Note that to enable JSON mode, some AI models may also require you to instruct the model to produce JSON /// via a system or user message. /// - public partial class ChatCompletionsResponseFormatJSON : ChatCompletionsResponseFormat + internal partial class ChatCompletionsResponseFormatJsonObject : ChatCompletionsResponseFormat { - /// Initializes a new instance of . - public ChatCompletionsResponseFormatJSON() + /// Initializes a new instance of . + public ChatCompletionsResponseFormatJsonObject() { Type = "json_object"; } - /// Initializes a new instance of . + /// Initializes a new instance of . /// The response format type to use for chat completions. /// Keeps track of any properties unknown to the library. - internal ChatCompletionsResponseFormatJSON(string type, IDictionary serializedAdditionalRawData) : base(type, serializedAdditionalRawData) + internal ChatCompletionsResponseFormatJsonObject(string type, IDictionary serializedAdditionalRawData) : base(type, serializedAdditionalRawData) { } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.Serialization.cs new file mode 100644 index 0000000000000..c830d922a9cff --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.Serialization.cs @@ -0,0 +1,134 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Inference +{ + internal partial class ChatCompletionsResponseFormatJsonSchema : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchema)} does not support writing '{format}' format."); + } + + base.JsonModelWriteCore(writer, options); + writer.WritePropertyName("json_schema"u8); + writer.WriteObjectValue(JsonSchema, options); + } + + ChatCompletionsResponseFormatJsonSchema IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchema)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeChatCompletionsResponseFormatJsonSchema(document.RootElement, options); + } + + internal static ChatCompletionsResponseFormatJsonSchema DeserializeChatCompletionsResponseFormatJsonSchema(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + ChatCompletionsResponseFormatJsonSchemaDefinition jsonSchema = default; + string type = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("json_schema"u8)) + { + jsonSchema = ChatCompletionsResponseFormatJsonSchemaDefinition.DeserializeChatCompletionsResponseFormatJsonSchemaDefinition(property.Value, options); + continue; + } + if (property.NameEquals("type"u8)) + { + type = property.Value.GetString(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new ChatCompletionsResponseFormatJsonSchema(type, serializedAdditionalRawData, jsonSchema); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options); + default: + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchema)} does not support writing '{options.Format}' format."); + } + } + + ChatCompletionsResponseFormatJsonSchema IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data); + return DeserializeChatCompletionsResponseFormatJsonSchema(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchema)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static new ChatCompletionsResponseFormatJsonSchema FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content); + return DeserializeChatCompletionsResponseFormatJsonSchema(document.RootElement); + } + + /// Convert into a . + internal override RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.cs new file mode 100644 index 0000000000000..f6932d2b49559 --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchema.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Inference +{ + /// + /// A response format for Chat Completions that restricts responses to emitting valid JSON objects, with a + /// JSON schema specified by the caller. + /// + internal partial class ChatCompletionsResponseFormatJsonSchema : ChatCompletionsResponseFormat + { + /// Initializes a new instance of . + /// The definition of the required JSON schema in the response, and associated metadata. + /// is null. + public ChatCompletionsResponseFormatJsonSchema(ChatCompletionsResponseFormatJsonSchemaDefinition jsonSchema) + { + Argument.AssertNotNull(jsonSchema, nameof(jsonSchema)); + + Type = "json_schema"; + JsonSchema = jsonSchema; + } + + /// Initializes a new instance of . + /// The response format type to use for chat completions. + /// Keeps track of any properties unknown to the library. + /// The definition of the required JSON schema in the response, and associated metadata. + internal ChatCompletionsResponseFormatJsonSchema(string type, IDictionary serializedAdditionalRawData, ChatCompletionsResponseFormatJsonSchemaDefinition jsonSchema) : base(type, serializedAdditionalRawData) + { + JsonSchema = jsonSchema; + } + + /// Initializes a new instance of for deserialization. + internal ChatCompletionsResponseFormatJsonSchema() + { + } + + /// The definition of the required JSON schema in the response, and associated metadata. + public ChatCompletionsResponseFormatJsonSchemaDefinition JsonSchema { get; } + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.Serialization.cs new file mode 100644 index 0000000000000..0701b54653a03 --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.Serialization.cs @@ -0,0 +1,206 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.Text.Json; +using Azure.Core; + +namespace Azure.AI.Inference +{ + internal partial class ChatCompletionsResponseFormatJsonSchemaDefinition : IUtf8JsonSerializable, IJsonModel + { + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + writer.WriteStartObject(); + JsonModelWriteCore(writer, options); + writer.WriteEndObject(); + } + + /// The JSON writer. + /// The client options for reading and writing models. + protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchemaDefinition)} does not support writing '{format}' format."); + } + + writer.WritePropertyName("name"u8); + writer.WriteStringValue(Name); + writer.WritePropertyName("schema"u8); + writer.WriteStartObject(); + foreach (var item in Schema) + { + writer.WritePropertyName(item.Key); + if (item.Value == null) + { + writer.WriteNullValue(); + continue; + } +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + writer.WriteEndObject(); + if (Optional.IsDefined(Description)) + { + writer.WritePropertyName("description"u8); + writer.WriteStringValue(Description); + } + if (Optional.IsDefined(Strict)) + { + writer.WritePropertyName("strict"u8); + writer.WriteBooleanValue(Strict.Value); + } + if (options.Format != "W" && _serializedAdditionalRawData != null) + { + foreach (var item in _serializedAdditionalRawData) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } +#endif + } + } + } + + ChatCompletionsResponseFormatJsonSchemaDefinition IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + if (format != "J") + { + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchemaDefinition)} does not support reading '{format}' format."); + } + + using JsonDocument document = JsonDocument.ParseValue(ref reader); + return DeserializeChatCompletionsResponseFormatJsonSchemaDefinition(document.RootElement, options); + } + + internal static ChatCompletionsResponseFormatJsonSchemaDefinition DeserializeChatCompletionsResponseFormatJsonSchemaDefinition(JsonElement element, ModelReaderWriterOptions options = null) + { + options ??= ModelSerializationExtensions.WireOptions; + + if (element.ValueKind == JsonValueKind.Null) + { + return null; + } + string name = default; + IDictionary schema = default; + string description = default; + bool? strict = default; + IDictionary serializedAdditionalRawData = default; + Dictionary rawDataDictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + if (property.NameEquals("name"u8)) + { + name = property.Value.GetString(); + continue; + } + if (property.NameEquals("schema"u8)) + { + Dictionary dictionary = new Dictionary(); + foreach (var property0 in property.Value.EnumerateObject()) + { + if (property0.Value.ValueKind == JsonValueKind.Null) + { + dictionary.Add(property0.Name, null); + } + else + { + dictionary.Add(property0.Name, BinaryData.FromString(property0.Value.GetRawText())); + } + } + schema = dictionary; + continue; + } + if (property.NameEquals("description"u8)) + { + description = property.Value.GetString(); + continue; + } + if (property.NameEquals("strict"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + strict = property.Value.GetBoolean(); + continue; + } + if (options.Format != "W") + { + rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + } + } + serializedAdditionalRawData = rawDataDictionary; + return new ChatCompletionsResponseFormatJsonSchemaDefinition(name, schema, description, strict, serializedAdditionalRawData); + } + + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + return ModelReaderWriter.Write(this, options); + default: + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchemaDefinition)} does not support writing '{options.Format}' format."); + } + } + + ChatCompletionsResponseFormatJsonSchemaDefinition IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + { + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + + switch (format) + { + case "J": + { + using JsonDocument document = JsonDocument.Parse(data); + return DeserializeChatCompletionsResponseFormatJsonSchemaDefinition(document.RootElement, options); + } + default: + throw new FormatException($"The model {nameof(ChatCompletionsResponseFormatJsonSchemaDefinition)} does not support reading '{options.Format}' format."); + } + } + + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + + /// Deserializes the model from a raw response. + /// The response to deserialize the model from. + internal static ChatCompletionsResponseFormatJsonSchemaDefinition FromResponse(Response response) + { + using var document = JsonDocument.Parse(response.Content); + return DeserializeChatCompletionsResponseFormatJsonSchemaDefinition(document.RootElement); + } + + /// Convert into a . + internal virtual RequestContent ToRequestContent() + { + var content = new Utf8JsonRequestContent(); + content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); + return content; + } + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.cs new file mode 100644 index 0000000000000..7491797047917 --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatJsonSchemaDefinition.cs @@ -0,0 +1,142 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Inference +{ + /// + /// Defines the response format for chat completions as JSON with a given schema. + /// The AI model will need to adhere to this schema when generating completions. + /// + internal partial class ChatCompletionsResponseFormatJsonSchemaDefinition + { + /// + /// Keeps track of any properties unknown to the library. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + private IDictionary _serializedAdditionalRawData; + + /// Initializes a new instance of . + /// A name that labels this JSON schema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + /// + /// The definition of the JSON schema. See https://json-schema.org/overview/what-is-jsonschema. + /// Note that AI models usually only support a subset of the keywords defined by JSON schema. + /// Consult your AI model documentation to determine what is supported. + /// + /// or is null. + public ChatCompletionsResponseFormatJsonSchemaDefinition(string name, IDictionary schema) + { + Argument.AssertNotNull(name, nameof(name)); + Argument.AssertNotNull(schema, nameof(schema)); + + Name = name; + Schema = schema; + } + + /// Initializes a new instance of . + /// A name that labels this JSON schema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + /// + /// The definition of the JSON schema. See https://json-schema.org/overview/what-is-jsonschema. + /// Note that AI models usually only support a subset of the keywords defined by JSON schema. + /// Consult your AI model documentation to determine what is supported. + /// + /// A description of the response format, used by the AI model to determine how to generate responses in this format. + /// + /// If set to true, the service will error out if the provided JSON schema contains keywords + /// not supported by the AI model. An example of such keyword may be `maxLength` for JSON type `string`. + /// If false, and the provided JSON schema contains keywords not supported by the AI model, + /// the AI model will not error out. Instead it will ignore the unsupported keywords. + /// + /// Keeps track of any properties unknown to the library. + internal ChatCompletionsResponseFormatJsonSchemaDefinition(string name, IDictionary schema, string description, bool? strict, IDictionary serializedAdditionalRawData) + { + Name = name; + Schema = schema; + Description = description; + Strict = strict; + _serializedAdditionalRawData = serializedAdditionalRawData; + } + + /// Initializes a new instance of for deserialization. + internal ChatCompletionsResponseFormatJsonSchemaDefinition() + { + } + + /// A name that labels this JSON schema. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. + public string Name { get; } + /// + /// The definition of the JSON schema. See https://json-schema.org/overview/what-is-jsonschema. + /// Note that AI models usually only support a subset of the keywords defined by JSON schema. + /// Consult your AI model documentation to determine what is supported. + /// + /// To assign an object to the value of this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + public IDictionary Schema { get; } + /// A description of the response format, used by the AI model to determine how to generate responses in this format. + public string Description { get; set; } + /// + /// If set to true, the service will error out if the provided JSON schema contains keywords + /// not supported by the AI model. An example of such keyword may be `maxLength` for JSON type `string`. + /// If false, and the provided JSON schema contains keywords not supported by the AI model, + /// the AI model will not error out. Instead it will ignore the unsupported keywords. + /// + public bool? Strict { get; set; } + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.Serialization.cs index 338a5e6052900..30bfe39f4c0f3 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.Serialization.cs @@ -13,7 +13,7 @@ namespace Azure.AI.Inference { - public partial class ChatCompletionsResponseFormatText : IUtf8JsonSerializable, IJsonModel + internal partial class ChatCompletionsResponseFormatText : IUtf8JsonSerializable, IJsonModel { void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.cs index e4df6128fe6cb..6ac05f84ce3e0 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatCompletionsResponseFormatText.cs @@ -11,7 +11,7 @@ namespace Azure.AI.Inference { /// A response format for Chat Completions that emits text responses. This is the default response format. - public partial class ChatCompletionsResponseFormatText : ChatCompletionsResponseFormat + internal partial class ChatCompletionsResponseFormatText : ChatCompletionsResponseFormat { /// Initializes a new instance of . public ChatCompletionsResponseFormatText() diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.Serialization.cs similarity index 50% rename from sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.Serialization.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.Serialization.cs index db940a8c92d07..bdc18645059ed 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.Serialization.cs @@ -13,11 +13,11 @@ namespace Azure.AI.Inference { - internal partial class EmbedRequest : IUtf8JsonSerializable, IJsonModel + public partial class ChatMessageAudioContentItem : IUtf8JsonSerializable, IJsonModel { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { writer.WriteStartObject(); JsonModelWriteCore(writer, options); @@ -26,46 +26,32 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOpti /// The JSON writer. /// The client options for reading and writing models. - protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(EmbedRequest)} does not support writing '{format}' format."); + throw new FormatException($"The model {nameof(ChatMessageAudioContentItem)} does not support writing '{format}' format."); } - writer.WritePropertyName("embeddingsOptions"u8); - writer.WriteObjectValue(EmbeddingsOptions, options); - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } + base.JsonModelWriteCore(writer, options); + writer.WritePropertyName("input_audio"u8); + writer.WriteObjectValue(InputAudio, options); } - EmbedRequest IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + ChatMessageAudioContentItem IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(EmbedRequest)} does not support reading '{format}' format."); + throw new FormatException($"The model {nameof(ChatMessageAudioContentItem)} does not support reading '{format}' format."); } using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeEmbedRequest(document.RootElement, options); + return DeserializeChatMessageAudioContentItem(document.RootElement, options); } - internal static EmbedRequest DeserializeEmbedRequest(JsonElement element, ModelReaderWriterOptions options = null) + internal static ChatMessageAudioContentItem DeserializeChatMessageAudioContentItem(JsonElement element, ModelReaderWriterOptions options = null) { options ??= ModelSerializationExtensions.WireOptions; @@ -73,14 +59,20 @@ internal static EmbedRequest DeserializeEmbedRequest(JsonElement element, ModelR { return null; } - EmbeddingsOptions embeddingsOptions = default; + ChatMessageInputAudio inputAudio = default; + string type = default; IDictionary serializedAdditionalRawData = default; Dictionary rawDataDictionary = new Dictionary(); foreach (var property in element.EnumerateObject()) { - if (property.NameEquals("embeddingsOptions"u8)) + if (property.NameEquals("input_audio"u8)) + { + inputAudio = ChatMessageInputAudio.DeserializeChatMessageInputAudio(property.Value, options); + continue; + } + if (property.NameEquals("type"u8)) { - embeddingsOptions = EmbeddingsOptions.DeserializeEmbeddingsOptions(property.Value, options); + type = property.Value.GetString(); continue; } if (options.Format != "W") @@ -89,50 +81,50 @@ internal static EmbedRequest DeserializeEmbedRequest(JsonElement element, ModelR } } serializedAdditionalRawData = rawDataDictionary; - return new EmbedRequest(embeddingsOptions, serializedAdditionalRawData); + return new ChatMessageAudioContentItem(type, serializedAdditionalRawData, inputAudio); } - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": return ModelReaderWriter.Write(this, options); default: - throw new FormatException($"The model {nameof(EmbedRequest)} does not support writing '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatMessageAudioContentItem)} does not support writing '{options.Format}' format."); } } - EmbedRequest IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + ChatMessageAudioContentItem IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": { using JsonDocument document = JsonDocument.Parse(data); - return DeserializeEmbedRequest(document.RootElement, options); + return DeserializeChatMessageAudioContentItem(document.RootElement, options); } default: - throw new FormatException($"The model {nameof(EmbedRequest)} does not support reading '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatMessageAudioContentItem)} does not support reading '{options.Format}' format."); } } - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; /// Deserializes the model from a raw response. /// The response to deserialize the model from. - internal static EmbedRequest FromResponse(Response response) + internal static new ChatMessageAudioContentItem FromResponse(Response response) { using var document = JsonDocument.Parse(response.Content); - return DeserializeEmbedRequest(document.RootElement); + return DeserializeChatMessageAudioContentItem(document.RootElement); } /// Convert into a . - internal virtual RequestContent ToRequestContent() + internal override RequestContent ToRequestContent() { var content = new Utf8JsonRequestContent(); content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.cs new file mode 100644 index 0000000000000..c92893f6ee43d --- /dev/null +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageAudioContentItem.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// + +#nullable disable + +using System; +using System.Collections.Generic; + +namespace Azure.AI.Inference +{ + /// A structured chat content item containing an audio content. + public partial class ChatMessageAudioContentItem : ChatMessageContentItem + { + /// Initializes a new instance of . + /// The details of the input audio. + /// is null. + public ChatMessageAudioContentItem(ChatMessageInputAudio inputAudio) + { + Argument.AssertNotNull(inputAudio, nameof(inputAudio)); + + Type = "input_audio"; + InputAudio = inputAudio; + } + + /// Initializes a new instance of . + /// The discriminated object type. + /// Keeps track of any properties unknown to the library. + /// The details of the input audio. + internal ChatMessageAudioContentItem(string type, IDictionary serializedAdditionalRawData, ChatMessageInputAudio inputAudio) : base(type, serializedAdditionalRawData) + { + InputAudio = inputAudio; + } + + /// Initializes a new instance of for deserialization. + internal ChatMessageAudioContentItem() + { + } + + /// The details of the input audio. + public ChatMessageInputAudio InputAudio { get; } + } +} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.Serialization.cs index 11e7c365f14ea..3a02f4155303a 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.Serialization.cs @@ -78,6 +78,7 @@ internal static ChatMessageContentItem DeserializeChatMessageContentItem(JsonEle switch (discriminator.GetString()) { case "image_url": return ChatMessageImageContentItem.DeserializeChatMessageImageContentItem(element, options); + case "input_audio": return ChatMessageAudioContentItem.DeserializeChatMessageAudioContentItem(element, options); case "text": return ChatMessageTextContentItem.DeserializeChatMessageTextContentItem(element, options); } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.cs index 3c1131052b9b3..2c314658a47ff 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageContentItem.cs @@ -13,7 +13,7 @@ namespace Azure.AI.Inference /// /// An abstract representation of a structured content item within a chat message. /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. - /// The available derived classes include and . + /// The available derived classes include , and . /// public abstract partial class ChatMessageContentItem { diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.Serialization.cs similarity index 59% rename from sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.Serialization.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.Serialization.cs index 0a344e8a3e879..55c7f032a84b7 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.Serialization.cs @@ -13,11 +13,11 @@ namespace Azure.AI.Inference { - public partial class EmbeddingsUsage : IUtf8JsonSerializable, IJsonModel + public partial class ChatMessageInputAudio : IUtf8JsonSerializable, IJsonModel { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); + void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) + void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) { writer.WriteStartObject(); JsonModelWriteCore(writer, options); @@ -28,16 +28,16 @@ void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterO /// The client options for reading and writing models. protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(EmbeddingsUsage)} does not support writing '{format}' format."); + throw new FormatException($"The model {nameof(ChatMessageInputAudio)} does not support writing '{format}' format."); } - writer.WritePropertyName("prompt_tokens"u8); - writer.WriteNumberValue(PromptTokens); - writer.WritePropertyName("total_tokens"u8); - writer.WriteNumberValue(TotalTokens); + writer.WritePropertyName("data"u8); + writer.WriteStringValue(Data); + writer.WritePropertyName("format"u8); + writer.WriteStringValue(Format.ToString()); if (options.Format != "W" && _serializedAdditionalRawData != null) { foreach (var item in _serializedAdditionalRawData) @@ -55,19 +55,19 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit } } - EmbeddingsUsage IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) + ChatMessageInputAudio IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; if (format != "J") { - throw new FormatException($"The model {nameof(EmbeddingsUsage)} does not support reading '{format}' format."); + throw new FormatException($"The model {nameof(ChatMessageInputAudio)} does not support reading '{format}' format."); } using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeEmbeddingsUsage(document.RootElement, options); + return DeserializeChatMessageInputAudio(document.RootElement, options); } - internal static EmbeddingsUsage DeserializeEmbeddingsUsage(JsonElement element, ModelReaderWriterOptions options = null) + internal static ChatMessageInputAudio DeserializeChatMessageInputAudio(JsonElement element, ModelReaderWriterOptions options = null) { options ??= ModelSerializationExtensions.WireOptions; @@ -75,20 +75,20 @@ internal static EmbeddingsUsage DeserializeEmbeddingsUsage(JsonElement element, { return null; } - int promptTokens = default; - int totalTokens = default; + string data = default; + AudioContentFormat format = default; IDictionary serializedAdditionalRawData = default; Dictionary rawDataDictionary = new Dictionary(); foreach (var property in element.EnumerateObject()) { - if (property.NameEquals("prompt_tokens"u8)) + if (property.NameEquals("data"u8)) { - promptTokens = property.Value.GetInt32(); + data = property.Value.GetString(); continue; } - if (property.NameEquals("total_tokens"u8)) + if (property.NameEquals("format"u8)) { - totalTokens = property.Value.GetInt32(); + format = new AudioContentFormat(property.Value.GetString()); continue; } if (options.Format != "W") @@ -97,46 +97,46 @@ internal static EmbeddingsUsage DeserializeEmbeddingsUsage(JsonElement element, } } serializedAdditionalRawData = rawDataDictionary; - return new EmbeddingsUsage(promptTokens, totalTokens, serializedAdditionalRawData); + return new ChatMessageInputAudio(data, format, serializedAdditionalRawData); } - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) + BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": return ModelReaderWriter.Write(this, options); default: - throw new FormatException($"The model {nameof(EmbeddingsUsage)} does not support writing '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatMessageInputAudio)} does not support writing '{options.Format}' format."); } } - EmbeddingsUsage IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) + ChatMessageInputAudio IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; + var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; switch (format) { case "J": { using JsonDocument document = JsonDocument.Parse(data); - return DeserializeEmbeddingsUsage(document.RootElement, options); + return DeserializeChatMessageInputAudio(document.RootElement, options); } default: - throw new FormatException($"The model {nameof(EmbeddingsUsage)} does not support reading '{options.Format}' format."); + throw new FormatException($"The model {nameof(ChatMessageInputAudio)} does not support reading '{options.Format}' format."); } } - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; + string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; /// Deserializes the model from a raw response. /// The response to deserialize the model from. - internal static EmbeddingsUsage FromResponse(Response response) + internal static ChatMessageInputAudio FromResponse(Response response) { using var document = JsonDocument.Parse(response.Content); - return DeserializeEmbeddingsUsage(document.RootElement); + return DeserializeChatMessageInputAudio(document.RootElement); } /// Convert into a . diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.cs similarity index 57% rename from sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.cs rename to sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.cs index 4cb73d47f7cdd..008c3db1d9f30 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbedRequest.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatMessageInputAudio.cs @@ -10,8 +10,8 @@ namespace Azure.AI.Inference { - /// The EmbedRequest. - internal partial class EmbedRequest + /// The details of an audio chat message content part. + public partial class ChatMessageInputAudio { /// /// Keeps track of any properties unknown to the library. @@ -45,31 +45,37 @@ internal partial class EmbedRequest /// private IDictionary _serializedAdditionalRawData; - /// Initializes a new instance of . - /// - /// is null. - internal EmbedRequest(EmbeddingsOptions embeddingsOptions) + /// Initializes a new instance of . + /// Base64 encoded audio data. + /// The audio format of the audio content. + /// is null. + public ChatMessageInputAudio(string data, AudioContentFormat format) { - Argument.AssertNotNull(embeddingsOptions, nameof(embeddingsOptions)); + Argument.AssertNotNull(data, nameof(data)); - EmbeddingsOptions = embeddingsOptions; + Data = data; + Format = format; } - /// Initializes a new instance of . - /// + /// Initializes a new instance of . + /// Base64 encoded audio data. + /// The audio format of the audio content. /// Keeps track of any properties unknown to the library. - internal EmbedRequest(EmbeddingsOptions embeddingsOptions, IDictionary serializedAdditionalRawData) + internal ChatMessageInputAudio(string data, AudioContentFormat format, IDictionary serializedAdditionalRawData) { - EmbeddingsOptions = embeddingsOptions; + Data = data; + Format = format; _serializedAdditionalRawData = serializedAdditionalRawData; } - /// Initializes a new instance of for deserialization. - internal EmbedRequest() + /// Initializes a new instance of for deserialization. + internal ChatMessageInputAudio() { } - /// Gets the embeddings options. - public EmbeddingsOptions EmbeddingsOptions { get; } + /// Base64 encoded audio data. + public string Data { get; } + /// The audio format of the audio content. + public AudioContentFormat Format { get; } } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestAssistantMessage.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestAssistantMessage.Serialization.cs index 76ab70699d710..eb2393dae82f1 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestAssistantMessage.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestAssistantMessage.Serialization.cs @@ -37,15 +37,8 @@ protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWri base.JsonModelWriteCore(writer, options); if (Optional.IsDefined(Content)) { - if (Content != null) - { - writer.WritePropertyName("content"u8); - writer.WriteStringValue(Content); - } - else - { - writer.WriteNull("content"); - } + writer.WritePropertyName("content"u8); + writer.WriteStringValue(Content); } if (Optional.IsCollectionDefined(ToolCalls)) { @@ -88,11 +81,6 @@ internal static ChatRequestAssistantMessage DeserializeChatRequestAssistantMessa { if (property.NameEquals("content"u8)) { - if (property.Value.ValueKind == JsonValueKind.Null) - { - content = null; - continue; - } content = property.Value.GetString(); continue; } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.Serialization.cs index cb137da445a70..8bc691ac2a0c7 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.Serialization.cs @@ -35,15 +35,11 @@ protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWri } base.JsonModelWriteCore(writer, options); - if (Content != null) + if (Optional.IsDefined(Content)) { writer.WritePropertyName("content"u8); writer.WriteStringValue(Content); } - else - { - writer.WriteNull("content"); - } writer.WritePropertyName("tool_call_id"u8); writer.WriteStringValue(ToolCallId); } @@ -77,11 +73,6 @@ internal static ChatRequestToolMessage DeserializeChatRequestToolMessage(JsonEle { if (property.NameEquals("content"u8)) { - if (property.Value.ValueKind == JsonValueKind.Null) - { - content = null; - continue; - } content = property.Value.GetString(); continue; } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.cs index 0d848616d0783..d496a231572ff 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ChatRequestToolMessage.cs @@ -14,15 +14,13 @@ namespace Azure.AI.Inference public partial class ChatRequestToolMessage : ChatRequestMessage { /// Initializes a new instance of . - /// The content of the message. /// The ID of the tool call resolved by the provided content. /// is null. - public ChatRequestToolMessage(string content, string toolCallId) + public ChatRequestToolMessage(string toolCallId) { Argument.AssertNotNull(toolCallId, nameof(toolCallId)); Role = ChatRole.Tool; - Content = content; ToolCallId = toolCallId; } @@ -43,7 +41,7 @@ internal ChatRequestToolMessage() } /// The content of the message. - public string Content { get; } + public string Content { get; set; } /// The ID of the tool call resolved by the provided content. public string ToolCallId { get; } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.Serialization.cs index 5b75cc0ab4da0..f5a9dc2847bf3 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.Serialization.cs @@ -34,22 +34,101 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit throw new FormatException($"The model {nameof(CompleteRequest)} does not support writing '{format}' format."); } - writer.WritePropertyName("chatCompletionsOptions"u8); - writer.WriteObjectValue(ChatCompletionsOptions, options); - if (options.Format != "W" && _serializedAdditionalRawData != null) + writer.WritePropertyName("messages"u8); + writer.WriteStartArray(); + foreach (var item in Messages) { - foreach (var item in _serializedAdditionalRawData) + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + if (Optional.IsDefined(FrequencyPenalty)) + { + writer.WritePropertyName("frequency_penalty"u8); + writer.WriteNumberValue(FrequencyPenalty.Value); + } + if (Optional.IsDefined(InternalShouldStreamResponse)) + { + writer.WritePropertyName("stream"u8); + writer.WriteBooleanValue(InternalShouldStreamResponse.Value); + } + if (Optional.IsDefined(PresencePenalty)) + { + writer.WritePropertyName("presence_penalty"u8); + writer.WriteNumberValue(PresencePenalty.Value); + } + if (Optional.IsDefined(Temperature)) + { + writer.WritePropertyName("temperature"u8); + writer.WriteNumberValue(Temperature.Value); + } + if (Optional.IsDefined(NucleusSamplingFactor)) + { + writer.WritePropertyName("top_p"u8); + writer.WriteNumberValue(NucleusSamplingFactor.Value); + } + if (Optional.IsDefined(MaxTokens)) + { + writer.WritePropertyName("max_tokens"u8); + writer.WriteNumberValue(MaxTokens.Value); + } + if (Optional.IsDefined(ResponseFormat)) + { + writer.WritePropertyName("response_format"u8); + writer.WriteObjectValue(ResponseFormat, options); + } + if (Optional.IsCollectionDefined(StopSequences)) + { + writer.WritePropertyName("stop"u8); + writer.WriteStartArray(); + foreach (var item in StopSequences) + { + writer.WriteStringValue(item); + } + writer.WriteEndArray(); + } + if (Optional.IsCollectionDefined(Tools)) + { + writer.WritePropertyName("tools"u8); + writer.WriteStartArray(); + foreach (var item in Tools) { - writer.WritePropertyName(item.Key); + writer.WriteObjectValue(item, options); + } + writer.WriteEndArray(); + } + if (Optional.IsDefined(ToolChoice)) + { + writer.WritePropertyName("tool_choice"u8); #if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); + writer.WriteRawValue(ToolChoice); #else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } + using (JsonDocument document = JsonDocument.Parse(ToolChoice)) + { + JsonSerializer.Serialize(writer, document.RootElement); + } #endif + } + if (Optional.IsDefined(Seed)) + { + writer.WritePropertyName("seed"u8); + writer.WriteNumberValue(Seed.Value); + } + if (Optional.IsDefined(Model)) + { + writer.WritePropertyName("model"u8); + writer.WriteStringValue(Model); + } + foreach (var item in AdditionalProperties) + { + writer.WritePropertyName(item.Key); +#if NET6_0_OR_GREATER + writer.WriteRawValue(item.Value); +#else + using (JsonDocument document = JsonDocument.Parse(item.Value)) + { + JsonSerializer.Serialize(writer, document.RootElement); } +#endif } } @@ -73,23 +152,165 @@ internal static CompleteRequest DeserializeCompleteRequest(JsonElement element, { return null; } - ChatCompletionsOptions chatCompletionsOptions = default; - IDictionary serializedAdditionalRawData = default; - Dictionary rawDataDictionary = new Dictionary(); + IReadOnlyList messages = default; + float? frequencyPenalty = default; + bool? stream = default; + float? presencePenalty = default; + float? temperature = default; + float? topP = default; + int? maxTokens = default; + ChatCompletionsResponseFormat responseFormat = default; + IReadOnlyList stop = default; + IReadOnlyList tools = default; + BinaryData toolChoice = default; + long? seed = default; + string model = default; + IReadOnlyDictionary additionalProperties = default; + Dictionary additionalPropertiesDictionary = new Dictionary(); foreach (var property in element.EnumerateObject()) { - if (property.NameEquals("chatCompletionsOptions"u8)) + if (property.NameEquals("messages"u8)) { - chatCompletionsOptions = ChatCompletionsOptions.DeserializeChatCompletionsOptions(property.Value, options); + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(ChatRequestMessage.DeserializeChatRequestMessage(item, options)); + } + messages = array; + continue; + } + if (property.NameEquals("frequency_penalty"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + frequencyPenalty = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("stream"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + stream = property.Value.GetBoolean(); continue; } - if (options.Format != "W") + if (property.NameEquals("presence_penalty"u8)) { - rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + presencePenalty = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("temperature"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + temperature = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("top_p"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + topP = property.Value.GetSingle(); + continue; + } + if (property.NameEquals("max_tokens"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + maxTokens = property.Value.GetInt32(); + continue; + } + if (property.NameEquals("response_format"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + responseFormat = ChatCompletionsResponseFormat.DeserializeChatCompletionsResponseFormat(property.Value, options); + continue; + } + if (property.NameEquals("stop"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(item.GetString()); + } + stop = array; + continue; + } + if (property.NameEquals("tools"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + List array = new List(); + foreach (var item in property.Value.EnumerateArray()) + { + array.Add(ChatCompletionsToolDefinition.DeserializeChatCompletionsToolDefinition(item, options)); + } + tools = array; + continue; + } + if (property.NameEquals("tool_choice"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + toolChoice = BinaryData.FromString(property.Value.GetRawText()); + continue; + } + if (property.NameEquals("seed"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + seed = property.Value.GetInt64(); + continue; + } + if (property.NameEquals("model"u8)) + { + model = property.Value.GetString(); + continue; } + additionalPropertiesDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); } - serializedAdditionalRawData = rawDataDictionary; - return new CompleteRequest(chatCompletionsOptions, serializedAdditionalRawData); + additionalProperties = additionalPropertiesDictionary; + return new CompleteRequest( + messages, + frequencyPenalty, + stream, + presencePenalty, + temperature, + topP, + maxTokens, + responseFormat, + stop ?? new ChangeTrackingList(), + tools ?? new ChangeTrackingList(), + toolChoice, + seed, + model, + additionalProperties); } BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.cs b/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.cs index 5e6b00ae756c7..e723129160d6c 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/CompleteRequest.cs @@ -7,14 +7,239 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Azure.AI.Inference { /// The CompleteRequest. internal partial class CompleteRequest { + /// Initializes a new instance of . + /// + /// The collection of context messages associated with this chat completions request. + /// Typical usage begins with a chat message for the System role that provides instructions for + /// the behavior of the assistant, followed by alternating messages between the User and + /// Assistant roles. + /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. + /// The available derived classes include , , and . + /// + /// is null. + internal CompleteRequest(IEnumerable messages) + { + Argument.AssertNotNull(messages, nameof(messages)); + + Messages = messages.ToList(); + StopSequences = new ChangeTrackingList(); + Tools = new ChangeTrackingList(); + AdditionalProperties = new ChangeTrackingDictionary(); + } + + /// Initializes a new instance of . + /// + /// The collection of context messages associated with this chat completions request. + /// Typical usage begins with a chat message for the System role that provides instructions for + /// the behavior of the assistant, followed by alternating messages between the User and + /// Assistant roles. + /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. + /// The available derived classes include , , and . + /// + /// + /// A value that influences the probability of generated tokens appearing based on their cumulative + /// frequency in generated text. + /// Positive values will make tokens less likely to appear as their frequency increases and + /// decrease the likelihood of the model repeating the same statements verbatim. + /// Supported range is [-2, 2]. + /// + /// A value indicating whether chat completions should be streamed for this request. + /// + /// A value that influences the probability of generated tokens appearing based on their existing + /// presence in generated text. + /// Positive values will make tokens less likely to appear when they already exist and increase the + /// model's likelihood to output new topics. + /// Supported range is [-2, 2]. + /// + /// + /// The sampling temperature to use that controls the apparent creativity of generated completions. + /// Higher values will make output more random while lower values will make results more focused + /// and deterministic. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// + /// An alternative to sampling with temperature called nucleus sampling. This value causes the + /// model to consider the results of tokens with the provided probability mass. As an example, a + /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be + /// considered. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + /// The maximum number of tokens to generate. + /// + /// An object specifying the format that the model must output. + /// + /// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. + /// + /// Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + /// + /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. + /// The available derived classes include , and . + /// + /// A collection of textual sequences that will end completions generation. + /// + /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model + /// may response with a function call request and provide the input arguments in JSON format for that function. + /// + /// If specified, the model will configure which of the provided tools it can use for the chat completions response. + /// + /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the + /// same seed and parameters should return the same result. Determinism is not guaranteed. + /// + /// ID of the specific AI model to use, if more than one model is available on the endpoint. + /// Additional Properties. + internal CompleteRequest(IReadOnlyList messages, float? frequencyPenalty, bool? internalShouldStreamResponse, float? presencePenalty, float? temperature, float? nucleusSamplingFactor, int? maxTokens, ChatCompletionsResponseFormat responseFormat, IReadOnlyList stopSequences, IReadOnlyList tools, BinaryData toolChoice, long? seed, string model, IReadOnlyDictionary additionalProperties) + { + Messages = messages; + FrequencyPenalty = frequencyPenalty; + InternalShouldStreamResponse = internalShouldStreamResponse; + PresencePenalty = presencePenalty; + Temperature = temperature; + NucleusSamplingFactor = nucleusSamplingFactor; + MaxTokens = maxTokens; + ResponseFormat = responseFormat; + StopSequences = stopSequences; + Tools = tools; + ToolChoice = toolChoice; + Seed = seed; + Model = model; + AdditionalProperties = additionalProperties; + } + + /// Initializes a new instance of for deserialization. + internal CompleteRequest() + { + } + + /// + /// The collection of context messages associated with this chat completions request. + /// Typical usage begins with a chat message for the System role that provides instructions for + /// the behavior of the assistant, followed by alternating messages between the User and + /// Assistant roles. + /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. + /// The available derived classes include , , and . + /// + public IReadOnlyList Messages { get; } + /// + /// A value that influences the probability of generated tokens appearing based on their cumulative + /// frequency in generated text. + /// Positive values will make tokens less likely to appear as their frequency increases and + /// decrease the likelihood of the model repeating the same statements verbatim. + /// Supported range is [-2, 2]. + /// + public float? FrequencyPenalty { get; } + /// A value indicating whether chat completions should be streamed for this request. + public bool? InternalShouldStreamResponse { get; } + /// + /// A value that influences the probability of generated tokens appearing based on their existing + /// presence in generated text. + /// Positive values will make tokens less likely to appear when they already exist and increase the + /// model's likelihood to output new topics. + /// Supported range is [-2, 2]. + /// + public float? PresencePenalty { get; } /// - /// Keeps track of any properties unknown to the library. + /// The sampling temperature to use that controls the apparent creativity of generated completions. + /// Higher values will make output more random while lower values will make results more focused + /// and deterministic. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + public float? Temperature { get; } + /// + /// An alternative to sampling with temperature called nucleus sampling. This value causes the + /// model to consider the results of tokens with the provided probability mass. As an example, a + /// value of 0.15 will cause only the tokens comprising the top 15% of probability mass to be + /// considered. + /// It is not recommended to modify temperature and top_p for the same completions request as the + /// interaction of these two settings is difficult to predict. + /// Supported range is [0, 1]. + /// + public float? NucleusSamplingFactor { get; } + /// The maximum number of tokens to generate. + public int? MaxTokens { get; } + /// + /// An object specifying the format that the model must output. + /// + /// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which ensures the model will match your supplied JSON schema. + /// + /// Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the message the model generates is valid JSON. + /// + /// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length. + /// Please note is the base class. According to the scenario, a derived class of the base class might need to be assigned here, or this property needs to be casted to one of the possible derived classes. + /// The available derived classes include , and . + /// + public ChatCompletionsResponseFormat ResponseFormat { get; } + /// A collection of textual sequences that will end completions generation. + public IReadOnlyList StopSequences { get; } + /// + /// A list of tools the model may request to call. Currently, only functions are supported as a tool. The model + /// may response with a function call request and provide the input arguments in JSON format for that function. + /// + public IReadOnlyList Tools { get; } + /// + /// If specified, the model will configure which of the provided tools it can use for the chat completions response. + /// + /// To assign an object to this property use . + /// + /// + /// To assign an already formatted json string to this property use . + /// + /// + /// + /// Supported types: + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// Examples: + /// + /// + /// BinaryData.FromObjectAsJson("foo") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromString("\"foo\"") + /// Creates a payload of "foo". + /// + /// + /// BinaryData.FromObjectAsJson(new { key = "value" }) + /// Creates a payload of { "key": "value" }. + /// + /// + /// BinaryData.FromString("{\"key\": \"value\"}") + /// Creates a payload of { "key": "value" }. + /// + /// + /// + /// + public BinaryData ToolChoice { get; } + /// + /// If specified, the system will make a best effort to sample deterministically such that repeated requests with the + /// same seed and parameters should return the same result. Determinism is not guaranteed. + /// + public long? Seed { get; } + /// ID of the specific AI model to use, if more than one model is available on the endpoint. + public string Model { get; } + /// + /// Additional Properties /// /// To assign an object to the value of this property use . /// @@ -43,33 +268,6 @@ internal partial class CompleteRequest /// /// /// - private IDictionary _serializedAdditionalRawData; - - /// Initializes a new instance of . - /// - /// is null. - internal CompleteRequest(ChatCompletionsOptions chatCompletionsOptions) - { - Argument.AssertNotNull(chatCompletionsOptions, nameof(chatCompletionsOptions)); - - ChatCompletionsOptions = chatCompletionsOptions; - } - - /// Initializes a new instance of . - /// - /// Keeps track of any properties unknown to the library. - internal CompleteRequest(ChatCompletionsOptions chatCompletionsOptions, IDictionary serializedAdditionalRawData) - { - ChatCompletionsOptions = chatCompletionsOptions; - _serializedAdditionalRawData = serializedAdditionalRawData; - } - - /// Initializes a new instance of for deserialization. - internal CompleteRequest() - { - } - - /// Gets the chat completions options. - public ChatCompletionsOptions ChatCompletionsOptions { get; } + public IReadOnlyDictionary AdditionalProperties { get; } } } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/Docs/EmbeddingsClient.xml b/sdk/ai/Azure.AI.Inference/src/Generated/Docs/EmbeddingsClient.xml deleted file mode 100644 index 2f52491f2297c..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/Docs/EmbeddingsClient.xml +++ /dev/null @@ -1,217 +0,0 @@ - - - - - -This sample shows how to call EmbedAsync. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -EmbeddingsOptions embeddingsOptions = null; -Response response = await client.EmbedAsync(embeddingsOptions); -]]> -This sample shows how to call EmbedAsync. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -EmbeddingsOptions embeddingsOptions = null; -Response response = await client.EmbedAsync(embeddingsOptions); -]]> - - - -This sample shows how to call Embed. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -EmbeddingsOptions embeddingsOptions = null; -Response response = client.Embed(embeddingsOptions); -]]> -This sample shows how to call Embed. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -EmbeddingsOptions embeddingsOptions = null; -Response response = client.Embed(embeddingsOptions); -]]> - - - -This sample shows how to call EmbedAsync and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -using RequestContent content = RequestContent.Create(new object()); -Response response = await client.EmbedAsync(content); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("id").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); -Console.WriteLine(result.GetProperty("model").ToString()); -]]> -This sample shows how to call EmbedAsync and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -using RequestContent content = RequestContent.Create(new object()); -Response response = await client.EmbedAsync(content); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("id").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); -Console.WriteLine(result.GetProperty("model").ToString()); -]]> - - - -This sample shows how to call Embed and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -using RequestContent content = RequestContent.Create(new object()); -Response response = client.Embed(content); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("id").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); -Console.WriteLine(result.GetProperty("model").ToString()); -]]> -This sample shows how to call Embed and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -using RequestContent content = RequestContent.Create(new object()); -Response response = client.Embed(content); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("id").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); -Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); -Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); -Console.WriteLine(result.GetProperty("model").ToString()); -]]> - - - -This sample shows how to call GetModelInfoAsync. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = await client.GetModelInfoAsync(); -]]> -This sample shows how to call GetModelInfoAsync. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = await client.GetModelInfoAsync(); -]]> - - - -This sample shows how to call GetModelInfo. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = client.GetModelInfo(); -]]> -This sample shows how to call GetModelInfo. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = client.GetModelInfo(); -]]> - - - -This sample shows how to call GetModelInfoAsync and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = await client.GetModelInfoAsync(null); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("model_name").ToString()); -Console.WriteLine(result.GetProperty("model_type").ToString()); -Console.WriteLine(result.GetProperty("model_provider_name").ToString()); -]]> -This sample shows how to call GetModelInfoAsync and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = await client.GetModelInfoAsync(null); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("model_name").ToString()); -Console.WriteLine(result.GetProperty("model_type").ToString()); -Console.WriteLine(result.GetProperty("model_provider_name").ToString()); -]]> - - - -This sample shows how to call GetModelInfo and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = client.GetModelInfo(null); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("model_name").ToString()); -Console.WriteLine(result.GetProperty("model_type").ToString()); -Console.WriteLine(result.GetProperty("model_provider_name").ToString()); -]]> -This sample shows how to call GetModelInfo and parse the result. -"); -AzureKeyCredential credential = new AzureKeyCredential(""); -EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - -Response response = client.GetModelInfo(null); - -JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; -Console.WriteLine(result.GetProperty("model_name").ToString()); -Console.WriteLine(result.GetProperty("model_type").ToString()); -Console.WriteLine(result.GetProperty("model_provider_name").ToString()); -]]> - - - \ No newline at end of file diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingEncodingFormat.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingEncodingFormat.cs deleted file mode 100644 index 968dfae30f12e..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingEncodingFormat.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ComponentModel; - -namespace Azure.AI.Inference -{ - /// - /// The format of the embeddings result. - /// Returns a 422 error if the model doesn't support the value or parameter. - /// - public readonly partial struct EmbeddingEncodingFormat : IEquatable - { - private readonly string _value; - - /// Initializes a new instance of . - /// is null. - public EmbeddingEncodingFormat(string value) - { - _value = value ?? throw new ArgumentNullException(nameof(value)); - } - - private const string Base64Value = "base64"; - private const string BinaryValue = "binary"; - private const string SingleValue = "float"; - private const string SByteValue = "int8"; - private const string UbinaryValue = "ubinary"; - private const string ByteValue = "uint8"; - - /// Base64. - public static EmbeddingEncodingFormat Base64 { get; } = new EmbeddingEncodingFormat(Base64Value); - /// Binary. - public static EmbeddingEncodingFormat Binary { get; } = new EmbeddingEncodingFormat(BinaryValue); - /// ubinary. - public static EmbeddingEncodingFormat Ubinary { get; } = new EmbeddingEncodingFormat(UbinaryValue); - /// Determines if two values are the same. - public static bool operator ==(EmbeddingEncodingFormat left, EmbeddingEncodingFormat right) => left.Equals(right); - /// Determines if two values are not the same. - public static bool operator !=(EmbeddingEncodingFormat left, EmbeddingEncodingFormat right) => !left.Equals(right); - /// Converts a to a . - public static implicit operator EmbeddingEncodingFormat(string value) => new EmbeddingEncodingFormat(value); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => obj is EmbeddingEncodingFormat other && Equals(other); - /// - public bool Equals(EmbeddingEncodingFormat other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => _value != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(_value) : 0; - /// - public override string ToString() => _value; - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingInputType.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingInputType.cs deleted file mode 100644 index 9e75eba6dc3c7..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingInputType.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ComponentModel; - -namespace Azure.AI.Inference -{ - /// Represents the input types used for embedding search. - public readonly partial struct EmbeddingInputType : IEquatable - { - private readonly string _value; - - /// Initializes a new instance of . - /// is null. - public EmbeddingInputType(string value) - { - _value = value ?? throw new ArgumentNullException(nameof(value)); - } - - private const string TextValue = "text"; - private const string QueryValue = "query"; - private const string DocumentValue = "document"; - - /// Indicates the input is a general text input. - public static EmbeddingInputType Text { get; } = new EmbeddingInputType(TextValue); - /// Indicates the input represents a search query to find the most relevant documents in your vector database. - public static EmbeddingInputType Query { get; } = new EmbeddingInputType(QueryValue); - /// Indicates the input represents a document that is stored in a vector database. - public static EmbeddingInputType Document { get; } = new EmbeddingInputType(DocumentValue); - /// Determines if two values are the same. - public static bool operator ==(EmbeddingInputType left, EmbeddingInputType right) => left.Equals(right); - /// Determines if two values are not the same. - public static bool operator !=(EmbeddingInputType left, EmbeddingInputType right) => !left.Equals(right); - /// Converts a to a . - public static implicit operator EmbeddingInputType(string value) => new EmbeddingInputType(value); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override bool Equals(object obj) => obj is EmbeddingInputType other && Equals(other); - /// - public bool Equals(EmbeddingInputType other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public override int GetHashCode() => _value != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(_value) : 0; - /// - public override string ToString() => _value; - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.Serialization.cs deleted file mode 100644 index d0a9f2e6bdb0f..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.Serialization.cs +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Inference -{ - public partial class EmbeddingItem : IUtf8JsonSerializable, IJsonModel - { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - writer.WriteStartObject(); - JsonModelWriteCore(writer, options); - writer.WriteEndObject(); - } - - /// The JSON writer. - /// The client options for reading and writing models. - protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingItem)} does not support writing '{format}' format."); - } - - writer.WritePropertyName("embedding"u8); -#if NET6_0_OR_GREATER - writer.WriteRawValue(Embedding); -#else - using (JsonDocument document = JsonDocument.Parse(Embedding)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - writer.WritePropertyName("index"u8); - writer.WriteNumberValue(Index); - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - } - - EmbeddingItem IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingItem)} does not support reading '{format}' format."); - } - - using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeEmbeddingItem(document.RootElement, options); - } - - internal static EmbeddingItem DeserializeEmbeddingItem(JsonElement element, ModelReaderWriterOptions options = null) - { - options ??= ModelSerializationExtensions.WireOptions; - - if (element.ValueKind == JsonValueKind.Null) - { - return null; - } - BinaryData embedding = default; - int index = default; - IDictionary serializedAdditionalRawData = default; - Dictionary rawDataDictionary = new Dictionary(); - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("embedding"u8)) - { - embedding = BinaryData.FromString(property.Value.GetRawText()); - continue; - } - if (property.NameEquals("index"u8)) - { - index = property.Value.GetInt32(); - continue; - } - if (options.Format != "W") - { - rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); - } - } - serializedAdditionalRawData = rawDataDictionary; - return new EmbeddingItem(embedding, index, serializedAdditionalRawData); - } - - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - return ModelReaderWriter.Write(this, options); - default: - throw new FormatException($"The model {nameof(EmbeddingItem)} does not support writing '{options.Format}' format."); - } - } - - EmbeddingItem IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - { - using JsonDocument document = JsonDocument.Parse(data); - return DeserializeEmbeddingItem(document.RootElement, options); - } - default: - throw new FormatException($"The model {nameof(EmbeddingItem)} does not support reading '{options.Format}' format."); - } - } - - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; - - /// Deserializes the model from a raw response. - /// The response to deserialize the model from. - internal static EmbeddingItem FromResponse(Response response) - { - using var document = JsonDocument.Parse(response.Content); - return DeserializeEmbeddingItem(document.RootElement); - } - - /// Convert into a . - internal virtual RequestContent ToRequestContent() - { - var content = new Utf8JsonRequestContent(); - content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); - return content; - } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.cs deleted file mode 100644 index 676f0aba0bc7b..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingItem.cs +++ /dev/null @@ -1,128 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; - -namespace Azure.AI.Inference -{ - /// Representation of a single embeddings relatedness comparison. - public partial class EmbeddingItem - { - /// - /// Keeps track of any properties unknown to the library. - /// - /// To assign an object to the value of this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - private IDictionary _serializedAdditionalRawData; - - /// Initializes a new instance of . - /// - /// List of embedding values for the input prompt. These represent a measurement of the - /// vector-based relatedness of the provided input. Or a base64 encoded string of the embedding vector. - /// - /// Index of the prompt to which the EmbeddingItem corresponds. - /// is null. - internal EmbeddingItem(BinaryData embedding, int index) - { - Argument.AssertNotNull(embedding, nameof(embedding)); - - Embedding = embedding; - Index = index; - } - - /// Initializes a new instance of . - /// - /// List of embedding values for the input prompt. These represent a measurement of the - /// vector-based relatedness of the provided input. Or a base64 encoded string of the embedding vector. - /// - /// Index of the prompt to which the EmbeddingItem corresponds. - /// Keeps track of any properties unknown to the library. - internal EmbeddingItem(BinaryData embedding, int index, IDictionary serializedAdditionalRawData) - { - Embedding = embedding; - Index = index; - _serializedAdditionalRawData = serializedAdditionalRawData; - } - - /// Initializes a new instance of for deserialization. - internal EmbeddingItem() - { - } - - /// - /// List of embedding values for the input prompt. These represent a measurement of the - /// vector-based relatedness of the provided input. Or a base64 encoded string of the embedding vector. - /// - /// To assign an object to this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// - /// Supported types: - /// - /// - /// - /// - /// - /// where T is of type - /// - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - public BinaryData Embedding { get; } - /// Index of the prompt to which the EmbeddingItem corresponds. - public int Index { get; } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsClient.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsClient.cs deleted file mode 100644 index 1d747f7cedada..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsClient.cs +++ /dev/null @@ -1,229 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Azure.Core; -using Azure.Core.Pipeline; - -namespace Azure.AI.Inference -{ - // Data plane generated client. - /// The Embeddings service client. - public partial class EmbeddingsClient - { - private const string AuthorizationHeader = "Authorization"; - private readonly AzureKeyCredential _keyCredential; - private const string AuthorizationApiKeyPrefix = "Bearer"; - private static readonly string[] AuthorizationScopes = new string[] { "https://ml.azure.com/.default" }; - private readonly TokenCredential _tokenCredential; - private readonly HttpPipeline _pipeline; - private readonly Uri _endpoint; - private readonly string _apiVersion; - - /// The ClientDiagnostics is used to provide tracing support for the client library. - internal ClientDiagnostics ClientDiagnostics { get; } - - /// The HTTP pipeline for sending and receiving REST requests and responses. - public virtual HttpPipeline Pipeline => _pipeline; - - /// Initializes a new instance of EmbeddingsClient for mocking. - protected EmbeddingsClient() - { - } - - /// Initializes a new instance of EmbeddingsClient. - /// Service host. - /// A credential used to authenticate to an Azure Service. - /// or is null. - public EmbeddingsClient(Uri endpoint, AzureKeyCredential credential) : this(endpoint, credential, new AzureAIInferenceClientOptions()) - { - } - - /// Initializes a new instance of EmbeddingsClient. - /// Service host. - /// A credential used to authenticate to an Azure Service. - /// or is null. - public EmbeddingsClient(Uri endpoint, TokenCredential credential) : this(endpoint, credential, new AzureAIInferenceClientOptions()) - { - } - - /// Initializes a new instance of EmbeddingsClient. - /// Service host. - /// A credential used to authenticate to an Azure Service. - /// The options for configuring the client. - /// or is null. - public EmbeddingsClient(Uri endpoint, TokenCredential credential, AzureAIInferenceClientOptions options) - { - Argument.AssertNotNull(endpoint, nameof(endpoint)); - Argument.AssertNotNull(credential, nameof(credential)); - options ??= new AzureAIInferenceClientOptions(); - - ClientDiagnostics = new ClientDiagnostics(options, true); - _tokenCredential = credential; - _pipeline = HttpPipelineBuilder.Build(options, Array.Empty(), new HttpPipelinePolicy[] { new BearerTokenAuthenticationPolicy(_tokenCredential, AuthorizationScopes) }, new ResponseClassifier()); - _endpoint = endpoint; - _apiVersion = options.Version; - } - - /// - /// Returns information about the AI model. - /// The method makes a REST API call to the `/info` route on the given endpoint. - /// This method will only work when using Serverless API or Managed Compute endpoint. - /// It will not work for GitHub Models endpoint or Azure OpenAI endpoint. - /// - /// The cancellation token to use. - /// - public virtual async Task> GetModelInfoAsync(CancellationToken cancellationToken = default) - { - RequestContext context = FromCancellationToken(cancellationToken); - Response response = await GetModelInfoAsync(context).ConfigureAwait(false); - return Response.FromValue(ModelInfo.FromResponse(response), response); - } - - /// - /// Returns information about the AI model. - /// The method makes a REST API call to the `/info` route on the given endpoint. - /// This method will only work when using Serverless API or Managed Compute endpoint. - /// It will not work for GitHub Models endpoint or Azure OpenAI endpoint. - /// - /// The cancellation token to use. - /// - public virtual Response GetModelInfo(CancellationToken cancellationToken = default) - { - RequestContext context = FromCancellationToken(cancellationToken); - Response response = GetModelInfo(context); - return Response.FromValue(ModelInfo.FromResponse(response), response); - } - - /// - /// [Protocol Method] Returns information about the AI model. - /// The method makes a REST API call to the `/info` route on the given endpoint. - /// This method will only work when using Serverless API or Managed Compute endpoint. - /// It will not work for GitHub Models endpoint or Azure OpenAI endpoint. - /// - /// - /// - /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. - /// - /// - /// - /// - /// Please try the simpler convenience overload with strongly typed models first. - /// - /// - /// - /// - /// The request context, which can override default behaviors of the client pipeline on a per-call basis. - /// Service returned a non-success status code. - /// The response returned from the service. - /// - public virtual async Task GetModelInfoAsync(RequestContext context) - { - using var scope = ClientDiagnostics.CreateScope("EmbeddingsClient.GetModelInfo"); - scope.Start(); - try - { - using HttpMessage message = CreateGetModelInfoRequest(context); - return await _pipeline.ProcessMessageAsync(message, context).ConfigureAwait(false); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - /// - /// [Protocol Method] Returns information about the AI model. - /// The method makes a REST API call to the `/info` route on the given endpoint. - /// This method will only work when using Serverless API or Managed Compute endpoint. - /// It will not work for GitHub Models endpoint or Azure OpenAI endpoint. - /// - /// - /// - /// This protocol method allows explicit creation of the request and processing of the response for advanced scenarios. - /// - /// - /// - /// - /// Please try the simpler convenience overload with strongly typed models first. - /// - /// - /// - /// - /// The request context, which can override default behaviors of the client pipeline on a per-call basis. - /// Service returned a non-success status code. - /// The response returned from the service. - /// - public virtual Response GetModelInfo(RequestContext context) - { - using var scope = ClientDiagnostics.CreateScope("EmbeddingsClient.GetModelInfo"); - scope.Start(); - try - { - using HttpMessage message = CreateGetModelInfoRequest(context); - return _pipeline.ProcessMessage(message, context); - } - catch (Exception e) - { - scope.Failed(e); - throw; - } - } - - internal HttpMessage CreateEmbedRequest(RequestContent content, string extraParams, RequestContext context) - { - var message = _pipeline.CreateMessage(context, ResponseClassifier200); - var request = message.Request; - request.Method = RequestMethod.Post; - var uri = new RawRequestUriBuilder(); - uri.Reset(_endpoint); - uri.AppendPath("/embeddings", false); - uri.AppendQuery("api-version", _apiVersion, true); - request.Uri = uri; - request.Headers.Add("Accept", "application/json"); - if (extraParams != null) - { - request.Headers.Add("extra-parameters", extraParams); - } - request.Headers.Add("Content-Type", "application/json"); - request.Content = content; - return message; - } - - internal HttpMessage CreateGetModelInfoRequest(RequestContext context) - { - var message = _pipeline.CreateMessage(context, ResponseClassifier200); - var request = message.Request; - request.Method = RequestMethod.Get; - var uri = new RawRequestUriBuilder(); - uri.Reset(_endpoint); - uri.AppendPath("/info", false); - uri.AppendQuery("api-version", _apiVersion, true); - request.Uri = uri; - request.Headers.Add("Accept", "application/json"); - return message; - } - - private static RequestContext DefaultRequestContext = new RequestContext(); - internal static RequestContext FromCancellationToken(CancellationToken cancellationToken = default) - { - if (!cancellationToken.CanBeCanceled) - { - return DefaultRequestContext; - } - - return new RequestContext() { CancellationToken = cancellationToken }; - } - - private static ResponseClassifier _responseClassifier200; - private static ResponseClassifier ResponseClassifier200 => _responseClassifier200 ??= new StatusCodeClassifier(stackalloc ushort[] { 200 }); - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.Serialization.cs deleted file mode 100644 index 5627de6a0da87..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.Serialization.cs +++ /dev/null @@ -1,208 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Inference -{ - public partial class EmbeddingsOptions : IUtf8JsonSerializable, IJsonModel - { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - writer.WriteStartObject(); - JsonModelWriteCore(writer, options); - writer.WriteEndObject(); - } - - /// The JSON writer. - /// The client options for reading and writing models. - protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingsOptions)} does not support writing '{format}' format."); - } - - writer.WritePropertyName("input"u8); - writer.WriteStartArray(); - foreach (var item in Input) - { - writer.WriteStringValue(item); - } - writer.WriteEndArray(); - if (Optional.IsDefined(Dimensions)) - { - writer.WritePropertyName("dimensions"u8); - writer.WriteNumberValue(Dimensions.Value); - } - if (Optional.IsDefined(EncodingFormat)) - { - writer.WritePropertyName("encoding_format"u8); - writer.WriteStringValue(EncodingFormat.Value.ToString()); - } - if (Optional.IsDefined(InputType)) - { - writer.WritePropertyName("input_type"u8); - writer.WriteStringValue(InputType.Value.ToString()); - } - if (Optional.IsDefined(Model)) - { - writer.WritePropertyName("model"u8); - writer.WriteStringValue(Model); - } - foreach (var item in AdditionalProperties) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - - EmbeddingsOptions IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingsOptions)} does not support reading '{format}' format."); - } - - using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeEmbeddingsOptions(document.RootElement, options); - } - - internal static EmbeddingsOptions DeserializeEmbeddingsOptions(JsonElement element, ModelReaderWriterOptions options = null) - { - options ??= ModelSerializationExtensions.WireOptions; - - if (element.ValueKind == JsonValueKind.Null) - { - return null; - } - IList input = default; - int? dimensions = default; - EmbeddingEncodingFormat? encodingFormat = default; - EmbeddingInputType? inputType = default; - string model = default; - IDictionary additionalProperties = default; - Dictionary additionalPropertiesDictionary = new Dictionary(); - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("input"u8)) - { - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(item.GetString()); - } - input = array; - continue; - } - if (property.NameEquals("dimensions"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - dimensions = property.Value.GetInt32(); - continue; - } - if (property.NameEquals("encoding_format"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - encodingFormat = new EmbeddingEncodingFormat(property.Value.GetString()); - continue; - } - if (property.NameEquals("input_type"u8)) - { - if (property.Value.ValueKind == JsonValueKind.Null) - { - continue; - } - inputType = new EmbeddingInputType(property.Value.GetString()); - continue; - } - if (property.NameEquals("model"u8)) - { - model = property.Value.GetString(); - continue; - } - additionalPropertiesDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); - } - additionalProperties = additionalPropertiesDictionary; - return new EmbeddingsOptions( - input, - dimensions, - encodingFormat, - inputType, - model, - additionalProperties); - } - - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - return ModelReaderWriter.Write(this, options); - default: - throw new FormatException($"The model {nameof(EmbeddingsOptions)} does not support writing '{options.Format}' format."); - } - } - - EmbeddingsOptions IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - { - using JsonDocument document = JsonDocument.Parse(data); - return DeserializeEmbeddingsOptions(document.RootElement, options); - } - default: - throw new FormatException($"The model {nameof(EmbeddingsOptions)} does not support reading '{options.Format}' format."); - } - } - - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; - - /// Deserializes the model from a raw response. - /// The response to deserialize the model from. - internal static EmbeddingsOptions FromResponse(Response response) - { - using var document = JsonDocument.Parse(response.Content); - return DeserializeEmbeddingsOptions(document.RootElement); - } - - /// Convert into a . - internal virtual RequestContent ToRequestContent() - { - var content = new Utf8JsonRequestContent(); - content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); - return content; - } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.cs deleted file mode 100644 index 879babca66046..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsOptions.cs +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Azure.AI.Inference -{ - /// The configuration information for an embeddings request. - public partial class EmbeddingsOptions - { - /// Initializes a new instance of . - /// - /// Input text to embed, encoded as a string or array of tokens. - /// To embed multiple inputs in a single request, pass an array - /// of strings or array of token arrays. - /// - /// is null. - public EmbeddingsOptions(IEnumerable input) - { - Argument.AssertNotNull(input, nameof(input)); - - Input = input.ToList(); - AdditionalProperties = new ChangeTrackingDictionary(); - } - - /// Initializes a new instance of . - /// - /// Input text to embed, encoded as a string or array of tokens. - /// To embed multiple inputs in a single request, pass an array - /// of strings or array of token arrays. - /// - /// - /// Optional. The number of dimensions the resulting output embeddings should have. - /// Passing null causes the model to use its default value. - /// Returns a 422 error if the model doesn't support the value or parameter. - /// - /// Optional. The desired format for the returned embeddings. - /// - /// Optional. The type of the input. - /// Returns a 422 error if the model doesn't support the value or parameter. - /// - /// ID of the specific AI model to use, if more than one model is available on the endpoint. - /// Additional Properties. - internal EmbeddingsOptions(IList input, int? dimensions, EmbeddingEncodingFormat? encodingFormat, EmbeddingInputType? inputType, string model, IDictionary additionalProperties) - { - Input = input; - Dimensions = dimensions; - EncodingFormat = encodingFormat; - InputType = inputType; - Model = model; - AdditionalProperties = additionalProperties; - } - - /// Initializes a new instance of for deserialization. - internal EmbeddingsOptions() - { - } - - /// - /// Input text to embed, encoded as a string or array of tokens. - /// To embed multiple inputs in a single request, pass an array - /// of strings or array of token arrays. - /// - public IList Input { get; } - /// - /// Optional. The number of dimensions the resulting output embeddings should have. - /// Passing null causes the model to use its default value. - /// Returns a 422 error if the model doesn't support the value or parameter. - /// - public int? Dimensions { get; set; } - /// Optional. The desired format for the returned embeddings. - public EmbeddingEncodingFormat? EncodingFormat { get; set; } - /// - /// Optional. The type of the input. - /// Returns a 422 error if the model doesn't support the value or parameter. - /// - public EmbeddingInputType? InputType { get; set; } - /// ID of the specific AI model to use, if more than one model is available on the endpoint. - public string Model { get; set; } - /// - /// Additional Properties - /// - /// To assign an object to the value of this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - public IDictionary AdditionalProperties { get; } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.Serialization.cs deleted file mode 100644 index c7ee31c5d7f11..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.Serialization.cs +++ /dev/null @@ -1,176 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using Azure.Core; - -namespace Azure.AI.Inference -{ - public partial class EmbeddingsResult : IUtf8JsonSerializable, IJsonModel - { - void IUtf8JsonSerializable.Write(Utf8JsonWriter writer) => ((IJsonModel)this).Write(writer, ModelSerializationExtensions.WireOptions); - - void IJsonModel.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - writer.WriteStartObject(); - JsonModelWriteCore(writer, options); - writer.WriteEndObject(); - } - - /// The JSON writer. - /// The client options for reading and writing models. - protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingsResult)} does not support writing '{format}' format."); - } - - writer.WritePropertyName("id"u8); - writer.WriteStringValue(Id); - writer.WritePropertyName("data"u8); - writer.WriteStartArray(); - foreach (var item in Data) - { - writer.WriteObjectValue(item, options); - } - writer.WriteEndArray(); - writer.WritePropertyName("usage"u8); - writer.WriteObjectValue(Usage, options); - writer.WritePropertyName("model"u8); - writer.WriteStringValue(Model); - if (options.Format != "W" && _serializedAdditionalRawData != null) - { - foreach (var item in _serializedAdditionalRawData) - { - writer.WritePropertyName(item.Key); -#if NET6_0_OR_GREATER - writer.WriteRawValue(item.Value); -#else - using (JsonDocument document = JsonDocument.Parse(item.Value)) - { - JsonSerializer.Serialize(writer, document.RootElement); - } -#endif - } - } - } - - EmbeddingsResult IJsonModel.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - if (format != "J") - { - throw new FormatException($"The model {nameof(EmbeddingsResult)} does not support reading '{format}' format."); - } - - using JsonDocument document = JsonDocument.ParseValue(ref reader); - return DeserializeEmbeddingsResult(document.RootElement, options); - } - - internal static EmbeddingsResult DeserializeEmbeddingsResult(JsonElement element, ModelReaderWriterOptions options = null) - { - options ??= ModelSerializationExtensions.WireOptions; - - if (element.ValueKind == JsonValueKind.Null) - { - return null; - } - string id = default; - IReadOnlyList data = default; - EmbeddingsUsage usage = default; - string model = default; - IDictionary serializedAdditionalRawData = default; - Dictionary rawDataDictionary = new Dictionary(); - foreach (var property in element.EnumerateObject()) - { - if (property.NameEquals("id"u8)) - { - id = property.Value.GetString(); - continue; - } - if (property.NameEquals("data"u8)) - { - List array = new List(); - foreach (var item in property.Value.EnumerateArray()) - { - array.Add(EmbeddingItem.DeserializeEmbeddingItem(item, options)); - } - data = array; - continue; - } - if (property.NameEquals("usage"u8)) - { - usage = EmbeddingsUsage.DeserializeEmbeddingsUsage(property.Value, options); - continue; - } - if (property.NameEquals("model"u8)) - { - model = property.Value.GetString(); - continue; - } - if (options.Format != "W") - { - rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); - } - } - serializedAdditionalRawData = rawDataDictionary; - return new EmbeddingsResult(id, data, usage, model, serializedAdditionalRawData); - } - - BinaryData IPersistableModel.Write(ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - return ModelReaderWriter.Write(this, options); - default: - throw new FormatException($"The model {nameof(EmbeddingsResult)} does not support writing '{options.Format}' format."); - } - } - - EmbeddingsResult IPersistableModel.Create(BinaryData data, ModelReaderWriterOptions options) - { - var format = options.Format == "W" ? ((IPersistableModel)this).GetFormatFromOptions(options) : options.Format; - - switch (format) - { - case "J": - { - using JsonDocument document = JsonDocument.Parse(data); - return DeserializeEmbeddingsResult(document.RootElement, options); - } - default: - throw new FormatException($"The model {nameof(EmbeddingsResult)} does not support reading '{options.Format}' format."); - } - } - - string IPersistableModel.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; - - /// Deserializes the model from a raw response. - /// The response to deserialize the model from. - internal static EmbeddingsResult FromResponse(Response response) - { - using var document = JsonDocument.Parse(response.Content); - return DeserializeEmbeddingsResult(document.RootElement); - } - - /// Convert into a . - internal virtual RequestContent ToRequestContent() - { - var content = new Utf8JsonRequestContent(); - content.JsonWriter.WriteObjectValue(this, ModelSerializationExtensions.WireOptions); - return content; - } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.cs deleted file mode 100644 index c44b8e64397b8..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsResult.cs +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Azure.AI.Inference -{ - /// - /// Representation of the response data from an embeddings request. - /// Embeddings measure the relatedness of text strings and are commonly used for search, clustering, - /// recommendations, and other similar scenarios. - /// - public partial class EmbeddingsResult - { - /// - /// Keeps track of any properties unknown to the library. - /// - /// To assign an object to the value of this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - private IDictionary _serializedAdditionalRawData; - - /// Initializes a new instance of . - /// Unique identifier for the embeddings result. - /// Embedding values for the prompts submitted in the request. - /// Usage counts for tokens input using the embeddings API. - /// The model ID used to generate this result. - /// , , or is null. - internal EmbeddingsResult(string id, IEnumerable data, EmbeddingsUsage usage, string model) - { - Argument.AssertNotNull(id, nameof(id)); - Argument.AssertNotNull(data, nameof(data)); - Argument.AssertNotNull(usage, nameof(usage)); - Argument.AssertNotNull(model, nameof(model)); - - Id = id; - Data = data.ToList(); - Usage = usage; - Model = model; - } - - /// Initializes a new instance of . - /// Unique identifier for the embeddings result. - /// Embedding values for the prompts submitted in the request. - /// Usage counts for tokens input using the embeddings API. - /// The model ID used to generate this result. - /// Keeps track of any properties unknown to the library. - internal EmbeddingsResult(string id, IReadOnlyList data, EmbeddingsUsage usage, string model, IDictionary serializedAdditionalRawData) - { - Id = id; - Data = data; - Usage = usage; - Model = model; - _serializedAdditionalRawData = serializedAdditionalRawData; - } - - /// Initializes a new instance of for deserialization. - internal EmbeddingsResult() - { - } - - /// Unique identifier for the embeddings result. - public string Id { get; } - /// Embedding values for the prompts submitted in the request. - public IReadOnlyList Data { get; } - /// Usage counts for tokens input using the embeddings API. - public EmbeddingsUsage Usage { get; } - /// The model ID used to generate this result. - public string Model { get; } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.cs b/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.cs deleted file mode 100644 index f0808a0740122..0000000000000 --- a/sdk/ai/Azure.AI.Inference/src/Generated/EmbeddingsUsage.cs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Collections.Generic; - -namespace Azure.AI.Inference -{ - /// Measurement of the amount of tokens used in this request and response. - public partial class EmbeddingsUsage - { - /// - /// Keeps track of any properties unknown to the library. - /// - /// To assign an object to the value of this property use . - /// - /// - /// To assign an already formatted json string to this property use . - /// - /// - /// Examples: - /// - /// - /// BinaryData.FromObjectAsJson("foo") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromString("\"foo\"") - /// Creates a payload of "foo". - /// - /// - /// BinaryData.FromObjectAsJson(new { key = "value" }) - /// Creates a payload of { "key": "value" }. - /// - /// - /// BinaryData.FromString("{\"key\": \"value\"}") - /// Creates a payload of { "key": "value" }. - /// - /// - /// - /// - private IDictionary _serializedAdditionalRawData; - - /// Initializes a new instance of . - /// Number of tokens in the request. - /// - /// Total number of tokens transacted in this request/response. Should equal the - /// number of tokens in the request. - /// - internal EmbeddingsUsage(int promptTokens, int totalTokens) - { - PromptTokens = promptTokens; - TotalTokens = totalTokens; - } - - /// Initializes a new instance of . - /// Number of tokens in the request. - /// - /// Total number of tokens transacted in this request/response. Should equal the - /// number of tokens in the request. - /// - /// Keeps track of any properties unknown to the library. - internal EmbeddingsUsage(int promptTokens, int totalTokens, IDictionary serializedAdditionalRawData) - { - PromptTokens = promptTokens; - TotalTokens = totalTokens; - _serializedAdditionalRawData = serializedAdditionalRawData; - } - - /// Initializes a new instance of for deserialization. - internal EmbeddingsUsage() - { - } - - /// Number of tokens in the request. - public int PromptTokens { get; } - /// - /// Total number of tokens transacted in this request/response. Should equal the - /// number of tokens in the request. - /// - public int TotalTokens { get; } - } -} diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ExtraParameters.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ExtraParameters.cs index 9ffda5f51e8a6..f049bc41d649e 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ExtraParameters.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ExtraParameters.cs @@ -11,7 +11,7 @@ namespace Azure.AI.Inference { /// Controls what happens if extra parameters, undefined by the REST API, are passed in the JSON request payload. - public readonly partial struct ExtraParameters : IEquatable + internal readonly partial struct ExtraParameters : IEquatable { private readonly string _value; diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/ModelType.cs b/sdk/ai/Azure.AI.Inference/src/Generated/ModelType.cs index 960fae6ae43b2..c9a683ad75628 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/ModelType.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/ModelType.cs @@ -27,20 +27,20 @@ public ModelType(string value) private const string TextGenerationValue = "text_generation"; private const string ImageEmbeddingsValue = "image_embeddings"; private const string AudioGenerationValue = "audio_generation"; - private const string ChatValue = "chat"; + private const string ChatCompletionValue = "chat_completion"; - /// Embeddings. + /// A model capable of generating embeddings from a text. public static ModelType Embeddings { get; } = new ModelType(EmbeddingsValue); - /// Image generation. + /// A model capable of generating images from an image and text description. public static ModelType ImageGeneration { get; } = new ModelType(ImageGenerationValue); - /// Text generation. + /// A text generation model. public static ModelType TextGeneration { get; } = new ModelType(TextGenerationValue); - /// Image embeddings. + /// A model capable of generating embeddings from an image. public static ModelType ImageEmbeddings { get; } = new ModelType(ImageEmbeddingsValue); - /// Audio generation. + /// A text-to-audio generative model. public static ModelType AudioGeneration { get; } = new ModelType(AudioGenerationValue); - /// Chat completions. - public static ModelType Chat { get; } = new ModelType(ChatValue); + /// A model capable of taking chat-formatted messages and generate responses. + public static ModelType ChatCompletion { get; } = new ModelType(ChatCompletionValue); /// Determines if two values are the same. public static bool operator ==(ModelType left, ModelType right) => left.Equals(right); /// Determines if two values are not the same. diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.Serialization.cs b/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.Serialization.cs index 6a0c2f39f3126..fdd8543b88eb2 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.Serialization.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.Serialization.cs @@ -40,8 +40,6 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit writer.WriteNumberValue(Created, "U"); writer.WritePropertyName("model"u8); writer.WriteStringValue(Model); - writer.WritePropertyName("usage"u8); - writer.WriteObjectValue(Usage, options); writer.WritePropertyName("choices"u8); writer.WriteStartArray(); foreach (var item in Choices) @@ -49,6 +47,11 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit writer.WriteObjectValue(item, options); } writer.WriteEndArray(); + if (Optional.IsDefined(Usage)) + { + writer.WritePropertyName("usage"u8); + writer.WriteObjectValue(Usage, options); + } if (options.Format != "W" && _serializedAdditionalRawData != null) { foreach (var item in _serializedAdditionalRawData) @@ -89,8 +92,8 @@ internal static StreamingChatCompletionsUpdate DeserializeStreamingChatCompletio string id = default; DateTimeOffset created = default; string model = default; - CompletionsUsage usage = default; IReadOnlyList choices = default; + CompletionsUsage usage = default; IDictionary serializedAdditionalRawData = default; Dictionary rawDataDictionary = new Dictionary(); foreach (var property in element.EnumerateObject()) @@ -110,11 +113,6 @@ internal static StreamingChatCompletionsUpdate DeserializeStreamingChatCompletio model = property.Value.GetString(); continue; } - if (property.NameEquals("usage"u8)) - { - usage = CompletionsUsage.DeserializeCompletionsUsage(property.Value, options); - continue; - } if (property.NameEquals("choices"u8)) { List array = new List(); @@ -125,6 +123,15 @@ internal static StreamingChatCompletionsUpdate DeserializeStreamingChatCompletio choices = array; continue; } + if (property.NameEquals("usage"u8)) + { + if (property.Value.ValueKind == JsonValueKind.Null) + { + continue; + } + usage = CompletionsUsage.DeserializeCompletionsUsage(property.Value, options); + continue; + } if (options.Format != "W") { rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); @@ -135,8 +142,8 @@ internal static StreamingChatCompletionsUpdate DeserializeStreamingChatCompletio id, created, model, - usage, choices, + usage, serializedAdditionalRawData); } diff --git a/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.cs b/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.cs index de1a266e5616a..6cdc90bb00368 100644 --- a/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.cs +++ b/sdk/ai/Azure.AI.Inference/src/Generated/StreamingChatCompletionsUpdate.cs @@ -58,24 +58,21 @@ public partial class StreamingChatCompletionsUpdate /// represented as seconds since the beginning of the Unix epoch of 00:00 on 1 Jan 1970. /// /// The model used for the chat completion. - /// Usage information for tokens processed and generated as part of this completions operation. /// /// An update to the collection of completion choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// - /// , , or is null. - internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, string model, CompletionsUsage usage, IEnumerable choices) + /// , or is null. + internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, string model, IEnumerable choices) { Argument.AssertNotNull(id, nameof(id)); Argument.AssertNotNull(model, nameof(model)); - Argument.AssertNotNull(usage, nameof(usage)); Argument.AssertNotNull(choices, nameof(choices)); Id = id; Created = created; Model = model; - Usage = usage; Choices = choices.ToList(); } @@ -86,20 +83,20 @@ internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, strin /// represented as seconds since the beginning of the Unix epoch of 00:00 on 1 Jan 1970. /// /// The model used for the chat completion. - /// Usage information for tokens processed and generated as part of this completions operation. /// /// An update to the collection of completion choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// + /// Usage information for tokens processed and generated as part of this completions operation. /// Keeps track of any properties unknown to the library. - internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, string model, CompletionsUsage usage, IReadOnlyList choices, IDictionary serializedAdditionalRawData) + internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, string model, IReadOnlyList choices, CompletionsUsage usage, IDictionary serializedAdditionalRawData) { Id = id; Created = created; Model = model; - Usage = usage; Choices = choices; + Usage = usage; _serializedAdditionalRawData = serializedAdditionalRawData; } @@ -107,13 +104,13 @@ internal StreamingChatCompletionsUpdate(string id, DateTimeOffset created, strin internal StreamingChatCompletionsUpdate() { } - /// Usage information for tokens processed and generated as part of this completions operation. - public CompletionsUsage Usage { get; } /// /// An update to the collection of completion choices associated with this completions response. /// Generally, `n` choices are generated per provided prompt with a default value of 1. /// Token limits and other settings may limit the number of choices generated. /// public IReadOnlyList Choices { get; } + /// Usage information for tokens processed and generated as part of this completions operation. + public CompletionsUsage Usage { get; } } } diff --git a/sdk/ai/Azure.AI.Inference/tests/Generated/Samples/Samples_EmbeddingsClient.cs b/sdk/ai/Azure.AI.Inference/tests/Generated/Samples/Samples_EmbeddingsClient.cs deleted file mode 100644 index 1244fa0425405..0000000000000 --- a/sdk/ai/Azure.AI.Inference/tests/Generated/Samples/Samples_EmbeddingsClient.cs +++ /dev/null @@ -1,255 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// - -#nullable disable - -using System; -using System.Text.Json; -using System.Threading.Tasks; -using Azure.Core; -using Azure.Identity; -using NUnit.Framework; - -namespace Azure.AI.Inference.Samples -{ - public partial class Samples_EmbeddingsClient - { - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_Embed_MaximumSetEmbeddings() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - using RequestContent content = RequestContent.Create(new object()); - Response response = client.Embed(content); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("id").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); - Console.WriteLine(result.GetProperty("model").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_Embed_MaximumSetEmbeddings_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - using RequestContent content = RequestContent.Create(new object()); - Response response = await client.EmbedAsync(content); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("id").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); - Console.WriteLine(result.GetProperty("model").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_Embed_MaximumSetEmbeddings_Convenience() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - EmbeddingsOptions embeddingsOptions = null; - Response response = client.Embed(embeddingsOptions); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_Embed_MaximumSetEmbeddings_Convenience_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - EmbeddingsOptions embeddingsOptions = null; - Response response = await client.EmbedAsync(embeddingsOptions); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_Embed_MinimumSetEmbeddings() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - using RequestContent content = RequestContent.Create(new object()); - Response response = client.Embed(content); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("id").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); - Console.WriteLine(result.GetProperty("model").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_Embed_MinimumSetEmbeddings_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - using RequestContent content = RequestContent.Create(new object()); - Response response = await client.EmbedAsync(content); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("id").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("embedding").ToString()); - Console.WriteLine(result.GetProperty("data")[0].GetProperty("index").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("prompt_tokens").ToString()); - Console.WriteLine(result.GetProperty("usage").GetProperty("total_tokens").ToString()); - Console.WriteLine(result.GetProperty("model").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_Embed_MinimumSetEmbeddings_Convenience() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - EmbeddingsOptions embeddingsOptions = null; - Response response = client.Embed(embeddingsOptions); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_Embed_MinimumSetEmbeddings_Convenience_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - EmbeddingsOptions embeddingsOptions = null; - Response response = await client.EmbedAsync(embeddingsOptions); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_GetModelInfo_MaximumSetModelInformation() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = client.GetModelInfo(null); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("model_name").ToString()); - Console.WriteLine(result.GetProperty("model_type").ToString()); - Console.WriteLine(result.GetProperty("model_provider_name").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_GetModelInfo_MaximumSetModelInformation_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = await client.GetModelInfoAsync(null); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("model_name").ToString()); - Console.WriteLine(result.GetProperty("model_type").ToString()); - Console.WriteLine(result.GetProperty("model_provider_name").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_GetModelInfo_MaximumSetModelInformation_Convenience() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = client.GetModelInfo(); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_GetModelInfo_MaximumSetModelInformation_Convenience_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = await client.GetModelInfoAsync(); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_GetModelInfo_MinimumSetModelInformation() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = client.GetModelInfo(null); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("model_name").ToString()); - Console.WriteLine(result.GetProperty("model_type").ToString()); - Console.WriteLine(result.GetProperty("model_provider_name").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_GetModelInfo_MinimumSetModelInformation_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = await client.GetModelInfoAsync(null); - - JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement; - Console.WriteLine(result.GetProperty("model_name").ToString()); - Console.WriteLine(result.GetProperty("model_type").ToString()); - Console.WriteLine(result.GetProperty("model_provider_name").ToString()); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public void Example_Client2_GetModelInfo_MinimumSetModelInformation_Convenience() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = client.GetModelInfo(); - } - - [Test] - [Ignore("Only validating compilation of examples")] - public async Task Example_Client2_GetModelInfo_MinimumSetModelInformation_Convenience_Async() - { - Uri endpoint = new Uri(""); - AzureKeyCredential credential = new AzureKeyCredential(""); - EmbeddingsClient client = new EmbeddingsClient(endpoint, credential); - - Response response = await client.GetModelInfoAsync(); - } - } -} diff --git a/sdk/ai/Azure.AI.Inference/tsp-location.yaml b/sdk/ai/Azure.AI.Inference/tsp-location.yaml index a656912858521..2a6c4e479e5a7 100644 --- a/sdk/ai/Azure.AI.Inference/tsp-location.yaml +++ b/sdk/ai/Azure.AI.Inference/tsp-location.yaml @@ -1,3 +1,4 @@ directory: specification/ai/ModelClient -repo: trangevi/azure-rest-api-specs -commit: aa97e50e44d2c620c92e5a4c791bb387a741d64c +commit: cca025fa6ce483a4878daf0dd3082c97fd29c83b +repo: Azure/azure-rest-api-specs +additionalDirectories: