querychat 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- querychat-0.1.0/.gitignore +4 -0
- querychat-0.1.0/LICENSE +21 -0
- querychat-0.1.0/PKG-INFO +165 -0
- querychat-0.1.0/README.md +118 -0
- querychat-0.1.0/pyproject.toml +48 -0
- querychat-0.1.0/src/querychat/__about__.py +0 -0
- querychat-0.1.0/src/querychat/__init__.py +3 -0
- querychat-0.1.0/src/querychat/prompt/prompt.md +91 -0
- querychat-0.1.0/src/querychat/querychat.py +467 -0
- querychat-0.1.0/src/querychat/static/css/styles.css +93 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/annotated_types-0.7.0.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/anyio-4.9.0.dist-info/LICENSE +20 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/anywidget-0.9.18.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/asgiref-3.8.1.dist-info/LICENSE +27 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/asttokens-3.0.0.dist-info/LICENSE +201 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/certifi-2025.4.26.dist-info/licenses/LICENSE +20 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/charset_normalizer-3.4.2.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/chatlas-0.7.1.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/comm-0.2.2.dist-info/licenses/LICENSE +29 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/distro-1.9.0.dist-info/LICENSE +202 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/duckdb/experimental/spark/LICENSE +260 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/htmltools-0.6.0.dist-info/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/ipython-9.2.0.dist-info/licenses/LICENSE +33 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/ipython_pygments_lexers-1.1.1.dist-info/LICENSE +30 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/ipywidgets-8.1.7.dist-info/licenses/LICENSE +27 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/jedi/third_party/typeshed/LICENSE +238 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/jupyter_core-5.7.2.dist-info/licenses/LICENSE +30 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/jupyterlab_widgets-3.0.15.dist-info/licenses/LICENSE +292 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/linkify_it_py-2.0.3.dist-info/LICENSE +48 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/markdown_it_py-3.0.0.dist-info/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/matplotlib_inline-0.1.7.dist-info/LICENSE +29 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/admon/LICENSE +24 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/container/LICENSE +22 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/container/README.md +95 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/deflist/LICENSE +22 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/deflist/README.md +38 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/footnote/LICENSE +22 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/front_matter/LICENSE +22 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/texmath/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins/texmath/README.md +137 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdit_py_plugins-0.4.2.dist-info/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/mdurl-0.1.2.dist-info/LICENSE +46 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/numpy/ma/LICENSE +24 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/openai-1.82.0.dist-info/licenses/LICENSE +201 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/packaging-25.0.dist-info/licenses/LICENSE +3 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/pandas-2.2.3.dist-info/LICENSE +1250 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/pexpect-4.9.0.dist-info/LICENSE +20 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/platformdirs-4.3.8.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/prompt_toolkit-3.0.51.dist-info/licenses/LICENSE +27 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/psygnal-0.13.0.dist-info/licenses/LICENSE +26 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/ptyprocess-0.7.0.dist-info/LICENSE +16 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/pydantic-2.11.5.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/pydantic_core-2.33.2.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/pygments-2.19.1.dist-info/licenses/LICENSE +25 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/python_dateutil-2.9.0.post0.dist-info/LICENSE +54 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/querychat-0.1.0.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/questionary-2.1.0.dist-info/LICENSE +19 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/requests-2.32.3.dist-info/LICENSE +175 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/rich-14.0.0.dist-info/LICENSE +19 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/autocommand-2.2.2.dist-info/LICENSE +166 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/backports.tarfile-1.2.0.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/importlib_metadata-8.0.0.dist-info/LICENSE +202 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/inflect-7.3.1.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/jaraco.collections-5.1.0.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/jaraco.context-5.3.0.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/jaraco.functools-4.0.1.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/jaraco.text-3.12.1.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/more_itertools-10.3.0.dist-info/LICENSE +19 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/packaging-24.2.dist-info/LICENSE +3 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/platformdirs-4.2.2.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/tomli-2.0.1.dist-info/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/typeguard-4.3.0.dist-info/LICENSE +19 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/typing_extensions-4.12.2.dist-info/LICENSE +279 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/wheel/vendored/packaging/LICENSE +3 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools/_vendor/zipp-3.19.2.dist-info/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/setuptools-80.8.0.dist-info/licenses/LICENSE +17 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/bookmark/README.md +582 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/templates/app/04-dashboard-tips/README.md +1 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/templates/package/js-input/README.md +63 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/templates/package/js-output/README.md +63 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/templates/package/js-react/README.md +63 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/www/shared/busy-indicators/spinners/LICENSE +20 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny/www/shared/highlight/LICENSE +24 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shiny-1.4.0.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/shinywidgets-0.6.2.dist-info/licenses/LICENSE +22 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/six-1.17.0.dist-info/LICENSE +18 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/sniffio-1.3.1.dist-info/LICENSE +3 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/traitlets-5.14.3.dist-info/licenses/LICENSE +30 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/typing_extensions-4.13.2.dist-info/licenses/LICENSE +279 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/typing_inspection-0.4.1.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/tzdata-2025.2.dist-info/licenses/LICENSE +15 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/uc_micro_py-1.0.3.dist-info/LICENSE +46 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/watchfiles-1.0.5.dist-info/licenses/LICENSE +21 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/wcwidth-0.2.13.dist-info/LICENSE +27 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/websockets-15.0.1.dist-info/LICENSE +24 -0
- querychat-0.1.0/venv/lib/python3.12/site-packages/widgetsnbextension-4.0.14.dist-info/licenses/LICENSE +27 -0
querychat-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Posit Software, PBC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
querychat-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: querychat
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Chat with your data using natural language
|
|
5
|
+
Project-URL: Homepage, https://github.com/posit-dev/querychat
|
|
6
|
+
Project-URL: Issues, https://github.com/posit-dev/querychat/issues
|
|
7
|
+
Project-URL: Source, https://github.com/posit-dev/querychat/tree/main/python-package
|
|
8
|
+
Author-email: Posit <info@posit.co>
|
|
9
|
+
License: # MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2025 Posit Software, PBC
|
|
12
|
+
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
License-File: LICENSE
|
|
31
|
+
Classifier: Programming Language :: Python
|
|
32
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
33
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
35
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
38
|
+
Requires-Python: >=3.8
|
|
39
|
+
Requires-Dist: chatlas
|
|
40
|
+
Requires-Dist: duckdb
|
|
41
|
+
Requires-Dist: htmltools
|
|
42
|
+
Requires-Dist: narwhals
|
|
43
|
+
Requires-Dist: pandas
|
|
44
|
+
Requires-Dist: shiny
|
|
45
|
+
Requires-Dist: shinywidgets
|
|
46
|
+
Description-Content-Type: text/markdown
|
|
47
|
+
|
|
48
|
+
# querychat for Python
|
|
49
|
+
|
|
50
|
+
Chat with your Shiny Python apps using natural language.
|
|
51
|
+
|
|
52
|
+
Imagine typing questions like these directly into your Shiny dashboard, and seeing the results in realtime:
|
|
53
|
+
|
|
54
|
+
* "Show only data from 2008 with highway MPG greater than 30."
|
|
55
|
+
* "What's the average city MPG for SUVs vs compact cars?"
|
|
56
|
+
* "Sort the data by highway fuel efficiency descending."
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install "querychat @ git+https://github.com/posit-dev/querychat#subdirectory=python-package"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## How to use
|
|
65
|
+
|
|
66
|
+
First, you'll need access to an LLM that supports tools/function calling. querychat uses [chatlas](https://github.com/posit-dev/chatlas) to interface with various providers.
|
|
67
|
+
|
|
68
|
+
Here's a minimal example (see [examples/app.py](examples/app.py) for an unabridged version):
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from pathlib import Path
|
|
72
|
+
|
|
73
|
+
from seaborn import load_dataset
|
|
74
|
+
from shiny import App, render, ui
|
|
75
|
+
|
|
76
|
+
import querychat
|
|
77
|
+
|
|
78
|
+
titanic = load_dataset("titanic")
|
|
79
|
+
|
|
80
|
+
# 1. Configure querychat
|
|
81
|
+
querychat_config = querychat.init(titanic, "titanic")
|
|
82
|
+
|
|
83
|
+
# Create UI
|
|
84
|
+
app_ui = ui.page_sidebar(
|
|
85
|
+
# 2. Place the chat component in the sidebar
|
|
86
|
+
querychat.sidebar("chat"),
|
|
87
|
+
# Main panel with data viewer
|
|
88
|
+
ui.output_data_frame("data_table"),
|
|
89
|
+
title="querychat with Python",
|
|
90
|
+
fillable=True,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Define server logic
|
|
95
|
+
def server(input, output, session):
|
|
96
|
+
# 3. Initialize querychat server with the config from step 1
|
|
97
|
+
chat = querychat.server("chat", querychat_config)
|
|
98
|
+
|
|
99
|
+
# 4. Display the filtered dataframe
|
|
100
|
+
@render.data_frame
|
|
101
|
+
def data_table():
|
|
102
|
+
# Access filtered data via chat.df() reactive
|
|
103
|
+
return chat["df"]()
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# Create Shiny app
|
|
107
|
+
app = App(app_ui, server)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Features
|
|
111
|
+
|
|
112
|
+
querychat uses LLMs to generate SQL queries from natural language, offering:
|
|
113
|
+
|
|
114
|
+
- **Reliability**: LLMs are excellent at writing SQL but bad at direct calculation
|
|
115
|
+
- **Transparency**: SQL is always displayed to the user
|
|
116
|
+
- **Reproducibility**: Generated SQL can be copied and reused elsewhere
|
|
117
|
+
|
|
118
|
+
## Customization
|
|
119
|
+
|
|
120
|
+
### Configure with your own data
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
querychat_config = querychat.init(
|
|
124
|
+
df=my_dataframe,
|
|
125
|
+
table_name="my_data", # Optional: defaults to "data"
|
|
126
|
+
greeting="Welcome! Ask me anything about the data.",
|
|
127
|
+
data_description="""
|
|
128
|
+
This dataset contains information about...
|
|
129
|
+
- column1: Description of column 1
|
|
130
|
+
- column2: Description of column 2
|
|
131
|
+
""",
|
|
132
|
+
extra_instructions="Please use British English spelling conventions."
|
|
133
|
+
)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Use a different LLM provider
|
|
137
|
+
|
|
138
|
+
You can change the LLM provider or model by providing a callback function that takes a `system_prompt` argument and returns a `chatlas.Chat` object. (The parameter must be called `system_prompt` as it is passed by keyword.)
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
import chatlas
|
|
142
|
+
|
|
143
|
+
def my_chat_func(system_prompt: str) -> chatlas.Chat:
|
|
144
|
+
return chatlas.ChatAnthropic(
|
|
145
|
+
model="claude-3-5-sonnet-latest",
|
|
146
|
+
system_prompt=system_prompt
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
querychat_config = querychat.init(
|
|
150
|
+
df=my_dataframe,
|
|
151
|
+
create_chat_callback=my_chat_func
|
|
152
|
+
)
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## How it works
|
|
156
|
+
|
|
157
|
+
querychat works by:
|
|
158
|
+
|
|
159
|
+
1. Converting your pandas DataFrame to a DuckDB table
|
|
160
|
+
2. Creating a system prompt with your data schema
|
|
161
|
+
3. Setting up tools that let the LLM execute SQL queries
|
|
162
|
+
4. Processing natural language to SQL queries
|
|
163
|
+
5. Returning filtered data to your Shiny app
|
|
164
|
+
|
|
165
|
+
See the `examples/` directory for more complete examples.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# querychat for Python
|
|
2
|
+
|
|
3
|
+
Chat with your Shiny Python apps using natural language.
|
|
4
|
+
|
|
5
|
+
Imagine typing questions like these directly into your Shiny dashboard, and seeing the results in realtime:
|
|
6
|
+
|
|
7
|
+
* "Show only data from 2008 with highway MPG greater than 30."
|
|
8
|
+
* "What's the average city MPG for SUVs vs compact cars?"
|
|
9
|
+
* "Sort the data by highway fuel efficiency descending."
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install "querychat @ git+https://github.com/posit-dev/querychat#subdirectory=python-package"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## How to use
|
|
18
|
+
|
|
19
|
+
First, you'll need access to an LLM that supports tools/function calling. querychat uses [chatlas](https://github.com/posit-dev/chatlas) to interface with various providers.
|
|
20
|
+
|
|
21
|
+
Here's a minimal example (see [examples/app.py](examples/app.py) for an unabridged version):
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
from seaborn import load_dataset
|
|
27
|
+
from shiny import App, render, ui
|
|
28
|
+
|
|
29
|
+
import querychat
|
|
30
|
+
|
|
31
|
+
titanic = load_dataset("titanic")
|
|
32
|
+
|
|
33
|
+
# 1. Configure querychat
|
|
34
|
+
querychat_config = querychat.init(titanic, "titanic")
|
|
35
|
+
|
|
36
|
+
# Create UI
|
|
37
|
+
app_ui = ui.page_sidebar(
|
|
38
|
+
# 2. Place the chat component in the sidebar
|
|
39
|
+
querychat.sidebar("chat"),
|
|
40
|
+
# Main panel with data viewer
|
|
41
|
+
ui.output_data_frame("data_table"),
|
|
42
|
+
title="querychat with Python",
|
|
43
|
+
fillable=True,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Define server logic
|
|
48
|
+
def server(input, output, session):
|
|
49
|
+
# 3. Initialize querychat server with the config from step 1
|
|
50
|
+
chat = querychat.server("chat", querychat_config)
|
|
51
|
+
|
|
52
|
+
# 4. Display the filtered dataframe
|
|
53
|
+
@render.data_frame
|
|
54
|
+
def data_table():
|
|
55
|
+
# Access filtered data via chat.df() reactive
|
|
56
|
+
return chat["df"]()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# Create Shiny app
|
|
60
|
+
app = App(app_ui, server)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Features
|
|
64
|
+
|
|
65
|
+
querychat uses LLMs to generate SQL queries from natural language, offering:
|
|
66
|
+
|
|
67
|
+
- **Reliability**: LLMs are excellent at writing SQL but bad at direct calculation
|
|
68
|
+
- **Transparency**: SQL is always displayed to the user
|
|
69
|
+
- **Reproducibility**: Generated SQL can be copied and reused elsewhere
|
|
70
|
+
|
|
71
|
+
## Customization
|
|
72
|
+
|
|
73
|
+
### Configure with your own data
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
querychat_config = querychat.init(
|
|
77
|
+
df=my_dataframe,
|
|
78
|
+
table_name="my_data", # Optional: defaults to "data"
|
|
79
|
+
greeting="Welcome! Ask me anything about the data.",
|
|
80
|
+
data_description="""
|
|
81
|
+
This dataset contains information about...
|
|
82
|
+
- column1: Description of column 1
|
|
83
|
+
- column2: Description of column 2
|
|
84
|
+
""",
|
|
85
|
+
extra_instructions="Please use British English spelling conventions."
|
|
86
|
+
)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Use a different LLM provider
|
|
90
|
+
|
|
91
|
+
You can change the LLM provider or model by providing a callback function that takes a `system_prompt` argument and returns a `chatlas.Chat` object. (The parameter must be called `system_prompt` as it is passed by keyword.)
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import chatlas
|
|
95
|
+
|
|
96
|
+
def my_chat_func(system_prompt: str) -> chatlas.Chat:
|
|
97
|
+
return chatlas.ChatAnthropic(
|
|
98
|
+
model="claude-3-5-sonnet-latest",
|
|
99
|
+
system_prompt=system_prompt
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
querychat_config = querychat.init(
|
|
103
|
+
df=my_dataframe,
|
|
104
|
+
create_chat_callback=my_chat_func
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## How it works
|
|
109
|
+
|
|
110
|
+
querychat works by:
|
|
111
|
+
|
|
112
|
+
1. Converting your pandas DataFrame to a DuckDB table
|
|
113
|
+
2. Creating a system prompt with your data schema
|
|
114
|
+
3. Setting up tools that let the LLM execute SQL queries
|
|
115
|
+
4. Processing natural language to SQL queries
|
|
116
|
+
5. Returning filtered data to your Shiny app
|
|
117
|
+
|
|
118
|
+
See the `examples/` directory for more complete examples.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "querychat"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Chat with your data using natural language"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = {file = "LICENSE"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Posit", email = "info@posit.co"},
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"duckdb",
|
|
17
|
+
"pandas",
|
|
18
|
+
"shiny",
|
|
19
|
+
"shinywidgets",
|
|
20
|
+
"htmltools",
|
|
21
|
+
"chatlas",
|
|
22
|
+
"narwhals",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
classifiers = [
|
|
26
|
+
"Programming Language :: Python",
|
|
27
|
+
"Programming Language :: Python :: 3.8",
|
|
28
|
+
"Programming Language :: Python :: 3.9",
|
|
29
|
+
"Programming Language :: Python :: 3.10",
|
|
30
|
+
"Programming Language :: Python :: 3.11",
|
|
31
|
+
"Programming Language :: Python :: 3.12",
|
|
32
|
+
"Programming Language :: Python :: 3.13",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/posit-dev/querychat"
|
|
37
|
+
Issues = "https://github.com/posit-dev/querychat/issues"
|
|
38
|
+
Source = "https://github.com/posit-dev/querychat/tree/main/python-package"
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.wheel]
|
|
41
|
+
packages = ["querychat"]
|
|
42
|
+
|
|
43
|
+
[tool.hatch.build.targets.sdist]
|
|
44
|
+
include = [
|
|
45
|
+
"querychat",
|
|
46
|
+
"LICENSE",
|
|
47
|
+
"README.md",
|
|
48
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
You are a chatbot that is displayed in the sidebar of a data dashboard. You will be asked to perform various tasks on the data, such as filtering, sorting, and answering questions.
|
|
2
|
+
|
|
3
|
+
It's important that you get clear, unambiguous instructions from the user, so if the user's request is unclear in any way, you should ask for clarification. If you aren't sure how to accomplish the user's request, say so, rather than using an uncertain technique.
|
|
4
|
+
|
|
5
|
+
The user interface in which this conversation is being shown is a narrow sidebar of a dashboard, so keep your answers concise and don't include unnecessary patter, nor additional prompts or offers for further assistance.
|
|
6
|
+
|
|
7
|
+
You have at your disposal a DuckDB database containing this schema:
|
|
8
|
+
|
|
9
|
+
{{schema}}
|
|
10
|
+
|
|
11
|
+
For security reasons, you may only query this specific table.
|
|
12
|
+
|
|
13
|
+
{{data_description}}
|
|
14
|
+
|
|
15
|
+
There are several tasks you may be asked to do:
|
|
16
|
+
|
|
17
|
+
## Task: Filtering and sorting
|
|
18
|
+
|
|
19
|
+
The user may ask you to perform filtering and sorting operations on the dashboard; if so, your job is to write the appropriate SQL query for this database. Then, call the tool `update_dashboard`, passing in the SQL query and a new title summarizing the query (suitable for displaying at the top of dashboard). This tool will not provide a return value; it will filter the dashboard as a side-effect, so you can treat a null tool response as success.
|
|
20
|
+
|
|
21
|
+
* **Call `update_dashboard` every single time** the user wants to filter/sort; never tell the user you've updated the dashboard unless you've called `update_dashboard` and it returned without error.
|
|
22
|
+
* The SQL query must be a **DuckDB SQL** SELECT query. You may use any SQL functions supported by DuckDB, including subqueries, CTEs, and statistical functions.
|
|
23
|
+
* The user may ask to "reset" or "start over"; that means clearing the filter and title. Do this by calling `update_dashboard({"query": "", "title": ""})`.
|
|
24
|
+
* Queries passed to `update_dashboard` MUST always **return all columns that are in the schema** (feel free to use `SELECT *`); you must refuse the request if this requirement cannot be honored, as the downstream code that will read the queried data will not know how to display it. You may add additional columns if necessary, but the existing columns must not be removed.
|
|
25
|
+
* When calling `update_dashboard`, **don't describe the query itself** unless the user asks you to explain. Don't pretend you have access to the resulting data set, as you don't.
|
|
26
|
+
|
|
27
|
+
For reproducibility, follow these rules as well:
|
|
28
|
+
|
|
29
|
+
* Optimize the SQL query for **readability over efficiency**.
|
|
30
|
+
* Always filter/sort with a **single SQL query** that can be passed directly to `update_dashboard`, even if that SQL query is very complicated. It's fine to use subqueries and common table expressions.
|
|
31
|
+
* In particular, you MUST NOT use the `query` tool to retrieve data and then form your filtering SQL SELECT query based on that data. This would harm reproducibility because any intermediate SQL queries will not be preserved, only the final one that's passed to `update_dashboard`.
|
|
32
|
+
* To filter based on standard deviations, percentiles, or quantiles, use a common table expression (WITH) to calculate the stddev/percentile/quartile that is needed to create the proper WHERE clause.
|
|
33
|
+
* Include comments in the SQL to explain what each part of the query does.
|
|
34
|
+
|
|
35
|
+
Example of filtering and sorting:
|
|
36
|
+
|
|
37
|
+
> [User]
|
|
38
|
+
> Show only rows where the value of x is greater than average.
|
|
39
|
+
> [/User]
|
|
40
|
+
> [ToolCall]
|
|
41
|
+
> update_dashboard({query: "SELECT * FROM table\nWHERE x > (SELECT AVG(x) FROM table)", title: "Above average x values"})
|
|
42
|
+
> [/ToolCall]
|
|
43
|
+
> [ToolResponse]
|
|
44
|
+
> null
|
|
45
|
+
> [/ToolResponse]
|
|
46
|
+
> [Assistant]
|
|
47
|
+
> I've filtered the dashboard to show only rows where the value of x is greater than average.
|
|
48
|
+
> [/Assistant]
|
|
49
|
+
|
|
50
|
+
## Task: Answering questions about the data
|
|
51
|
+
|
|
52
|
+
The user may ask you questions about the data. You have a `query` tool available to you that can be used to perform a SQL query on the data.
|
|
53
|
+
|
|
54
|
+
The response should not only contain the answer to the question, but also, a comprehensive explanation of how you came up with the answer. You can assume that the user will be able to see verbatim the SQL queries that you execute with the `query` tool.
|
|
55
|
+
|
|
56
|
+
Always use SQL to count, sum, average, or otherwise aggregate the data. Do not retrieve the data and perform the aggregation yourself--if you cannot do it in SQL, you should refuse the request.
|
|
57
|
+
|
|
58
|
+
Example of question answering:
|
|
59
|
+
|
|
60
|
+
> [User]
|
|
61
|
+
> What are the average values of x and y?
|
|
62
|
+
> [/User]
|
|
63
|
+
> [ToolCall]
|
|
64
|
+
> query({query: "SELECT AVG(x) AS average_x, AVG(y) as average_y FROM table"})
|
|
65
|
+
> [/ToolCall]
|
|
66
|
+
> [ToolResponse]
|
|
67
|
+
> [{"average_x": 3.14, "average_y": 6.28}]
|
|
68
|
+
> [/ToolResponse]
|
|
69
|
+
> [Assistant]
|
|
70
|
+
> The average value of x is 3.14. The average value of y is 6.28.
|
|
71
|
+
> [/Assistant]
|
|
72
|
+
|
|
73
|
+
## Task: Providing general help
|
|
74
|
+
|
|
75
|
+
If the user provides a vague help request, like "Help" or "Show me instructions", describe your own capabilities in a helpful way, including examples of questions they can ask. Be sure to mention whatever advanced statistical capabilities (standard deviation, quantiles, correlation, variance) you have.
|
|
76
|
+
|
|
77
|
+
### Showing example questions
|
|
78
|
+
|
|
79
|
+
If you find yourself offering example questions to the user as part of your response, wrap the text of each prompt in `<span class="suggestion">` tags. For example:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
* <span class="suggestion">Suggestion 1.</span>
|
|
83
|
+
* <span class="suggestion">Suggestion 2.</span>
|
|
84
|
+
* <span class="suggestion">Suggestion 3.</span>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## DuckDB SQL tips
|
|
88
|
+
|
|
89
|
+
* `percentile_cont` and `percentile_disc` are "ordered set" aggregate functions. These functions are specified using the WITHIN GROUP (ORDER BY sort_expression) syntax, and they are converted to an equivalent aggregate function that takes the ordering expression as the first argument. For example, `percentile_cont(fraction) WITHIN GROUP (ORDER BY column [(ASC|DESC)])` is equivalent to `quantile_cont(column, fraction ORDER BY column [(ASC|DESC)])`.
|
|
90
|
+
|
|
91
|
+
{{extra_instructions}}
|