Skip to content

Commit

Permalink
Use ImportOpenAIPluginFunctionsAsync to import OpenAI functions (#624)
Browse files Browse the repository at this point in the history
### Motivation and Context
We are currently, using OpenApiFunctionExecutionParameters to import
OpenAI functions, which is incorrect and doesn't work.

### Description
Use OpenApiFunctionExecutionParameters to load OpenAI functions

### Contribution Checklist
- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [Contribution
Guidelines](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/chat-copilot/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
glahaye authored Nov 18, 2023
1 parent 103b071 commit f590446
Showing 1 changed file with 21 additions and 10 deletions.
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

0 comments on commit f590446

Please sign in to comment.