forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Enable use of authentication info from Open AI plugin manifest (m…
…icrosoft#3304) ### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> - today the authentication info defined in `ai-plugin.json` is not considered. - this PR enables the use of that info both when fetching the plugin's spec and when subsequent calls are made to the plugin's endpoint. - Note: an example utilizing this functionality will be added in a follow-up PR. ### Description 1. split `ImportAIPluginFunctionsAsync` into two functions: `ImportOpenApiPluginFunctionsAsync` and `ImportOpenAIPluginFunctionsAsync` 2. added `OpenAIFunctionExecutionParameters` that makes use of `OpenAIAuthenticateRequestAsyncCallback`, which takes `OpenAIAuthentication` as an argument. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Mark Wallace <[email protected]>
- Loading branch information
1 parent
6ff0c2b
commit 49e057f
Showing
21 changed files
with
644 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel.Diagnostics; | ||
using Microsoft.SemanticKernel.Functions.OpenAPI.Authentication; | ||
|
||
namespace Microsoft.SemanticKernel.Functions.OpenAPI; | ||
|
||
internal static class DocumentLoader | ||
{ | ||
internal static async Task<string> LoadDocumentFromUriAsync( | ||
Uri uri, | ||
ILogger logger, | ||
HttpClient httpClient, | ||
AuthenticateRequestAsyncCallback? authCallback, | ||
string? userAgent, | ||
CancellationToken cancellationToken) | ||
{ | ||
using var request = new HttpRequestMessage(HttpMethod.Get, uri.ToString()); | ||
request.Headers.UserAgent.Add(ProductInfoHeaderValue.Parse(userAgent ?? Telemetry.HttpUserAgent)); | ||
|
||
if (authCallback is not null) | ||
{ | ||
await authCallback(request).ConfigureAwait(false); | ||
} | ||
|
||
logger.LogTrace("Importing document from {0}", uri); | ||
|
||
using var response = await httpClient.SendWithSuccessCheckAsync(request, cancellationToken).ConfigureAwait(false); | ||
return await response.Content.ReadAsStringWithExceptionMappingAsync().ConfigureAwait(false); | ||
} | ||
|
||
internal static async Task<string> LoadDocumentFromFilePathAsync( | ||
string filePath, | ||
ILogger logger, | ||
CancellationToken cancellationToken) | ||
{ | ||
var pluginJson = string.Empty; | ||
|
||
if (!File.Exists(filePath)) | ||
{ | ||
throw new FileNotFoundException($"Invalid URI. The specified path '{filePath}' does not exist."); | ||
} | ||
|
||
logger.LogTrace("Importing document from {0}", filePath); | ||
|
||
using (var sr = File.OpenText(filePath)) | ||
{ | ||
return await sr.ReadToEndAsync().ConfigureAwait(false); // must await here to avoid stream reader being disposed before the string is read | ||
} | ||
} | ||
|
||
internal static async Task<string> LoadDocumentFromStreamAsync(Stream stream) | ||
{ | ||
using StreamReader reader = new(stream); | ||
return await reader.ReadToEndAsync().ConfigureAwait(false); | ||
} | ||
} |
Oops, something went wrong.