Execute a BlendSQL Query
blend()
The blend()
function is used to execute a BlendSQL query against a database and
return the final result, in addition to the intermediate reasoning steps taken.
Execution is done on a database given an ingredient context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
str
|
The BlendSQL query to execute |
required |
db
|
Database
|
Database connector object |
required |
ingredients
|
Optional[Collection[Type[Ingredient]]]
|
Collection of ingredient objects, to use in interpreting BlendSQL query |
None
|
verbose
|
bool
|
Boolean defining whether to run with logger in debug mode |
False
|
default_model
|
Optional[Model]
|
Which BlendSQL model to use in performing ingredient tasks in the current query |
None
|
infer_gen_constraints
|
bool
|
Optionally infer the output format of an |
True
|
table_to_title
|
Optional[Dict[str, str]]
|
Optional mapping from table name to title of table. Useful for datasets like WikiTableQuestions, where relevant info is stored in table title. |
None
|
schema_qualify
|
bool
|
Optional bool, determines if we run qualify_columns() from sqlglot
This enables us to write BlendSQL scripts over multi-table databases without manually qualifying columns ourselves
However, we need to call |
True
|
Returns:
Name | Type | Description |
---|---|---|
smoothie |
Smoothie
|
|
Examples:
import pandas as pd
from blendsql import blend, LLMMap, LLMQA, LLMJoin
from blendsql.db import Pandas
from blendsql.models import TransformersLLM
# Load model
model = TransformersLLM('Qwen/Qwen1.5-0.5B')
# Prepare our local database
db = Pandas(
{
"w": pd.DataFrame(
(
['11 jun', 'western districts', 'bathurst', 'bathurst ground', '11-0'],
['12 jun', 'wallaroo & university nsq', 'sydney', 'cricket ground',
'23-10'],
['5 jun', 'northern districts', 'newcastle', 'sports ground', '29-0']
),
columns=['date', 'rival', 'city', 'venue', 'score']
),
"documents": pd.DataFrame(
(
['bathurst, new south wales', 'bathurst /ˈbæθərst/ is a city in the central tablelands of new south wales , australia . it is about 200 kilometres ( 120 mi ) west-northwest of sydney and is the seat of the bathurst regional council .'],
['sydney', 'sydney ( /ˈsɪdni/ ( listen ) sid-nee ) is the state capital of new south wales and the most populous city in australia and oceania . located on australia s east coast , the metropolis surrounds port jackson.'],
['newcastle, new south wales', 'the newcastle ( /ˈnuːkɑːsəl/ new-kah-səl ) metropolitan area is the second most populated area in the australian state of new south wales and includes the newcastle and lake macquarie local government areas .']
),
columns=['title', 'content']
)
}
)
# Write BlendSQL query
blendsql = """
SELECT * FROM w
WHERE city = {{
LLMQA(
'Which city is located 120 miles west of Sydney?',
(SELECT * FROM documents WHERE content LIKE '%sydney%'),
options='w::city'
)
}}
"""
smoothie = blend(
query=blendsql,
db=db,
ingredients={LLMMap, LLMQA, LLMJoin},
default_model=model,
# Optional args below
infer_gen_constraints=True,
verbose=True
)
print(smoothie.df)
# ┌────────┬───────────────────┬──────────┬─────────────────┬─────────┐
# │ date │ rival │ city │ venue │ score │
# ├────────┼───────────────────┼──────────┼─────────────────┼─────────┤
# │ 11 jun │ western districts │ bathurst │ bathurst ground │ 11-0 │
# └────────┴───────────────────┴──────────┴─────────────────┴─────────┘
print(smoothie.meta.prompts)
# [
# {
# 'answer': 'sydney',
# 'question': 'Which city is located 120 miles west of Sydney?',
# 'context': [
# {'title': 'bathurst, new south wales', 'content': 'bathurst /ˈbæθərst/ is a city in the central tablelands of new south wales , australia . it is about...'},
# {'title': 'sydney', 'content': 'sydney ( /ˈsɪdni/ ( listen ) sid-nee ) is the state capital of new south wales and the most populous city in...'}
# ]
# }
# ]
Source code in blendsql/blend.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
|
Appendix
preprocess_blendsql()
Parses BlendSQL string with our pyparsing grammar and returns objects required for interpretation and execution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
str
|
The BlendSQL query to preprocess |
required |
default_model
|
Model
|
Model object, which we attach to each parsed_dict |
required |
Returns:
Type | Description |
---|---|
Tuple[str, dict, set, Kitchen, Set[Ingredient]]
|
Tuple, containing:
|
Examples:
preprocess_blendsql(
query="SELECT * FROM documents JOIN {{LLMJoin(left_on='w::player', right_on='documents::title')}} WHERE rank = 2",
default_model=default_model
)
(
{
'{{A()}}': {
'function': 'LLMJoin',
'args': [],
'ingredient_aliasname': 'A',
'raw': "{{ LLMJoin ( left_on= 'w::player' , right_on= 'documents::title' ) }}",
'kwargs_dict': {
'left_on': 'w::player',
'right_on': 'documents::title'
}
}
},
{'documents', 'w'}
)
Source code in blendsql/blend.py
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 307 308 309 310 311 312 313 314 315 316 317 318 |
|