|
| 1 | +# querychat |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +querychat is a multilingual package that allows users to chat with their data using natural language queries. It's available for: |
| 6 | + |
| 7 | +- R (Shiny) |
| 8 | +- Python (Shiny for Python) |
| 9 | + |
| 10 | +The core functionality translates natural language queries into SQL statements that are executed against data sources. This approach ensures reliability, transparency, and reproducibility by: |
| 11 | +1. Leveraging LLMs' strengths in writing SQL |
| 12 | +2. Providing transparency with visible SQL queries |
| 13 | +3. Enabling reproducibility through reusable queries |
| 14 | + |
| 15 | +## Repository Structure |
| 16 | + |
| 17 | +The repository contains separate packages for R and Python: |
| 18 | + |
| 19 | +``` |
| 20 | +/ |
| 21 | +├── pkg-r/ # R package implementation |
| 22 | +│ ├── R/ # R source files |
| 23 | +│ ├── inst/ # Installed files |
| 24 | +│ │ ├── examples-shiny/ # Shiny example applications |
| 25 | +│ │ ├── htmldep/ # HTML dependencies |
| 26 | +│ │ └── prompts/ # Prompt templates |
| 27 | +│ └── tests/ # testthat test suite |
| 28 | +│ |
| 29 | +├── pkg-py/ # Python package implementation |
| 30 | +│ ├── src/ # Python source files |
| 31 | +│ ├── tests/ # pytest test suite |
| 32 | +│ └── examples/ # Example applications |
| 33 | +│ |
| 34 | +├── docs/ # Documentation site |
| 35 | +├── _dev/ # Development utilities and demos (local scratch space only) |
| 36 | +``` |
| 37 | + |
| 38 | +## Common Commands |
| 39 | + |
| 40 | +### Python Package |
| 41 | + |
| 42 | +We use `uv` for Python package management and `make` for common tasks. |
| 43 | + |
| 44 | +```bash |
| 45 | +# Setup Python environment |
| 46 | +make py-setup |
| 47 | + |
| 48 | +# Run Python checks (format, types, tests) |
| 49 | +make py-check |
| 50 | +make py-check-format |
| 51 | +make py-check-types |
| 52 | +make py-check-tests |
| 53 | + |
| 54 | +# Format Python code |
| 55 | +make py-format |
| 56 | + |
| 57 | +# Build Python package |
| 58 | +make py-build |
| 59 | + |
| 60 | +# Build Python documentation |
| 61 | +make py-docs |
| 62 | +``` |
| 63 | + |
| 64 | +### R Package |
| 65 | + |
| 66 | +```bash |
| 67 | +# Install R dependencies |
| 68 | +make r-setup |
| 69 | + |
| 70 | +# Run R checks (format, tests, package) |
| 71 | +make r-check |
| 72 | +make r-check-format |
| 73 | +make r-check-tests |
| 74 | +make r-check-package |
| 75 | + |
| 76 | +# Format R code |
| 77 | +make r-format |
| 78 | + |
| 79 | +# Document R package |
| 80 | +make r-document |
| 81 | + |
| 82 | +# Build R documentation |
| 83 | +make r-docs |
| 84 | +``` |
| 85 | + |
| 86 | +### Documentation |
| 87 | + |
| 88 | +```bash |
| 89 | +# Build all documentation |
| 90 | +make docs |
| 91 | + |
| 92 | +# Preview R docs |
| 93 | +make r-docs-preview |
| 94 | + |
| 95 | +# Preview Python docs |
| 96 | +make py-docs-preview |
| 97 | +``` |
| 98 | + |
| 99 | +## Code Architecture |
| 100 | + |
| 101 | +### Core Components |
| 102 | + |
| 103 | +1. **Data Sources**: Abstractions for data frames and database connections that provide schema information and execute SQL queries |
| 104 | + - R: `querychat_data_source()` in `pkg-r/R/data_source.R` |
| 105 | + - Python: `DataSource` classes in `pkg-py/src/querychat/datasource.py` |
| 106 | + |
| 107 | +2. **LLM Client**: Integration with LLM providers (OpenAI, Anthropic, etc.) through: |
| 108 | + - R: ellmer package |
| 109 | + - Python: chatlas package |
| 110 | + |
| 111 | +3. **Query Chat Interface**: UI components and server logic for the chat experience: |
| 112 | + - R: `querychat_sidebar()`, `querychat_ui()`, and `querychat_server()` in `pkg-r/R/querychat.R` |
| 113 | + - Python: `QueryChat` class in `pkg-py/src/querychat/querychat.py` |
| 114 | + |
| 115 | +4. **Prompt Engineering**: System prompts and tool definitions that guide the LLM: |
| 116 | + - R: `pkg-r/inst/prompts/` |
| 117 | + - Main prompt (`prompt.md`) |
| 118 | + - Tool descriptions (`tool-query.md`, `tool-reset-dashboard.md`, `tool-update-dashboard.md`) |
| 119 | + - Python: `pkg-py/src/querychat/prompts/` |
| 120 | + - Main prompt (`prompt.md`) |
| 121 | + - Tool descriptions (`tool-query.md`, `tool-reset-dashboard.md`, `tool-update-dashboard.md`) |
| 122 | + |
| 123 | +### Data Flow |
| 124 | + |
| 125 | +1. User enters a natural language query in the UI |
| 126 | +2. The query is sent to an LLM along with schema information |
| 127 | +3. The LLM generates a SQL query using tool calling |
| 128 | +4. The SQL query is executed against the data source |
| 129 | +5. Results are returned and displayed in the UI |
| 130 | +6. The SQL query is also displayed for transparency |
| 131 | + |
| 132 | +## Recommendations for Development |
| 133 | + |
| 134 | +1. Always test changes with both R and Python implementations to maintain consistency |
| 135 | +2. Use the provided Make commands for development tasks |
| 136 | +3. Follow the existing code style (ruff for Python, `air format .` for R) |
| 137 | +4. Ask before running tests (the user may want to run them themselves) |
| 138 | +5. Update documentation when adding new features |
| 139 | +6. Always ask about file names before writing any new code |
| 140 | +7. Always pay attention to your working directory when running commands, especially when working in a sub-package. |
| 141 | +8. When planning, talk through all function and argument names, file names and locations. |
0 commit comments