|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +using Microsoft.SemanticKernel; |
| 3 | +using Microsoft.SemanticKernel.Agents; |
| 4 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 5 | +using Microsoft.SemanticKernel.Connectors.OpenAI; |
| 6 | + |
| 7 | +namespace Agents; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Demonstrate service selection for <see cref="ChatCompletionAgent"/> through setting service-id |
| 11 | +/// on <see cref="ChatCompletionAgent.Arguments"/> and also providing override <see cref="KernelArguments"/> |
| 12 | +/// when calling <see cref="ChatCompletionAgent.InvokeAsync"/> |
| 13 | +/// </summary> |
| 14 | +public class ChatCompletion_ServiceSelection(ITestOutputHelper output) : BaseTest(output) |
| 15 | +{ |
| 16 | + private const string ServiceKeyGood = "chat-good"; |
| 17 | + private const string ServiceKeyBad = "chat-bad"; |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public async Task UseServiceSelectionWithChatCompletionAgentAsync() |
| 21 | + { |
| 22 | + // Create kernel with two instances of IChatCompletionService |
| 23 | + // One service is configured with a valid API key and the other with an |
| 24 | + // invalid key that will result in a 401 Unauthorized error. |
| 25 | + Kernel kernel = CreateKernelWithTwoServices(); |
| 26 | + |
| 27 | + // Define the agent targeting ServiceId = ServiceKeyGood |
| 28 | + ChatCompletionAgent agentGood = |
| 29 | + new() |
| 30 | + { |
| 31 | + Kernel = kernel, |
| 32 | + Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { ServiceId = ServiceKeyGood }), |
| 33 | + }; |
| 34 | + |
| 35 | + // Define the agent targeting ServiceId = ServiceKeyBad |
| 36 | + ChatCompletionAgent agentBad = |
| 37 | + new() |
| 38 | + { |
| 39 | + Kernel = kernel, |
| 40 | + Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { ServiceId = ServiceKeyBad }), |
| 41 | + }; |
| 42 | + |
| 43 | + // Define the agent with no explicit ServiceId defined |
| 44 | + ChatCompletionAgent agentDefault = new() { Kernel = kernel }; |
| 45 | + |
| 46 | + // Invoke agent as initialized with ServiceId = ServiceKeyGood: Expect agent response |
| 47 | + Console.WriteLine("\n[Agent With Good ServiceId]"); |
| 48 | + await InvokeAgentAsync(agentGood); |
| 49 | + |
| 50 | + // Invoke agent as initialized with ServiceId = ServiceKeyBad: Expect failure due to invalid service key |
| 51 | + Console.WriteLine("\n[Agent With Bad ServiceId]"); |
| 52 | + await InvokeAgentAsync(agentBad); |
| 53 | + |
| 54 | + // Invoke agent as initialized with no explicit ServiceId: Expect agent response |
| 55 | + Console.WriteLine("\n[Agent With No ServiceId]"); |
| 56 | + await InvokeAgentAsync(agentDefault); |
| 57 | + |
| 58 | + // Invoke agent with override arguments where ServiceId = ServiceKeyGood: Expect agent response |
| 59 | + Console.WriteLine("\n[Bad Agent: Good ServiceId Override]"); |
| 60 | + await InvokeAgentAsync(agentBad, new(new OpenAIPromptExecutionSettings() { ServiceId = ServiceKeyGood })); |
| 61 | + |
| 62 | + // Invoke agent with override arguments where ServiceId = ServiceKeyBad: Expect failure due to invalid service key |
| 63 | + Console.WriteLine("\n[Good Agent: Bad ServiceId Override]"); |
| 64 | + await InvokeAgentAsync(agentGood, new(new OpenAIPromptExecutionSettings() { ServiceId = ServiceKeyBad })); |
| 65 | + Console.WriteLine("\n[Default Agent: Bad ServiceId Override]"); |
| 66 | + await InvokeAgentAsync(agentDefault, new(new OpenAIPromptExecutionSettings() { ServiceId = ServiceKeyBad })); |
| 67 | + |
| 68 | + // Invoke agent with override arguments with no explicit ServiceId: Expect agent response |
| 69 | + Console.WriteLine("\n[Good Agent: No ServiceId Override]"); |
| 70 | + await InvokeAgentAsync(agentGood, new(new OpenAIPromptExecutionSettings())); |
| 71 | + Console.WriteLine("\n[Bad Agent: No ServiceId Override]"); |
| 72 | + await InvokeAgentAsync(agentBad, new(new OpenAIPromptExecutionSettings())); |
| 73 | + Console.WriteLine("\n[Default Agent: No ServiceId Override]"); |
| 74 | + await InvokeAgentAsync(agentDefault, new(new OpenAIPromptExecutionSettings())); |
| 75 | + |
| 76 | + // Local function to invoke agent and display the conversation messages. |
| 77 | + async Task InvokeAgentAsync(ChatCompletionAgent agent, KernelArguments? arguments = null) |
| 78 | + { |
| 79 | + ChatHistory chat = [new(AuthorRole.User, "Hello")]; |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + await foreach (ChatMessageContent response in agent.InvokeAsync(chat, arguments)) |
| 84 | + { |
| 85 | + Console.WriteLine(response.Content); |
| 86 | + } |
| 87 | + } |
| 88 | + catch (HttpOperationException exception) |
| 89 | + { |
| 90 | + Console.WriteLine($"Status: {exception.StatusCode}"); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private Kernel CreateKernelWithTwoServices() |
| 96 | + { |
| 97 | + IKernelBuilder builder = Kernel.CreateBuilder(); |
| 98 | + |
| 99 | + if (this.UseOpenAIConfig) |
| 100 | + { |
| 101 | + builder.AddOpenAIChatCompletion( |
| 102 | + TestConfiguration.OpenAI.ChatModelId, |
| 103 | + "bad-key", |
| 104 | + serviceId: ServiceKeyBad); |
| 105 | + |
| 106 | + builder.AddOpenAIChatCompletion( |
| 107 | + TestConfiguration.OpenAI.ChatModelId, |
| 108 | + TestConfiguration.OpenAI.ApiKey, |
| 109 | + serviceId: ServiceKeyGood); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + builder.AddAzureOpenAIChatCompletion( |
| 114 | + TestConfiguration.AzureOpenAI.ChatDeploymentName, |
| 115 | + TestConfiguration.AzureOpenAI.Endpoint, |
| 116 | + "bad-key", |
| 117 | + serviceId: ServiceKeyBad); |
| 118 | + |
| 119 | + builder.AddAzureOpenAIChatCompletion( |
| 120 | + TestConfiguration.AzureOpenAI.ChatDeploymentName, |
| 121 | + TestConfiguration.AzureOpenAI.Endpoint, |
| 122 | + TestConfiguration.AzureOpenAI.ApiKey, |
| 123 | + serviceId: ServiceKeyGood); |
| 124 | + } |
| 125 | + |
| 126 | + return builder.Build(); |
| 127 | + } |
| 128 | +} |
0 commit comments