agentic-data-analyst 0.4.0__py3-none-any.whl

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.
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: agentic-data-analyst
3
+ Version: 0.4.0
4
+ Summary: One assistant for your data and your docs: reviewed read-only SQL over DuckDB, cited full-text search over Markdown, guards that cap the cost
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Requires-Dist: anthropic>=2.3.0
8
+ Requires-Dist: cachetools>=6.0.0
9
+ Requires-Dist: duckdb>=1.4.0
10
+ Requires-Dist: limits>=5.0.0
11
+ Requires-Dist: markdown-it-py>=4.0.0
12
+ Requires-Dist: pydantic>=2.0.0
13
+ Requires-Dist: chainlit>=2.12.0 ; extra == 'demo'
14
+ Requires-Dist: loguru>=0.7.3 ; extra == 'demo'
15
+ Requires-Dist: pandas>=2.0.0 ; extra == 'demo'
16
+ Requires-Dist: plotly>=6.0.0 ; extra == 'demo'
17
+ Requires-Dist: python-dotenv>=1.0.0 ; extra == 'demo'
18
+ Requires-Python: >=3.12
19
+ Provides-Extra: demo
20
+ Description-Content-Type: text/markdown
21
+
22
+ # agentic-data-analyst
23
+
24
+ ![A robot detective at a desk answers a visitor's question from the bookshelf of docs and the database of data, while a second robot checks the answer and a coin jar keeps the costs](https://raw.githubusercontent.com/datenwissenschaften/agentic-data-analyst/main/assets/banner.webp)
25
+
26
+ **One assistant for your data and your docs: people ask in plain language and get answers they can check, at a cost
27
+ you set.** Numbers come from your DuckDB data through SQL that Claude writes and a second agent reviews; explanations
28
+ come from your Markdown pages through a full-text search, and every page used is cited. When neither the data nor the
29
+ docs hold the answer, the assistant says so instead of guessing. Deterministic guards decide what may run and what it
30
+ may cost.
31
+
32
+ It runs [RetroSpeedLab](https://www.retrospeedlab.com)'s **Ask me anything**: visitors at
33
+ [ask.retrospeedlab.com](https://ask.retrospeedlab.com) and `/ask` on Discord ask about the AI's speedruns, records,
34
+ games, highscores and streams, and how the studio works. Both come from one nightly DuckDB snapshot: the lake's public
35
+ tables and the docs-site and website pages, answered on a cheap model within a daily budget.
36
+
37
+ Without data tables it is a **website chat**: a company answers visitors from its own pages, each answer linking them.
38
+
39
+ ## Why
40
+
41
+ - **Self-service answers** without a dashboard or a support ticket for every question, and without database access.
42
+ - **Answers you can check**: every figure comes from a query you can inspect, every explanation links its page.
43
+ - **No guessing**: the model answers only from what its tools returned and says when they do not hold the answer.
44
+ - **Safe by construction**: one read-only, sandboxed `SELECT` over an allowlist of tables; the guards hold whatever
45
+ the model is told.
46
+ - **Predictable cost**: a cheap model, cached answers, per-person rate limits and a hard daily budget.
47
+ - **Nothing extra to host**: the docs index is BM25 in the same DuckDB file as the data; no embeddings, no vector store.
48
+
49
+ ## Modes
50
+
51
+ The mode is part of the `Domain` you configure; it is never guessed from the database.
52
+
53
+ | Mode | Answers from | Tools | Example |
54
+ | --- | --- | --- | --- |
55
+ | `data_and_docs` | Tables and pages | `search_docs` and the data tools | RetroSpeedLab's Ask me anything |
56
+ | `docs` | Pages only; politely declines data questions | `search_docs` | A company website chat |
57
+ | `data` | Tables only | `list_tables`, `describe_table`, `run_sql`, `show_table`, `show_chart` | An internal data desk |
58
+
59
+ ## Use it as a library
60
+
61
+ ```bash
62
+ uv add "agentic-data-analyst @ git+https://github.com/datenwissenschaften/agentic-data-analyst"
63
+ ```
64
+
65
+ Index the pages into the DuckDB file once per snapshot, next to the data tables:
66
+
67
+ ```python
68
+ from pathlib import Path
69
+
70
+ from agentic_data_analyst import index_docs, read_pages
71
+
72
+ pages = read_pages(Path("docs-site/pages"), lambda page: f"https://docs.retrospeedlab.com/{page.with_suffix('.html').as_posix()}")
73
+ index_docs(Path("retrospeedlab.duckdb"), pages, "porter") # the stemmer for the pages' language, e.g. "german"
74
+ ```
75
+
76
+ Each page is split at its headings into passages with the page title, heading, URL and text. Pages built some other
77
+ way go in as `Page(url, markdown)`.
78
+
79
+ Then answer questions:
80
+
81
+ ```python
82
+ from datetime import UTC, datetime
83
+
84
+ import anthropic
85
+ from agentic_data_analyst import (
86
+ Analyst,
87
+ AnswerCache,
88
+ DailyBudget,
89
+ Domain,
90
+ GuardedAnalyst,
91
+ Limits,
92
+ Models,
93
+ RateLimit,
94
+ connect,
95
+ network_key,
96
+ )
97
+
98
+ domain = Domain(
99
+ about="RetroSpeedLab, a game studio run by AI agents: an AI speedruns its own 8-bit games and humans race it",
100
+ mode="data_and_docs",
101
+ sql_rules="- Lower seconds are better; only won runs have a record.",
102
+ review_rules="- times compared across games without saying so.",
103
+ )
104
+ limits = Limits(max_question_chars=300, max_model_rounds=6, max_queries=3, answer_budget_usd=0.05)
105
+ models = Models(analyst="claude-haiku-4-5", analyst_effort="low", reviewer="claude-haiku-4-5", reviewer_effort="low")
106
+ analyst = Analyst(anthropic.AsyncAnthropic(api_key="..."), connect(Path("retrospeedlab.duckdb")), models, limits, domain)
107
+
108
+ answer = await analyst.answer("How does the speedrun search work, and which game has the fastest AI record?")
109
+ # answer.text, answer.queries (SQL with rows), answer.passages (the pages it found), answer.usage, answer.complete
110
+
111
+ guarded = GuardedAnalyst(
112
+ analyst,
113
+ DailyBudget(Path("state/budget.json"), 2.00, limits.answer_budget_usd, lambda: datetime.now(UTC)),
114
+ AnswerCache(500, 6 * 3600),
115
+ burst=RateLimit("20/minute"),
116
+ networks=RateLimit("15/hour"),
117
+ )
118
+ visitors = RateLimit("5/hour")
119
+ reply = await guarded.ask("Which game has the fastest AI record?", visitors, "web:203.0.113.7", network_key("203.0.113.7"))
120
+ ```
121
+
122
+ A docs-only website chat indexes only its pages and sets
123
+ `Domain(about="...", mode="docs", sql_rules=None, review_rules=None)`.
124
+
125
+ `Analyst.converse(history, observe)` answers within a chat history and reports each step, for chat UIs.
126
+
127
+ The search uses DuckDB's `fts` extension. `index_docs` installs it; the sandboxed connection only loads it, so install
128
+ it once where the app runs, e.g. at image build:
129
+ `python -c "import duckdb; duckdb.install_extension('fts')"`.
130
+
131
+ ## Guards
132
+
133
+ | Guard | What it stops | Where |
134
+ | --- | --- | --- |
135
+ | Question check | Empty and over-long questions, before any model call | `check_question` |
136
+ | Answer cache | Paying twice for the same question (case and spacing ignored); only complete answers are kept | `AnswerCache` |
137
+ | Rate limit | One person asking too much, per asker key in a moving window | `RateLimit` (`limits`) |
138
+ | Network limit | One host rotating addresses: a limit per IPv4 /24 or IPv6 /64 | `network_key`, `GuardedAnalyst` |
139
+ | Burst cap | A flood from many askers: a limit on everyone's questions per minute | `GuardedAnalyst` |
140
+ | Daily budget | The total bill: released in 24 hourly shares that roll forward within the UTC day, so a flood spends only what is released; each question reserves its answer budget first, so parallel questions cannot pass the limit together; spending survives restarts | `DailyBudget` |
141
+ | Rounds and queries | Runaway loops: at most `max_model_rounds` model calls per question, docs searches included, and `max_queries` reviewed queries | `Limits` |
142
+ | Answer budget | One expensive answer: it stops before the next call once it cost more than `answer_budget_usd` | `Limits` |
143
+ | Prices | Unbounded bills: a model without a known price is refused at start | `Models` |
144
+ | Mode | Data tools in a docs-only chat and the docs search in a data-only one | `Domain` |
145
+ | Statement | Anything but exactly one `SELECT` (DDL, DML, `COPY`, `ATTACH`, `SET`, `INSTALL`), from DuckDB's parser | `check_sql` |
146
+ | Allowlist | Tables outside the file's own (the docs index included), file and system table functions, from DuckDB's syntax tree | `check_sql` |
147
+ | Sandbox | Writes, files, network and further extensions: read-only, `enable_external_access=false`, 1 GB, 2 threads, locked settings | `connect` |
148
+ | Runtime | Slow and huge queries: 20 s deadline, 200 rows, 8,000 characters of SQL; at most 5 passages of 2,000 characters per search | `run_query`, `search_docs` |
149
+
150
+ The limits count from the narrowest out (asker, network, everyone), so one asker over their own limit uses up nobody
151
+ else's share. The reviewer agent catches *wrong* SQL; the deterministic layers keep the system safe whatever either
152
+ model does. Docs searches read public pages, so they need no review. An answer may overshoot its budget by the one
153
+ model round that crossed it; the daily budget books the real cost.
154
+
155
+ ## Flow
156
+
157
+ ```mermaid
158
+ flowchart LR
159
+ Q[Question] --> G{Guards}
160
+ G -->|ok| A[Assistant]
161
+ A -->|search_docs| P[(Pages, BM25)]
162
+ P -->|passages with URLs| A
163
+ A -->|list_tables / describe_table| M[(Schema only)]
164
+ A -->|run_sql| C{Deterministic checks}
165
+ C -->|ok| R[SQL reviewer agent]
166
+ R -->|approved| D[(DuckDB, read-only)]
167
+ D -->|rows| A
168
+ A --> Ans[Answer with its queries and cited pages]
169
+ ```
170
+
171
+ ## Demo chat
172
+
173
+ A [Chainlit](https://chainlit.io) chat over a synthetic database of humans and AI agents racing five RetroSpeedLab
174
+ games and two demo pages in `data/docs` (the levels, rules, runners and runs are invented), with tables and Plotly
175
+ charts on request.
176
+
177
+ ```bash
178
+ make install
179
+ cp .env.example .env # set ANTHROPIC_API_KEY
180
+ make run # builds data/speedruns.duckdb with the pages, then opens http://localhost:8000
181
+ make check # ruff, strict mypy, pytest-bdd; the models are scripted, so no key is needed
182
+ ```
183
+
184
+ Docker: `docker build -t agentic-data-analyst . && docker run --rm -p 8000:8000 --env-file .env agentic-data-analyst`
185
+
186
+ ## Layout
187
+
188
+ | Module | Responsibility |
189
+ | --- | --- |
190
+ | `agent.py` | `Analyst`: prompt, tools per mode, the bounded tool loop |
191
+ | `docs.py` | Pages into passages, the full-text index, `search_docs` |
192
+ | `reviewer.py` | The SQL reviewer agent (structured output) |
193
+ | `database.py` | Sandboxed connection, schema inspection, SQL checks, bounded execution |
194
+ | `guards.py`, `guarded.py` | The guards and `GuardedAnalyst`, which puts them in front of an analyst |
195
+ | `config.py`, `usage.py` | `Models`, `Limits`, `Domain` with its mode; token cost |
196
+ | `charts.py` | Chart requests the analyst may make |
197
+ | `app.py`, `figures.py`, `seed.py`, `settings.py`, `log.py` | The demo (`demo` extra) |
198
+
199
+ ## Limitations
200
+
201
+ - BM25 matches words, not meaning: a question worded unlike the pages may find nothing. The assistant then searches
202
+ once more with other words, or says it does not know.
203
+ - Prompt injection can still make the analyst write a *different* `SELECT`; the guards limit any query to reading the
204
+ allowed tables within the deadline, the row cap and the budget.
205
+ - Rate limits and the cache live in one process; run one process per database, or put both in a shared store.
206
+ - Answer quality and injection resistance against the real models are not covered by the automated tests.
207
+
208
+ ## License
209
+
210
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,19 @@
1
+ agentic_data_analyst/__init__.py,sha256=PNQQHg31ICRSI3S3-clLugPnG8smcyVtgpyaZ86q5II,1028
2
+ agentic_data_analyst/agent.py,sha256=JsdqxGC4CBTP57mDNxIAgk1LEWlW2IthYxfiw2nPG1M,17315
3
+ agentic_data_analyst/charts.py,sha256=tHU1bLIXdoY9AnvUGX_yiVzLa-WhKCUKt6Et_tzWSpM,1118
4
+ agentic_data_analyst/config.py,sha256=1-UbhI5iZ4GbrgbkRed5niMMDf-lfNCYT1Vn3JeQn2s,3877
5
+ agentic_data_analyst/database.py,sha256=pd90FAuW0e8u82t0STzSkqQIzOUjZ9G64Qm9l0NkJbg,9124
6
+ agentic_data_analyst/docs.py,sha256=2fvWkQzGVWXl_6_85QPWlJIo88jbpTSbEhR-omy1OnI,5560
7
+ agentic_data_analyst/figures.py,sha256=yPxV6afJm37JM07q9xmTZBC0eDGjiMyV8S3weXHR-84,867
8
+ agentic_data_analyst/guarded.py,sha256=n02_0KXO1KGgZDLNMn7osQtA2C9haigQg2K5p2nWiuA,3889
9
+ agentic_data_analyst/guards.py,sha256=Bgfq7168NC9AgABs_AojwpSuHvdqF-cWuANHacks-P4,5547
10
+ agentic_data_analyst/log.py,sha256=NnQytcl1dxXhu2pAd8Y9cYR5k2H90mkvQISnmiH6-Iw,1493
11
+ agentic_data_analyst/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ agentic_data_analyst/reviewer.py,sha256=X6Wi799NNUqvcmuHqy1OYWtJLCNg63kUz9zdHLD2_2s,2492
13
+ agentic_data_analyst/seed.py,sha256=l7h4DRwC9BK4eiolLqIaj_tQ1GViLXz_qDkHKSjebzE,16095
14
+ agentic_data_analyst/settings.py,sha256=TwJM93zmrOB_YScuEaclL7QxzCrqJUx9G3OQ73RhHOM,1857
15
+ agentic_data_analyst/usage.py,sha256=gIT1KpkWpB0-w4apS3pBMdxOV2Ni40QwbO2Gl07U_9I,2587
16
+ agentic_data_analyst-0.4.0.dist-info/licenses/LICENSE,sha256=rFweHr6Uve-f5o0ukIZJg-DSEeRsilFq--cOP3oWE_Q,1090
17
+ agentic_data_analyst-0.4.0.dist-info/WHEEL,sha256=kiPuyMTIXtn3KwIlb6zIkrxfbMkdQGyzN-kjAsvva48,81
18
+ agentic_data_analyst-0.4.0.dist-info/METADATA,sha256=zvd8TDT5ETO6O2tZKjx7GsPwUbXYhRHMazv0eBG3Zbc,10816
19
+ agentic_data_analyst-0.4.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.13.12
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 agentic-data-analyst contributors
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.