pynlqe 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.
- pynlqe-0.1.0/.coveragerc +17 -0
- pynlqe-0.1.0/.dockerignore +15 -0
- pynlqe-0.1.0/.env.example +19 -0
- pynlqe-0.1.0/.github/workflows/ci.yml +47 -0
- pynlqe-0.1.0/.github/workflows/publish.yml +43 -0
- pynlqe-0.1.0/.gitignore +14 -0
- pynlqe-0.1.0/.python-version +1 -0
- pynlqe-0.1.0/CHANGELOG.md +33 -0
- pynlqe-0.1.0/Dockerfile +33 -0
- pynlqe-0.1.0/EXAMPLE_QUERIES.md +322 -0
- pynlqe-0.1.0/FILE_INVENTORY.md +210 -0
- pynlqe-0.1.0/IMPLEMENTATION_SUMMARY.md +506 -0
- pynlqe-0.1.0/LICENSE +21 -0
- pynlqe-0.1.0/Makefile +42 -0
- pynlqe-0.1.0/PKG-INFO +194 -0
- pynlqe-0.1.0/README.md +162 -0
- pynlqe-0.1.0/create_sample_data.py +168 -0
- pynlqe-0.1.0/docs/API.md +715 -0
- pynlqe-0.1.0/docs/ARCHITECTURE.md +679 -0
- pynlqe-0.1.0/docs/DESIGN.md +570 -0
- pynlqe-0.1.0/docs/FAQ.md +630 -0
- pynlqe-0.1.0/docs/ROADMAP.md +695 -0
- pynlqe-0.1.0/docs/TESTING.md +744 -0
- pynlqe-0.1.0/fixtures/README.md +222 -0
- pynlqe-0.1.0/fixtures/customers.parquet +0 -0
- pynlqe-0.1.0/fixtures/example_queries.yaml +327 -0
- pynlqe-0.1.0/fixtures/golden_datasets.yaml +22132 -0
- pynlqe-0.1.0/fixtures/products.parquet +0 -0
- pynlqe-0.1.0/fixtures/regions.parquet +0 -0
- pynlqe-0.1.0/fixtures/transactions.parquet +0 -0
- pynlqe-0.1.0/main.py +6 -0
- pynlqe-0.1.0/prototype.ipynb +298 -0
- pynlqe-0.1.0/prototype_advanced.ipynb +504 -0
- pynlqe-0.1.0/pyproject.toml +62 -0
- pynlqe-0.1.0/src/nlqe/__init__.py +81 -0
- pynlqe-0.1.0/src/nlqe/config.py +83 -0
- pynlqe-0.1.0/src/nlqe/conversation/__init__.py +5 -0
- pynlqe-0.1.0/src/nlqe/conversation/manager.py +163 -0
- pynlqe-0.1.0/src/nlqe/datasource/__init__.py +6 -0
- pynlqe-0.1.0/src/nlqe/datasource/introspector.py +204 -0
- pynlqe-0.1.0/src/nlqe/datasource/manager.py +83 -0
- pynlqe-0.1.0/src/nlqe/duckdb/__init__.py +5 -0
- pynlqe-0.1.0/src/nlqe/duckdb/executor.py +204 -0
- pynlqe-0.1.0/src/nlqe/engine.py +262 -0
- pynlqe-0.1.0/src/nlqe/llm/__init__.py +15 -0
- pynlqe-0.1.0/src/nlqe/llm/client.py +354 -0
- pynlqe-0.1.0/src/nlqe/openai/__init__.py +10 -0
- pynlqe-0.1.0/src/nlqe/openai/client.py +5 -0
- pynlqe-0.1.0/src/nlqe/query/__init__.py +5 -0
- pynlqe-0.1.0/src/nlqe/query/loop.py +206 -0
- pynlqe-0.1.0/src/nlqe/synthesis/__init__.py +5 -0
- pynlqe-0.1.0/src/nlqe/synthesis/answer.py +33 -0
- pynlqe-0.1.0/src/nlqe/testing/__init__.py +28 -0
- pynlqe-0.1.0/src/nlqe/testing/cli.py +255 -0
- pynlqe-0.1.0/src/nlqe/testing/datasets.py +197 -0
- pynlqe-0.1.0/src/nlqe/testing/evaluator.py +318 -0
- pynlqe-0.1.0/src/nlqe/testing/metrics.py +386 -0
- pynlqe-0.1.0/src/nlqe/testing/reporter.py +289 -0
- pynlqe-0.1.0/src/nlqe/types.py +107 -0
- pynlqe-0.1.0/src/nlqe/utils/__init__.py +48 -0
- pynlqe-0.1.0/src/nlqe/utils/errors.py +115 -0
- pynlqe-0.1.0/src/nlqe/utils/logging.py +37 -0
- pynlqe-0.1.0/tests/__init__.py +0 -0
- pynlqe-0.1.0/tests/conftest.py +281 -0
- pynlqe-0.1.0/tests/integration/__init__.py +0 -0
- pynlqe-0.1.0/tests/integration/test_pipeline.py +222 -0
- pynlqe-0.1.0/tests/unit/__init__.py +0 -0
- pynlqe-0.1.0/tests/unit/core/__init__.py +0 -0
- pynlqe-0.1.0/tests/unit/core/test_config.py +49 -0
- pynlqe-0.1.0/tests/unit/core/test_engine.py +163 -0
- pynlqe-0.1.0/tests/unit/core/test_query_loop.py +167 -0
- pynlqe-0.1.0/tests/unit/datasource/test_introspector.py +101 -0
- pynlqe-0.1.0/tests/unit/duckdb/test_executor.py +140 -0
- pynlqe-0.1.0/tests/unit/llm/__init__.py +0 -0
- pynlqe-0.1.0/tests/unit/llm/test_client.py +184 -0
- pynlqe-0.1.0/tests/unit/synthesis/test_answer.py +23 -0
- pynlqe-0.1.0/tests/unit/testing/__init__.py +0 -0
- pynlqe-0.1.0/tests/unit/testing/test_cli.py +177 -0
- pynlqe-0.1.0/tests/unit/testing/test_datasets.py +193 -0
- pynlqe-0.1.0/tests/unit/testing/test_evaluator.py +222 -0
- pynlqe-0.1.0/tests/unit/testing/test_metrics.py +202 -0
- pynlqe-0.1.0/tests/unit/testing/test_reporter.py +181 -0
- pynlqe-0.1.0/uv.lock +4369 -0
pynlqe-0.1.0/.coveragerc
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[run]
|
|
2
|
+
omit =
|
|
3
|
+
src/nlqe/openai/client.py
|
|
4
|
+
src/nlqe/openai/__init__.py
|
|
5
|
+
src/nlqe/testing/__init__.py
|
|
6
|
+
src/nlqe/utils/logging.py
|
|
7
|
+
|
|
8
|
+
[report]
|
|
9
|
+
exclude_lines =
|
|
10
|
+
pragma: no cover
|
|
11
|
+
def __repr__
|
|
12
|
+
if self.debug:
|
|
13
|
+
if __name__ == .__main__.:
|
|
14
|
+
if 0:
|
|
15
|
+
class .*\bProtocol\):
|
|
16
|
+
@(abc\.)?abstractmethod
|
|
17
|
+
except Exception as e:
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# OpenAI API Configuration
|
|
2
|
+
NLQE_OPENAI_API_KEY=sk-your-api-key-here
|
|
3
|
+
|
|
4
|
+
# OpenAI Model Settings (optional)
|
|
5
|
+
NLQE_OPENAI_MODEL=gpt-4
|
|
6
|
+
NLQE_OPENAI_TEMPERATURE=0.0
|
|
7
|
+
NLQE_OPENAI_MAX_TOKENS=2000
|
|
8
|
+
|
|
9
|
+
# Query Execution Settings (optional)
|
|
10
|
+
NLQE_QUERY_TIMEOUT_SECONDS=30
|
|
11
|
+
NLQE_MAX_DEBUG_ATTEMPTS=3
|
|
12
|
+
|
|
13
|
+
# Datasource Settings (optional)
|
|
14
|
+
# NLQE_DATASOURCE_PATH=./data/sales.parquet
|
|
15
|
+
# NLQE_DATASOURCE_TYPE=parquet
|
|
16
|
+
|
|
17
|
+
# Operational Settings (optional)
|
|
18
|
+
NLQE_LOG_LEVEL=INFO
|
|
19
|
+
NLQE_LOG_QUERIES=true
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main, master ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main, master ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
lint-and-test:
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.11", "3.12"]
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Install uv
|
|
19
|
+
uses: astral-sh/setup-uv@v3
|
|
20
|
+
with:
|
|
21
|
+
enable-cache: true
|
|
22
|
+
cache-dependency-path: uv.lock
|
|
23
|
+
|
|
24
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
25
|
+
uses: actions/setup-python@v5
|
|
26
|
+
with:
|
|
27
|
+
python-version: ${{ matrix.python-version }}
|
|
28
|
+
|
|
29
|
+
- name: Install dependencies
|
|
30
|
+
run: |
|
|
31
|
+
uv sync --frozen --all-extras
|
|
32
|
+
|
|
33
|
+
- name: Lint with ruff
|
|
34
|
+
run: |
|
|
35
|
+
make lint
|
|
36
|
+
|
|
37
|
+
- name: Run tests with pytest
|
|
38
|
+
run: |
|
|
39
|
+
make test-cov
|
|
40
|
+
|
|
41
|
+
- name: Upload coverage to Codecov
|
|
42
|
+
uses: codecov/codecov-action@v4
|
|
43
|
+
with:
|
|
44
|
+
file: ./coverage.xml
|
|
45
|
+
fail_ci_if_error: false
|
|
46
|
+
env:
|
|
47
|
+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*' # Run on tags like v0.1.0, v1.0.0, etc.
|
|
7
|
+
workflow_dispatch: # Allow manual triggering if needed
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build-and-publish:
|
|
11
|
+
name: Build and Publish
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
environment:
|
|
14
|
+
name: pypi
|
|
15
|
+
url: https://pypi.org/p/pynlqe
|
|
16
|
+
permissions:
|
|
17
|
+
id-token: write # Necessary for trusted publishing
|
|
18
|
+
contents: read # Required for actions/checkout
|
|
19
|
+
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
|
|
23
|
+
- name: Install uv
|
|
24
|
+
uses: astral-sh/setup-uv@v3
|
|
25
|
+
with:
|
|
26
|
+
enable-cache: true
|
|
27
|
+
cache-dependency-path: uv.lock
|
|
28
|
+
|
|
29
|
+
- name: Set up Python
|
|
30
|
+
uses: actions/setup-python@v5
|
|
31
|
+
with:
|
|
32
|
+
python-version: "3.11"
|
|
33
|
+
|
|
34
|
+
- name: Build package
|
|
35
|
+
run: |
|
|
36
|
+
uv build
|
|
37
|
+
|
|
38
|
+
- name: Publish to PyPI
|
|
39
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
40
|
+
# No password needed if you use Trusted Publishing in PyPI
|
|
41
|
+
# Otherwise, use secrets.PYPI_API_TOKEN:
|
|
42
|
+
# with:
|
|
43
|
+
# password: ${{ secrets.PYPI_API_TOKEN }}
|
pynlqe-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.14
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the Natural Language Query Engine (NLQE) will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [0.1.0] - 2026-03-30
|
|
9
|
+
|
|
10
|
+
This is the initial open-source release of the **Natural Language Query Engine (NLQE)**.
|
|
11
|
+
|
|
12
|
+
NLQE is a modular library designed to bridge the gap between human language and structured data. It leverages Large Language Models (LLMs) to automatically generate, validate, and execute SQL queries over in-process datasources.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
- **Core Engine API**: Programmatic `QueryEngine` for executing natural language queries against arbitrary datasets.
|
|
16
|
+
- **Datasource Introspection**: Automatic schema discovery for locally stored `.csv` and `.parquet` files via DuckDB.
|
|
17
|
+
- **LLM Integrations**:
|
|
18
|
+
- First-class support for OpenAI's language models (`gpt-4o`, `gpt-3.5-turbo`, etc.).
|
|
19
|
+
- Support for Anthropic's Claude (`claude-3-5-sonnet-20241022`, etc.) via `custom_llm_client`.
|
|
20
|
+
- Extensible `LLMClient` class that wraps LangChain integrations.
|
|
21
|
+
- **DuckDB Execution Layer**: Secure, in-memory analytical query engine utilizing `duckdb>=1.5.0` to read `parquet` and `csv` files directly.
|
|
22
|
+
- **Iterative Debug Loop**: Automatic error recovery system. When DuckDB encounters a SQL syntax or schema mismatch error, the LLM is provided the error context to self-correct and re-execute the query (up to 3 attempts by default).
|
|
23
|
+
- **Multi-turn Conversations**: Context-aware `start_conversation()` feature enabling users to ask follow-up questions referencing previous tabular results natively.
|
|
24
|
+
- **Evaluation Framework**: `nlqe.testing.cli` evaluation system using "golden datasets" to securely score LLM generation accuracy, completeness, and confidence calibration.
|
|
25
|
+
- **Safety Checks**: Built-in AST-level checks mapping out dangerous queries (e.g. `DROP`, `DELETE`, `TRUNCATE`) prior to database execution.
|
|
26
|
+
|
|
27
|
+
### Security
|
|
28
|
+
- Verified protection against SQL injection attacks targeting the local process.
|
|
29
|
+
- Strictly uncoupled architecture to ensure API keys are injected at runtime exclusively via environment variables (`NLQE_OPENAI_API_KEY`) or securely initiated configurations.
|
|
30
|
+
|
|
31
|
+
### Performance
|
|
32
|
+
- Fully decoupled in-process querying resulting in sub-500ms analytical execution overhead.
|
|
33
|
+
- Configurable timeouts limiting run-away query consumption.
|
pynlqe-0.1.0/Dockerfile
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Use an official Python runtime as a parent image
|
|
2
|
+
FROM python:3.11-slim
|
|
3
|
+
|
|
4
|
+
# Set the working directory in the container
|
|
5
|
+
WORKDIR /app
|
|
6
|
+
|
|
7
|
+
# Install system dependencies
|
|
8
|
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
9
|
+
build-essential \
|
|
10
|
+
curl \
|
|
11
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
12
|
+
|
|
13
|
+
# Install uv
|
|
14
|
+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
|
|
15
|
+
|
|
16
|
+
# Copy the project files
|
|
17
|
+
COPY pyproject.toml uv.lock ./
|
|
18
|
+
COPY README.md ./
|
|
19
|
+
|
|
20
|
+
# Install dependencies
|
|
21
|
+
RUN uv sync --frozen --no-dev
|
|
22
|
+
|
|
23
|
+
# Copy the rest of the application code
|
|
24
|
+
COPY src/ ./src/
|
|
25
|
+
COPY fixtures/ ./fixtures/
|
|
26
|
+
|
|
27
|
+
# Set environment variables
|
|
28
|
+
ENV PYTHONPATH="/app/src:${PYTHONPATH}"
|
|
29
|
+
ENV PYTHONUNBUFFERED=1
|
|
30
|
+
|
|
31
|
+
# Command to run the application (assuming main.py is the entry point)
|
|
32
|
+
# Adjust if there's a specific CLI command
|
|
33
|
+
CMD ["uv", "run", "python", "-m", "nlqe.testing.cli", "--help"]
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
# Example Queries for NLQE Testing
|
|
2
|
+
|
|
3
|
+
This document lists all 25+ example query patterns organized by complexity and SQL feature.
|
|
4
|
+
|
|
5
|
+
## Simple Queries (Easy - Good for Testing Basics)
|
|
6
|
+
|
|
7
|
+
### 1. Count Transactions
|
|
8
|
+
**Question**: "How many transactions are in the dataset?"
|
|
9
|
+
**Difficulty**: Easy
|
|
10
|
+
**SQL Feature**: COUNT(*)
|
|
11
|
+
**Expected Approach**: Basic aggregation without WHERE clause
|
|
12
|
+
|
|
13
|
+
### 2. Total Revenue
|
|
14
|
+
**Question**: "What is the total revenue from all transactions?"
|
|
15
|
+
**Difficulty**: Easy
|
|
16
|
+
**SQL Feature**: SUM()
|
|
17
|
+
**Expected Approach**: Basic SUM aggregation
|
|
18
|
+
|
|
19
|
+
### 3. Average Amount
|
|
20
|
+
**Question**: "What is the average transaction amount?"
|
|
21
|
+
**Difficulty**: Easy
|
|
22
|
+
**SQL Feature**: AVG()
|
|
23
|
+
**Expected Approach**: Simple average calculation
|
|
24
|
+
|
|
25
|
+
### 4. Filter by Category
|
|
26
|
+
**Question**: "Show me all Electronics transactions"
|
|
27
|
+
**Difficulty**: Easy
|
|
28
|
+
**SQL Feature**: WHERE with string comparison
|
|
29
|
+
**Expected Approach**: Filter single table by category
|
|
30
|
+
|
|
31
|
+
### 5. Filter by Date Range
|
|
32
|
+
**Question**: "Show transactions from February 2024"
|
|
33
|
+
**Difficulty**: Easy
|
|
34
|
+
**SQL Feature**: WHERE with date range (>=, <)
|
|
35
|
+
**Expected Approach**: Date range filtering
|
|
36
|
+
|
|
37
|
+
### 6. Filter by Amount
|
|
38
|
+
**Question**: "Show me transactions over 1000 dollars"
|
|
39
|
+
**Difficulty**: Easy
|
|
40
|
+
**SQL Feature**: WHERE with numeric comparison
|
|
41
|
+
**Expected Approach**: Simple numeric filter
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Grouping Queries (Medium - Test GROUP BY)
|
|
46
|
+
|
|
47
|
+
### 7. Revenue by Category
|
|
48
|
+
**Question**: "What is the total revenue by product category?"
|
|
49
|
+
**Difficulty**: Medium
|
|
50
|
+
**SQL Features**: GROUP BY, SUM(), ORDER BY
|
|
51
|
+
**Expected Approach**: GROUP BY with aggregation and sorting
|
|
52
|
+
|
|
53
|
+
### 8. Transaction Count by Category
|
|
54
|
+
**Question**: "How many transactions are there for each category?"
|
|
55
|
+
**Difficulty**: Medium
|
|
56
|
+
**SQL Features**: GROUP BY, COUNT(), ORDER BY
|
|
57
|
+
**Expected Approach**: Count transactions per category
|
|
58
|
+
|
|
59
|
+
### 9. Revenue by Region and Category
|
|
60
|
+
**Question**: "Show total revenue by region and category"
|
|
61
|
+
**Difficulty**: Medium
|
|
62
|
+
**SQL Features**: GROUP BY (multiple columns), SUM(), JOIN
|
|
63
|
+
**Expected Approach**: Multi-column GROUP BY with join
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Sorting and Limiting (Medium - Test ORDER BY and LIMIT)
|
|
68
|
+
|
|
69
|
+
### 10. Top 10 Transactions
|
|
70
|
+
**Question**: "Show the 10 largest transactions"
|
|
71
|
+
**Difficulty**: Easy
|
|
72
|
+
**SQL Features**: ORDER BY DESC, LIMIT
|
|
73
|
+
**Expected Approach**: Sort descending and limit results
|
|
74
|
+
|
|
75
|
+
### 11. Smallest Transactions
|
|
76
|
+
**Question**: "Show the 5 smallest transactions"
|
|
77
|
+
**Difficulty**: Easy
|
|
78
|
+
**SQL Features**: ORDER BY ASC, LIMIT
|
|
79
|
+
**Expected Approach**: Sort ascending for minimum values
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Join Queries (Medium - Test INNER and LEFT JOINs)
|
|
84
|
+
|
|
85
|
+
### 12. Revenue by Region Name
|
|
86
|
+
**Question**: "Show revenue by region name"
|
|
87
|
+
**Difficulty**: Medium
|
|
88
|
+
**SQL Features**: INNER JOIN, GROUP BY, SUM()
|
|
89
|
+
**Expected Approach**: Join transactions with regions table
|
|
90
|
+
|
|
91
|
+
### 13. Transactions with Details
|
|
92
|
+
**Question**: "Show transactions with customer name and region name"
|
|
93
|
+
**Difficulty**: Medium
|
|
94
|
+
**SQL Features**: Multiple JOINs, SELECT specific columns
|
|
95
|
+
**Expected Approach**: Denormalize data with two joins
|
|
96
|
+
|
|
97
|
+
### 14. All Customers and Spending
|
|
98
|
+
**Question**: "Show all customers and their total spending (including inactive)"
|
|
99
|
+
**Difficulty**: Medium
|
|
100
|
+
**SQL Features**: LEFT JOIN, GROUP BY, NULLS
|
|
101
|
+
**Expected Approach**: Preserve unmatched customers with LEFT JOIN
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Complex Queries (Hard - Advanced SQL Features)
|
|
106
|
+
|
|
107
|
+
### 15. Multi-Dimensional Revenue Analysis
|
|
108
|
+
**Question**: "Show revenue, transaction count, and average amount by region and category"
|
|
109
|
+
**Difficulty**: Hard
|
|
110
|
+
**SQL Features**: GROUP BY (multi-column), multiple aggregations
|
|
111
|
+
**Expected Approach**: Multiple aggregate functions with 2-level grouping
|
|
112
|
+
|
|
113
|
+
### 16. Customer Segmentation
|
|
114
|
+
**Question**: "Show spending analysis for Gold tier customers who are active"
|
|
115
|
+
**Difficulty**: Hard
|
|
116
|
+
**SQL Features**: WHERE multiple conditions, LEFT JOIN, multiple aggregations
|
|
117
|
+
**Expected Approach**: Filter on customer attributes, join with transactions
|
|
118
|
+
|
|
119
|
+
### 17. High-Value Customers
|
|
120
|
+
**Question**: "List customers with lifetime value over 10000 dollars and at least 5 transactions"
|
|
121
|
+
**Difficulty**: Hard
|
|
122
|
+
**SQL Features**: GROUP BY, HAVING clause, aggregate filtering
|
|
123
|
+
**Expected Approach**: HAVING clause to filter aggregated results
|
|
124
|
+
|
|
125
|
+
### 18. Return Rate by Category
|
|
126
|
+
**Question**: "Show return rate by category"
|
|
127
|
+
**Difficulty**: Hard
|
|
128
|
+
**SQL Features**: CASE WHEN, conditional aggregation, percentage calculation
|
|
129
|
+
**Expected Approach**: Use CASE WHEN for conditional counting
|
|
130
|
+
|
|
131
|
+
### 19. Quarterly Revenue Comparison
|
|
132
|
+
**Question**: "Show quarterly revenue and growth comparison"
|
|
133
|
+
**Difficulty**: Hard
|
|
134
|
+
**SQL Features**: QUARTER(), EXTRACT(), GROUP BY date components
|
|
135
|
+
**Expected Approach**: Date functions for temporal grouping
|
|
136
|
+
|
|
137
|
+
### 20. Profit Margin Analysis
|
|
138
|
+
**Question**: "Which category has the highest profit margin and is it growing?"
|
|
139
|
+
**Difficulty**: Hard
|
|
140
|
+
**SQL Features**: SUM(), AVG(), percentage calculations
|
|
141
|
+
**Expected Approach**: Calculate margins and percentages
|
|
142
|
+
|
|
143
|
+
### 21. Top Customers by Region
|
|
144
|
+
**Question**: "Show the top 3 spenders in each region"
|
|
145
|
+
**Difficulty**: Hard
|
|
146
|
+
**SQL Features**: Multiple JOINs, GROUP BY, LIMIT with grouping
|
|
147
|
+
**Expected Approach**: Top N per group pattern
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## Multi-Turn Conversation Examples
|
|
152
|
+
|
|
153
|
+
### 22. Regional Analysis (3-turn conversation)
|
|
154
|
+
**Turn 1**: "Which product category has the highest total revenue?"
|
|
155
|
+
**Turn 2**: "How many returns did we have in that category?"
|
|
156
|
+
**Turn 3**: "How does the return rate compare to other categories?"
|
|
157
|
+
**Purpose**: Test context preservation and follow-up understanding
|
|
158
|
+
|
|
159
|
+
### 23. Customer Analysis (3-turn conversation)
|
|
160
|
+
**Turn 1**: "Show me our Gold tier customers"
|
|
161
|
+
**Turn 2**: "What is their average lifetime value?"
|
|
162
|
+
**Turn 3**: "How does that compare to other tiers?"
|
|
163
|
+
**Purpose**: Test multi-level context and comparison
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## SQL Features Coverage Matrix
|
|
168
|
+
|
|
169
|
+
| Feature | Query | Difficulty |
|
|
170
|
+
|---------|-------|------------|
|
|
171
|
+
| COUNT(*) | #1 | Easy |
|
|
172
|
+
| SUM() | #2 | Easy |
|
|
173
|
+
| AVG() | #3 | Easy |
|
|
174
|
+
| WHERE (string) | #4 | Easy |
|
|
175
|
+
| WHERE (date range) | #5 | Easy |
|
|
176
|
+
| WHERE (numeric) | #6 | Easy |
|
|
177
|
+
| GROUP BY | #7-9 | Medium |
|
|
178
|
+
| ORDER BY | #10-11 | Easy |
|
|
179
|
+
| LIMIT | #10-11 | Easy |
|
|
180
|
+
| INNER JOIN | #12-13 | Medium |
|
|
181
|
+
| LEFT JOIN | #14 | Medium |
|
|
182
|
+
| Multiple JOINs | #13 | Medium |
|
|
183
|
+
| Multi-column GROUP BY | #9, #15 | Medium-Hard |
|
|
184
|
+
| Multiple Aggregations | #15, #20 | Hard |
|
|
185
|
+
| HAVING clause | #17 | Hard |
|
|
186
|
+
| CASE WHEN | #18 | Hard |
|
|
187
|
+
| Date Functions | #19 | Hard |
|
|
188
|
+
| Percentage Calc | #18, #20 | Hard |
|
|
189
|
+
| WHERE (multi-condition) | #16 | Hard |
|
|
190
|
+
| Top N per group | #21 | Hard |
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Query Difficulty Distribution
|
|
195
|
+
|
|
196
|
+
**Easy (6)**: Basic operations
|
|
197
|
+
- Simple aggregations (COUNT, SUM, AVG)
|
|
198
|
+
- Single table filtering
|
|
199
|
+
- Basic sorting and limiting
|
|
200
|
+
|
|
201
|
+
**Medium (10)**: Combine multiple concepts
|
|
202
|
+
- GROUP BY with aggregation
|
|
203
|
+
- Joining multiple tables
|
|
204
|
+
- Sorting with limiting
|
|
205
|
+
- Basic multi-column operations
|
|
206
|
+
|
|
207
|
+
**Hard (9+)**: Advanced patterns
|
|
208
|
+
- Multi-dimensional analysis
|
|
209
|
+
- Complex filtering with aggregation
|
|
210
|
+
- HAVING clauses
|
|
211
|
+
- Conditional aggregation (CASE WHEN)
|
|
212
|
+
- Date/time functions
|
|
213
|
+
- Top N per group
|
|
214
|
+
- Multi-turn conversation
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Running the Examples
|
|
219
|
+
|
|
220
|
+
### Option 1: Interactive Notebooks
|
|
221
|
+
```bash
|
|
222
|
+
# Basic examples
|
|
223
|
+
jupyter notebook prototype.ipynb
|
|
224
|
+
|
|
225
|
+
# Advanced examples with complex queries
|
|
226
|
+
jupyter notebook prototype_advanced.ipynb
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Option 2: Python API
|
|
230
|
+
```python
|
|
231
|
+
from nlqe import QueryEngine, QueryEngineConfig
|
|
232
|
+
|
|
233
|
+
config = QueryEngineConfig.from_env()
|
|
234
|
+
engine = QueryEngine(config)
|
|
235
|
+
engine.load_datasource("fixtures/transactions.parquet")
|
|
236
|
+
|
|
237
|
+
# Try any query
|
|
238
|
+
response = engine.query("Show revenue by region and category")
|
|
239
|
+
print(response.answer)
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Option 3: From YAML
|
|
243
|
+
See `fixtures/example_queries.yaml` for all 25+ examples in structured format with:
|
|
244
|
+
- Natural language question
|
|
245
|
+
- Expected SQL
|
|
246
|
+
- Explanation
|
|
247
|
+
- Difficulty level
|
|
248
|
+
- SQL features used
|
|
249
|
+
- Tags for filtering
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## Expected Accuracy by Category
|
|
254
|
+
|
|
255
|
+
Based on the design goals (>85% accuracy):
|
|
256
|
+
|
|
257
|
+
| Category | Expected Accuracy |
|
|
258
|
+
|----------|------------------|
|
|
259
|
+
| Simple Aggregations | >95% |
|
|
260
|
+
| Filtering | >90% |
|
|
261
|
+
| Single GROUP BY | >90% |
|
|
262
|
+
| Joins | 80-85% |
|
|
263
|
+
| Multi-column GROUP BY | 75-85% |
|
|
264
|
+
| Complex (HAVING, CASE) | 70-80% |
|
|
265
|
+
| Multi-turn Context | 75-85% |
|
|
266
|
+
|
|
267
|
+
**Note**: Accuracy improves with better examples and prompt engineering in Phase 2.
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Extending the Examples
|
|
272
|
+
|
|
273
|
+
To add more example queries to the system:
|
|
274
|
+
|
|
275
|
+
1. **Add to `fixtures/example_queries.yaml`:**
|
|
276
|
+
```yaml
|
|
277
|
+
- id: "unique_id"
|
|
278
|
+
category: "category_name"
|
|
279
|
+
difficulty: "easy|medium|hard"
|
|
280
|
+
description: "What this tests"
|
|
281
|
+
question: "Natural language question"
|
|
282
|
+
sql: "Expected SQL"
|
|
283
|
+
explanation: "Why this SQL works"
|
|
284
|
+
tags: ["tag1", "tag2"]
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
2. **Add test case to notebook:**
|
|
288
|
+
- Create cell with question
|
|
289
|
+
- Call `engine.query(question)`
|
|
290
|
+
- Verify results
|
|
291
|
+
|
|
292
|
+
3. **Track metrics:**
|
|
293
|
+
- Did it generate correct SQL?
|
|
294
|
+
- Did it execute successfully?
|
|
295
|
+
- Was the answer helpful?
|
|
296
|
+
- Confidence score appropriate?
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## Next Phase Goals
|
|
301
|
+
|
|
302
|
+
**Phase 2 (Weeks 2-3):**
|
|
303
|
+
- ✓ Add 50+ more example patterns
|
|
304
|
+
- ✓ Create golden dataset with expected results
|
|
305
|
+
- ✓ Implement evaluation metrics
|
|
306
|
+
- ✓ Measure accuracy by category
|
|
307
|
+
- ✓ Optimize prompts based on failures
|
|
308
|
+
|
|
309
|
+
**Phase 3 (Weeks 4-5):**
|
|
310
|
+
- ✓ Add domain-specific examples
|
|
311
|
+
- ✓ Fine-tune confidence scoring
|
|
312
|
+
- ✓ Improve multi-turn handling
|
|
313
|
+
- ✓ Reach 85%+ accuracy target
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## Resources
|
|
318
|
+
|
|
319
|
+
- See `fixtures/README.md` for sample data documentation
|
|
320
|
+
- See `docs/TESTING.md` for accuracy evaluation methodology
|
|
321
|
+
- See `docs/API.md` for QueryEngine usage
|
|
322
|
+
- See `IMPLEMENTATION_SUMMARY.md` for architecture overview
|