Skip to content

Commit 40b0af6

Browse files
.Net: Some fixes to clean up after previous PR's (#3745)
### Motivation and Context <!-- 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! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] 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 - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
1 parent c765cb1 commit 40b0af6

File tree

3 files changed

+51
-83
lines changed

3 files changed

+51
-83
lines changed

dotnet/src/Extensions/TemplateEngine.Handlebars/HandlebarsPromptTemplate.cs

-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using System;
43
using System.Collections.Generic;
54
using System.Threading;
65
using System.Threading.Tasks;
@@ -23,7 +22,6 @@ public HandlebarsPromptTemplate(PromptTemplateConfig promptConfig, ILoggerFactor
2322
this._loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
2423
this._logger = this._loggerFactory.CreateLogger(typeof(HandlebarsPromptTemplate));
2524
this._promptModel = promptConfig;
26-
this._parameters = new(() => this.InitParameters());
2725
}
2826

2927
/// <inheritdoc/>
@@ -54,22 +52,6 @@ public async Task<string> RenderAsync(Kernel kernel, ContextVariables variables,
5452
private readonly ILoggerFactory _loggerFactory;
5553
private readonly ILogger _logger;
5654
private readonly PromptTemplateConfig _promptModel;
57-
private readonly Lazy<IReadOnlyList<KernelParameterMetadata>> _parameters;
58-
59-
private List<KernelParameterMetadata> InitParameters()
60-
{
61-
List<KernelParameterMetadata> parameters = new(this._promptModel.InputParameters.Count);
62-
foreach (var p in this._promptModel.InputParameters)
63-
{
64-
parameters.Add(new KernelParameterMetadata(p.Name)
65-
{
66-
Description = p.Description,
67-
DefaultValue = p.DefaultValue
68-
});
69-
}
70-
71-
return parameters;
72-
}
7355

7456
private Dictionary<string, string> GetVariables(ContextVariables variables)
7557
{
@@ -89,7 +71,5 @@ private Dictionary<string, string> GetVariables(ContextVariables variables)
8971

9072
return result;
9173
}
92-
9374
#endregion
94-
9575
}

dotnet/src/InternalUtilities/planning/Extensions/KernelFunctionMetadataExtensions.cs

+51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System;
4+
using System.Collections.Generic;
35
using System.Linq;
46

57
#pragma warning disable IDE0130
@@ -11,6 +13,55 @@ namespace Microsoft.SemanticKernel.Planning;
1113
/// </summary>
1214
internal static class KernelFunctionMetadataExtensions
1315
{
16+
private const string SuccessfulResponseCode = "200";
17+
private const string SuccessfulResponseDescription = "Success";
18+
19+
/// <summary>
20+
/// Creates a <see cref="JsonSchemaFunctionView"/> for a function.
21+
/// </summary>
22+
/// <param name="function">The function.</param>
23+
/// <param name="jsonSchemaDelegate">A delegate that creates a JSON Schema from a <see cref="Type"/> and a semantic description.</param>
24+
/// <param name="includeOutputSchema">Indicates if the schema should include information about the output or return type of the function.</param>
25+
/// <returns>An instance of <see cref="JsonSchemaFunctionView"/></returns>
26+
public static JsonSchemaFunctionView ToJsonSchemaFunctionView(this KernelFunctionMetadata function, Func<Type?, string?, KernelJsonSchema?> jsonSchemaDelegate, bool includeOutputSchema = true)
27+
{
28+
var functionView = new JsonSchemaFunctionView
29+
{
30+
Name = $"{function.PluginName}_{function.Name}",
31+
Description = function.Description,
32+
};
33+
34+
var requiredProperties = new List<string>();
35+
foreach (var parameter in function.Parameters)
36+
{
37+
var schema = parameter.Schema ?? jsonSchemaDelegate(parameter.ParameterType, parameter.Description);
38+
if (schema is not null)
39+
{
40+
functionView.Parameters.Properties.Add(parameter.Name, schema);
41+
}
42+
if (parameter.IsRequired)
43+
{
44+
requiredProperties.Add(parameter.Name);
45+
}
46+
}
47+
48+
if (includeOutputSchema)
49+
{
50+
var functionResponse = new JsonSchemaFunctionResponse
51+
{
52+
Description = SuccessfulResponseDescription
53+
};
54+
55+
var schema = function.ReturnParameter.Schema ?? jsonSchemaDelegate(function.ReturnParameter.ParameterType, SuccessfulResponseDescription);
56+
functionResponse.Content.JsonResponse.Schema = schema;
57+
58+
functionView.FunctionResponses.Add(SuccessfulResponseCode, functionResponse);
59+
}
60+
61+
functionView.Parameters.Required = requiredProperties;
62+
return functionView;
63+
}
64+
1465
/// <summary>
1566
/// Create a manual-friendly string for a function.
1667
/// </summary>

dotnet/src/InternalUtilities/planning/Schema/KernelFunctionMetadataExtensions.cs

-63
This file was deleted.

0 commit comments

Comments
 (0)