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

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
))

@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"]
)

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.3.2.tar.gz (15.9 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.3.2-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_nl2sql-0.3.2.tar.gz
  • Upload date:
  • Size: 15.9 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.3.2.tar.gz
Algorithm Hash digest
SHA256 01fac2f6a2e6ace88f2ee75b59bf3f6a4525ff733db29f42d5b2022e57fa850a
MD5 96c5ed1ba9c66dc2e85aa2b52c4f9f5f
BLAKE2b-256 551820a6fc83e340fedfcc12a373a7cef8dc24eaa2c65647519473e45f33c790

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_nl2sql-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 21.3 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.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f68cde2b4024fcbe55dc838eec70662d108c31992abdfe4cc9dbf69653929ee
MD5 63cd1c8f0667241ee5a4440d07cad7bf
BLAKE2b-256 84c9c65e33e91ab111ff6425fa22af01b6c46a5df5cf78d6f1fcfc8b34d49fbb

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