text2sql

package
v0.0.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package text2sql provides natural language to ClickHouse SQL translation using a local Ollama LLM with structural validation feedback.

The LLM generates SQL freely. The nanopass pipeline validates and canonicalizes the output. On failure, the error is fed back for retry.

Usage:

gen := text2sql.New("http://localhost:11434", "qwen2.5-coder:7b", schema)
result, err := gen.Generate(ctx, "count orders by country for 2024")
if err != nil { ... }
fmt.Println(result.SQL)       // canonical SQL
fmt.Println(result.AST)       // ast.Query
fmt.Println(result.Attempts)  // number of LLM calls

Index

Constants

This section is empty.

Variables

View Source
var PackageProps = packageprops.Props{
	WASMWASI:         packageprops.WASMCompiles,
	WASMJS:           packageprops.WASMBlocked,
	WASMFreestanding: packageprops.WASMCompiles,
}

PackageProps records this package's curated properties (ADR-0080). Seeded by `boxer code analysis golang wasmsurvey props generate`; curate by hand. The same group's `props verify` reconciles it.

Functions

func NewCliCommand

func NewCliCommand() (cmd *cli.Command)

NewCliCommand returns the `text2sql` subcommand. Mounted directly under the boxer top-level CLI by public/app/main.go.

In non-interactive mode the question is supplied as positional arguments (joined with a single space). Interactive mode reads from stdin, prompts for a question per line, and prints the result.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator translates natural language to ClickHouse SQL.

func New

func New(endpoint, model string, schema []SchemaTable, opts ...Option) *Generator

New creates a Generator.

func (*Generator) Generate

func (inst *Generator) Generate(ctx context.Context, question string) (result Result, err error)

Generate translates a natural language question to ClickHouse SQL.

type Option

type Option func(*Generator)

Option configures a Generator.

func WithMaxAttempts

func WithMaxAttempts(n int) Option

WithMaxAttempts sets the maximum number of LLM calls (default: 3).

func WithSystemPromptOverride

func WithSystemPromptOverride(prompt string) Option

WithSystemPromptOverride replaces the default system prompt entirely.

type Result

type Result struct {
	SQL         string    // canonical (Grammar2-compliant) SQL
	AST         ast.Query // parsed and converted AST
	RawSQL      string    // SQL as generated by the LLM (before normalization)
	Attempts    int       // number of LLM calls (1 = first try)
	Model       string    // model used
	TotalTokens int       // approximate total tokens across all attempts
}

Result is the output of a successful text-to-SQL generation.

type SchemaColumn

type SchemaColumn struct {
	Name    string
	Type    string
	Comment string // optional human-readable description
}

SchemaColumn describes a column.

type SchemaTable

type SchemaTable struct {
	Database string
	Table    string
	Columns  []SchemaColumn
	Comment  string // optional human-readable description
}

SchemaTable describes a table for the LLM prompt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL