-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add article about accessing data in AI functions #49944
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new how-to article about accessing data in AI functions and reorganizes the AI documentation TOC. The changes update existing quickstart examples to use API keys instead of DefaultAzureCredential, upgrade to .NET 10 and newer package versions, and introduce examples showing how to access contextual data within AI function delegates.
Key changes:
- New how-to article explaining data access patterns in AI functions using
AIFunctionArgumentsandAdditionalProperties - TOC restructuring to group function calling and text-to-image content more logically
- Updates to existing function calling examples with authentication and code comment improvements
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| docs/ai/toc.yml | Adds new "Call functions" section and reorganizes "Text to image" section for better structure |
| docs/ai/quickstarts/use-function-calling.md | Updates date, improves grammar, fixes snippet reference case, removes Semantic Kernel note, adds link to new article |
| docs/ai/quickstarts/snippets/function-calling/openai/Program.cs | Updates function description text for consistency |
| docs/ai/quickstarts/snippets/function-calling/azure-openai/Program.cs | Switches from DefaultAzureCredential to API key authentication, updates code comments and formatting |
| docs/ai/quickstarts/snippets/function-calling/azure-openai/FunctionCallingAI.csproj | Upgrades to .NET 10 and Microsoft.Extensions.AI 10.0.0 |
| docs/ai/quickstarts/includes/semantic-kernel.md | Removes entire file (Semantic Kernel reference note) |
| docs/ai/quickstarts/includes/create-ai-service.md | Adds API key configuration to user secrets setup |
| docs/ai/how-to/snippets/access-data/Project.csproj | New project file for access-data examples targeting .NET 10 |
| docs/ai/how-to/snippets/access-data/Program.cs | New minimal program entry point for running access-data examples |
| docs/ai/how-to/snippets/access-data/ArgumentsExample.cs | New code examples demonstrating AIFunctionArguments and AdditionalProperties usage patterns |
| docs/ai/how-to/access-data-in-functions.md | New how-to article explaining data access mechanisms in AI functions |
|
@verdie-g Do you want to take a look? |
|
|
||
| ## Pass data | ||
|
|
||
| You can associate data with the function at the time it's created, either via closure or via <xref:Microsoft.Extensions.AI.ChatOptions.AdditionalProperties>. If you're creating your own function, you can populate `AdditionalProperties` however you want. If you use <xref:Microsoft.Extensions.AI.AIFunctionFactory> to create the function, you can populate data using <xref:Microsoft.Extensions.AI.AIFunctionFactoryOptions.AdditionalProperties?displayProperty=nameWithType>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also of course capture any references to data as part of the delegate provided to AIFunctionFactory, i.e. bake in whatever you want to reference as part of the AIFunction itself.
(It's probably also worth highlighting somewhere that AIFunction is just a base class; AIFunctionFactory.Create is one producer of AIFunctions, but anyone can derive from it and implement their own. DelegatingAIFunction provides an easy way to wrap an existing AIFunction and layer in additional functionality, including capturing additional data to be used.)
| - <xref:Microsoft.Extensions.AI.AIFunctionArguments.Context>: An arbitrary `IDictionary<object, object>` for passing additional ambient data into the function. | ||
| - <xref:Microsoft.Extensions.AI.AIFunctionArguments.Services>: An <xref:System.IServiceProvider> that lets the `AIFunction` resolve arbitrary state from a [dependency injection (DI)](../../core/extensions/dependency-injection.md) container. | ||
|
|
||
| If you want to access either the `AIFunctionArguments` or the `IServiceProvider` from within your <xref:Microsoft.Extensions.AI.AIFunctionFactory.Create*?displayProperty=nameWithType> delegate, create a parameter typed as `IServiceProvider` or `AIFunctionArguments`. That parameter will be bound to the relevant data from the `AIFunctionArguments` passed to `AIFunction.InvokeAsync()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mention here as well that CancellationToken is also special-cased: if the AIFunctionFactory.Create delegate/lambda has a CancellationToken parameter, it'll be bound to the CT that was passed to InvokeAsync.
BillWagner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you address the open conversations, this LGTM.
Fixes #48469.
Internal previews