Skip to content

Commit

Permalink
Merge with latest main
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMenshykh committed Jul 24, 2023
2 parents 64fdaa4 + f8b6e2a commit ef99d98
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Please help reviewers and future users, providing the following information:
<!-- 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#dev-scripts) raises no violations
- [ ] 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 :smile:
13 changes: 11 additions & 2 deletions .github/workflows/label-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@ jobs:
let labels = ["triage"]
// Check if the body or the title contains the word 'python' (case-insensitive)
if (body.match(/python/i) || title.match(/python/i)) {
if ((body != null && body.match(/python/i)) || (title != null && title.match(/python/i))) {
// Add the 'python' label to the array
labels.push("python")
}
// Check if the body or the title contains the word 'java' (case-insensitive)
if (body.match(/java/i) || title.match(/java/i)) {
if ((body != null && body.match(/java/i)) || (title != null && title.match(/java/i))) {
// Add the 'java' label to the array
labels.push("java")
}
// Check if the body or the title contains the words 'dotnet', '.net', 'c#' or 'csharp' (case-insensitive)
if ((body != null && body.match(/.net/i)) || (title != null && title.match(/.net/i)) ||
(body != null && body.match(/dotnet/i)) || (title != null && title.match(/dotnet/i)) ||
(body != null && body.match(/C#/i)) || (title != null && title.match(/C#/i)) ||
(body != null && body.match(/csharp/i)) || (title != null && title.match(/csharp/i))) {
// Add the '.NET' label to the array
labels.push(".NET")
}
// Add the labels to the issue
github.rest.issues.addLabels({
Expand Down
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<!-- Skills -->
<PackageVersion Include="DocumentFormat.OpenXml" Version="2.20.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="7.0.8" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="7.0.9" />
<PackageVersion Include="DuckDB.NET.Data.Full" Version="0.8.1" />
<PackageVersion Include="DuckDB.NET.Data" Version="0.8.1" />
<PackageVersion Include="Microsoft.Graph" Version="[4.51.0, 5)" />
Expand Down
2 changes: 1 addition & 1 deletion dotnet/nuget/nuget-package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<!-- Central version prefix - applies to all nuget packages. -->
<Version>0.17</Version>
<Version>0.18</Version>

<Configurations>Debug;Release;Publish</Configurations>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private void PopulateList(StringBuilder list, IDictionary<string, List<FunctionV
// Function parameters
foreach (var p in func.Parameters)
{
var description = string.IsNullOrEmpty(p.Description) ? p.Name : p.Description;
var description = string.IsNullOrEmpty(p.Description) ? p.Name : p.Description!;
var defaultValueString = string.IsNullOrEmpty(p.DefaultValue) ? string.Empty : $" (default value: {p.DefaultValue})";
list.AppendLine($"Parameter \"{p.Name}\": {AddPeriod(description)} {defaultValueString}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,12 @@ public static async Task<IOrderedEnumerable<FunctionView>> GetAvailableFunctions
string? semanticQuery = null,
CancellationToken cancellationToken = default)
{
var excludedSkills = config.ExcludedSkills ?? new();
var excludedFunctions = config.ExcludedFunctions ?? new();
var includedFunctions = config.IncludedFunctions ?? new();

if (context.Skills == null)
{
throw new SKException("Skill collection not found in the context");
}

var functionsView = context.Skills.GetFunctionsView();

var availableFunctions = functionsView.SemanticFunctions
.Concat(functionsView.NativeFunctions)
.SelectMany(x => x.Value)
.Where(s => !excludedSkills.Contains(s.SkillName) && !excludedFunctions.Contains(s.Name))
.Where(s => !config.ExcludedSkills.Contains(s.SkillName) && !config.ExcludedFunctions.Contains(s.Name))
.ToList();

List<FunctionView>? result = null;
Expand Down Expand Up @@ -103,7 +94,7 @@ public static async Task<IOrderedEnumerable<FunctionView>> GetAvailableFunctions
result.AddRange(await GetRelevantFunctionsAsync(context, availableFunctions, memories, cancellationToken).ConfigureAwait(false));

// Add any missing functions that were included but not found in the search results.
var missingFunctions = includedFunctions
var missingFunctions = config.IncludedFunctions
.Except(result.Select(x => x.Name))
.Join(availableFunctions, f => f, af => af.Name, (_, af) => af);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ISemanticTextMemory Memory
/// <summary>
/// Read only skills collection
/// </summary>
public IReadOnlySkillCollection? Skills { get; internal set; }
public IReadOnlySkillCollection Skills { get; }

/// <summary>
/// Access registered functions by skill + name. Not case sensitive.
Expand All @@ -120,11 +120,6 @@ public ISemanticTextMemory Memory
/// <returns>Delegate to execute the function</returns>
public ISKFunction Func(string skillName, string functionName)
{
if (this.Skills is null)
{
throw new SKException("Skill collection not found in the context");
}

return this.Skills.GetFunction(skillName, functionName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.SemanticKernel.SkillDefinition;
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class ParameterView
{
private string _name = "";
private string _name = string.Empty;

/// <summary>
/// Parameter name. Alphanumeric chars + "_" only.
Expand All @@ -30,12 +30,12 @@ public string Name
/// <summary>
/// Parameter description.
/// </summary>
public string Description { get; set; } = string.Empty;
public string? Description { get; set; }

/// <summary>
/// Default value when the value is not provided.
/// </summary>
public string DefaultValue { get; set; } = string.Empty;
public string? DefaultValue { get; set; }

/// <summary>
/// Constructor
Expand All @@ -52,11 +52,9 @@ public ParameterView()
/// <param name="defaultValue">Default parameter value, if not provided</param>
public ParameterView(
string name,
string description,
string defaultValue)
string? description = null,
string? defaultValue = null)
{
Verify.ValidFunctionParamName(name);

this.Name = name;
this.Description = description;
this.DefaultValue = defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,4 @@ Task<string> RenderAsync(
string templateText,
SKContext context,
CancellationToken cancellationToken = default);

/// <summary>
/// Given a list of blocks render each block and compose the final result
/// </summary>
/// <param name="blocks">Template blocks generated by ExtractBlocks</param>
/// <param name="context">Access into the current kernel execution context</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The prompt template ready to be used for an AI request</returns>
Task<string> RenderAsync(
IList<Block> blocks,
SKContext context,
CancellationToken cancellationToken = default);

/// <summary>
/// Given a list of blocks, render the Variable Blocks, replacing placeholders with the actual value in memory
/// </summary>
/// <param name="blocks">List of blocks, typically all the blocks found in a template</param>
/// <param name="variables">Container of all the temporary variables known to the kernel</param>
/// <returns>An updated list of blocks where Variable Blocks have rendered to Text Blocks</returns>
IList<Block> RenderVariables(
IList<Block> blocks,
ContextVariables? variables);
}
48 changes: 12 additions & 36 deletions dotnet/src/SemanticKernel/SemanticFunctions/PromptTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.TemplateEngine;
Expand All @@ -22,9 +20,6 @@ public sealed class PromptTemplate : IPromptTemplate
private readonly string _template;
private readonly IPromptTemplateEngine _templateEngine;

// ReSharper disable once NotAccessedField.Local
private readonly ILogger _log = NullLogger.Instance;

// ReSharper disable once NotAccessedField.Local
private readonly PromptTemplateConfig _promptConfig;

Expand All @@ -35,7 +30,7 @@ public sealed class PromptTemplate : IPromptTemplate
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
/// <param name="kernel">Kernel in which template is to take effect.</param>
public PromptTemplate(string template, PromptTemplateConfig promptTemplateConfig, IKernel kernel)
: this(template, promptTemplateConfig, kernel.PromptTemplateEngine, kernel.Log)
: this(template, promptTemplateConfig, kernel.PromptTemplateEngine)
{
}

Expand All @@ -45,17 +40,14 @@ public PromptTemplate(string template, PromptTemplateConfig promptTemplateConfig
/// <param name="template">Template.</param>
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
/// <param name="promptTemplateEngine">Prompt template engine.</param>
/// <param name="log">Optional logger for prompt template.</param>
public PromptTemplate(
string template,
PromptTemplateConfig promptTemplateConfig,
IPromptTemplateEngine promptTemplateEngine,
ILogger? log = null)
IPromptTemplateEngine promptTemplateEngine)
{
this._template = template;
this._templateEngine = promptTemplateEngine;
this._promptConfig = promptTemplateConfig;
if (log != null) { this._log = log; }
}

/// <summary>
Expand All @@ -65,40 +57,24 @@ public PromptTemplate(
/// <returns>List of parameters</returns>
public IList<ParameterView> GetParameters()
{
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

// Parameters from config.json
List<ParameterView> result = new();
foreach (PromptTemplateConfig.InputParameter? p in this._promptConfig.Input.Parameters)
Dictionary<string, ParameterView> result = new(StringComparer.OrdinalIgnoreCase);
foreach (var p in this._promptConfig.Input.Parameters)
{
if (p == null) { continue; }

result.Add(new ParameterView
{
Name = p.Name,
Description = p.Description,
DefaultValue = p.DefaultValue
});

seen.Add(p.Name);
result[p.Name] = new ParameterView(p.Name, p.Description, p.DefaultValue);
}

// Parameters from the template
List<VarBlock> listFromTemplate = this._templateEngine.ExtractBlocks(this._template)
.Where(x => x.Type == BlockTypes.Variable)
.Select(x => (VarBlock)x)
.Where(x => x != null)
.ToList();

foreach (VarBlock x in listFromTemplate)
foreach (var block in this._templateEngine.ExtractBlocks(this._template))
{
if (seen.Contains(x.Name)) { continue; }

result.Add(new ParameterView { Name = x.Name });
seen.Add(x.Name);
string? blockName = (block as VarBlock)?.Name;
if (!string.IsNullOrEmpty(blockName) && !result.ContainsKey(blockName!))
{
result.Add(blockName!, new ParameterView(blockName!));
}
}

return result;
return result.Values.ToList();
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ public FunctionView Describe()
/// <inheritdoc/>
public async Task<SKContext> InvokeAsync(SKContext context, CompleteRequestSettings? settings = null, CancellationToken cancellationToken = default)
{
// If the function is invoked manually, the user might have left out the skill collection
context.Skills ??= this._skillCollection;

if (this.IsSemantic)
{
this.AddDefaultValues(context.Variables);
Expand Down
19 changes: 15 additions & 4 deletions dotnet/src/SemanticKernel/TemplateEngine/PromptTemplateEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ public async Task<string> RenderAsync(string templateText, SKContext context, Ca
return await this.RenderAsync(blocks, context, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc/>
public async Task<string> RenderAsync(IList<Block> blocks, SKContext context, CancellationToken cancellationToken = default)
/// <summary>
/// Given a list of blocks render each block and compose the final result
/// </summary>
/// <param name="blocks">Template blocks generated by ExtractBlocks</param>
/// <param name="context">Access into the current kernel execution context</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>The prompt template ready to be used for an AI request</returns>
internal async Task<string> RenderAsync(IList<Block> blocks, SKContext context, CancellationToken cancellationToken = default)
{
this._log.LogTrace("Rendering list of {0} blocks", blocks.Count);
var tasks = new List<Task<string>>(blocks.Count);
Expand Down Expand Up @@ -99,8 +105,13 @@ public async Task<string> RenderAsync(IList<Block> blocks, SKContext context, Ca
return result.ToString();
}

/// <inheritdoc/>
public IList<Block> RenderVariables(IList<Block> blocks, ContextVariables? variables)
/// <summary>
/// Given a list of blocks, render the Variable Blocks, replacing placeholders with the actual value in memory
/// </summary>
/// <param name="blocks">List of blocks, typically all the blocks found in a template</param>
/// <param name="variables">Container of all the temporary variables known to the kernel</param>
/// <returns>An updated list of blocks where Variable Blocks have rendered to Text Blocks</returns>
internal IList<Block> RenderVariables(IList<Block> blocks, ContextVariables? variables)
{
this._log.LogTrace("Rendering variables");
return blocks.Select(block => block.Type != BlockTypes.Variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ async Task<SKContext> ExecuteAsync(SKContext context)
{
Name = p.AlternativeName ?? p.Name,
Description = $"{p.Description ?? p.Name}{(p.IsRequired ? " (required)" : string.Empty)}",
DefaultValue = p.DefaultValue ?? string.Empty
DefaultValue = p.DefaultValue
})
.ToList();

Expand Down
Loading

0 comments on commit ef99d98

Please sign in to comment.