text2sql-engine 0.1.0__py3-none-any.whl → 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- text2sql/__init__.py +1 -1
- text2sql/config.py +18 -2
- text2sql/pipeline/generation.py +39 -3
- text2sql/prompts/generation_raw.txt +27 -0
- {text2sql_engine-0.1.0.dist-info → text2sql_engine-0.1.1.dist-info}/METADATA +10 -4
- {text2sql_engine-0.1.0.dist-info → text2sql_engine-0.1.1.dist-info}/RECORD +9 -8
- {text2sql_engine-0.1.0.dist-info → text2sql_engine-0.1.1.dist-info}/licenses/LICENSE +1 -1
- {text2sql_engine-0.1.0.dist-info → text2sql_engine-0.1.1.dist-info}/WHEEL +0 -0
- {text2sql_engine-0.1.0.dist-info → text2sql_engine-0.1.1.dist-info}/top_level.txt +0 -0
text2sql/__init__.py
CHANGED
text2sql/config.py
CHANGED
|
@@ -52,6 +52,9 @@ class StageSettings:
|
|
|
52
52
|
timeout_seconds: float = 60.0
|
|
53
53
|
# Used by linking only. Generation ignores it.
|
|
54
54
|
max_tables: int = 8
|
|
55
|
+
# Used by generation only. "structured" forces JSON (sql + explanation).
|
|
56
|
+
# "raw" asks for plain SQL text. Linking ignores it.
|
|
57
|
+
output_mode: str = "structured"
|
|
55
58
|
|
|
56
59
|
|
|
57
60
|
@dataclass
|
|
@@ -149,6 +152,7 @@ def _stage_from_toml(
|
|
|
149
152
|
if include_max_tables
|
|
150
153
|
else defaults.max_tables
|
|
151
154
|
),
|
|
155
|
+
output_mode=str(table.get("output_mode", defaults.output_mode)).lower(),
|
|
152
156
|
)
|
|
153
157
|
|
|
154
158
|
|
|
@@ -226,9 +230,21 @@ def load_settings(
|
|
|
226
230
|
default_template="linking.txt",
|
|
227
231
|
include_max_tables=True,
|
|
228
232
|
)
|
|
233
|
+
|
|
234
|
+
gen_tbl = toml_data.get("generation", {})
|
|
235
|
+
gen_output_mode = str(gen_tbl.get("output_mode", "structured")).lower()
|
|
236
|
+
if gen_output_mode not in ("structured", "raw"):
|
|
237
|
+
raise ConfigError(
|
|
238
|
+
f"[generation] output_mode must be 'structured' or 'raw', "
|
|
239
|
+
f"got '{gen_output_mode}'."
|
|
240
|
+
)
|
|
241
|
+
# In raw mode, default to the raw template unless the user set one.
|
|
242
|
+
gen_default_template = "generation.txt"
|
|
243
|
+
if gen_output_mode == "raw" and "prompt_template" not in gen_tbl:
|
|
244
|
+
gen_default_template = "generation_raw.txt"
|
|
229
245
|
generation = _stage_from_toml(
|
|
230
|
-
|
|
231
|
-
default_template=
|
|
246
|
+
gen_tbl,
|
|
247
|
+
default_template=gen_default_template,
|
|
232
248
|
include_max_tables=False,
|
|
233
249
|
)
|
|
234
250
|
|
text2sql/pipeline/generation.py
CHANGED
|
@@ -2,13 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Takes the reduced schema from Stage 1 and produces a SQL string, plus an
|
|
4
4
|
optional short explanation, in the configured dialect.
|
|
5
|
+
|
|
6
|
+
Two output modes (config: [generation] output_mode):
|
|
7
|
+
|
|
8
|
+
* "structured" (default) — force JSON with "sql" and "explanation" fields.
|
|
9
|
+
Reliable parsing. Best for general chat/instruct models.
|
|
10
|
+
* "raw" — ask for plain SQL text. No explanation. Better for specialized
|
|
11
|
+
text-to-SQL models that are trained to emit SQL only.
|
|
5
12
|
"""
|
|
6
13
|
|
|
7
14
|
from __future__ import annotations
|
|
8
15
|
|
|
16
|
+
import re
|
|
9
17
|
from typing import Any
|
|
10
18
|
|
|
11
19
|
from ..config import Settings
|
|
20
|
+
from ..errors import StructuredOutputError
|
|
12
21
|
from ..logging_utils import get_logger
|
|
13
22
|
from ..prompts import PromptTemplates
|
|
14
23
|
from ..providers.base import LLMProvider
|
|
@@ -16,6 +25,18 @@ from ..schema import Schema, schema_to_prompt
|
|
|
16
25
|
from ..types import LinkedSchema, LLMResponse, StageMetadata, _Timer
|
|
17
26
|
from .json_utils import extract_json, with_retry
|
|
18
27
|
|
|
28
|
+
# Matches a ```sql ... ``` or ``` ... ``` fenced block.
|
|
29
|
+
_FENCE_RE = re.compile(r"```(?:sql)?\s*(.*?)```", re.DOTALL | re.IGNORECASE)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _strip_sql(text: str) -> str:
|
|
33
|
+
"""Return plain SQL from a raw response. Drops markdown fences if present."""
|
|
34
|
+
stripped = text.strip()
|
|
35
|
+
fence = _FENCE_RE.search(stripped)
|
|
36
|
+
if fence:
|
|
37
|
+
return fence.group(1).strip()
|
|
38
|
+
return stripped
|
|
39
|
+
|
|
19
40
|
logger = get_logger("pipeline.generation")
|
|
20
41
|
|
|
21
42
|
GENERATION_SCHEMA_NAME = "sql_generation"
|
|
@@ -72,7 +93,7 @@ class GenerationStage:
|
|
|
72
93
|
|
|
73
94
|
attempts = {"n": 0}
|
|
74
95
|
|
|
75
|
-
def
|
|
96
|
+
def attempt_structured() -> tuple[str, str, LLMResponse]:
|
|
76
97
|
attempts["n"] += 1
|
|
77
98
|
response = self._provider.complete_json(
|
|
78
99
|
system=system,
|
|
@@ -87,11 +108,26 @@ class GenerationStage:
|
|
|
87
108
|
sql = str(data.get("sql", "")).strip()
|
|
88
109
|
explanation = str(data.get("explanation", "")).strip()
|
|
89
110
|
if not sql:
|
|
90
|
-
from ..errors import StructuredOutputError
|
|
91
|
-
|
|
92
111
|
raise StructuredOutputError("Generation returned an empty 'sql' field.")
|
|
93
112
|
return sql, explanation, response
|
|
94
113
|
|
|
114
|
+
def attempt_raw() -> tuple[str, str, LLMResponse]:
|
|
115
|
+
attempts["n"] += 1
|
|
116
|
+
response = self._provider.complete(
|
|
117
|
+
system=system,
|
|
118
|
+
user=user,
|
|
119
|
+
temperature=stage_cfg.temperature,
|
|
120
|
+
top_p=stage_cfg.top_p,
|
|
121
|
+
max_tokens=stage_cfg.max_tokens,
|
|
122
|
+
)
|
|
123
|
+
sql = _strip_sql(response.text)
|
|
124
|
+
if not sql:
|
|
125
|
+
raise StructuredOutputError("Generation returned empty SQL.")
|
|
126
|
+
# Raw mode does not produce an explanation.
|
|
127
|
+
return sql, "", response
|
|
128
|
+
|
|
129
|
+
attempt = attempt_raw if stage_cfg.output_mode == "raw" else attempt_structured
|
|
130
|
+
|
|
95
131
|
with _Timer() as timer:
|
|
96
132
|
sql, explanation, response = with_retry(
|
|
97
133
|
attempt,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
SYSTEM:
|
|
2
|
+
You are an expert SQL generator. You get a user request and a reduced schema
|
|
3
|
+
(only the relevant tables/columns/joins picked by an upstream linking step).
|
|
4
|
+
Produce one correct SQL query in the target dialect.
|
|
5
|
+
|
|
6
|
+
Rules:
|
|
7
|
+
- Use only the tables and columns in the reduced schema.
|
|
8
|
+
- Write idiomatic SQL for the target dialect: {dialect}.
|
|
9
|
+
- Prefer explicit JOINs using the given relations.
|
|
10
|
+
- Do not run the query. Only produce it.
|
|
11
|
+
- Output ONLY the SQL query. No explanation, no comment, no markdown fences.
|
|
12
|
+
|
|
13
|
+
USER:
|
|
14
|
+
Target SQL dialect: {dialect}
|
|
15
|
+
|
|
16
|
+
Reduced schema (relevant subset):
|
|
17
|
+
--------------------------------
|
|
18
|
+
{linked_schema}
|
|
19
|
+
--------------------------------
|
|
20
|
+
|
|
21
|
+
Detected entities: {entities}
|
|
22
|
+
Filter hints: {filters}
|
|
23
|
+
|
|
24
|
+
User request:
|
|
25
|
+
"{request}"
|
|
26
|
+
|
|
27
|
+
Write the SQL query that answers the request.
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: text2sql-engine
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Schema-agnostic, config-driven Text-to-SQL library (2-stage LLM pipeline). Produces a SQL string; never connects to or executes against a database.
|
|
5
|
-
Author:
|
|
5
|
+
Author: Mehmet Kaan Durupunar
|
|
6
6
|
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/defectGI/sqlretrieve
|
|
8
|
+
Project-URL: Repository, https://github.com/defectGI/sqlretrieve
|
|
9
|
+
Project-URL: Issues, https://github.com/defectGI/sqlretrieve/issues
|
|
7
10
|
Keywords: text-to-sql,nl2sql,llm,sql-generation,schema-linking
|
|
8
11
|
Classifier: Development Status :: 4 - Beta
|
|
9
12
|
Classifier: Intended Audience :: Developers
|
|
@@ -68,7 +71,9 @@ user request
|
|
|
68
71
|
forced via `response_format` (OpenAI) or tool-calling (Anthropic), with
|
|
69
72
|
strict-JSON parsing and retry as a fallback.
|
|
70
73
|
- **Stage 2 (generation)** takes the request and that reduced subset. It writes
|
|
71
|
-
SQL in the target dialect
|
|
74
|
+
SQL in the target dialect. Two output modes (config `output_mode`):
|
|
75
|
+
`structured` forces JSON with `sql` + `explanation`; `raw` asks for plain SQL
|
|
76
|
+
text, for specialized text-to-SQL models.
|
|
72
77
|
- The two stages can use **different providers and models**. For example a cheap
|
|
73
78
|
general model for linking, a stronger SQL model for generation.
|
|
74
79
|
|
|
@@ -165,8 +170,9 @@ No magic numbers in code. Every tunable is here.
|
|
|
165
170
|
| `temperature` | float | `0.0` | Sampling temperature. |
|
|
166
171
|
| `top_p` | float | `1.0` | Nucleus-sampling cutoff. |
|
|
167
172
|
| `max_tokens` | int | `1024` | Max tokens for the SQL response. |
|
|
168
|
-
| `prompt_template` | string | `generation.txt` | Template file, looked up in `PROMPT_DIR`. |
|
|
173
|
+
| `prompt_template` | string | `generation.txt` | Template file, looked up in `PROMPT_DIR`. In `raw` mode, defaults to `generation_raw.txt` when unset. |
|
|
169
174
|
| `timeout_seconds` | float | `60` | Per-request timeout. |
|
|
175
|
+
| `output_mode` | string | `structured` | `structured`: force JSON with `sql` + `explanation`, reliable parsing, best for general models. `raw`: ask for plain SQL text (no explanation), better for specialized text-to-SQL models trained to emit SQL only. |
|
|
170
176
|
|
|
171
177
|
#### `[retry]` — shared by both stages
|
|
172
178
|
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
text2sql/__init__.py,sha256=
|
|
2
|
-
text2sql/config.py,sha256=
|
|
1
|
+
text2sql/__init__.py,sha256=oMYRHWUUR6cRjpG0EAMdmDAPF82UCPbMEd4lyRx_4Sc,1442
|
|
2
|
+
text2sql/config.py,sha256=6snv99rf8jf8NtD3xetSrvIaltbLyq2eoISoMoPadSw,8503
|
|
3
3
|
text2sql/errors.py,sha256=Y5ORy7g_UsFvDJ2KorB2JFvTyOBqmrPv9MWc8Cl9MI8,1052
|
|
4
4
|
text2sql/logging_utils.py,sha256=hiEH1SDqP16kctUj4uPP0tbXdrCO9AlRcdS-mHYDg5s,1633
|
|
5
5
|
text2sql/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
text2sql/types.py,sha256=MxcdHP7Gnftk1yKdcBQKaVVyeqRnBu5kxbhSZ2xJJ80,3532
|
|
7
7
|
text2sql/pipeline/__init__.py,sha256=rjEOi4U9C-SKP15cyKPdvG_EZBOW0VW01LouS3PJVdY,245
|
|
8
|
-
text2sql/pipeline/generation.py,sha256=
|
|
8
|
+
text2sql/pipeline/generation.py,sha256=rXzU6e4LyyPTlHSkqT4IsZA-Xf-DjK3Kp9QAbpA2PJ4,5030
|
|
9
9
|
text2sql/pipeline/json_utils.py,sha256=92viA3xyw_GMF_nKdjSPgAyQ8iBUZgnyNRAoyWC5ZlE,3145
|
|
10
10
|
text2sql/pipeline/linking.py,sha256=U28LOcWJn3cItwAsv2fG4fYv9KcJ80XmP1Fi8nNx4qc,6966
|
|
11
11
|
text2sql/pipeline/pipeline.py,sha256=TE0cHSEJyeY43P89HsQDbnPGSfgRQmtw7KZAq-Yjj1U,5675
|
|
12
12
|
text2sql/prompts/__init__.py,sha256=HfeflfAGsZl3xHnNrmklTP6K_8EKxX6vWJrayq6QuEQ,3315
|
|
13
13
|
text2sql/prompts/generation.txt,sha256=r4125WCr7gsJIzTa5wZTAmbTyn5WvP3QrfGf4CWNP_w,849
|
|
14
|
+
text2sql/prompts/generation_raw.txt,sha256=wAvUvHd2Qywfy-Poc-7lqW5Gmg1nf3IyFDwtZNBCJls,789
|
|
14
15
|
text2sql/prompts/linking.txt,sha256=nLPSkaq3PiWiJ7wXLeD0BvK_-rR3hhc_tLhZ-daPnGY,889
|
|
15
16
|
text2sql/providers/__init__.py,sha256=R5a-YKtyaUFpkVJGWrmhDGyRHeh84gA3F7nEH57dSGo,345
|
|
16
17
|
text2sql/providers/anthropic_provider.py,sha256=Zhh8zM6EfHqkjTgNmhYKdZLHQ69b0Pu2-5WbCW2rD6s,4342
|
|
@@ -22,8 +23,8 @@ text2sql/schema/file_provider.py,sha256=Nv9x2-Eh9mXISk5XvUQaLfagth2s3LbX5F7jWp-o
|
|
|
22
23
|
text2sql/schema/models.py,sha256=yX5zdKbTuy-g-nkwT0v-0HUTK81jBRoabV-OC-kD-JI,2263
|
|
23
24
|
text2sql/schema/provider.py,sha256=JPMRsjRgEB4RdQR_pFwbP2LwLIXZhQVWSQPoYzaXC7s,798
|
|
24
25
|
text2sql/schema/serialize.py,sha256=_2MeA1IwDJ51-69PXyqXBSFZSyzovbqJkRIMpekkfJA,1833
|
|
25
|
-
text2sql_engine-0.1.
|
|
26
|
-
text2sql_engine-0.1.
|
|
27
|
-
text2sql_engine-0.1.
|
|
28
|
-
text2sql_engine-0.1.
|
|
29
|
-
text2sql_engine-0.1.
|
|
26
|
+
text2sql_engine-0.1.1.dist-info/licenses/LICENSE,sha256=qfdfVnpb5NOM3-SK13PnmWagjCLy5sE9ORlgOGXc8hU,1078
|
|
27
|
+
text2sql_engine-0.1.1.dist-info/METADATA,sha256=ixiIwDgwu2F0KvULAh2niQSs-OW_dI7faEMGC4hgfb4,12971
|
|
28
|
+
text2sql_engine-0.1.1.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
29
|
+
text2sql_engine-0.1.1.dist-info/top_level.txt,sha256=ZONWO1r8wsWpyHhF5qPzAjnsNoCEhwZ2Z-M_V8nKALc,9
|
|
30
|
+
text2sql_engine-0.1.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|