AIFunctionFactory.Create Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
Create(Delegate, AIFunctionFactoryOptions) |
Creates an AIFunction instance for a method, specified via a delegate. |
Create(MethodInfo, Object, AIFunctionFactoryOptions) |
Creates an AIFunction instance for a method, specified via an MethodInfo instance and an optional target object if the method is an instance method. |
Create(MethodInfo, Type, AIFunctionFactoryOptions) |
Creates an AIFunction instance for a method, specified via an MethodInfo for and instance method, along with a Type representing the type of the target object to instantiate each time the method is invoked. |
Create(Delegate, String, String, JsonSerializerOptions) |
Creates an AIFunction instance for a method, specified via a delegate. |
Create(MethodInfo, Object, String, String, JsonSerializerOptions) |
Creates an AIFunction instance for a method, specified via an MethodInfo instance and an optional target object if the method is an instance method. |
Create(Delegate, AIFunctionFactoryOptions)
Creates an AIFunction instance for a method, specified via a delegate.
public:
static Microsoft::Extensions::AI::AIFunction ^ Create(Delegate ^ method, Microsoft::Extensions::AI::AIFunctionFactoryOptions ^ options);
public static Microsoft.Extensions.AI.AIFunction Create(Delegate method, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options);
static member Create : Delegate * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction
Public Shared Function Create (method As Delegate, options As AIFunctionFactoryOptions) As AIFunction
Parameters
- method
- Delegate
The method to be represented via the created AIFunction.
- options
- AIFunctionFactoryOptions
Metadata to use to override defaults inferred from method
.
Returns
The created AIFunction for invoking method
.
Exceptions
method
is null
.
A parameter to method
is not serializable.
Remarks
By default, any parameters to method
are sourced from the AIFunctionArguments's dictionary of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned AIFunction's JsonSchema. There are a few exceptions to this:
- CancellationToken parameters are automatically bound to the CancellationToken passed into the invocation via InvokeAsync(AIFunctionArguments, CancellationToken)'s CancellationToken parameter. The parameter is not included in the generated JSON schema. The behavior of CancellationToken parameters may not be overridden.
- By default, IServiceProvider parameters are bound from the Services property and are not included in the JSON schema. If the parameter is optional, such that a default value is provided, Services is allowed to be
null
; otherwise, Services must be non-null
, or else the invocation will fail with an exception due to the required nature of the parameter. The handling of IServiceProvider parameters may be overridden via ConfigureParameterBinding. - By default, AIFunctionArguments parameters are bound directly to AIFunctionArguments instance passed into InvokeAsync(AIFunctionArguments, CancellationToken) and are not included in the JSON schema. If the AIFunctionArguments instance passed to InvokeAsync(AIFunctionArguments, CancellationToken) is
null
, the AIFunction implementation manufactures an empty instance, such that parameters of type AIFunctionArguments may always be satisfied, whether optional or not. The handling of AIFunctionArguments parameters may be overridden via ConfigureParameterBinding.
options
parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the AIFunctionArguments dictionary will result in an exception being thrown). Loosely-typed additional context information may be passed into InvokeAsync(AIFunctionArguments, CancellationToken) via the AIFunctionArguments's Context dictionary; the default binding ignores this collection, but a custom binding supplied via ConfigureParameterBinding may choose to source arguments from this data.The default marshaling of parameters from the AIFunctionArguments dictionary permits values to be passed into the method
's invocation directly if the object is already of a compatible type. Otherwise, if the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized into the parameter type, utilizing SerializerOptions if provided, or else using DefaultOptions. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
In general, the data supplied via an AIFunctionArguments's dictionary is supplied from an AI service and should be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of method
, consider having method
point to an instance method on an instance configured to hold the appropriate state. An IServiceProvider parameter may also be used to resolve services from a dependency injection container.
By default, return values are serialized to JsonElement using options
's SerializerOptions if provided, or else using DefaultOptions. Handling of return values may be overridden via MarshalResult.
Applies to
Create(MethodInfo, Object, AIFunctionFactoryOptions)
Creates an AIFunction instance for a method, specified via an MethodInfo instance and an optional target object if the method is an instance method.
public:
static Microsoft::Extensions::AI::AIFunction ^ Create(System::Reflection::MethodInfo ^ method, System::Object ^ target, Microsoft::Extensions::AI::AIFunctionFactoryOptions ^ options);
public static Microsoft.Extensions.AI.AIFunction Create(System.Reflection.MethodInfo method, object? target, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options);
static member Create : System.Reflection.MethodInfo * obj * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction
Public Shared Function Create (method As MethodInfo, target As Object, options As AIFunctionFactoryOptions) As AIFunction
Parameters
- method
- MethodInfo
The method to be represented via the created AIFunction.
- target
- Object
The target object for the method
if it represents an instance method.
This should be null
if and only if method
is a static method.
- options
- AIFunctionFactoryOptions
Metadata to use to override defaults inferred from method
.
Returns
The created AIFunction for invoking method
.
Exceptions
method
represents an instance method but target
is null.
method
contains a parameter without a parameter name.
A parameter to method
or its return type is not serializable.
Remarks
By default, any parameters to method
are sourced from the AIFunctionArguments's dictionary of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned AIFunction's JsonSchema. There are a few exceptions to this:
- CancellationToken parameters are automatically bound to the CancellationToken passed into the invocation via InvokeAsync(AIFunctionArguments, CancellationToken)'s CancellationToken parameter. The parameter is not included in the generated JSON schema. The behavior of CancellationToken parameters may not be overridden.
- By default, IServiceProvider parameters are bound from the Services property and are not included in the JSON schema. If the parameter is optional, such that a default value is provided, Services is allowed to be
null
; otherwise, Services must be non-null
, or else the invocation will fail with an exception due to the required nature of the parameter. The handling of IServiceProvider parameters may be overridden via ConfigureParameterBinding. - By default, AIFunctionArguments parameters are bound directly to AIFunctionArguments instance passed into InvokeAsync(AIFunctionArguments, CancellationToken) and are not included in the JSON schema. If the AIFunctionArguments instance passed to InvokeAsync(AIFunctionArguments, CancellationToken) is
null
, the AIFunction implementation manufactures an empty instance, such that parameters of type AIFunctionArguments may always be satisfied, whether optional or not. The handling of AIFunctionArguments parameters may be overridden via ConfigureParameterBinding.
options
parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the AIFunctionArguments dictionary will result in an exception being thrown). Loosely-typed additional context information may be passed into InvokeAsync(AIFunctionArguments, CancellationToken) via the AIFunctionArguments's Context dictionary; the default binding ignores this collection, but a custom binding supplied via ConfigureParameterBinding may choose to source arguments from this data.The default marshaling of parameters from the AIFunctionArguments dictionary permits values to be passed into the method
's invocation directly if the object is already of a compatible type. Otherwise, if the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized into the parameter type, utilizing SerializerOptions if provided, or else using DefaultOptions. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
In general, the data supplied via an AIFunctionArguments's dictionary is supplied from an AI service and should be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of method
, consider having method
point to an instance method on an instance configured to hold the appropriate state. An IServiceProvider parameter may also be used to resolve services from a dependency injection container.
By default, return values are serialized to JsonElement using options
's SerializerOptions if provided, or else using DefaultOptions. Handling of return values may be overridden via MarshalResult.
Applies to
Create(MethodInfo, Type, AIFunctionFactoryOptions)
Creates an AIFunction instance for a method, specified via an MethodInfo for and instance method, along with a Type representing the type of the target object to instantiate each time the method is invoked.
public static Microsoft.Extensions.AI.AIFunction Create(System.Reflection.MethodInfo method, Type targetType, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options = default);
static member Create : System.Reflection.MethodInfo * Type * Microsoft.Extensions.AI.AIFunctionFactoryOptions -> Microsoft.Extensions.AI.AIFunction
Public Shared Function Create (method As MethodInfo, targetType As Type, Optional options As AIFunctionFactoryOptions = Nothing) As AIFunction
Parameters
- method
- MethodInfo
The instance method to be represented via the created AIFunction.
- targetType
- Type
The Type to construct an instance of on which to invoke method
when
the resulting AIFunction is invoked. CreateInstance(Type) is used,
utilizing the type's public parameterless constructor. If an instance can't be constructed, an exception is
thrown during the function's invocation.
- options
- AIFunctionFactoryOptions
Metadata to use to override defaults inferred from method
.
Returns
The created AIFunction for invoking method
.
Exceptions
targetType
is null
.
targetType
is not assignable to method
's declaring type.
A parameter to method
or its return type is not serializable.
Remarks
Return values are serialized to JsonElement using options
's SerializerOptions. Arguments that are not already of the expected type are marshaled to the expected type via JSON and using options
's SerializerOptions. If the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized directly. If the argument is anything else unknown, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
By default, any parameters to method
are sourced from the AIFunctionArguments's dictionary of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned AIFunction's JsonSchema. There are a few exceptions to this:
- CancellationToken parameters are automatically bound to the CancellationToken passed into the invocation via InvokeAsync(AIFunctionArguments, CancellationToken)'s CancellationToken parameter. The parameter is not included in the generated JSON schema. The behavior of CancellationToken parameters may not be overridden.
- By default, IServiceProvider parameters are bound from the Services property and are not included in the JSON schema. If the parameter is optional, such that a default value is provided, Services is allowed to be
null
; otherwise, Services must be non-null
, or else the invocation will fail with an exception due to the required nature of the parameter. The handling of IServiceProvider parameters may be overridden via ConfigureParameterBinding. - By default, AIFunctionArguments parameters are bound directly to AIFunctionArguments instance passed into InvokeAsync(AIFunctionArguments, CancellationToken) and are not included in the JSON schema. If the AIFunctionArguments instance passed to InvokeAsync(AIFunctionArguments, CancellationToken) is
null
, the AIFunction implementation manufactures an empty instance, such that parameters of type AIFunctionArguments may always be satisfied, whether optional or not. The handling of AIFunctionArguments parameters may be overridden via ConfigureParameterBinding.
options
parameter; for every parameter, the delegate is enabled to choose if the parameter should be included in the generated schema and how its value should be bound, including handling of optionality (by default, required parameters that are not included in the AIFunctionArguments dictionary will result in an exception being thrown). Loosely-typed additional context information may be passed into InvokeAsync(AIFunctionArguments, CancellationToken) via the AIFunctionArguments's Context dictionary; the default binding ignores this collection, but a custom binding supplied via ConfigureParameterBinding may choose to source arguments from this data.The default marshaling of parameters from the AIFunctionArguments dictionary permits values to be passed into the method
's invocation directly if the object is already of a compatible type. Otherwise, if the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized into the parameter type, utilizing SerializerOptions if provided, or else using DefaultOptions. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
In general, the data supplied via an AIFunctionArguments's dictionary is supplied from an AI service and should be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of method
, the instance constructed for each invocation may contain that data in it, such that it's then available to method
as instance data. An IServiceProvider parameter may also be used to resolve services from a dependency injection container.
By default, return values are serialized to JsonElement using options
's SerializerOptions if provided, or else using DefaultOptions. Handling of return values may be overridden via MarshalResult.
Applies to
Create(Delegate, String, String, JsonSerializerOptions)
- Source:
- AIFunctionFactory.cs
Creates an AIFunction instance for a method, specified via a delegate.
public static Microsoft.Extensions.AI.AIFunction Create(Delegate method, string? name = default, string? description = default, System.Text.Json.JsonSerializerOptions? serializerOptions = default);
static member Create : Delegate * string * string * System.Text.Json.JsonSerializerOptions -> Microsoft.Extensions.AI.AIFunction
Public Shared Function Create (method As Delegate, Optional name As String = Nothing, Optional description As String = Nothing, Optional serializerOptions As JsonSerializerOptions = Nothing) As AIFunction
Parameters
- method
- Delegate
The method to be represented via the created AIFunction.
- name
- String
The name to use for the AIFunction. If null
, the name will be derived from
the name of method
.
- description
- String
The description to use for the AIFunction. If null
, a description will be derived from
any DescriptionAttribute on method
, if available.
- serializerOptions
- JsonSerializerOptions
The JsonSerializerOptions used to marshal function parameters and any return value.
Returns
The created AIFunction for invoking method
.
Exceptions
method
is null
.
A parameter to method
is not serializable.
Remarks
Any parameters to method
are sourced from the AIFunctionArguments's dictionary of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned AIFunction's JsonSchema. There are a few exceptions to this:
- CancellationToken parameters are automatically bound to the CancellationToken passed into the invocation via InvokeAsync(AIFunctionArguments, CancellationToken)'s CancellationToken parameter. The parameter is not included in the generated JSON schema.
- By default, IServiceProvider parameters are bound from the Services property and are not included in the JSON schema. If the parameter is optional, such that a default value is provided, Services is allowed to be
null
; otherwise, Services must be non-null
, or else the invocation will fail with an exception due to the required nature of the parameter. - By default, AIFunctionArguments parameters are bound directly to AIFunctionArguments instance passed into InvokeAsync(AIFunctionArguments, CancellationToken) and are not included in the JSON schema. If the AIFunctionArguments instance passed to InvokeAsync(AIFunctionArguments, CancellationToken) is
null
, the AIFunction implementation manufactures an empty instance, such that parameters of type AIFunctionArguments may always be satisfied, whether optional or not.
The marshaling of parameters from the AIFunctionArguments dictionary permits values to be passed into the method
's invocation directly if the object is already of a compatible type. Otherwise, if the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized into the parameter type, utilizing serializerOptions
if provided, or else DefaultOptions. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
In general, the data supplied via an AIFunctionArguments's dictionary is supplied from an AI service and should be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of method
, consider having method
point to an instance method on an instance configured to hold the appropriate state. An IServiceProvider parameter may also be used to resolve services from a dependency injection container.
Return values are serialized to JsonElement using serializerOptions
if provided, or else using DefaultOptions.
Applies to
Create(MethodInfo, Object, String, String, JsonSerializerOptions)
- Source:
- AIFunctionFactory.cs
Creates an AIFunction instance for a method, specified via an MethodInfo instance and an optional target object if the method is an instance method.
public static Microsoft.Extensions.AI.AIFunction Create(System.Reflection.MethodInfo method, object? target, string? name = default, string? description = default, System.Text.Json.JsonSerializerOptions? serializerOptions = default);
static member Create : System.Reflection.MethodInfo * obj * string * string * System.Text.Json.JsonSerializerOptions -> Microsoft.Extensions.AI.AIFunction
Public Shared Function Create (method As MethodInfo, target As Object, Optional name As String = Nothing, Optional description As String = Nothing, Optional serializerOptions As JsonSerializerOptions = Nothing) As AIFunction
Parameters
- method
- MethodInfo
The method to be represented via the created AIFunction.
- target
- Object
The target object for the method
if it represents an instance method.
This should be null
if and only if method
is a static method.
- name
- String
The name to use for the AIFunction. If null
, the name will be derived from
the name of method
.
- description
- String
The description to use for the AIFunction. If null
, a description will be derived from
any DescriptionAttribute on method
, if available.
- serializerOptions
- JsonSerializerOptions
The JsonSerializerOptions used to marshal function parameters and return value.
Returns
The created AIFunction for invoking method
.
Exceptions
method
represents an instance method but target
is null.
method
contains a parameter without a parameter name.
A parameter to method
or its return type is not serializable.
Remarks
Any parameters to method
are sourced from the AIFunctionArguments's dictionary of key/value pairs and are represented in the JSON schema for the function, as exposed in the returned AIFunction's JsonSchema. There are a few exceptions to this:
- CancellationToken parameters are automatically bound to the CancellationToken passed into the invocation via InvokeAsync(AIFunctionArguments, CancellationToken)'s CancellationToken parameter. The parameter is not included in the generated JSON schema.
- By default, IServiceProvider parameters are bound from the Services property and are not included in the JSON schema. If the parameter is optional, such that a default value is provided, Services is allowed to be
null
; otherwise, Services must be non-null
, or else the invocation will fail with an exception due to the required nature of the parameter. - By default, AIFunctionArguments parameters are bound directly to AIFunctionArguments instance passed into InvokeAsync(AIFunctionArguments, CancellationToken) and are not included in the JSON schema. If the AIFunctionArguments instance passed to InvokeAsync(AIFunctionArguments, CancellationToken) is
null
, the AIFunction implementation manufactures an empty instance, such that parameters of type AIFunctionArguments may always be satisfied, whether optional or not.
The marshaling of parameters from the AIFunctionArguments dictionary permits values to be passed into the method
's invocation directly if the object is already of a compatible type. Otherwise, if the argument is a JsonElement, JsonDocument, or JsonNode, it is deserialized into the parameter type, utilizing serializerOptions
if provided, or else DefaultOptions. If the argument is anything else, it is round-tripped through JSON, serializing the object as JSON and then deserializing it to the expected type.
In general, the data supplied via an AIFunctionArguments's dictionary is supplied from an AI service and should be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of method
, consider having method
point to an instance method on an instance configured to hold the appropriate state. An IServiceProvider parameter may also be used to resolve services from a dependency injection container.
Return values are serialized to JsonElement using serializerOptions
if provided, or else using DefaultOptions.