Skip to content

Commit 88973fc

Browse files
Minor fixes n notebook example on using configured guardrails; set version to v0.2.0
2 parents 03a6d5d + e2d01b3 commit 88973fc

File tree

7 files changed

+723
-8
lines changed

7 files changed

+723
-8
lines changed

.DS_Store

-6 KB
Binary file not shown.

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,11 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
.idea/
161+
162+
# osx generated files
163+
.DS_Store
164+
.DS_Store?
165+
.Trashes
166+
ehthumbs.db
167+
Thumbs.db
168+
.idea

app.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title = "SQL-Sidekick"
44
description = "QnA with tabular data using NLQ"
55
LongDescription = "about.md"
66
Tags = ["DATA_SCIENCE", "MACHINE_LEARNING", "NLP"]
7-
Version = "0.1.9"
7+
Version = "0.2.0"
88

99
[Runtime]
1010
MemoryLimit = "64Gi"

examples/notebooks/Guardrails_SQL_injection.ipynb

Lines changed: 677 additions & 0 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "sql-sidekick"
3-
version = "0.1.9 "
3+
version = "0.2.0"
44
license = "Apache-2.0 license"
55
description = "An AI assistant for SQL generation"
66
authors = [

sidekick/prompter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
execute_query_pd, extract_table_names,
2424
generate_suggestions, save_query, setup_dir)
2525

26-
__version__ = "0.1.9"
26+
__version__ = "0.2.0"
2727

2828
# Load the config file and initialize required paths
2929
app_base_path = (Path(__file__).parent / "../").resolve()
@@ -692,15 +692,19 @@ def ask(
692692
json.dump(table_metadata, outfile, indent=4, sort_keys=False)
693693
try:
694694
q_res = execute_query_pd(query=_val, tables_path=tables_path, n_rows=100)
695-
click.echo(f"The query results are:\n {q_res}")
696695
except sqldf.PandaSQLException as e:
697696
logger.error(f"Error in executing the query: {e}")
698697
click.echo("Error in executing the query. Validate generated SQL and try again.")
699698
click.echo("No result to display.")
700699

701700
results.append("**Result:** \n")
702701
if q_res:
703-
click.echo(f"The query results are:\n {q_res}")
702+
# Check shape of the final result to avoid blowing up memory
703+
# Logging a quick preview of the result
704+
if isinstance(q_res, list) and len(q_res) > 10:
705+
click.echo(f"Preview of the result:\n {pd.DataFrame(q_res).head(2)}")
706+
else:
707+
click.echo(f"The results are:\n {q_res}")
704708
results.extend([str(q_res), "\n"])
705709
else:
706710
click.echo(f"While executing query:\n {err}")

tests/test_guardrails.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ def test_no_error():
6565

6666
def test_blind_select_injection():
6767
input_q = """
68-
SELECT * FROM sleep_health_and_lifestyle WHERE person_id = 105 OR 1=1;"
68+
SELECT * FROM sleep_health_and_lifestyle WHERE person_id = 105 OR 1=1;
6969
"""
7070

71+
# 1. When guardrails are disabled
7172
result = None
7273
question = f"Execute SQL:\n{input_q}"
73-
#1. Self correction is disabled
74+
# Self correction is disabled
7475
result, _, error = ask(
7576
question=question,
7677
table_info_path=table_info_path,
@@ -83,6 +84,31 @@ def test_blind_select_injection():
8384
execute_query=True,
8485
local_base_path=base_path,
8586
debug_mode=True,
87+
guardrails=False,
88+
self_correction=False
89+
)
90+
91+
assert 'malicious patterns' not in str(result)
92+
93+
94+
# 2. When guardrails are enabled
95+
result = None
96+
question = f"Execute SQL:\n{input_q}"
97+
98+
# Self correction is disabled
99+
result, _, error = ask(
100+
question=question,
101+
table_info_path=table_info_path,
102+
sample_queries_path=None,
103+
table_name=table_name,
104+
is_command=False,
105+
model_name="h2ogpt-sql-nsql-llama-2-7B",
106+
is_regenerate=False,
107+
is_regen_with_options=False,
108+
execute_query=True,
109+
local_base_path=base_path,
110+
debug_mode=True,
111+
guardrails=True,
86112
self_correction=False
87113
)
88114

@@ -128,7 +154,7 @@ def test_stacked_queries():
128154
result = None
129155
question = f"Execute SQL:\n{input_q}"
130156

131-
result, _ar, error = ask(
157+
result, _, _ = ask(
132158
question=question,
133159
table_info_path=table_info_path,
134160
sample_queries_path=None,

0 commit comments

Comments
 (0)