Execute a BlendSQL Query
BlendSQL Class
Core BlendSQL
class that provides high level interface for executing BlendSQL queries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db
|
Union[DataFrame, dict, str, Database]
|
Database to connect to. Can be:
|
None
|
model
|
Optional[Model]
|
Model instance to use for LLM operations. Can also be provided during query execution. |
None
|
ingredients
|
Optional[Collection[Type[Ingredient]]]
|
Collection of ingredients to make available for queries. Can also be provided during query execution. |
list()
|
verbose
|
bool
|
Whether to output debug logging information. Defaults to False. |
False
|
infer_gen_constraints
|
bool
|
Whether to automatically infer constraints for LLM generation based on query context. Defaults to True. |
True
|
table_to_title
|
Optional[Dict[str, str]]
|
Optional mapping from table names to descriptive titles, useful for datasets where table titles contain metadata. |
None
|
schema_qualify
|
bool
|
Whether to qualify column names with table names. Required for multi-table queries but adds overhead. Can be disabled for single-table queries. Defaults to True. |
True
|
Source code in blendsql/blendsql.py
965 966 967 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 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 |
|
execute(query, ingredients=None, model=None, infer_gen_constraints=None, schema_qualify=None, verbose=None)
The execute()
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 |
ingredients
|
Optional[Collection[Type[Ingredient]]]
|
Collection of ingredient objects, to use in interpreting BlendSQL query |
None
|
verbose
|
Optional[bool]
|
Boolean defining whether to run with logger in debug mode |
None
|
default_model
|
Which BlendSQL model to use in performing ingredient tasks in the current query |
required | |
infer_gen_constraints
|
Optional[bool]
|
Optionally infer the output format of an |
None
|
table_to_title
|
Optional mapping from table name to title of table. Useful for datasets like WikiTableQuestions, where relevant info is stored in table title. |
required | |
schema_qualify
|
Optional[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 |
None
|
Returns:
Name | Type | Description |
---|---|---|
smoothie |
Smoothie
|
|
Examples:
import pandas as pd
from blendsql import BlendSQL, config
from blendsql.ingredients import LLMMap, LLMQA, LLMJoin
from blendsql.models import LiteLLM, TransformersLLM
# Optionally set how many async calls to allow concurrently
# This depends on your OpenAI/Anthropic/etc. rate limits
config.set_async_limit(10)
# Load model
model = LiteLLM("openai/gpt-4o-mini") # requires .env file with `OPENAI_API_KEY`
# model = LiteLLM("anthropic/claude-3-haiku-20240307") # requires .env file with `ANTHROPIC_API_KEY`
# model = TransformersLLM(
# "meta-llama/Llama-3.2-1B-Instruct",
# config={"chat_template": Llama3ChatTemplate, "device_map": "auto"},
# ) # run with any local Transformers model
# Prepare our BlendSQL connection
bsql = BlendSQL(
{
"People": pd.DataFrame(
{
"Name": [
"George Washington",
"John Quincy Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
"Alexander Hamilton",
"Sabrina Carpenter",
"Charli XCX",
"Elon Musk",
"Michelle Obama",
"Elvis Presley",
],
"Known_For": [
"Established federal government, First U.S. President",
"XYZ Affair, Alien and Sedition Acts",
"Louisiana Purchase, Declaration of Independence",
"War of 1812, Constitution",
"Monroe Doctrine, Missouri Compromise",
"Created national bank, Federalist Papers",
"Nonsense, Emails I Cant Send, Mean Girls musical",
"Crash, How Im Feeling Now, Boom Clap",
"Tesla, SpaceX, Twitter/X acquisition",
"Lets Move campaign, Becoming memoir",
"14 Grammys, King of Rock n Roll",
],
}
),
"Eras": pd.DataFrame({"Years": ["1800-1900", "1900-2000", "2000-Now"]}),
},
ingredients={LLMMap, LLMQA, LLMJoin},
model=model,
)
smoothie = bsql.execute(
"""
SELECT * FROM People P
WHERE P.Name IN {{
LLMQA('First 3 presidents of the U.S?', quantifier='{3}')
}}
"""
)
print(smoothie.df)
# ┌───────────────────┬───────────────────────────────────────────────────────┐
# │ Name │ Known_For │
# ├───────────────────┼───────────────────────────────────────────────────────┤
# │ George Washington │ Established federal government, First U.S. Preside... │
# │ John Quincy Adams │ XYZ Affair, Alien and Sedition Acts │
# │ Thomas Jefferson │ Louisiana Purchase, Declaration of Independence │
# └───────────────────┴───────────────────────────────────────────────────────┘
print(smoothie.summary())
# ┌────────────┬──────────────────────┬─────────────────┬─────────────────────┐
# │ Time (s) │ # Generation Calls │ Prompt Tokens │ Completion Tokens │
# ├────────────┼──────────────────────┼─────────────────┼─────────────────────┤
# │ 1.25158 │ 1 │ 296 │ 16 │
# └────────────┴──────────────────────┴─────────────────┴─────────────────────┘
Source code in blendsql/blendsql.py
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 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 |
|