Skip to content

Commit be32889

Browse files
committed
Update the prompt.
1 parent 2fc2cfc commit be32889

3 files changed

Lines changed: 69 additions & 21 deletions

File tree

litecli/packages/special/llm.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,32 @@ def __init__(self, results=None):
155155
"""
156156

157157
_SQL_CODE_FENCE = r"```sql\n(.*?)\n```"
158-
PROMPT = """A SQLite database has the following schema:
158+
PROMPT = """
159+
You are a helpful assistant who is a SQLite expert. You are embedded in a SQLite
160+
cli tool called litecli.
159161
160-
$db_schema
162+
Answer this question:
163+
164+
$question
161165
162-
Here is a sample row of data from each table: $sample_data
166+
Use the following context if it is relevant to answering the question. If the
167+
question is not about the current database then ignore the context.
163168
164-
Use the provided schema and the sample data to construct a SQL query that
165-
can be run in SQLite3 to answer
169+
You are connected to a SQLite database with the following schema:
166170
167-
$question
171+
$db_schema
172+
173+
Here is a sample row of data from each table:
168174
169-
Explain the reason for choosing each table in the SQL query you have
170-
written. Keep the explanation concise.
171-
Finally include a sql query in a code fence such as this one:
175+
$sample_data
176+
177+
If the answer can be found using a SQL query, include a sql query in a code
178+
fence such as this one:
172179
173180
```sql
174181
SELECT count(*) FROM table_name;
175182
```
176-
177-
If the question cannot be answered based on the database schema respond with "I
178-
cannot answer that question" in a sql code fence.
183+
Keep your explanation concise and focused on the question asked.
179184
"""
180185

181186

litecli/packages/special/main.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,30 @@ def parse_special_command(sql):
5252
an invocation mode (regular, verbose, or succinct), and the argument.
5353
"""
5454
raw, _, arg = sql.partition(" ")
55-
is_verbose = raw.endswith("+")
56-
is_succinct = raw.endswith("-")
57-
# strip out any + or - modifiers to get the actual command name
58-
command = raw.strip().rstrip("+-")
59-
if is_verbose:
60-
mode = Verbosity.VERBOSE
61-
elif is_succinct:
62-
mode = Verbosity.SUCCINCT
55+
raw = raw.strip()
56+
57+
suffix_count = 0
58+
idx = len(raw) - 1
59+
while idx >= 0 and raw[idx] in "+-":
60+
suffix_count += 1
61+
idx -= 1
62+
if suffix_count > 1:
63+
raise ValueError("Invalid special command: %s" % raw)
64+
65+
if suffix_count == 1:
66+
suffix = raw[-1]
67+
base_cmd = raw[:-1]
68+
mode = Verbosity.VERBOSE if suffix == "+" else Verbosity.SUCCINCT
6369
else:
70+
suffix = None
71+
base_cmd = raw
6472
mode = Verbosity.REGULAR
65-
return (command, mode, arg.strip())
73+
74+
last_char = base_cmd[-1] if base_cmd else None
75+
if suffix is None and last_char and not (last_char.isalnum() or last_char in "?."):
76+
raise ValueError("Invalid special command: %s" % raw)
77+
78+
return (base_cmd, mode, arg.strip())
6679

6780

6881
@export

tests/test_special_iocommands.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import litecli.packages.special
7+
from litecli.packages.special.main import parse_special_command, Verbosity
78

89

910
def test_once_command():
@@ -57,3 +58,32 @@ def test_pipe_once_command():
5758
litecli.packages.special.unset_pipe_once_if_written()
5859
f.seek(0)
5960
assert f.read() == b"hello world\n"
61+
62+
63+
@pytest.mark.parametrize(
64+
"sql,expected",
65+
[
66+
(r"\d table_name", ("\\d", Verbosity.REGULAR, "table_name")),
67+
(r"\d+ table_name", ("\\d", Verbosity.VERBOSE, "table_name")),
68+
(r"\?", ("\\?", Verbosity.REGULAR, "")),
69+
(r"\llm Question", ("\\llm", Verbosity.REGULAR, "Question")),
70+
(r"\llm-", ("\\llm", Verbosity.SUCCINCT, "")),
71+
(r"\llm+", ("\\llm", Verbosity.VERBOSE, "")),
72+
],
73+
)
74+
def test_parse_special_command(sql, expected):
75+
"""
76+
Ensure parse_special_command correctly splits the command and mode.
77+
"""
78+
result = parse_special_command(sql)
79+
assert result == expected
80+
81+
82+
def test_parse_special_command_error():
83+
sql = r"\llm* Question"
84+
with pytest.raises(ValueError):
85+
parse_special_command(sql)
86+
87+
sql = r"\llm+- Question"
88+
with pytest.raises(ValueError):
89+
parse_special_command(sql)

0 commit comments

Comments
 (0)