Skip to content

Commit 9be80f1

Browse files
.Net: Expose GeminiKernelFunctionMetadataExtensions (#12515)
### Motivation and Context `GeminiToolCallBehavior` exposes the method `EnableFunctions` to enable filtering which Kernel Functions are available for auto invoke. However, there's no way to construct a `GeminiFunction` instance since the constructor is internal. I saw there's an extension to build a `GeminiFunction` from a `KernelFunctionMetadata`, but it's under the test project. It seems that it was supposed to be exposed from the library, since there's also a test file for `KernelFunctionMetadata`. This way, it will be possible to use `GeminiToolCallBehavior.EnableFunctions` to explicitly specify which tools can be auto-called by the Google connector. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Move GeminiKernelFunctionMetadataExtensions from `SemanticKernel.Connectors.Google.UnitTests` to `Microsoft.SemanticKernel` namespace. - Remove `FunctionMetadataAsGeminiFunction` and start using `GeminiKernelFunctionMetadataExtensions` instead. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [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 😄
1 parent 55132da commit 9be80f1

File tree

2 files changed

+2
-42
lines changed

2 files changed

+2
-42
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

33
using System.Collections.Generic;
4-
using Microsoft.SemanticKernel;
54
using Microsoft.SemanticKernel.Connectors.Google;
65

7-
namespace SemanticKernel.Connectors.Google.UnitTests;
6+
namespace Microsoft.SemanticKernel;
87

98
/// <summary>
109
/// Extensions for <see cref="KernelFunctionMetadata"/> specific to the Gemini connector.

dotnet/src/Connectors/Connectors.Google/GeminiToolCallBehavior.cs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -127,50 +127,11 @@ internal override void ConfigureGeminiRequest(Kernel? kernel, GeminiRequest requ
127127
// Provide all functions from the kernel.
128128
foreach (var functionMetadata in kernel.Plugins.GetFunctionsMetadata())
129129
{
130-
request.AddFunction(FunctionMetadataAsGeminiFunction(functionMetadata));
130+
request.AddFunction(functionMetadata.ToGeminiFunction());
131131
}
132132
}
133133

134134
internal override bool AllowAnyRequestedKernelFunction => true;
135-
136-
/// <summary>
137-
/// Convert a <see cref="KernelFunctionMetadata"/> to an <see cref="GeminiFunction"/>.
138-
/// </summary>
139-
/// <param name="metadata">The <see cref="KernelFunctionMetadata"/> object to convert.</param>
140-
/// <returns>An <see cref="GeminiFunction"/> object.</returns>
141-
private static GeminiFunction FunctionMetadataAsGeminiFunction(KernelFunctionMetadata metadata)
142-
{
143-
IReadOnlyList<KernelParameterMetadata> metadataParams = metadata.Parameters;
144-
145-
var openAIParams = new GeminiFunctionParameter[metadataParams.Count];
146-
for (int i = 0; i < openAIParams.Length; i++)
147-
{
148-
var param = metadataParams[i];
149-
150-
openAIParams[i] = new GeminiFunctionParameter(
151-
param.Name,
152-
GetDescription(param),
153-
param.IsRequired,
154-
param.ParameterType,
155-
param.Schema);
156-
}
157-
158-
return new GeminiFunction(
159-
metadata.PluginName,
160-
metadata.Name,
161-
metadata.Description,
162-
openAIParams,
163-
new GeminiFunctionReturnParameter(
164-
metadata.ReturnParameter.Description,
165-
metadata.ReturnParameter.ParameterType,
166-
metadata.ReturnParameter.Schema));
167-
168-
static string GetDescription(KernelParameterMetadata param)
169-
{
170-
string? stringValue = InternalTypeConverter.ConvertToString(param.DefaultValue);
171-
return !string.IsNullOrEmpty(stringValue) ? $"{param.Description} (default value: {stringValue})" : param.Description;
172-
}
173-
}
174135
}
175136

176137
/// <summary>

0 commit comments

Comments
 (0)