Skip to main content

A simple implementation of NL2SQL.

Project description

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

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

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

agent for sql generating

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",
)
agent.initialize(
    name="your_agen_name",  # optional
    instructions="your instructions",  # optional
    tools=[],  # optional
)
agent.generate("公司的设备清单", ["assets", "users", "projects"])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_nl2sql-0.2.0.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_nl2sql-0.2.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file simple_nl2sql-0.2.0.tar.gz.

File metadata

  • Download URL: simple_nl2sql-0.2.0.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for simple_nl2sql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8ca9857f7ddded299c82a5232d7cb27717f408c4b866482c9a4ff2186283d4ab
MD5 b3822b55245a6610743c9e70aec419c6
BLAKE2b-256 0baa2c060792375b280a2cf2f4554a6baf6d0e5ca86d7d6ffe47268547ab1296

See more details on using hashes here.

File details

Details for the file simple_nl2sql-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: simple_nl2sql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for simple_nl2sql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e61bfc20c9f6b11a59f3a66802f8cf36f9c712098ef5a248b36419983353fec
MD5 0178d81edf812bfb66380a3503959435
BLAKE2b-256 a216460602b9415cbd0fb64199ead013d0a2ab67ebe79101c49025e9b931ec2a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page