Skip to main content

NL2SQL

A simple implementation of NL2SQL.

Requirements

  • Python 3.12+
  • Postgresql / Mysql
  • LLM API
  • Milvus 2.5.0+
  • Embedding API

Definition

LLM URI

A db-uri-style string that provides information for calling the model API.

Format

<model_type>+<api_type>://<model>[:<model_tag>]@[<api_key>]@<url>

Supported Model Type

  • llm
  • embedding

Supported API Type

  • ollama
  • openai

Example

llm+ollama://qwen2.5:32b@localhost:11434 llm+openai://gpt-3.5-turbo:32b@your_token@http://localhost:11434/v1

SQL References

Optional metadata stored in Milvus. Provide some sql references for problems similar to the current problem.

Format

Must contain fields: query, sql and tags. Vector field will be matched the embedding of query.

Usage

assembly sql generating

import asyncio
from nl2sql.tools.text2sql import Text2SQLAssembly
text2sql = Text2SQLAssembly(
    db_uri="postgresql+psycopg2://postgres:123456@localhost:5432/test",
    openai_baseurl="http://localhost:11434/v1",
    openai_apikey="your_token",
    llm_model="qwen2.5:0.5b",
    embedding_model="bge-m3",
    milvus_uri="http://read:123456@localhost:19530",
    collection_name="sql_references",
)
# request or get information in context engineering
# using more information to generate SQL exactly
asyncio.run(text2sql.generate(
    "公司的设备清单", 
    ["assets", "users", "projects"], 
    columns=["id", "name", "asset_type"],  # optional
    expressions=["order by id", "limit 10"],  # optional
))
# if you don't want to verify the SQL, you can set max_verification=0
asyncio.run(text2sql.generate("公司的设备清单", ["assets", "users", "projects"], max_verification=0))

@Deprecated(0.5.0) generate sql

from nl2sql.tools.text2sql import Text2SQL
worker = Text2SQL(
    db_uri="postgresql+psycopg2://postgres:123456@localhost:5432/test",
    llm_uri="llm+ollama://qwen2.5:32b@localhost:11434",
    milvus_uri="http://read:123456@localhost:19530",
    collection_name="sql_references",
    embedding_uri="embedding+ollama://bge-m3@localhost:11434"
)
sql = worker.generate("公司的设备清单", ["assets", "users", "projects"]).sql

@Deprecated(0.5.0) optimize sql

from nl2sql.tools.text2sql import Text2SQL

worker = Text2SQL(
    db_uri="postgresql+psycopg2://postgres:123456@localhost:5432/test",
    llm_uri="llm+ollama://qwen2.5:32b@localhost:11434",
    milvus_uri="http://read:123456@localhost:19530",
    collection_name="sql_references",
    embedding_uri="embedding+ollama://bge-m3@localhost:11434"
)
sql = worker.optimize("select * from users", "所有用户的用户名", "有多余的字段", ["users", "projects"]).sql

@Deprecated(1.0.0) agent for sql generating

import asyncio
from nl2sql.tools.text2sql import Text2SQLAgent

agent = Text2SQLAgent(
    db_uri="postgresql+psycopg2://postgres:123456@localhost:5432/test",
    openai_baseurl="http://localhost:11434/v1",
    openai_apikey="your_token",
    llm_model="qwen2.5:0.5b",
    embedding_model="bge-m3",
    milvus_uri="http://read:123456@localhost:19530",
    collection_name="sql_references",
)
# no longer need to specify name, instructions, tools
asyncio.run(agent.generate("公司的设备清单", ["assets", "users", "projects"]))

Check if the entity is ambiguous

from pydantic import BaseModel
from nl2sql.tools.text2sql import Text2SQLAssembly


class User(BaseModel):
    email: str
    full_name: str


text2sql = Text2SQLAssembly(
    db_uri="postgresql+psycopg2://postgres:123456@localhost:5432/test",
    openai_baseurl="http://localhost:11434/v1",
    openai_apikey="your_token",
    llm_model="qwen2.5:0.5b",
    embedding_model="bge-m3",
    milvus_uri="http://read:123456@localhost:19530",
    collection_name="sql_references",
)
result = text2sql.is_entity_ambiguous(
    User, "users", "test",
    display_cols=["email", "full_name", "id"]
)

Metadata

Release files for simple-nl2sql 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-nl2sql 0.4.0
File Size Uploaded Provenance Actions
simple_nl2sql-0.4.0.tar.gz 16.6 kB Without provenance Details for simple_nl2sql-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-nl2sql 0.4.0
File Interpreter ABI Platform Actions
simple_nl2sql-0.4.0-py3-none-any.whl Python 3 none any Details for simple_nl2sql-0.4.0-py3-none-any.whl

Total release size: 39.0 kB

Release files / simple_nl2sql-0.4.0.tar.gz

Download URL simple_nl2sql-0.4.0.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f1eeae2e994ed0b5b0d83ba51b7661c6e7a2b972773b68071c1aee3631237193
BLAKE2b-256 checksum
How to use checksums
9bbc6fab7bd8c89b58bfb529061e3128bd4e7bec4feeabfbd9bf57c27c60bfb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / simple_nl2sql-0.4.0-py3-none-any.whl

Download URL simple_nl2sql-0.4.0-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f39592bfdbb07626abb1d3b4cff2731cbcc4709365983eaedfc8e8d8fe53fc84
BLAKE2b-256 checksum
How to use checksums
2924bbae31b44fe4645cbd115b6409fce989246ecd65e6baea3877eccaf9fb11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Security for simple-nl2sql 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page