Repository navigation
.Net: OpenAPI customization - #8182
Merged
SergeyMenshykh merged 2 commits intoAug 16, 2024
Merged
SergeyMenshykh merged 2 commits into
SergeyMenshykh merged 2 commits into
Conversation
Mark Wallace (markwallace-microsoft)
approved these changes
Aug 15, 2024
Roger Barreto (rogerbarreto)
approved these changes
Aug 16, 2024
This was referenced Aug 16, 2024
github-merge-queue Bot
pushed a commit
that referenced
this pull request
Aug 16, 2024
### Motivation, Context and Description Today, the OpenAPI functionality that maps the data type of a parameter from the OpenAPI spec to `KernelParameterMetadata` supports only the mapping for string and boolean types. This PR extends the mapping to support other primitive types like `number`, `integer`, and `object`. Closes: #8182 Out of scope: - Support for the `array` data type.
LudoCorporateShark
pushed a commit
to LudoCorporateShark/semantic-kernel
that referenced
this pull request
Aug 25, 2024
### Motivation and Context
Today, OpenAPI functionality in SK supports a limited, predefined list
of content types: text/plain, application/json, etc. Moreover, the way
these content types are read from the "wire" might not be the most
efficient for certain scenarios. For example, SK OpenAPI functionality
reads JSON as a string and images as a byte array. While this may be
sufficient for most cases, there could be situations where this approach
is not optimal, and reading both JSON and image content as streams would
be preferable.
### Description
This PR adds functionality allowing to supply a HTTP content reader to
customize HTTP content reading. The reader accepts HTTP request and HTTP
response as parameters, so it can access all properties of both to
decide on the best way to read content and support content types not
supported by default. For example, this custom reader can read JSON as a
stream and any content as stream if the `x-stream` heder is provided in
HTTP request:
```csharp
private static async Task<object?> ReadHttpResponseContentAsync(HttpResponseContentReaderContext context, CancellationToken cancellationToken)
{
// Read JSON content as a stream rather than as a string, which is the default behavior
if (context.Response.Content.Headers.ContentType?.MediaType == "application/json")
{
return await context.Response.Content.ReadAsStreamAsync(cancellationToken);
}
// Read any content as a stream if the request contains the "x-stream" header.
if (context.Request.Headers.Contains("x-stream"))
{
return await context.Response.Content.ReadAsStreamAsync(cancellationToken);
}
// Return null to indicate that any other HTTP content not handled above should be read by the default reader.
return null;
}
// Register the custom HTTP content reader
var executionParameters = new OpenApiFunctionExecutionParameters() { HttpResponseContentReader = ReadHttpResponseContentAsync };
// Create OpenAPI plugin
var plugin = await OpenApiKernelPluginFactory.CreateFromOpenApiAsync("RepairService", "Resources/Plugins/RepairServicePlugin/repair-service.json", executionParameters);
```
Closes: microsoft#8070
### Contribution Checklist
- [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 😄
LudoCorporateShark
pushed a commit
to LudoCorporateShark/semantic-kernel
that referenced
this pull request
Aug 25, 2024
…t#8198) ### Motivation, Context and Description Today, the OpenAPI functionality that maps the data type of a parameter from the OpenAPI spec to `KernelParameterMetadata` supports only the mapping for string and boolean types. This PR extends the mapping to support other primitive types like `number`, `integer`, and `object`. Closes: microsoft#8182 Out of scope: - Support for the `array` data type.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Today, OpenAPI functionality in SK supports a limited, predefined list of content types: text/plain, application/json, etc. Moreover, the way these content types are read from the "wire" might not be the most efficient for certain scenarios. For example, SK OpenAPI functionality reads JSON as a string and images as a byte array. While this may be sufficient for most cases, there could be situations where this approach is not optimal, and reading both JSON and image content as streams would be preferable.
Description
This PR adds functionality allowing to supply a HTTP content reader to customize HTTP content reading. The reader accepts HTTP request and HTTP response as parameters, so it can access all properties of both to decide on the best way to read content and support content types not supported by default. For example, this custom reader can read JSON as a stream and any content as stream if the
x-streamheder is provided in HTTP request:Closes: #8070
Contribution Checklist