lang2sql v2์ ๊ธฐ๋ณธ ํ์ดํ๋ผ์ธ์ ์ค์ LLM๊ณผ DB๋ก ๋ฐ๋ก ์คํํฉ๋๋ค.
pip install lang2sql
export OPENAI_API_KEY="sk-..."์ํ DB ์์ฑ:
python scripts/setup_sample_db.py์๋ฃ๋๋ฉด ํ๋ก์ ํธ ๋ฃจํธ์ sample.db๊ฐ ์์ฑ๋ฉ๋๋ค.
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).
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