Skip to content

Commit 492dd0e

Browse files
cpsievertclaude
andauthored
fix(pkg-py): Set system_prompt on client during initialization (#188)
* fix(pkg-py): Set system_prompt on client during initialization The system_prompt was not being set on _client during initialization, causing the chat to have no context about the data. This resulted in nonsense greetings and broken functionality. Fixes #187 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(pkg-py): Add tools parameter to QueryChatExpress The tools parameter was missing from QueryChatExpress.__init__(), preventing Express users from customizing which tools are available. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2cd161f commit 492dd0e

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

pkg-py/src/querychat/_querychat.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def __init__(
7979
client = as_querychat_client(client)
8080
self._client = copy.deepcopy(client)
8181
self._client.set_turns([])
82+
self._client.system_prompt = self._system_prompt.render(self.tools)
8283

8384
# Storage for console client
8485
self._client_console = None
@@ -142,7 +143,7 @@ def app_server(input: Inputs, output: Outputs, session: Session):
142143
self.id,
143144
data_source=self._data_source,
144145
greeting=self.greeting,
145-
client=self._client,
146+
client=self.client(),
146147
enable_bookmarking=enable_bookmarking,
147148
)
148149

@@ -719,6 +720,19 @@ def data_table():
719720
If `client` is not provided, querychat consults the
720721
`QUERYCHAT_CLIENT` environment variable. If that is not set, it
721722
defaults to `"openai"`.
723+
tools
724+
Which querychat tools to include in the chat client by default. Can be:
725+
- A single tool string: `"update"` or `"query"`
726+
- A tuple of tools: `("update", "query")`
727+
- `None` or `()` to disable all tools
728+
729+
Default is `("update", "query")` (both tools enabled).
730+
731+
Set to `"update"` to prevent the LLM from accessing data values, only
732+
allowing dashboard filtering without answering questions.
733+
734+
The tools can be overridden per-client by passing a different `tools`
735+
parameter to the `.client()` method.
722736
data_description
723737
Description of the data in plain text or Markdown. If a pathlib.Path
724738
object is passed, querychat will read the contents of the path into a
@@ -751,6 +765,7 @@ def __init__(
751765
id: Optional[str] = None,
752766
greeting: Optional[str | Path] = None,
753767
client: Optional[str | chatlas.Chat] = None,
768+
tools: TOOL_GROUPS | tuple[TOOL_GROUPS, ...] | None = ("update", "query"),
754769
data_description: Optional[str | Path] = None,
755770
categorical_threshold: int = 20,
756771
extra_instructions: Optional[str | Path] = None,
@@ -771,6 +786,7 @@ def __init__(
771786
id=id,
772787
greeting=greeting,
773788
client=client,
789+
tools=tools,
774790
data_description=data_description,
775791
categorical_threshold=categorical_threshold,
776792
extra_instructions=extra_instructions,
@@ -793,7 +809,7 @@ def __init__(
793809
self.id,
794810
data_source=self._data_source,
795811
greeting=self.greeting,
796-
client=self._client,
812+
client=self.client(),
797813
enable_bookmarking=enable,
798814
)
799815

pkg-py/tests/test_querychat.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,30 @@ def test_querychat_custom_id(sample_df):
6060
)
6161

6262
assert qc.id == "custom_id"
63+
64+
65+
def test_querychat_client_has_system_prompt(sample_df):
66+
"""
67+
Test that the client returned by .client() has a system prompt set.
68+
69+
Regression test for issue #187: the system prompt was missing because
70+
_client.system_prompt wasn't being set during initialization.
71+
"""
72+
qc = QueryChat(
73+
data_source=sample_df,
74+
table_name="test_table",
75+
greeting="Hello!",
76+
)
77+
78+
# The client() method should return a chat with the system prompt set
79+
client = qc.client()
80+
assert client.system_prompt is not None
81+
assert len(client.system_prompt) > 0
82+
83+
# The system_prompt should contain the table name since it includes schema info
84+
assert "test_table" in client.system_prompt
85+
86+
# The internal _client should also have the system prompt set
87+
# (needed for methods like generate_greeting() that use _client directly)
88+
assert qc._client.system_prompt is not None
89+
assert "test_table" in qc._client.system_prompt

0 commit comments

Comments
 (0)