Skip to content

Commit 16acdbf

Browse files
committed
Merged PR 44778: Merging branch release/9.0 into internal/release/9.0
2 parents c78c7b1 + d468173 commit 16acdbf

File tree

81 files changed

+6677
-172
lines changed

Some content is hidden

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

81 files changed

+6677
-172
lines changed

eng/MSBuild/LegacySupport.props

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\StringSyntaxAttribute\*.cs" LinkBase="LegacySupport\StringSyntaxAttribute" />
4444
</ItemGroup>
4545

46+
<ItemGroup Condition="'$(InjectJsonSchemaExporterOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp3.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0')">
47+
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\Shared\JsonSchemaExporter\**\*.cs" LinkBase="Shared\EmptyCollections" />
48+
</ItemGroup>
49+
4650
<ItemGroup Condition="'$(InjectGetOrAddOnLegacy)' == 'true' AND ('$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0')">
4751
<Compile Include="$(MSBuildThisFileDirectory)\..\..\src\LegacySupport\GetOrAdd\*.cs" LinkBase="LegacySupport\GetOrAdd" />
4852
</ItemGroup>

eng/packages/TestOnly.props

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PackageVersion Include="BenchmarkDotNet" Version="0.13.5" />
88
<PackageVersion Include="FluentAssertions" Version="6.11.0" />
99
<PackageVersion Include="Grpc.AspNetCore" Version="2.65.0" />
10+
<PackageVersion Include="JsonSchema.Net" Version="7.2.3" />
1011
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2" />
1112
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.3" />
1213
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.22.0-preview.24378.1" />
@@ -20,6 +21,7 @@
2021
<PackageVersion Include="Verify.Xunit" Version="20.4.0" />
2122
<PackageVersion Include="Xunit.Combinatorial" Version="1.6.24" />
2223
<PackageVersion Include="xunit.extensibility.execution" Version="2.4.2" />
24+
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
2325
</ItemGroup>
2426

2527
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">

eng/spellchecking_exclusions.dic

22 Bytes
Binary file not shown.

src/Libraries/Microsoft.Extensions.AI.Abstractions/AdditionalPropertiesDictionary.cs

+90-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.Diagnostics.CodeAnalysis;
89
using System.Globalization;
910
using System.Linq;
11+
using Microsoft.Shared.Diagnostics;
12+
13+
#pragma warning disable S1144 // Unused private types or members should be removed
14+
#pragma warning disable S2365 // Properties should not make collection or array copies
15+
#pragma warning disable S3604 // Member initializer values should not be redundant
1016

1117
namespace Microsoft.Extensions.AI;
1218

1319
/// <summary>Provides a dictionary used as the AdditionalProperties dictionary on Microsoft.Extensions.AI objects.</summary>
20+
[DebuggerTypeProxy(typeof(DebugView))]
21+
[DebuggerDisplay("Count = {Count}")]
1422
public sealed class AdditionalPropertiesDictionary : IDictionary<string, object?>, IReadOnlyDictionary<string, object?>
1523
{
1624
/// <summary>The underlying dictionary.</summary>
@@ -77,6 +85,25 @@ public object? this[string key]
7785
/// <inheritdoc />
7886
public void Add(string key, object? value) => _dictionary.Add(key, value);
7987

88+
/// <summary>Attempts to add the specified key and value to the dictionary.</summary>
89+
/// <param name="key">The key of the element to add.</param>
90+
/// <param name="value">The value of the element to add.</param>
91+
/// <returns><see langword="true"/> if the key/value pair was added to the dictionary successfully; otherwise, <see langword="false"/>.</returns>
92+
public bool TryAdd(string key, object? value)
93+
{
94+
#if NET
95+
return _dictionary.TryAdd(key, value);
96+
#else
97+
if (!_dictionary.ContainsKey(key))
98+
{
99+
_dictionary.Add(key, value);
100+
return true;
101+
}
102+
103+
return false;
104+
#endif
105+
}
106+
80107
/// <inheritdoc />
81108
void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) => ((ICollection<KeyValuePair<string, object?>>)_dictionary).Add(item);
82109

@@ -93,11 +120,17 @@ public object? this[string key]
93120
void ICollection<KeyValuePair<string, object?>>.CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) =>
94121
((ICollection<KeyValuePair<string, object?>>)_dictionary).CopyTo(array, arrayIndex);
95122

123+
/// <summary>
124+
/// Returns an enumerator that iterates through the <see cref="AdditionalPropertiesDictionary"/>.
125+
/// </summary>
126+
/// <returns>An <see cref="AdditionalPropertiesDictionary.Enumerator"/> that enumerates the contents of the <see cref="AdditionalPropertiesDictionary"/>.</returns>
127+
public Enumerator GetEnumerator() => new(_dictionary.GetEnumerator());
128+
96129
/// <inheritdoc />
97-
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _dictionary.GetEnumerator();
130+
IEnumerator<KeyValuePair<string, object?>> IEnumerable<KeyValuePair<string, object?>>.GetEnumerator() => GetEnumerator();
98131

99132
/// <inheritdoc />
100-
IEnumerator IEnumerable.GetEnumerator() => _dictionary.GetEnumerator();
133+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
101134

102135
/// <inheritdoc />
103136
public bool Remove(string key) => _dictionary.Remove(key);
@@ -156,4 +189,59 @@ public bool TryGetValue<T>(string key, [NotNullWhen(true)] out T? value)
156189
value = default;
157190
return false;
158191
}
192+
193+
/// <summary>Enumerates the elements of an <see cref="AdditionalPropertiesDictionary"/>.</summary>
194+
public struct Enumerator : IEnumerator<KeyValuePair<string, object?>>
195+
{
196+
/// <summary>The wrapped dictionary enumerator.</summary>
197+
private Dictionary<string, object?>.Enumerator _dictionaryEnumerator;
198+
199+
/// <summary>Initializes a new instance of the <see cref="Enumerator"/> struct with the dictionary enumerator to wrap.</summary>
200+
/// <param name="dictionaryEnumerator">The dictionary enumerator to wrap.</param>
201+
internal Enumerator(Dictionary<string, object?>.Enumerator dictionaryEnumerator)
202+
{
203+
_dictionaryEnumerator = dictionaryEnumerator;
204+
}
205+
206+
/// <inheritdoc />
207+
public KeyValuePair<string, object?> Current => _dictionaryEnumerator.Current;
208+
209+
/// <inheritdoc />
210+
object IEnumerator.Current => Current;
211+
212+
/// <inheritdoc />
213+
public void Dispose() => _dictionaryEnumerator.Dispose();
214+
215+
/// <inheritdoc />
216+
public bool MoveNext() => _dictionaryEnumerator.MoveNext();
217+
218+
/// <inheritdoc />
219+
public void Reset() => Reset(ref _dictionaryEnumerator);
220+
221+
/// <summary>Calls <see cref="IEnumerator.Reset"/> on an enumerator.</summary>
222+
private static void Reset<TEnumerator>(ref TEnumerator enumerator)
223+
where TEnumerator : struct, IEnumerator
224+
{
225+
enumerator.Reset();
226+
}
227+
}
228+
229+
/// <summary>Provides a debugger view for the collection.</summary>
230+
private sealed class DebugView(AdditionalPropertiesDictionary properties)
231+
{
232+
private readonly AdditionalPropertiesDictionary _properties = Throw.IfNull(properties);
233+
234+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
235+
public AdditionalProperty[] Items => (from p in _properties select new AdditionalProperty(p.Key, p.Value)).ToArray();
236+
237+
[DebuggerDisplay("{Value}", Name = "[{Key}]")]
238+
public readonly struct AdditionalProperty(string key, object? value)
239+
{
240+
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
241+
public string Key { get; } = key;
242+
243+
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
244+
public object? Value { get; } = value;
245+
}
246+
}
159247
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Release History
2+
3+
## 9.0.0-preview.9.24525.1
4+
5+
- Lowered the required version of System.Text.Json to 8.0.5 when targeting net8.0 or older.
6+
- Annotated `FunctionCallContent.Exception` and `FunctionResultContent.Exception` as `[JsonIgnore]`, such that they're ignored when serializing instances with `JsonSerializer`. The corresponding constructors accepting an `Exception` were removed.
7+
- Annotated `ChatCompletion.Message` as `[JsonIgnore]`, such that it's ignored when serializing instances with `JsonSerializer`.
8+
- Added the `FunctionCallContent.CreateFromParsedArguments` method.
9+
- Added the `AdditionalPropertiesDictionary.TryGetValue<T>` method.
10+
- Added the `StreamingChatCompletionUpdate.ModelId` property and removed the `AIContent.ModelId` property.
11+
- Renamed the `GenerateAsync` extension method on `IEmbeddingGenerator<,>` to `GenerateEmbeddingsAsync` and updated it to return `Embedding<T>` rather than `GeneratedEmbeddings`.
12+
- Added `GenerateAndZipAsync` and `GenerateEmbeddingVectorAsync` extension methods for `IEmbeddingGenerator<,>`.
13+
- Added the `EmbeddingGeneratorOptions.Dimensions` property.
14+
- Added the `ChatOptions.TopK` property.
15+
- Normalized `null` inputs in `TextContent` to be empty strings.
16+
17+
## 9.0.0-preview.9.24507.7
18+
19+
Initial Preview

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

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class ChatOptions
2727
/// <summary>Gets or sets the presence penalty for generating chat responses.</summary>
2828
public float? PresencePenalty { get; set; }
2929

30+
/// <summary>Gets or sets a seed value used by a service to control the reproducability of results.</summary>
31+
public long? Seed { get; set; }
32+
3033
/// <summary>
3134
/// Gets or sets the response format for the chat request.
3235
/// </summary>
@@ -74,6 +77,7 @@ public virtual ChatOptions Clone()
7477
TopK = TopK,
7578
FrequencyPenalty = FrequencyPenalty,
7679
PresencePenalty = PresencePenalty,
80+
Seed = Seed,
7781
ResponseFormat = ResponseFormat,
7882
ModelId = ModelId,
7983
ToolMode = ToolMode,

src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
<TargetFrameworks>$(TargetFrameworks);netstandard2.0</TargetFrameworks>
1818
<NoWarn>$(NoWarn);CA2227;CA1034;SA1316;S3253</NoWarn>
1919
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
20+
<DisableNETStandardCompatErrors>true</DisableNETStandardCompatErrors>
2021
</PropertyGroup>
2122

2223
<PropertyGroup>
24+
<InjectJsonSchemaExporterOnLegacy>true</InjectJsonSchemaExporterOnLegacy>
2325
<InjectSharedEmptyCollections>true</InjectSharedEmptyCollections>
2426
<InjectStringHashOnLegacy>true</InjectStringHashOnLegacy>
2527
<InjectStringSyntaxAttributeOnLegacy>true</InjectStringSyntaxAttributeOnLegacy>

src/Libraries/Microsoft.Extensions.AI/Utilities/AIJsonUtilities.Defaults.cs src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ private static JsonSerializerOptions CreateDefaultOptions()
2323
{
2424
// If reflection-based serialization is enabled by default, use it, as it's the most permissive in terms of what it can serialize,
2525
// and we want to be flexible in terms of what can be put into the various collections in the object model.
26-
// Otherwise, use the source-generated options to enable Native AOT.
26+
// Otherwise, use the source-generated options to enable trimming and Native AOT.
2727

2828
if (JsonSerializer.IsReflectionEnabledByDefault)
2929
{
30-
// Keep in sync with the JsonSourceGenerationOptions on JsonContext below.
30+
// Keep in sync with the JsonSourceGenerationOptions attribute on JsonContext below.
3131
JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
3232
{
3333
TypeInfoResolver = new DefaultJsonTypeInfoResolver(),

0 commit comments

Comments
 (0)