Skip to content

Commit

Permalink
Merge pull request #5 from microsoft/feature-openapi
Browse files Browse the repository at this point in the history
Feature openapi
  • Loading branch information
adrianwyatt authored Apr 3, 2023
2 parents c16f0c9 + 059378a commit bc4dcfc
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
/// </summary>
public static class KernelOpenApiExtensions
{

/// <summary>
/// Imports OpenAPI document from a URL.
/// </summary>
Expand Down Expand Up @@ -217,7 +216,15 @@ async Task<SKContext> ExecuteAsync(SKContext context)
var arguments = new Dictionary<string, string>();
foreach (var parameter in restOperationParameters)
{
if (context.Variables.Get(parameter.Name, out var value))
//A try to resolve argument by alternative parameter name
if (!string.IsNullOrEmpty(parameter.AlternativeName) && context.Variables.Get(parameter.AlternativeName, out var value))
{
arguments.Add(parameter.Name, value);
continue;
}

//A try to resolve argument by original parameter name
if (context.Variables.Get(parameter.Name, out value))
{
arguments.Add(parameter.Name, value);
continue;
Expand Down Expand Up @@ -249,7 +256,7 @@ async Task<SKContext> ExecuteAsync(SKContext context)
var function = new SKFunction(
delegateType: SKFunction.DelegateTypes.ContextSwitchInSKContextOutTaskSKContext,
delegateFunction: ExecuteAsync,
parameters: restOperationParameters.Select(p => new ParameterView() { Name = p.Name, Description = p.Name, DefaultValue = p.DefaultValue ?? string.Empty }).ToList(), //functionConfig.PromptTemplate.GetParameters(),
parameters: restOperationParameters.Select(p => new ParameterView() { Name = p.AlternativeName ?? p.Name, Description = p.Name, DefaultValue = p.DefaultValue ?? string.Empty }).ToList(), //functionConfig.PromptTemplate.GetParameters(),
description: operation.Description,
skillName: skillName,
functionName: operation.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;

namespace Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
Expand All @@ -25,6 +26,12 @@ public static IReadOnlyList<RestApiOperationParameter> GetParameters(this RestAp
//Add Payload properties.
parameters.AddRange(CreateParametersFromPayloadProperties(operation.Payload));

//Create a property alternative name without special symbols that are not supported by SK template language.
foreach (var parameter in parameters)
{
parameter.AlternativeName = Regex.Replace(parameter.Name, @"[^0-9A-Za-z_]+", "_");
}

return parameters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ internal class RestApiOperationParameter
/// <summary>
/// The parameter name.
/// </summary>
public string Name { get; }
public string Name { get; }

/// <summary>
/// The property alternative name. It can be used as an alternative name in contexts where the original name can't be used.
/// </summary>
public string? AlternativeName { get; set; }

/// <summary>
/// The parameter type - string, integer, number, boolean, array and object.
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel/Diagnostics/Verify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static void ValidFunctionName([NotNull] string? functionName)
internal static void ValidFunctionParamName([NotNull] string? functionParamName)
{
NotEmpty(functionParamName, "The function parameter name cannot be empty");
Regex pattern = new("^[0-9A-Za-z_-]*$");
Regex pattern = new("^[0-9A-Za-z_]*$");
if (!pattern.IsMatch(functionParamName))
{
throw new KernelException(
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel/IKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ISKFunction RegisterSemanticFunction(
/// <param name="skillName">Name of the skill containing the function. The name can contain only alphanumeric chars + underscore.</param>
/// <param name="customFunction">The custom function to register.</param>
/// <returns>A C# function wrapping the function execution logic.</returns>
ISKFunction RegisterCustomFunction(string skillName, SKFunction customFunction);
ISKFunction RegisterCustomFunction(string skillName, ISKFunction customFunction);

/// <summary>
/// Import a set of functions from the given skill. The functions must have the `SKFunction` attribute.
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/SemanticKernel/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public IDictionary<string, ISKFunction> ImportSkill(object skillInstance, string
}

/// <inheritdoc/>
public ISKFunction RegisterCustomFunction(string skillName, SKFunction customFunction)
public ISKFunction RegisterCustomFunction(string skillName, ISKFunction customFunction)
{
// Future-proofing the name not to contain special chars
Verify.ValidSkillName(skillName);
Expand Down
15 changes: 8 additions & 7 deletions samples/dotnet/FileCompression/FileCompressionSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Skills.FileCompression;

namespace Microsoft.SemanticKernel.Skills.FileCompression;
namespace FileCompression;

/// <summary>
/// Skill for compressing and decompressing files.
Expand Down Expand Up @@ -79,8 +80,8 @@ public FileCompressionSkill(IFileCompressor fileCompressor, ILogger<FileCompress
}

await this._fileCompressor.CompressFileAsync(Environment.ExpandEnvironmentVariables(sourceFilePath),
Environment.ExpandEnvironmentVariables(destinationFilePath),
context.CancellationToken);
Environment.ExpandEnvironmentVariables(destinationFilePath),
context.CancellationToken);

return destinationFilePath;
}
Expand Down Expand Up @@ -109,8 +110,8 @@ await this._fileCompressor.CompressFileAsync(Environment.ExpandEnvironmentVariab
}

await this._fileCompressor.CompressDirectoryAsync(Environment.ExpandEnvironmentVariables(sourceDirectoryPath),
Environment.ExpandEnvironmentVariables(destinationFilePath),
context.CancellationToken);
Environment.ExpandEnvironmentVariables(destinationFilePath),
context.CancellationToken);

return destinationFilePath;
}
Expand Down Expand Up @@ -144,8 +145,8 @@ await this._fileCompressor.CompressDirectoryAsync(Environment.ExpandEnvironmentV
}

await this._fileCompressor.DecompressFileAsync(Environment.ExpandEnvironmentVariables(sourceFilePath),
Environment.ExpandEnvironmentVariables(destinationDirectoryPath),
context.CancellationToken);
Environment.ExpandEnvironmentVariables(destinationDirectoryPath),
context.CancellationToken);

return destinationDirectoryPath;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/dotnet/FileCompression/IFileCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.SemanticKernel.Skills.FileCompression;
namespace FileCompression;

/// <summary>
/// Interface for file compression / decompression.
Expand Down
2 changes: 1 addition & 1 deletion samples/dotnet/FileCompression/ZipFileCompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.SemanticKernel.Skills.FileCompression;
namespace FileCompression;

/// <summary>
/// Implementation of <see cref="IFileCompressor"/> that uses the Zip format.
Expand Down
4 changes: 2 additions & 2 deletions samples/dotnet/kernel-syntax-examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ public static async Task Main()

await Example20_HuggingFace.RunAsync();
Console.WriteLine("== DONE ==");

await Example21_ChatGptPlugins.RunAsync();
Console.WriteLine("== DONE ==");

await Example22_OpenApiSkill.RunAsync();
Console.WriteLine("== DONE ==");
}
Expand Down

0 comments on commit bc4dcfc

Please sign in to comment.