雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

.NET自然语言转换为SQL的Nl2Sql项目

2024-07-26 42

随着技术的发展,人工智能在各个领域的应用已经不再是新鲜事。在数据库查询领域能够自然地将人类语言转换为SQL语句将为不懂技术的人士提供极大的便捷,同时也能大幅提高专业开发者的工作效率。今天,我带大家深入了解一个非常有趣的项目——Nl2Sql,这个项目是基于.NET平台和Semantic Kernel的工具,它可以将自然语言转换为SQL查询语句。

Nl2Sql GitHub地址:https://github.com/microsoft/kernel-memory/tree/NL2SQL/examples/200-dotnet-nl2sql

GPT-4的出现,使得基于自然语言处理的技术跨越了一个新的门槛,尤其是在自然语言转换成SQL语句的能力上有了显著提升。Nl2Sql工具就是利用GPT-4和Semantic Kernel的强大功能,为我们提供了一个实验和测试平台,能够基于自然语言表达生成SQL查询语句。

项目结构与样例信息

在开源的Nl2Sql项目中,我们可以看到以下几个组成部分:

nl2sql.config - 包含了设置说明、数据模式和语义提示。 nl2sql.console - 控制台应用,用于将自然语言目标转换成SQL查询。 nl2sql.library - 支持库,同样用于自然语言到SQL的转换。 nl2sql.harness - 开发调试工具,用于实时逆向工程化数据库模式。 nl2sql.sln - Visual Studio解决方案文件。

运行样例的第一步是进行初始设置和配置。

核心方法解析

我们来看一看Nl2Sql功能的实现关键。该程序采用.NET的开发框架,通过一系列方法来完成从输入的自然语言到输出的SQL查询的转换。这个过程涉及到自然语言理解、数据库模式解析和查询生成等环节。

private async Task ExecuteConsoleAsync(CancellationToken stoppingToken)
{
 var schemaNames = SchemaDefinitions.GetNames().ToArray();
 await SchemaProvider.InitializeAsync(
 this._memory,
 schemaNames.Select(s => Path.Combine(Repo.RootConfigFolder, "schema", $"{s}.json"))).ConfigureAwait(false);

 this.WriteIntroduction(schemaNames);

 while (!stoppingToken.IsCancellationRequested)
 {
 var objective = await ReadInputAsync().ConfigureAwait(false);
 if (string.IsOrWhiteSpace(objective))
 {
 continue;
 }

 var result =
 await this._queryGenerator.SolveObjectiveAsync(objective).ConfigureAwait(false);

 await ProcessQueryAsync(result).ConfigureAwait(false);
 }

 this.WriteLine();

 // Capture console input with cancellation detection
 async Task<string?> ReadInputAsync()
 {
 this.Write(SystemColor, "# ");

 var inputTask = Console.In.ReadLineAsync(stoppingToken).AsTask();
 var objective = await inputTask.ConfigureAwait(false);

 // response occurs when blocking input is cancelled (CTRL+C)
 if ( == objective)
 {
 this.WriteLine();
 this.WriteLine(FocusColor, "Cancellation detected...");

 // Yield to sync stoppingToken state
 await Task.Delay(TimeSpan.FromMilliseconds(300), stoppingToken).ConfigureAwait(false);
 }
 else if (string.IsOrWhiteSpace(objective))
 {
 this.WriteLine(FocusColor, $"Please provide a query related to the defined schemas.{Environment.NewLine}");
 }
 else
 {
 this.ClearLine(previous: true);
 this.WriteLine(QueryColor, $"# {objective}");
 }

 return objective;
 }

 // Display query result and (optionally) execute.
 async Task ProcessQueryAsync(SqlQueryResult? result)
 {
 if (result == )
 {
 this.WriteLine(FocusColor, $"Unable to translate request into a query.{Environment.NewLine}");
 return;
 }

 this.WriteLine(SystemColor, $"{Environment.NewLine}SCHEMA:");
 this.WriteLine(QueryColor, result.Schema);
 this.WriteLine(SystemColor, $"{Environment.NewLine}QUERY:");
 this.WriteLine(QueryColor, result.Query);

 if (!this.Confirm($"{Environment.NewLine}Execute?"))
 {
 this.WriteLine();
 this.WriteLine();
 return;
 }

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

 this.ClearLine();
 this.Write(SystemColor, "Executing...");

 await ProcessDataAsync(
 result.Schema,
 result.Query,
 reader =>
 {
 this.ClearLine();
 this.WriteData(reader);
 }).ConfigureAwait(false);
 }

 // Execute query and display the resulting data-set.
 async Task ProcessDataAsync(string schema, string query, Action<IDataReader> callback)
 {
 try
 {
 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(stoppingToken).ConfigureAwait(false);
 callback.Invoke(reader);
 }
#pragma warning disable CA1031 // Do not catch general exception types
 catch (Exception exception)
#pragma warning restore CA1031 // Do not catch general exception types
 {
 this.ClearLine();
 this.WriteLine(FocusColor, exception.Message);
 }
 }
}

代码结构非常清晰,主要包含数据读取、SQL命令执行、命令行输入处理和数据展示等功能。整个转换过程使用了异步编程模式,保证了良好的用户交互体验和程序性能。

用户交互设计

在用户交互方面,该程序展现了良好的设计。用户可以在控制台中输入自然语言描述的目标,程序会解析这一描述并生成对应的SQL查询。如果成功生成,还会提供是否执行查询的选项,在用户确认后,能够将查询结果以格式化的方式显示在控制台上。

数据展示和交互

在数据展示方面,开发人员精心设计了一整套显示系统,能够将查询结果分页显示,同时考虑到了窗口宽度和数据字段宽度的问题,确保了数据显示的友好性和阅读性。

private void WriteData(IDataReader reader)
{
 int maxPage = Console.WindowHeight - 10;

 var widths = GetWidths().ToArray();
 var isColumnTruncation = widths.Length < reader.FieldCount;
 var rowFormatter = string.Join('│', widths.Select((width, index) => width == -1 ? $"{{{index}}}" : $"{{{index},-{width}}}"));

 if (isColumnTruncation)
 {
 rowFormatter = string.Concat(rowFormatter, isColumnTruncation ? $"│{{{widths.Length}}}" : string.Empty);
 }

 WriteRow(GetColumns());

 WriteSeparator(widths);

 bool showData;

 do
 {
 int count = 0;
 while (reader.Read() && count < maxPage)
 {
 WriteRow(GetValues());

 count++;
 }

 if (count >= maxPage)
 {
 showData = this.Confirm($"...More?");
 this.ClearLine();
 if (!showData)
 {
 this.WriteLine();
 }
 }
 else
 {
 showData = false;
 this.WriteLine();
 }
 } while (showData);

 void WriteRow(IEnumerable<string> fields)
 {
 fields = TrimValues(fields).Concat(isColumnTruncation ? new[] { "..." } : Array.Empty<string>());

 this.WriteLine(SystemColor, rowFormatter, fields.ToArray());
 }

 IEnumerable<string> TrimValues(IEnumerable<string> fields)
 {
 int index = 0;
 int totalWidth = 0;

 foreach (var field in fields)
 {
 if (index >= widths.Length)
 {
 yield break;
 }

 var width = widths[index];
 ++index;

 if (width == -1)
 {
 var remainingWidth = Console.WindowWidth - totalWidth;

 yield return TrimValue(field, remainingWidth);
 yield break;
 }

 totalWidth += width + 1;

 yield return TrimValue(field, width);
 }
 }

 string TrimValue(string? value, int width)
 {
 value ??= string.Empty;

 if (value.Length <= width)
 {
 return value;
 }

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

 void WriteSeparator(int[] widths)
 {
 int totalWidth = 0;

 for (int index = 0; index < widths.Length; index++)
 {
 if (index > 0)
 {
 this.Write(SystemColor, "┼");
 }

 var width = widths[index];

 this.Write(SystemColor, new string('─', width == -1 ? Console.WindowWidth - totalWidth : width));

 totalWidth += width + 1;
 }

 if (isColumnTruncation)
 {
 this.Write(SystemColor, "┼───");
 }

 this.WriteLine();
 }

 IEnumerable<int> GetWidths()
 {
 if (reader.FieldCount == 1)
 {
 yield return -1;
 yield break;
 }

 int totalWidth = 0;

 for (int index = 0; index < reader.FieldCount; ++index)
 {
 if (index == reader.FieldCount - 1)
 {
 // Last field gets remaining width
 yield return -1;
 yield break;
 }

 var width = GetWidth(reader.GetFieldType(index));

 if (totalWidth + width > Console.WindowWidth - 11)
 {
 yield break;
 }

 totalWidth += width;

 yield return width;
 }
 }

 static int GetWidth(Type type)
 {
 if (!s_typeWidths.TryGetValue(type, out var width))
 {
 return 16; // Default width
 }

 return width;
 }

 IEnumerable<string> GetColumns()
 {
 for (int index = 0; index < reader.FieldCount; ++index)
 {
 var label = reader.GetName(index);

 yield return string.IsOrWhiteSpace(label) ? $"#{index + 1}" : label;
 }
 }

 IEnumerable<string> GetValues()
 {
 for (int index = 0; index < reader.FieldCount; ++index)
 {
 yield return reader.GetValue(index)?.ToString() ?? string.Empty;
 }
 }
}

整个程序对于输入的处理非常人性化,如果用户输入了无关的或格式不正确的指令,程序会给出相应的提示,引导用户重新输入。

结语

Nl2Sql项目不仅展示了现代AI技术的强大能力,也体现了.NET生态在人工智能应用中的活跃度,并且推动了开发者之间的技术互助和分享。Nl2Sql证明了在未来,我们可以期待更多的自然语言处理工具来帮助我们更好地与数据交互。

更新于:1个月前
赞一波!2

文章评论

全部评论