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

Use ImportOpenAIPluginFunctionsAsync to import OpenAI functions #624

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions webapi/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
});
}
}
Expand Down Expand Up @@ -381,26 +388,30 @@ 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
{
this._logger.LogWarning("Failed to find plugin {0}.", enabledPlugin);
}
}

return;
}

Expand Down
Loading