-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable AIFunctionFactory to resolve parameters from an IServiceProvider
- Loading branch information
1 parent
8ea523c
commit 170d874
Showing
7 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...icrosoft.Extensions.AI.Abstractions/Utilities/SkipJsonFunctionSchemaParameterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
#pragma warning disable CA1813 // Avoid unsealed attributes | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Indicates that a parameter to a method should not be included in a generated JSON schema by <see cref="AIJsonUtilities.CreateFunctionJsonSchema"/>.</summary> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public class SkipJsonFunctionSchemaParameterAttribute : Attribute | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="SkipJsonFunctionSchemaParameterAttribute"/> class.</summary> | ||
public SkipJsonFunctionSchemaParameterAttribute() | ||
{ | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/AIFunctionArguments.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Microsoft.Shared.Diagnostics; | ||
|
||
#pragma warning disable CA1710 // Identifiers should have correct suffix | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Represents arguments to be used with <see cref="AIFunction.InvokeAsync"/>.</summary> | ||
/// <remarks> | ||
/// <see cref="AIFunction.InvokeAsync"/> may be invoked with arbitary <see cref="IEnumerable{T}"/> | ||
/// implementations. However, some <see cref="AIFunction"/> implementations may dynamically check | ||
/// the type of the arguments, and if it's an <see cref="AIFunctionArguments"/>, use it to access | ||
/// an <see cref="IServiceProvider"/> that's passed in separately from the arguments enumeration. | ||
/// </remarks> | ||
public class AIFunctionArguments : IEnumerable<KeyValuePair<string, object?>> | ||
{ | ||
/// <summary>The arguments represented by this instance.</summary> | ||
private readonly IEnumerable<KeyValuePair<string, object?>> _arguments; | ||
|
||
/// <summary>Initializes a new instance of the <see cref="AIFunctionArguments"/> class.</summary> | ||
/// <param name="arguments">The arguments represented by this instance.</param> | ||
/// <param name="serviceProvider">Options services associated with these arguments.</param> | ||
public AIFunctionArguments(IEnumerable<KeyValuePair<string, object?>>? arguments, IServiceProvider? serviceProvider = null) | ||
{ | ||
_arguments = Throw.IfNull(arguments); | ||
ServiceProvider = serviceProvider; | ||
} | ||
|
||
/// <summary>Gets the services associated with these arguments.</summary> | ||
public IServiceProvider? ServiceProvider { get; } | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _arguments.GetEnumerator(); | ||
|
||
/// <inheritdoc /> | ||
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_arguments).GetEnumerator(); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FromServiceProviderAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
|
||
namespace Microsoft.Extensions.AI; | ||
|
||
/// <summary>Indicates that a parameter to an <see cref="AIFunction"/> should be sourced from an associated <see cref="IServiceProvider"/>.</summary> | ||
[AttributeUsage(AttributeTargets.Parameter)] | ||
public sealed class FromServiceProviderAttribute : SkipJsonFunctionSchemaParameterAttribute | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="FromServiceProviderAttribute"/> class.</summary> | ||
/// <param name="serviceKey">Optional key to use when resolving the service.</param> | ||
public FromServiceProviderAttribute(object? serviceKey = null) | ||
{ | ||
ServiceKey = serviceKey; | ||
} | ||
|
||
/// <summary>Gets the key to use when resolving the service.</summary> | ||
public object? ServiceKey { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters