Skip to content

Commit d256f6b

Browse files
authored
Remove non-AsXx surface area from M.E.AI.OpenAI/AzureAIInference (#6138)
We expect the AsChatClient/AsEmbeddingGenerator extension methods from M.E.AI.OpenAI and M.E.AI.AzureAIInference to move into the OpenAI / Azure.AI.Inference libraries, respectively. To prepare for that, and the temporary M.E.AI.OpenAI/AzureAIInference libs then being deprecated, this PR removes the other surface area from these assemblies. For anything folks found useful, we should find another way to ship it, likely just as sample source somewhere.
1 parent 799a1db commit d256f6b

File tree

51 files changed

+875
-3251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+875
-3251
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatClientMetadata.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public ChatClientMetadata(string? providerName = null, Uri? providerUri = null,
3434

3535
/// <summary>Gets the ID of the default model used by this chat client.</summary>
3636
/// <remarks>
37-
/// This value can be null if no default model is set on the corresponding <see cref="IChatClient"/>.
37+
/// This value can be <see langword="null"/> if no default model is set on the corresponding <see cref="IChatClient"/>.
3838
/// An individual request may override this value via <see cref="ChatOptions.ModelId"/>.
3939
/// </remarks>
4040
public string? DefaultModelId { get; }

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatFinishReason.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace Microsoft.Extensions.AI;
1414
[JsonConverter(typeof(Converter))]
1515
public readonly struct ChatFinishReason : IEquatable<ChatFinishReason>
1616
{
17-
/// <summary>The finish reason value. If null because `default(ChatFinishReason)` was used, the instance will behave like <see cref="Stop"/>.</summary>
17+
/// <summary>The finish reason value. If <see langword="null"/> because `default(ChatFinishReason)` was used, the instance will behave like <see cref="Stop"/>.</summary>
1818
private readonly string? _value;
1919

2020
/// <summary>Initializes a new instance of the <see cref="ChatFinishReason"/> struct with a string that describes the reason.</summary>
2121
/// <param name="value">The reason value.</param>
22-
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
22+
/// <exception cref="ArgumentNullException"><paramref name="value"/> is <see langword="null"/>.</exception>
2323
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or composed entirely of whitespace.</exception>
2424
[JsonConstructor]
2525
public ChatFinishReason(string value)

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ChatOptions
6262
/// Gets or sets the response format for the chat request.
6363
/// </summary>
6464
/// <remarks>
65-
/// If null, no response format is specified and the client will use its default.
65+
/// If <see langword="null"/>, no response format is specified and the client will use its default.
6666
/// This property can be set to <see cref="ChatResponseFormat.Text"/> to specify that the response should be unstructured text,
6767
/// to <see cref="ChatResponseFormat.Json"/> to specify that the response should be structured JSON data, or
6868
/// an instance of <see cref="ChatResponseFormatJson"/> constructed with a specific JSON schema to request that the

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseFormatJson.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public ChatResponseFormatJson(
3232
SchemaDescription = schemaDescription;
3333
}
3434

35-
/// <summary>Gets the JSON schema associated with the response, or null if there is none.</summary>
35+
/// <summary>Gets the JSON schema associated with the response, or <see langword="null"/> if there is none.</summary>
3636
public JsonElement? Schema { get; }
3737

3838
/// <summary>Gets a name for the schema.</summary>

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatRole.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ChatRole(string value)
5454
/// </summary>
5555
/// <param name="left">The first <see cref="ChatRole"/> instance to compare.</param>
5656
/// <param name="right">The second <see cref="ChatRole"/> instance to compare.</param>
57-
/// <returns><see langword="true"/> if left and right are both null or have equivalent values; otherwise, <see langword="false"/>.</returns>
57+
/// <returns><see langword="true"/> if left and right are both <see langword="null"/> or have equivalent values; otherwise, <see langword="false"/>.</returns>
5858
public static bool operator ==(ChatRole left, ChatRole right)
5959
{
6060
return left.Equals(right);
@@ -66,7 +66,7 @@ public ChatRole(string value)
6666
/// </summary>
6767
/// <param name="left">The first <see cref="ChatRole"/> instance to compare. </param>
6868
/// <param name="right">The second <see cref="ChatRole"/> instance to compare. </param>
69-
/// <returns><see langword="true"/> if left and right have different values; <see langword="false"/> if they have equivalent values or are both null.</returns>
69+
/// <returns><see langword="true"/> if left and right have different values; <see langword="false"/> if they have equivalent values or are both <see langword="null"/>.</returns>
7070
public static bool operator !=(ChatRole left, ChatRole right)
7171
{
7272
return !(left == right);

src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataContent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public DataContent([StringSyntax(StringSyntaxAttribute.Uri)] string uri, string?
110110
/// </summary>
111111
/// <param name="data">The byte contents.</param>
112112
/// <param name="mediaType">The media type (also known as MIME type) represented by the content.</param>
113-
/// <exception cref="ArgumentNullException"><paramref name="mediaType"/> is null.</exception>
113+
/// <exception cref="ArgumentNullException"><paramref name="mediaType"/> is <see langword="null"/>.</exception>
114114
/// <exception cref="ArgumentException"><paramref name="mediaType"/> is empty or composed entirely of whitespace.</exception>
115115
public DataContent(ReadOnlyMemory<byte> data, string mediaType)
116116
{

src/Libraries/Microsoft.Extensions.AI.Abstractions/Embeddings/EmbeddingGeneratorMetadata.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public EmbeddingGeneratorMetadata(string? providerName = null, Uri? providerUri
3636

3737
/// <summary>Gets the ID of the default model used by this embedding generator.</summary>
3838
/// <remarks>
39-
/// This value can be null if no default model is set on the corresponding embedding generator.
39+
/// This value can be <see langword="null"/> if no default model is set on the corresponding embedding generator.
4040
/// An individual request may override this value via <see cref="EmbeddingGenerationOptions.ModelId"/>.
4141
/// </remarks>
4242
public string? DefaultModelId { get; }
4343

4444
/// <summary>Gets the number of dimensions in the embeddings produced by the default model.</summary>
4545
/// <remarks>
46-
/// This value can be null if either the number of dimensions is unknown or there are multiple possible lengths associated with this model.
46+
/// This value can be <see langword="null"/> if either the number of dimensions is unknown or there are multiple possible lengths associated with this model.
4747
/// An individual request may override this value via <see cref="EmbeddingGenerationOptions.Dimensions"/>.
4848
/// </remarks>
4949
public int? DefaultModelDimensions { get; }

src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceChatClient.cs

+11-21
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,29 @@
2222
namespace Microsoft.Extensions.AI;
2323

2424
/// <summary>Represents an <see cref="IChatClient"/> for an Azure AI Inference <see cref="ChatCompletionsClient"/>.</summary>
25-
public sealed class AzureAIInferenceChatClient : IChatClient
25+
internal sealed class AzureAIInferenceChatClient : IChatClient
2626
{
2727
/// <summary>Metadata about the client.</summary>
2828
private readonly ChatClientMetadata _metadata;
2929

3030
/// <summary>The underlying <see cref="ChatCompletionsClient" />.</summary>
3131
private readonly ChatCompletionsClient _chatCompletionsClient;
3232

33-
/// <summary>The <see cref="JsonSerializerOptions"/> use for any serialization activities related to tool call arguments and results.</summary>
34-
private JsonSerializerOptions _toolCallJsonSerializerOptions = AIJsonUtilities.DefaultOptions;
35-
3633
/// <summary>Gets a ChatRole.Developer value.</summary>
3734
private static ChatRole ChatRoleDeveloper { get; } = new("developer");
3835

3936
/// <summary>Initializes a new instance of the <see cref="AzureAIInferenceChatClient"/> class for the specified <see cref="ChatCompletionsClient"/>.</summary>
4037
/// <param name="chatCompletionsClient">The underlying client.</param>
41-
/// <param name="modelId">The ID of the model to use. If null, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
38+
/// <param name="defaultModelId">The ID of the model to use. If <see langword="null"/>, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
4239
/// <exception cref="ArgumentNullException"><paramref name="chatCompletionsClient"/> is <see langword="null"/>.</exception>
43-
/// <exception cref="ArgumentNullException"><paramref name="modelId"/> is empty or composed entirely of whitespace.</exception>
44-
public AzureAIInferenceChatClient(ChatCompletionsClient chatCompletionsClient, string? modelId = null)
40+
/// <exception cref="ArgumentNullException"><paramref name="defaultModelId"/> is empty or composed entirely of whitespace.</exception>
41+
public AzureAIInferenceChatClient(ChatCompletionsClient chatCompletionsClient, string? defaultModelId = null)
4542
{
4643
_ = Throw.IfNull(chatCompletionsClient);
4744

48-
if (modelId is not null)
45+
if (defaultModelId is not null)
4946
{
50-
_ = Throw.IfNullOrWhitespace(modelId);
47+
_ = Throw.IfNullOrWhitespace(defaultModelId);
5148
}
5249

5350
_chatCompletionsClient = chatCompletionsClient;
@@ -59,14 +56,7 @@ public AzureAIInferenceChatClient(ChatCompletionsClient chatCompletionsClient, s
5956
var providerUrl = typeof(ChatCompletionsClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
6057
?.GetValue(chatCompletionsClient) as Uri;
6158

62-
_metadata = new ChatClientMetadata("az.ai.inference", providerUrl, modelId);
63-
}
64-
65-
/// <summary>Gets or sets <see cref="JsonSerializerOptions"/> to use for any serialization activities related to tool call arguments and results.</summary>
66-
public JsonSerializerOptions ToolCallJsonSerializerOptions
67-
{
68-
get => _toolCallJsonSerializerOptions;
69-
set => _toolCallJsonSerializerOptions = Throw.IfNull(value);
59+
_metadata = new ChatClientMetadata("az.ai.inference", providerUrl, defaultModelId);
7060
}
7161

7262
/// <inheritdoc />
@@ -324,7 +314,7 @@ private ChatCompletionsOptions ToAzureAIOptions(IEnumerable<ChatMessage> chatCon
324314
default:
325315
if (prop.Value is not null)
326316
{
327-
byte[] data = JsonSerializer.SerializeToUtf8Bytes(prop.Value, ToolCallJsonSerializerOptions.GetTypeInfo(typeof(object)));
317+
byte[] data = JsonSerializer.SerializeToUtf8Bytes(prop.Value, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object)));
328318
result.AdditionalProperties[prop.Key] = new BinaryData(data);
329319
}
330320

@@ -413,7 +403,7 @@ private static ChatCompletionsToolDefinition ToAzureAIChatTool(AIFunction aiFunc
413403
}
414404

415405
/// <summary>Converts an Extensions chat message enumerable to an AzureAI chat message enumerable.</summary>
416-
private IEnumerable<ChatRequestMessage> ToAzureAIInferenceChatMessages(IEnumerable<ChatMessage> inputs)
406+
private static IEnumerable<ChatRequestMessage> ToAzureAIInferenceChatMessages(IEnumerable<ChatMessage> inputs)
417407
{
418408
// Maps all of the M.E.AI types to the corresponding AzureAI types.
419409
// Unrecognized or non-processable content is ignored.
@@ -439,7 +429,7 @@ private IEnumerable<ChatRequestMessage> ToAzureAIInferenceChatMessages(IEnumerab
439429
{
440430
try
441431
{
442-
result = JsonSerializer.Serialize(resultContent.Result, ToolCallJsonSerializerOptions.GetTypeInfo(typeof(object)));
432+
result = JsonSerializer.Serialize(resultContent.Result, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(object)));
443433
}
444434
catch (NotSupportedException)
445435
{
@@ -482,7 +472,7 @@ private IEnumerable<ChatRequestMessage> ToAzureAIInferenceChatMessages(IEnumerab
482472
callRequest.CallId,
483473
new FunctionCall(
484474
callRequest.Name,
485-
JsonSerializer.Serialize(callRequest.Arguments, ToolCallJsonSerializerOptions.GetTypeInfo(typeof(IDictionary<string, object>))))));
475+
JsonSerializer.Serialize(callRequest.Arguments, AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(IDictionary<string, object>))))));
486476
}
487477
}
488478

src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceEmbeddingGenerator.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace Microsoft.Extensions.AI;
2323

2424
/// <summary>Represents an <see cref="IEmbeddingGenerator{String, Embedding}"/> for an Azure.AI.Inference <see cref="EmbeddingsClient"/>.</summary>
25-
public sealed class AzureAIInferenceEmbeddingGenerator :
25+
internal sealed class AzureAIInferenceEmbeddingGenerator :
2626
IEmbeddingGenerator<string, Embedding<float>>
2727
{
2828
/// <summary>Metadata about the embedding generator.</summary>
@@ -36,31 +36,31 @@ public sealed class AzureAIInferenceEmbeddingGenerator :
3636

3737
/// <summary>Initializes a new instance of the <see cref="AzureAIInferenceEmbeddingGenerator"/> class.</summary>
3838
/// <param name="embeddingsClient">The underlying client.</param>
39-
/// <param name="modelId">
39+
/// <param name="defaultModelId">
4040
/// The ID of the model to use. This can also be overridden per request via <see cref="EmbeddingGenerationOptions.ModelId"/>.
4141
/// Either this parameter or <see cref="EmbeddingGenerationOptions.ModelId"/> must provide a valid model ID.
4242
/// </param>
43-
/// <param name="dimensions">The number of dimensions to generate in each embedding.</param>
43+
/// <param name="defaultModelDimensions">The number of dimensions to generate in each embedding.</param>
4444
/// <exception cref="ArgumentNullException"><paramref name="embeddingsClient"/> is <see langword="null"/>.</exception>
45-
/// <exception cref="ArgumentException"><paramref name="modelId"/> is empty or composed entirely of whitespace.</exception>
46-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="dimensions"/> is not positive.</exception>
45+
/// <exception cref="ArgumentException"><paramref name="defaultModelId"/> is empty or composed entirely of whitespace.</exception>
46+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="defaultModelDimensions"/> is not positive.</exception>
4747
public AzureAIInferenceEmbeddingGenerator(
48-
EmbeddingsClient embeddingsClient, string? modelId = null, int? dimensions = null)
48+
EmbeddingsClient embeddingsClient, string? defaultModelId = null, int? defaultModelDimensions = null)
4949
{
5050
_ = Throw.IfNull(embeddingsClient);
5151

52-
if (modelId is not null)
52+
if (defaultModelId is not null)
5353
{
54-
_ = Throw.IfNullOrWhitespace(modelId);
54+
_ = Throw.IfNullOrWhitespace(defaultModelId);
5555
}
5656

57-
if (dimensions is < 1)
57+
if (defaultModelDimensions is < 1)
5858
{
59-
Throw.ArgumentOutOfRangeException(nameof(dimensions), "Value must be greater than 0.");
59+
Throw.ArgumentOutOfRangeException(nameof(defaultModelDimensions), "Value must be greater than 0.");
6060
}
6161

6262
_embeddingsClient = embeddingsClient;
63-
_dimensions = dimensions;
63+
_dimensions = defaultModelDimensions;
6464

6565
// https://github.com/Azure/azure-sdk-for-net/issues/46278
6666
// The endpoint isn't currently exposed, so use reflection to get at it, temporarily. Once packages
@@ -69,7 +69,7 @@ public AzureAIInferenceEmbeddingGenerator(
6969
var providerUrl = typeof(EmbeddingsClient).GetField("_endpoint", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
7070
?.GetValue(embeddingsClient) as Uri;
7171

72-
_metadata = new EmbeddingGeneratorMetadata("az.ai.inference", providerUrl, modelId, dimensions);
72+
_metadata = new EmbeddingGeneratorMetadata("az.ai.inference", providerUrl, defaultModelId, defaultModelDimensions);
7373
}
7474

7575
/// <inheritdoc />

src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceExtensions.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public static class AzureAIInferenceExtensions
1010
{
1111
/// <summary>Gets an <see cref="IChatClient"/> for use with this <see cref="ChatCompletionsClient"/>.</summary>
1212
/// <param name="chatCompletionsClient">The client.</param>
13-
/// <param name="modelId">The ID of the model to use. If null, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
13+
/// <param name="modelId">The ID of the model to use. If <see langword="null"/>, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
1414
/// <returns>An <see cref="IChatClient"/> that can be used to converse via the <see cref="ChatCompletionsClient"/>.</returns>
15-
public static IChatClient AsChatClient(
15+
public static IChatClient AsIChatClient(
1616
this ChatCompletionsClient chatCompletionsClient, string? modelId = null) =>
1717
new AzureAIInferenceChatClient(chatCompletionsClient, modelId);
1818

1919
/// <summary>Gets an <see cref="IEmbeddingGenerator{String, Single}"/> for use with this <see cref="EmbeddingsClient"/>.</summary>
2020
/// <param name="embeddingsClient">The client.</param>
21-
/// <param name="modelId">The ID of the model to use. If null, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
22-
/// <param name="dimensions">The number of dimensions to generate in each embedding.</param>
21+
/// <param name="defaultModelId">The ID of the model to use. If <see langword="null"/>, it can be provided per request via <see cref="ChatOptions.ModelId"/>.</param>
22+
/// <param name="defaultModelDimensions">The number of dimensions generated in each embedding.</param>
2323
/// <returns>An <see cref="IEmbeddingGenerator{String, Embedding}"/> that can be used to generate embeddings via the <see cref="EmbeddingsClient"/>.</returns>
24-
public static IEmbeddingGenerator<string, Embedding<float>> AsEmbeddingGenerator(
25-
this EmbeddingsClient embeddingsClient, string? modelId = null, int? dimensions = null) =>
26-
new AzureAIInferenceEmbeddingGenerator(embeddingsClient, modelId, dimensions);
24+
public static IEmbeddingGenerator<string, Embedding<float>> AsIEmbeddingGenerator(
25+
this EmbeddingsClient embeddingsClient, string? defaultModelId = null, int? defaultModelDimensions = null) =>
26+
new AzureAIInferenceEmbeddingGenerator(embeddingsClient, defaultModelId, defaultModelDimensions);
2727
}

0 commit comments

Comments
 (0)