Task automation with agents
Most AI agents today simply retrieve data and respond to user queries. AI agents, however, can achieve much more by using plugins to automate tasks on behalf of users. This allows users to delegate tasks to AI agents, freeing up time for more important work.
Once AI Agents start performing actions, however, it's important to ensure that they are acting in the best interest of the user. This is why we provide hooks / filters to allow you to control what actions the AI agent can take.
Requiring user consent
When an AI agent is about to perform an action on behalf of a user, it should first ask for the user's consent. This is especially important when the action involves sensitive data or financial transactions.
In Semantic Kernel, you can use the function invocation filter. This filter is always called whenever a function is invoked from an AI agent. To create a filter, you need to implement the IFunctionInvocationFilter
interface and then add it as a service to the kernel.
Here's an example of a function invocation filter that requires user consent:
public class ApprovalFilterExample() : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
if (context.Function.PluginName == "DynamicsPlugin" && context.Function.Name == "create_order")
{
Console.WriteLine("System > The agent wants to create an approval, do you want to proceed? (Y/N)");
string shouldProceed = Console.ReadLine()!;
if (shouldProceed != "Y")
{
context.Result = new FunctionResult(context.Result, "The order creation was not approved by the user");
return;
}
}
await next(context);
}
}
You can then add the filter as a service to the kernel:
IKernelBuilder builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<IFunctionInvocationFilter, ApprovalFilterExample>();
Kernel kernel = builder.Build();
from typing import Any, Coroutine
from semantic_kernel.filters.filter_types import FilterTypes
from semantic_kernel.filters.functions.function_invocation_context import FunctionInvocationContext
from semantic_kernel.functions.function_result import FunctionResult
# The `filter` decorator within kernel, creates and adds the filter in one go.
@kernel.filter(filter_type=FilterTypes.FUNCTION_INVOCATION)
async def approval_filter_example(
context: FunctionInvocationContext, next: Coroutine[FunctionInvocationContext, Any, None]
):
if context.function.plugin_name == "DynamicsPlugin" and context.function.name == "create_order":
should_proceed = input("System > The agent wants to create an approval, do you want to proceed? (Y/N)")
if should_proceed.lower() != "y":
context.result = FunctionResult(
function=context.function.metadata, value="The order creation was not approved by the user"
)
return
await next(context)
Java sample is coming soon.
Now, whenever the AI agent tries to create an order using the DynamicsPlugin
, the user will be prompted to approve the action.
Tip
Whenever a function is cancelled or fails, you should provide the AI agent with a meaningful error message so it can respond appropriately. For example, if we didn't let the AI agent know that the order creation was not approved, it would assume that the order failed due to a technical issue and would try to create the order again.
Next steps
Now that you've learned how to allow agents to automate tasks, you can learn how to allow agents to automatically create plans to address user needs.