Skip to content

Latest commit

ย 

History

History
99 lines (73 loc) ยท 2.27 KB

File metadata and controls

99 lines (73 loc) ยท 2.27 KB

01. ๋น ๋ฅธ ์‹œ์ž‘ โ€” 5๋ถ„ ์•ˆ์— NL2SQL ์‹คํ–‰ํ•˜๊ธฐ

lang2sql v2์˜ ๊ธฐ๋ณธ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‹ค์ œ LLM๊ณผ DB๋กœ ๋ฐ”๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.


์‚ฌ์ „ ์ค€๋น„

pip install lang2sql
export OPENAI_API_KEY="sk-..."

์ƒ˜ํ”Œ DB ์ƒ์„ฑ:

python scripts/setup_sample_db.py

์™„๋ฃŒ๋˜๋ฉด ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ์— sample.db๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.


BaselineNL2SQL ์‹คํ–‰

from lang2sql import BaselineNL2SQL
from lang2sql.integrations.db import SQLAlchemyDB
from lang2sql.integrations.llm import OpenAILLM

catalog = [
    {
        "name": "orders",
        "description": "๊ณ ๊ฐ ์ฃผ๋ฌธ ์ •๋ณด",
        "columns": {
            "order_id": "์ฃผ๋ฌธ ๊ณ ์œ  ID",
            "customer_id": "๊ณ ๊ฐ ID",
            "order_date": "์ฃผ๋ฌธ ์ผ์‹œ",
            "amount": "์ฃผ๋ฌธ ๊ธˆ์•ก",
            "status": "์ฃผ๋ฌธ ์ƒํƒœ",
        },
    },
    {
        "name": "customers",
        "description": "๊ณ ๊ฐ ๋งˆ์Šคํ„ฐ",
        "columns": {
            "customer_id": "๊ณ ๊ฐ ID",
            "name": "๊ณ ๊ฐ ์ด๋ฆ„",
            "grade": "๊ณ ๊ฐ ๋“ฑ๊ธ‰: bronze / silver / gold",
        },
    },
]

pipeline = BaselineNL2SQL(
    catalog=catalog,
    llm=OpenAILLM(model="gpt-4o-mini"),
    db=SQLAlchemyDB("sqlite:///sample.db"),
    db_dialect="sqlite",
)

rows = pipeline.run("์ง€๋‚œ๋‹ฌ ์ฃผ๋ฌธ ๊ฑด์ˆ˜๋ฅผ ์•Œ๋ ค์ค˜")
print(rows)

ํŒŒ์ดํ”„๋ผ์ธ ๊ตฌ์กฐ

BaselineNL2SQL
  โ”œโ”€โ”€ KeywordRetriever   โ€” catalog์—์„œ ๊ด€๋ จ ํ…Œ์ด๋ธ” ๊ฒ€์ƒ‰ (BM25)
  โ”œโ”€โ”€ SQLGenerator       โ€” LLM์œผ๋กœ SQL ์ƒ์„ฑ
  โ””โ”€โ”€ SQLExecutor        โ€” DB ์‹คํ–‰ ํ›„ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜

BaselineNL2SQL์€ ํ‚ค์›Œ๋“œ ๊ธฐ๋ฐ˜ ๊ฒ€์ƒ‰(KeywordRetriever)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์ด ํ•„์š”ํ•˜๋ฉด HybridNL2SQL์„ ์‚ฌ์šฉํ•˜์„ธ์š” (โ†’ 04-hybrid.md).


์ง€์› LLM

LLMPort๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ตฌํ˜„์ฒด๋กœ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

from lang2sql.integrations.llm import AnthropicLLM
llm = AnthropicLLM(model="claude-sonnet-4-6")

from lang2sql.integrations.llm import OpenAILLM
llm = OpenAILLM(model="gpt-4o")

๋‘˜ ๋‹ค LLMPort.invoke(messages) ๊ณ„์•ฝ์„ ๋”ฐ๋ฅด๋ฏ€๋กœ ํŒŒ์ดํ”„๋ผ์ธ ์ฝ”๋“œ๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.


๋‹ค์Œ ๋‹จ๊ณ„

DB Explorer, ๋‹ค์ค‘ ํ…Œ์ด๋ธ” ์นดํƒˆ๋กœ๊ทธ, ์‹ค์ œ ์šด์˜ DB ์—ฐ๊ฒฐ โ†’ 02-baseline.md