- 
                Notifications
    You must be signed in to change notification settings 
- Fork 841
Change AIFunction.InvokeAsync to accept AIFunctionArguments #6158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
      
        
          +523
        
        
          −113
        
        
          
        
      
    
  
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
  
    
      
          
            119 changes: 119 additions & 0 deletions
          
          119 
        
  src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionArguments.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,119 @@ | ||
| // 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; | ||
|  | ||
| #pragma warning disable SA1111 // Closing parenthesis should be on line of last parameter | ||
| #pragma warning disable SA1112 // Closing parenthesis should be on line of opening parenthesis | ||
| #pragma warning disable SA1114 // Parameter list should follow declaration | ||
| #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="AIFunctionArguments"/> is a dictionary of name/value pairs that are used | ||
| /// as inputs to an <see cref="AIFunction"/>. However, an instance carries additional non-nominal | ||
| /// information, such as an optional <see cref="IServiceProvider"/> that can be used by | ||
| /// an <see cref="AIFunction"/> if it needs to resolve any services from a dependency injection | ||
| /// container. | ||
| /// </remarks> | ||
| public sealed class AIFunctionArguments : IDictionary<string, object?>, IReadOnlyDictionary<string, object?> | ||
| { | ||
| /// <summary>The nominal arguments.</summary> | ||
| private readonly Dictionary<string, object?> _arguments; | ||
|  | ||
| /// <summary>Initializes a new instance of the <see cref="AIFunctionArguments"/> class.</summary> | ||
| public AIFunctionArguments() | ||
| { | ||
| _arguments = []; | ||
| } | ||
|  | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AIFunctionArguments"/> class containing | ||
| /// the specified <paramref name="arguments"/>. | ||
| /// </summary> | ||
| /// <param name="arguments">The arguments represented by this instance.</param> | ||
| /// <remarks> | ||
| /// The <paramref name="arguments"/> reference will be stored if the instance is | ||
| /// already a <see cref="Dictionary{TKey, TValue}"/>, in which case all dictionary | ||
| /// operations on this instance will be routed directly to that instance. If <paramref name="arguments"/> | ||
| /// is not a dictionary, a shallow clone of its data will be used to populate this | ||
| /// instance. A <see langword="null"/> <paramref name="arguments"/> is treated as an | ||
| /// empty dictionary. | ||
| /// </remarks> | ||
| public AIFunctionArguments(IDictionary<string, object?>? arguments) | ||
| { | ||
| _arguments = | ||
| arguments is null ? [] : | ||
| arguments as Dictionary<string, object?> ?? | ||
| new Dictionary<string, object?>(arguments); | ||
| } | ||
|  | ||
| /// <summary>Gets or sets services optionally associated with these arguments.</summary> | ||
| public IServiceProvider? Services { get; set; } | ||
|         
                  SteveSandersonMS marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| /// <inheritdoc /> | ||
| public object? this[string key] | ||
| { | ||
| get => _arguments[key]; | ||
| set => _arguments[key] = value; | ||
| } | ||
|  | ||
| /// <inheritdoc /> | ||
| public ICollection<string> Keys => _arguments.Keys; | ||
|  | ||
| /// <inheritdoc /> | ||
| public ICollection<object?> Values => _arguments.Values; | ||
|  | ||
| /// <inheritdoc /> | ||
| public int Count => _arguments.Count; | ||
|  | ||
| /// <inheritdoc /> | ||
| bool ICollection<KeyValuePair<string, object?>>.IsReadOnly => false; | ||
|  | ||
| /// <inheritdoc /> | ||
| IEnumerable<string> IReadOnlyDictionary<string, object?>.Keys => Keys; | ||
|  | ||
| /// <inheritdoc /> | ||
| IEnumerable<object?> IReadOnlyDictionary<string, object?>.Values => Values; | ||
|  | ||
| /// <inheritdoc /> | ||
| public void Add(string key, object? value) => _arguments.Add(key, value); | ||
|  | ||
| /// <inheritdoc /> | ||
| void ICollection<KeyValuePair<string, object?>>.Add(KeyValuePair<string, object?> item) => | ||
| ((ICollection<KeyValuePair<string, object?>>)_arguments).Add(item); | ||
|  | ||
| /// <inheritdoc /> | ||
| public void Clear() => _arguments.Clear(); | ||
|  | ||
| /// <inheritdoc /> | ||
| bool ICollection<KeyValuePair<string, object?>>.Contains(KeyValuePair<string, object?> item) => | ||
| ((ICollection<KeyValuePair<string, object?>>)_arguments).Contains(item); | ||
|  | ||
| /// <inheritdoc /> | ||
| public bool ContainsKey(string key) => _arguments.ContainsKey(key); | ||
|  | ||
| /// <inheritdoc /> | ||
| public void CopyTo(KeyValuePair<string, object?>[] array, int arrayIndex) => | ||
| ((ICollection<KeyValuePair<string, object?>>)_arguments).CopyTo(array, arrayIndex); | ||
|  | ||
| /// <inheritdoc /> | ||
| public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _arguments.GetEnumerator(); | ||
|  | ||
| /// <inheritdoc /> | ||
| public bool Remove(string key) => _arguments.Remove(key); | ||
|  | ||
| /// <inheritdoc /> | ||
| bool ICollection<KeyValuePair<string, object?>>.Remove(KeyValuePair<string, object?> item) => | ||
| ((ICollection<KeyValuePair<string, object?>>)_arguments).Remove(item); | ||
|  | ||
| /// <inheritdoc /> | ||
| public bool TryGetValue(string key, out object? value) => _arguments.TryGetValue(key, out value); | ||
|  | ||
| /// <inheritdoc /> | ||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } | ||
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.