-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gRPC skills functionality #814
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
06096e6
MVP components for gRPC skills (#639)
SergeyMenshykh a02707a
Import gRPC skills (#730)
SergeyMenshykh fd1096a
Merge from main
02edc15
add Java sample skill (gRPC server) for gRPC operation runner (client…
thegovind e1df6c0
Merge from main
ef7d592
Fix for the latest, partially successful, merge of gRPC feature branc…
SergeyMenshykh 3eda5eb
Addressing merge PR comments (#824)
SergeyMenshykh a962a67
Merge main
ab57792
Merge branch 'main' into feature-grpc
shawncal a86bb44
Merge branch 'main' into feature-grpc
shawncal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) | ||
SergeyMenshykh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.