Skip to content
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

.Net: Example of retry logic using Filters #6152

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 72 additions & 0 deletions dotnet/samples/Concepts/Filtering/RetryWithFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace Filtering;

/// <summary>
/// This example shows how to perform retry with filter and switch to another model as a fallback.
/// </summary>
public class RetryWithFilters(ITestOutputHelper output) : BaseTest(output)
dmytrostruk marked this conversation as resolved.
Show resolved Hide resolved
{
[Fact]
public async Task ChangeModelAndRetryAsync()
{
// Default and fallback models for demonstration purposes
const string DefaultModelId = "gpt-4";
const string FallbackModelId = "gpt-3.5-turbo-1106";

var builder = Kernel.CreateBuilder();

// Add OpenAI chat completion service with invalid API key to force a 401 Unauthorized response
builder.AddOpenAIChatCompletion(modelId: DefaultModelId, apiKey: "invalid_key");

// Add OpenAI chat completion service with valid configuration as a fallback
builder.AddOpenAIChatCompletion(modelId: FallbackModelId, apiKey: TestConfiguration.OpenAI.ApiKey);

// Add retry filter
builder.Services.AddSingleton<IFunctionInvocationFilter>(new RetryFilter(FallbackModelId));

// Build kernel
var kernel = builder.Build();

// Initially, use "gpt-4" with invalid API key to simulate exception
var executionSettings = new OpenAIPromptExecutionSettings { ModelId = DefaultModelId, MaxTokens = 20 };

var result = await kernel.InvokePromptAsync("Hi, can you help me today?", new(executionSettings));

Console.WriteLine(result);

// Output: Of course! I'll do my best to help you. What do you need assistance with?
}

/// <summary>
/// Filter to change the model and perform retry in case of exception.
/// </summary>
private sealed class RetryFilter(string fallbackModelId) : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
try
{
// Try to invoke function
await next(context);
}
// Catch specific exception
catch (HttpOperationException exception) when (exception.StatusCode == HttpStatusCode.Unauthorized)
{
// Get current execution settings
PromptExecutionSettings executionSettings = context.Arguments.ExecutionSettings![PromptExecutionSettings.DefaultServiceId];

// Override settings with fallback model id
executionSettings.ModelId = fallbackModelId;

// Try to invoke function again
await next(context);
}
}
}
}
1 change: 1 addition & 0 deletions dotnet/samples/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Down below you can find the code snippets that demonstrate the usage of many Sem
- [FunctionInvocationFiltering](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/FunctionInvocationFiltering.cs)
- [Legacy_KernelHooks](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/Legacy_KernelHooks.cs)
- [PromptRenderFiltering](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/PromptRenderFiltering.cs)
- [RetryWithFilters](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Filtering/RetryWithFilters.cs)

## Functions - Invoking [`Method`](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Core/Functions/KernelFunctionFromMethod.cs) or [`Prompt`](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Core/Functions/KernelFunctionFromPrompt.cs) functions with [`Kernel`](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel.Abstractions/Kernel.cs)

Expand Down