From 89d45584b1df2a9971c0368c62dad3e7deb1a6b8 Mon Sep 17 00:00:00 2001 From: Gil LaHaye Date: Fri, 17 Nov 2023 21:37:02 -0800 Subject: [PATCH] Use ImportOpenAIPluginFunctionsAsync to import OpenAI functions --- webapi/Controllers/ChatController.cs | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/webapi/Controllers/ChatController.cs b/webapi/Controllers/ChatController.cs index a01c48e3e..2519303b0 100644 --- a/webapi/Controllers/ChatController.cs +++ b/webapi/Controllers/ChatController.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Reflection; using System.Text.Json; using System.Text.RegularExpressions; @@ -30,6 +31,7 @@ using Microsoft.SemanticKernel.Diagnostics; using Microsoft.SemanticKernel.Functions.OpenAPI.Authentication; using Microsoft.SemanticKernel.Functions.OpenAPI.Extensions; +using Microsoft.SemanticKernel.Functions.OpenAPI.OpenAI; using Microsoft.SemanticKernel.Orchestration; using Microsoft.SemanticKernel.Plugins.MsGraph; using Microsoft.SemanticKernel.Plugins.MsGraph.Connectors; @@ -332,16 +334,21 @@ await planner.Kernel.ImportOpenApiPluginFunctionsAsync( // TODO: [Issue #44] Support other forms of auth. Currently, we only support user PAT or no auth. var requiresAuth = !plugin.AuthType.Equals("none", StringComparison.OrdinalIgnoreCase); - BearerAuthenticationProvider authenticationProvider = new(() => Task.FromResult(PluginAuthValue)); + OpenAIAuthenticateRequestAsyncCallback authCallback = (request, _, _) => + { + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", PluginAuthValue); - await planner.Kernel.ImportOpenApiPluginFunctionsAsync( + return Task.CompletedTask; + }; + + await planner.Kernel.ImportOpenAIPluginFunctionsAsync( $"{plugin.NameForModel}Plugin", PluginUtils.GetPluginManifestUri(plugin.ManifestDomain), - new OpenApiFunctionExecutionParameters + new OpenAIFunctionExecutionParameters { HttpClient = this._httpClientFactory.CreateClient("Plugin"), IgnoreNonCompliantErrors = true, - AuthCallback = requiresAuth ? authenticationProvider.AuthenticateRequestAsync : null + AuthCallback = requiresAuth ? authCallback : null }); } } @@ -381,19 +388,22 @@ private async Task RegisterPlannerHostedFunctionsUsedAsync(CopilotChatPlanner pl { this._logger.LogDebug("Enabling hosted plugin {0}.", plugin.Name); - CustomAuthenticationProvider authenticationProvider = new( - () => Task.FromResult("X-Functions-Key"), - () => Task.FromResult(plugin.Key)); + OpenAIAuthenticateRequestAsyncCallback authCallback = (request, _, _) => + { + request.Headers.Add("X-Functions-Key", plugin.Key); + + return Task.CompletedTask; + }; // Register the ChatGPT plugin with the planner's kernel. - await planner.Kernel.ImportOpenApiPluginFunctionsAsync( + await planner.Kernel.ImportOpenAIPluginFunctionsAsync( PluginUtils.SanitizePluginName(plugin.Name), PluginUtils.GetPluginManifestUri(plugin.ManifestDomain), - new OpenApiFunctionExecutionParameters + new OpenAIFunctionExecutionParameters { HttpClient = this._httpClientFactory.CreateClient("Plugin"), IgnoreNonCompliantErrors = true, - AuthCallback = authenticationProvider.AuthenticateRequestAsync + AuthCallback = authCallback }); } else @@ -401,6 +411,7 @@ await planner.Kernel.ImportOpenApiPluginFunctionsAsync( this._logger.LogWarning("Failed to find plugin {0}.", enabledPlugin); } } + return; }