From 6c9f49b5da85c30ddcdc357b50f751948bb8fdb4 Mon Sep 17 00:00:00 2001
From: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
Date: Tue, 20 Jun 2023 17:15:31 +0100
Subject: [PATCH] Http consistency: Grpc skills (#1545)
### Description
The HTTP stack for Grpc skills aligned with other SK components enabling
the following features:
- using the internal SK HTTP client using provided configuration during
Kernel building.
- accepting a custom/external HTTP client, giving hosting
applications/client code the freedom to use their own instance.
### Contribution Checklist
- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [ ] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone :smile:
---
.../Extensions/KernelGrpcExtensions.cs | 23 ++++++++++++++-----
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/dotnet/src/Skills/Skills.Grpc/Extensions/KernelGrpcExtensions.cs b/dotnet/src/Skills/Skills.Grpc/Extensions/KernelGrpcExtensions.cs
index e358addcbbaf..580a6511afcf 100644
--- a/dotnet/src/Skills/Skills.Grpc/Extensions/KernelGrpcExtensions.cs
+++ b/dotnet/src/Skills/Skills.Grpc/Extensions/KernelGrpcExtensions.cs
@@ -27,8 +27,13 @@ public static class KernelGrpcExtensions
/// Semantic Kernel instance.
/// Directory containing the skill directory.
/// Name of the directory containing the selected skill.
+ /// HttpClient to use for sending requests.
/// A list of all the semantic functions representing the skill.
- public static IDictionary ImportGrpcSkillFromDirectory(this IKernel kernel, string parentDirectory, string skillDirectoryName)
+ public static IDictionary ImportGrpcSkillFromDirectory(
+ this IKernel kernel,
+ string parentDirectory,
+ string skillDirectoryName,
+ HttpClient? httpClient = null)
{
const string ProtoFile = "grpc.proto";
@@ -47,7 +52,7 @@ public static IDictionary ImportGrpcSkillFromDirectory(this
using var stream = File.OpenRead(filePath);
- return kernel.RegisterGrpcSkill(stream, skillDirectoryName);
+ return kernel.RegisterGrpcSkill(stream, skillDirectoryName, httpClient);
}
///
@@ -56,11 +61,13 @@ public static IDictionary ImportGrpcSkillFromDirectory(this
/// Semantic Kernel instance.
/// Name of the skill to register.
/// File path to .proto document.
+ /// HttpClient to use for sending requests.
/// A list of all the semantic functions representing the skill.
public static IDictionary ImportGrpcSkillFromFile(
this IKernel kernel,
string skillName,
- string filePath)
+ string filePath,
+ HttpClient? httpClient = null)
{
if (!File.Exists(filePath))
{
@@ -71,7 +78,7 @@ public static IDictionary ImportGrpcSkillFromFile(
using var stream = File.OpenRead(filePath);
- return kernel.RegisterGrpcSkill(stream, skillName);
+ return kernel.RegisterGrpcSkill(stream, skillName, httpClient);
}
///
@@ -80,11 +87,13 @@ public static IDictionary ImportGrpcSkillFromFile(
/// Semantic Kernel instance.
/// .proto document stream.
/// Skill name.
+ /// HttpClient to use for sending requests.
/// A list of all the semantic functions representing the skill.
public static IDictionary RegisterGrpcSkill(
this IKernel kernel,
Stream documentStream,
- string skillName)
+ string skillName,
+ HttpClient? httpClient = null)
{
Verify.NotNull(kernel);
Verify.ValidSkillName(skillName);
@@ -96,7 +105,9 @@ public static IDictionary RegisterGrpcSkill(
var skill = new Dictionary();
- var runner = new GrpcOperationRunner(new HttpClient());
+ var client = HttpClientProvider.GetHttpClient(kernel.Config, httpClient, kernel.Log);
+
+ var runner = new GrpcOperationRunner(client);
foreach (var operation in operations)
{