-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,703 additions
and
12 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
31 changes: 31 additions & 0 deletions
31
dotnet/src/Skills/Skills.Grpc/Extensions/GrpcOperationExtensions.cs
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,31 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.SemanticKernel.SkillDefinition; | ||
using Microsoft.SemanticKernel.Skills.Grpc.Model; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.SemanticKernel.Skills.Grpc.Extensions; | ||
|
||
/// <summary> | ||
/// Class for extensions methods for the <see cref="GrpcOperation"/> class. | ||
/// </summary> | ||
internal static class GrpcOperationExtensions | ||
{ | ||
/// <summary> | ||
/// Returns list of gRPC operation parameters. | ||
/// </summary> | ||
/// <returns>The list of parameters.</returns> | ||
public static IReadOnlyList<ParameterView> GetParameters(this GrpcOperation operation) | ||
{ | ||
var parameters = new List<ParameterView>(); | ||
|
||
//Register the "address" parameter so that it's possible to override it if needed. | ||
parameters.Add(new ParameterView(GrpcOperation.AddressArgumentName, "Address for gRPC channel to use.", string.Empty)); | ||
|
||
//Register the "payload" parameter to be used as gRPC operation request message. | ||
parameters.Add(new ParameterView(GrpcOperation.PayloadArgumentName, "gRPC request message.", string.Empty)); | ||
|
||
return parameters; | ||
} | ||
} |
191 changes: 191 additions & 0 deletions
191
dotnet/src/Skills/Skills.Grpc/Extensions/KernelGrpcExtensions.cs
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,191 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel.Diagnostics; | ||
using Microsoft.SemanticKernel.Orchestration; | ||
using Microsoft.SemanticKernel.SkillDefinition; | ||
using Microsoft.SemanticKernel.Skills.Grpc.Model; | ||
using Microsoft.SemanticKernel.Skills.Grpc.Protobuf; | ||
|
||
namespace Microsoft.SemanticKernel.Skills.Grpc.Extensions; | ||
|
||
/// <summary> | ||
/// <see cref="IKernel"/> extensions methods for gRPC functionality. | ||
/// </summary> | ||
public static class KernelGrpcExtensions | ||
{ | ||
/// <summary> | ||
/// Imports gRPC document from a directory. | ||
/// </summary> | ||
/// <param name="kernel">Semantic Kernel instance.</param> | ||
/// <param name="parentDirectory">Directory containing the skill directory.</param> | ||
/// <param name="skillDirectoryName">Name of the directory containing the selected skill.</param> | ||
/// <returns>A list of all the semantic functions representing the skill.</returns> | ||
public static IDictionary<string, ISKFunction> ImportGrpcSkillFromDirectory(this IKernel kernel, string parentDirectory, string skillDirectoryName) | ||
{ | ||
const string PROTO_FILE = "grpc.proto"; | ||
|
||
Verify.ValidSkillName(skillDirectoryName); | ||
|
||
var skillDir = Path.Combine(parentDirectory, skillDirectoryName); | ||
Verify.DirectoryExists(skillDir); | ||
|
||
var filePath = Path.Combine(skillDir, PROTO_FILE); | ||
if (!File.Exists(filePath)) | ||
{ | ||
throw new FileNotFoundException($"No .proto document for the specified path - {filePath} is found."); | ||
} | ||
|
||
kernel.Log.LogTrace("Registering gRPC functions from {0} .proto document.", filePath); | ||
|
||
using var stream = File.OpenRead(filePath); | ||
|
||
return kernel.RegisterGrpcSkill(stream, skillDirectoryName); | ||
} | ||
|
||
/// <summary> | ||
/// Imports gRPC document from a file. | ||
/// </summary> | ||
/// <param name="kernel">Semantic Kernel instance.</param> | ||
/// <param name="skillName">Name of the skill to register.</param> | ||
/// <param name="filePath">File path to .proto document.</param> | ||
/// <returns>A list of all the semantic functions representing the skill.</returns> | ||
public static IDictionary<string, ISKFunction> ImportGrpcSkillFromFile( | ||
this IKernel kernel, | ||
string skillName, | ||
string filePath) | ||
{ | ||
if (!File.Exists(filePath)) | ||
{ | ||
throw new FileNotFoundException($"No .proto document for the specified path - {filePath} is found."); | ||
} | ||
|
||
kernel.Log.LogTrace("Registering gRPC functions from {0} .proto document.", filePath); | ||
|
||
using var stream = File.OpenRead(filePath); | ||
|
||
return kernel.RegisterGrpcSkill(stream, skillName); | ||
} | ||
|
||
/// <summary> | ||
/// Registers an gRPC skill. | ||
/// </summary> | ||
/// <param name="kernel">Semantic Kernel instance.</param> | ||
/// <param name="documentStream">.proto document stream.</param> | ||
/// <param name="skillName">Skill name.</param> | ||
/// <returns>A list of all the semantic functions representing the skill.</returns> | ||
public static IDictionary<string, ISKFunction> RegisterGrpcSkill( | ||
this IKernel kernel, | ||
Stream documentStream, | ||
string skillName) | ||
{ | ||
Verify.NotNull(kernel, nameof(kernel)); | ||
Verify.ValidSkillName(skillName); | ||
|
||
// Parse | ||
var parser = new ProtoDocumentParser(); | ||
|
||
var operations = parser.Parse(documentStream, skillName); | ||
|
||
var skill = new Dictionary<string, ISKFunction>(); | ||
|
||
var runner = new GrpcOperationRunner(new HttpClient()); | ||
|
||
foreach (var operation in operations) | ||
{ | ||
try | ||
{ | ||
kernel.Log.LogTrace("Registering gRPC function {0}.{1}", skillName, operation.Name); | ||
var function = kernel.RegisterGrpcFunction(runner, skillName, operation); | ||
skill[function.Name] = function; | ||
} | ||
catch (Exception ex) when (!ex.IsCriticalException()) | ||
{ | ||
//Logging the exception and keep registering other gRPC functions | ||
kernel.Log.LogWarning(ex, "Something went wrong while rendering the gRPC function. Function: {0}.{1}. Error: {2}", | ||
skillName, operation.Name, ex.Message); | ||
} | ||
} | ||
|
||
return skill; | ||
} | ||
|
||
#region private | ||
|
||
/// <summary> | ||
/// Registers SKFunction for a gRPC operation. | ||
/// </summary> | ||
/// <param name="kernel">Semantic Kernel instance.</param> | ||
/// <param name="runner">gRPC operation runner.</param> | ||
/// <param name="skillName">Skill name.</param> | ||
/// <param name="operation">The gRPC operation.</param> | ||
/// <returns>An instance of <see cref="SKFunction"/> class.</returns> | ||
private static ISKFunction RegisterGrpcFunction( | ||
this IKernel kernel, | ||
GrpcOperationRunner runner, | ||
string skillName, | ||
GrpcOperation operation) | ||
{ | ||
var operationParameters = operation.GetParameters(); | ||
|
||
async Task<SKContext> ExecuteAsync(SKContext context) | ||
{ | ||
try | ||
{ | ||
var arguments = new Dictionary<string, string>(); | ||
|
||
//Extract function arguments from context | ||
foreach (var parameter in operationParameters) | ||
{ | ||
//A try to resolve argument parameter name. | ||
if (context.Variables.Get(parameter.Name, out var value)) | ||
{ | ||
arguments.Add(parameter.Name, value); | ||
continue; | ||
} | ||
|
||
throw new KeyNotFoundException($"No variable found in context to use as an argument for the '{parameter.Name}' parameter of the '{skillName}.{operation.Name}' gRPC function."); | ||
} | ||
|
||
//SKFunction should be extended to pass cancellation token for delegateFunction calls. | ||
var result = await runner.RunAsync(operation, arguments, CancellationToken.None).ConfigureAwait(false); | ||
|
||
if (result != null) | ||
{ | ||
context.Variables.Update(result.ToString()); | ||
} | ||
} | ||
catch (Exception ex) when (!ex.IsCriticalException()) | ||
{ | ||
kernel.Log.LogWarning(ex, "Something went wrong while rendering the gRPC function. Function: {0}.{1}. Error: {2}", skillName, operation.Name, | ||
ex.Message); | ||
context.Fail(ex.Message, ex); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
#pragma warning disable CA2000 // Dispose objects before losing scope. | ||
var function = new SKFunction( | ||
delegateType: SKFunction.DelegateTypes.ContextSwitchInSKContextOutTaskSKContext, | ||
delegateFunction: ExecuteAsync, | ||
parameters: operationParameters.ToList(), | ||
description: operation.Name, | ||
skillName: skillName, | ||
functionName: operation.Name, | ||
isSemantic: false, | ||
log: kernel.Log); | ||
#pragma warning restore CA2000 // Dispose objects before losing scope | ||
|
||
return kernel.RegisterCustomFunction(skillName, function); | ||
} | ||
|
||
#endregion | ||
} |
Oops, something went wrong.