Warning
Documentation here is a work in progress
Translating Natural Language to BlendSQL
nl_to_blendsql
Takes a natural language question, and attempts to parse BlendSQL representation for answering against a databse.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
question |
str
|
The natural language question to parse |
required |
db |
Database
|
Database to use in translating |
required |
model |
Model
|
BlendSQL model to use in translating the question |
required |
ingredients |
Optional[Collection[Type[Ingredient]]]
|
Which ingredients to treat as valid in the output parse. Only these ingredient descriptions are included in the system prompt. |
required |
few_shot_examples |
Union[str, FewShot]
|
String prompt introducing few shot nl-to-blendsql examples. |
''
|
args |
Optional[NLtoBlendSQLArgs]
|
Optional NLtoBlendSQLArgs object, containing additional parameters. |
None
|
verbose |
bool
|
Boolean defining whether to run in logger mode |
False
|
Returns:
Name | Type | Description |
---|---|---|
ret_prediction |
str
|
Final BlendSQL query prediction |
Examples:
from blendsql import LLMMap, LLMQA
from blendsql.models import TransformersLLM, OllamaLLM
from blendsql.nl_to_blendsql import nl_to_blendsql, NLtoBlendSQLArgs
from blendsql.db import SQLite
from blendsql.utils import fetch_from_hub
from blendsql.prompts import FewShot
db = SQLite(
fetch_from_hub("1884_New_Zealand_rugby_union_tour_of_New_South_Wales_1.db")
)
parser_model = OllamaLLM("phi3", caching=False)
correction_model = TransformersLLM("Qwen/Qwen1.5-0.5B")
ingredients = {LLMMap, LLMQA}
filtered_few_shot = FewShot.hybridqa.filter(ingredients)
blendsql = nl_to_blendsql(
"What was the result of the game played 120 miles west of Sydney?",
db=db,
model=parser_model,
correction_model=correction_model,
ingredients=ingredients,
few_shot_examples=filtered_few_shot,
verbose=True,
args=NLtoBlendSQLArgs(
max_grammar_corrections=5,
use_tables=["w"],
include_db_content_tables=["w"],
num_serialized_rows=3,
use_bridge_encoder=True,
),
)
Source code in blendsql/nl_to_blendsql/nl_to_blendsql.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
NLtoBlendSQLArgs
Source code in blendsql/nl_to_blendsql/args.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Grammar-Based Correction
If you use the grammar correction feature of BlendSQL, please cite the original grammar prompting paper below.
@article{wang2024grammar,
title={Grammar prompting for domain-specific language generation with large language models},
author={Wang, Bailin and Wang, Zi and Wang, Xuezhi and Cao, Yuan and A Saurous, Rif and Kim, Yoon},
journal={Advances in Neural Information Processing Systems},
volume={36},
year={2024}
}
FewShot
A collection of few-shot examples, with some utility functions for easy manipulation.
Examples:
from blendsql import LLMMap, LLMQA
from blendsql.prompts import FewShot, Examples
# Fetch the examples for HybridQA
fewshot_prompts: Examples = FewShot.hybridqa
print(f"We have {len(fewshot_prompts)} examples")
# We can select a subset by indexing
first_three_examples = fewshot_prompts[:3]
# Additionally, we can filter to keep only those examples using specified ingredients
filtered_fewshot = fewshot_prompts.filter({LLMQA, LLMMap})
Source code in blendsql/prompts/_prompts.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
Examples
Class for holding few-shot examples.
Examples:
from blendsql.prompts import FewShot, Examples
fewshot_prompts: Examples = FewShot.hybridqa
print(fewshot_prompts[:2])
Examples:
This is the first example
---
This is the second example
Source code in blendsql/prompts/_prompts.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|