Skip to content
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

Upgrade repo to semantickernel 19.2 #13

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="7.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.17.230711.7-preview" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

Expand Down
17 changes: 11 additions & 6 deletions samples/008-dotnet-nl2sql/nl2sql.console/Nl2SqlConsole.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
using System;

namespace SemanticKernel.Data.Nl2Sql;

using System;
Expand All @@ -11,14 +13,12 @@ namespace SemanticKernel.Data.Nl2Sql;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
using SemanticKernel.Data.Nl2Sql.Library;
using SemanticKernel.Data.Nl2Sql.Library.Schema;

internal sealed class Nl2SqlConsole : BackgroundService
{
private const ConsoleColor FocusColor = ConsoleColor.Yellow;
private const ConsoleColor PromptColor = ConsoleColor.Gray;
private const ConsoleColor QueryColor = ConsoleColor.Green;
private const ConsoleColor SystemColor = ConsoleColor.Cyan;

Expand Down Expand Up @@ -67,7 +67,9 @@ await SchemaProvider.InitializeAsync(
continue;
}

#pragma warning disable CA2016 // CancellationToken overload obsolete
var context = this.kernel.CreateNewContext();
#pragma warning restore CA2016
var query =
await this.queryGenerator.SolveObjectiveAsync(
objective,
Expand Down Expand Up @@ -95,7 +97,7 @@ await this.queryGenerator.SolveObjectiveAsync(
this.WriteLine(FocusColor, "Cancellation detected...");

// Yield to sync stoppingToken state
await Task.Delay(TimeSpan.FromMilliseconds(300));
await Task.Delay(TimeSpan.FromMilliseconds(300), stoppingToken).ConfigureAwait(false);
}
else if (string.IsNullOrWhiteSpace(objective))
{
Expand Down Expand Up @@ -131,7 +133,7 @@ async Task ProcessQueryAsync(string? schema, string? query)
return;
}

await Task.Delay(300).ConfigureAwait(false); // Human feedback window
await Task.Delay(300, stoppingToken).ConfigureAwait(false); // Human feedback window

this.ClearLine();
this.Write(SystemColor, "Executing...");
Expand All @@ -154,9 +156,12 @@ async Task ProcessDataAsync(string schema, string query, Action<IDataReader> cal
{
using var connection = await this.sqlProvider.ConnectAsync(schema).ConfigureAwait(false);
using var command = connection.CreateCommand();

#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
command.CommandText = query;
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);
using var reader = await command.ExecuteReaderAsync(stoppingToken).ConfigureAwait(false);
callback.Invoke(reader);
}
#pragma warning disable CA1031 // Do not catch general exception types
Expand Down Expand Up @@ -264,7 +269,7 @@ string TrimValue(string? value, int width)
return value;
}

return value.Substring(0, width - 4) + "...";
return string.Concat(value.AsSpan(0, width - 4), "...");
}

void WriteSeparator(int[] widths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ private async Task<SqlDataReader> ExecuteQueryAsync(string statement)
{
using var cmd = this.connection.CreateCommand();

#pragma warning disable CA2100 // Queries passed in from static resource
cmd.CommandText = statement;
#pragma warning restore CA2100

return await cmd.ExecuteReaderAsync().ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ public static string GetResult(this SKContext context, string? label = null)

if (context.ErrorOccurred)
{
if (context.LastException != null)
{
throw new InvalidDataException("No result available due to an unexpected failure.", context.LastException);
}

throw new InvalidDataException(context.LastErrorDescription);
throw new InvalidDataException("No result available due to an unexpected failure.", context.LastException);
}

var result = context.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.17.230711.7-preview" />
<PackageReference Include="Microsoft.SemanticKernel" Version="0.19.230804.2-preview" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

Expand Down
13 changes: 8 additions & 5 deletions samples/008-dotnet-nl2sql/nl2sql.library/SqlQueryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace SemanticKernel.Data.Nl2Sql.Library;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using SemanticKernel.Data.Nl2Sql.Library.Internal;
Expand All @@ -31,12 +32,14 @@ public sealed class SqlQueryGenerator

private readonly ISKFunction promptEval;
private readonly ISKFunction promptGenerator;
private readonly ISemanticTextMemory memory;

public SqlQueryGenerator(IKernel kernel, string rootSkillFolder)
{
var functions = kernel.ImportSemanticSkillFromDirectory(rootSkillFolder, SkillName);
this.promptEval = functions["isquery"];
this.promptGenerator = functions["generatequery"];
this.memory = kernel.Memory;

kernel.ImportSkill(this, SkillName);
}
Expand All @@ -53,7 +56,7 @@ public SqlQueryGenerator(IKernel kernel, string rootSkillFolder)
{
// Search for schema with best similarity match to the objective
var recall =
await context.Memory.SearchAsync(
await this.memory.SearchAsync(
SchemaProvider.MemoryCollectionName,
objective,
limit: 1,
Expand All @@ -70,10 +73,10 @@ await context.Memory.SearchAsync(
var schemaText = best.Metadata.Text;
var sqlPlatform = best.Metadata.AdditionalMetadata;

context[ContextParamObjective] = objective;
context[ContextParamSchema] = schemaText;
context[ContextParamSchemaId] = schemaName;
context[ContextParamPlatform] = sqlPlatform;
context.Variables[ContextParamObjective] = objective;
context.Variables[ContextParamSchema] = schemaText;
context.Variables[ContextParamSchemaId] = schemaName;
context.Variables[ContextParamPlatform] = sqlPlatform;

// Screen objective to determine if it can be solved with the selected schema.
if (!await this.ScreenObjectiveAsync(context).ConfigureAwait(false))
Expand Down
5 changes: 0 additions & 5 deletions samples/008-dotnet-nl2sql/run.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ setlocal

cd %~dp0/nl2sql.console

dotnet restore
dotnet build

cls

dotnet run
5 changes: 0 additions & 5 deletions samples/008-dotnet-nl2sql/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ set -e

cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/nl2sql.console"

dotnet restore
dotnet build

tput reset

dotnet run