NaturalQuery 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package NaturalQuery --version 0.2.0
                    
NuGet\Install-Package NaturalQuery -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NaturalQuery" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NaturalQuery" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="NaturalQuery" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NaturalQuery --version 0.2.0
                    
#r "nuget: NaturalQuery, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NaturalQuery@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NaturalQuery&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=NaturalQuery&version=0.2.0
                    
Install as a Cake Tool

NaturalQuery

NL2SQL engine for .NET — convert natural language questions into SQL queries using LLMs.

"top 10 products by sales"  →  SELECT product AS label, SUM(amount) AS value FROM orders GROUP BY product ORDER BY value DESC LIMIT 10

What it does

NaturalQuery takes a natural language question, sends it to an LLM with your database schema, and returns:

  • A validated SQL query
  • A recommended chart type (bar, pie, line, table, etc.)
  • Title, description, and follow-up suggestions

It then executes the query and returns structured results ready for visualization.

Quick Start

dotnet add package NaturalQuery
builder.Services.AddNaturalQuery(options =>
{
    options.Tables = new List<TableSchema>
    {
        new("orders", new[]
        {
            new ColumnDef("id", "string"),
            new ColumnDef("product", "string"),
            new ColumnDef("amount", "double"),
            new ColumnDef("status", "string", "Active, Cancelled, Refunded"),
            new ColumnDef("created_at", "string", "ISO 8601 timestamp"),
        })
    };
    options.TenantIdColumn = "tenant_id";
    options.TenantIdPlaceholder = "{TENANT_ID}";
})
.UseBedrockProvider("us.anthropic.claude-haiku-4-5-20251001-v1:0")
.UseAthenaExecutor("my_database", "my_workgroup", "s3://my-results/");
app.MapGet("/ask", async (string question, INaturalQueryEngine engine) =>
{
    var result = await engine.AskAsync(question, tenantId: "my-tenant");
    return Results.Ok(result);
});

Architecture

Question (natural language)
    ↓
ILlmProvider (AWS Bedrock / custom)
    ↓
SQL generation + validation
    ↓
IQueryExecutor (Amazon Athena / custom)
    ↓
QueryResult (data + chart type + metadata)

Built-in Providers

Provider Component Package
AWS Bedrock (Claude) ILlmProvider Built-in
Amazon Athena IQueryExecutor Built-in

Custom Providers

Implement ILlmProvider for other LLMs (OpenAI, Ollama, etc.):

public class OpenAiProvider : ILlmProvider
{
    public async Task<LlmResponse> GenerateAsync(string systemPrompt, string userPrompt, CancellationToken ct)
    {
        // Your implementation
    }
}

builder.Services.AddNaturalQuery(options => { ... })
    .UseLlmProvider<OpenAiProvider>()
    .UseQueryExecutor<PostgresExecutor>();

Implement IQueryExecutor for other databases (PostgreSQL, BigQuery, etc.):

public class PostgresExecutor : IQueryExecutor
{
    public async Task<List<DataPoint>> ExecuteChartQueryAsync(string sql, CancellationToken ct) { ... }
    public async Task<List<Dictionary<string, string>>> ExecuteTableQueryAsync(string sql, CancellationToken ct) { ... }
}

Features

  • Schema-aware: Auto-generates system prompts from your table definitions
  • Multi-tenant: Built-in tenant isolation with placeholder replacement
  • SQL validation: Blocks destructive queries (DELETE, DROP, etc.)
  • CTE support: Handles WITH ... AS (...) queries for deduplication
  • Chart type inference: LLM picks the best visualization for each question
  • Customizable: Override the system prompt, add rules, block keywords

Configuration

options.Tables = new List<TableSchema> { ... };       // Your database schema
options.TenantIdColumn = "tenant_id";                  // Multi-tenant column
options.TenantIdPlaceholder = "{TENANT_ID}";           // Placeholder in SQL
options.MaxTokens = 1000;                              // LLM max tokens
options.Temperature = 0.1;                             // LLM temperature
options.CustomSystemPrompt = "...{TABLES_SCHEMA}...";  // Override prompt
options.AdditionalRules = new() { "Use UTC dates" };   // Extra instructions
options.ForbiddenSqlKeywords = new() { "UNION " };     // Extra blocked keywords

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 30 4/5/2026
1.1.0 41 4/4/2026
1.0.1 51 4/4/2026
1.0.0 28 4/4/2026
0.2.0 29 4/4/2026
0.1.1 29 4/4/2026
0.1.0 29 4/4/2026