探索.NET平台下自然语言转换为SQL的Nl2Sql项目

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

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

GPT-4带来的新突破

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

项目结构与样例信息

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

  1. nl2sql.config - 包含了设置说明、数据模式和语义提示。

  2. nl2sql.console - 控制台应用,用于将自然语言目标转换成SQL查询。

  3. nl2sql.library - 支持库,同样用于自然语言到SQL的转换。

  4. nl2sql.harness - 开发调试工具,用于实时逆向工程化数据库模式。

  5. nl2sql.sln - Visual Studio解决方案文件。

  6. 929ca463f2ca49baba9e578d0df47bbc.png

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

81f66e3aadae34afed04e47963e31d2b.png

核心方法解析

        我们来看一看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.IsNullOrWhiteSpace(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);


        // Null response occurs when blocking input is cancelled (CTRL+C)
        if (null == 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.IsNullOrWhiteSpace(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 == null)
        {
            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.IsNullOrWhiteSpace(label) ? $"#{index + 1}" : label;
        }
    }


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

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

结语

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

        此篇博文的目的在于向您介绍Nl2Sql项目的设计理念、项目结构、核心功能以及用户交互体验。但要注意的是,该项目是否适用于特定的用例,仍然需要结合具体的业务场景和预期来评估。在整个数据检索和处理生态系统中,Nl2Sql展示的仅仅是其中的一环,但绝对是非常值得我们关注的那部分。

        希望今天的分享能够激发您的兴趣,同时也希望能够在这个领域看到更多.NET开发者的参与和贡献。让我们一同期待在人工智能和自然语言处理领域能够取得更多技术突破,为我们的开发和生活带来更多便捷!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值