intent-router 0.2.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.
- intent_router-0.2.0/.github/workflows/ci.yml +43 -0
- intent_router-0.2.0/.github/workflows/publish.yml +85 -0
- intent_router-0.2.0/.gitignore +22 -0
- intent_router-0.2.0/CHANGELOG.md +25 -0
- intent_router-0.2.0/LICENSE +21 -0
- intent_router-0.2.0/PKG-INFO +223 -0
- intent_router-0.2.0/README.md +159 -0
- intent_router-0.2.0/examples/ecommerce_support.yaml +156 -0
- intent_router-0.2.0/pyproject.toml +123 -0
- intent_router-0.2.0/src/intent_router/__init__.py +87 -0
- intent_router-0.2.0/src/intent_router/adapters.py +38 -0
- intent_router-0.2.0/src/intent_router/cli.py +144 -0
- intent_router-0.2.0/src/intent_router/config.py +279 -0
- intent_router-0.2.0/src/intent_router/embedders/__init__.py +73 -0
- intent_router-0.2.0/src/intent_router/embedders/artifacts.py +168 -0
- intent_router-0.2.0/src/intent_router/embedders/base.py +67 -0
- intent_router-0.2.0/src/intent_router/embedders/http.py +153 -0
- intent_router-0.2.0/src/intent_router/embedders/onnx.py +120 -0
- intent_router-0.2.0/src/intent_router/errors.py +62 -0
- intent_router-0.2.0/src/intent_router/models.py +50 -0
- intent_router-0.2.0/src/intent_router/py.typed +1 -0
- intent_router-0.2.0/src/intent_router/router.py +499 -0
- intent_router-0.2.0/src/intent_router/storage.py +404 -0
- intent_router-0.2.0/src/intent_router/vectors.py +62 -0
- intent_router-0.2.0/tests/test_cli.py +53 -0
- intent_router-0.2.0/tests/test_config.py +175 -0
- intent_router-0.2.0/tests/test_embedders.py +269 -0
- intent_router-0.2.0/tests/test_router.py +220 -0
- intent_router-0.2.0/tests/test_storage.py +124 -0
- intent_router-0.2.0/tests/test_vectors.py +66 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: ${{ matrix.python-version }}
|
|
19
|
+
- name: Install package and dev dependencies
|
|
20
|
+
run: |
|
|
21
|
+
python -m pip install --upgrade pip
|
|
22
|
+
pip install -e ".[dev]"
|
|
23
|
+
- name: Run tests
|
|
24
|
+
run: pytest -v
|
|
25
|
+
- name: Run lint and type checks
|
|
26
|
+
run: |
|
|
27
|
+
ruff check src tests
|
|
28
|
+
mypy src
|
|
29
|
+
|
|
30
|
+
build:
|
|
31
|
+
runs-on: ubuntu-latest
|
|
32
|
+
needs: test
|
|
33
|
+
steps:
|
|
34
|
+
- uses: actions/checkout@v4
|
|
35
|
+
- uses: actions/setup-python@v5
|
|
36
|
+
with:
|
|
37
|
+
python-version: "3.12"
|
|
38
|
+
- name: Build sdist and wheel
|
|
39
|
+
run: |
|
|
40
|
+
python -m pip install --upgrade pip
|
|
41
|
+
pip install build twine
|
|
42
|
+
python -m build
|
|
43
|
+
twine check dist/*
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
name: Release and Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
tags:
|
|
8
|
+
- "v*"
|
|
9
|
+
workflow_dispatch:
|
|
10
|
+
|
|
11
|
+
concurrency:
|
|
12
|
+
group: release-${{ github.ref }}
|
|
13
|
+
cancel-in-progress: false
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
verify:
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
permissions:
|
|
19
|
+
contents: read
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@v4
|
|
22
|
+
- uses: actions/setup-python@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: "3.12"
|
|
25
|
+
- name: Install package and dev dependencies
|
|
26
|
+
run: |
|
|
27
|
+
python -m pip install --upgrade pip
|
|
28
|
+
pip install -e ".[dev]"
|
|
29
|
+
- name: Run tests, lint, and type checks
|
|
30
|
+
run: |
|
|
31
|
+
pytest -v
|
|
32
|
+
ruff check src tests
|
|
33
|
+
mypy src
|
|
34
|
+
|
|
35
|
+
release:
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
needs: verify
|
|
38
|
+
if: github.ref == 'refs/heads/main'
|
|
39
|
+
permissions:
|
|
40
|
+
contents: write
|
|
41
|
+
outputs:
|
|
42
|
+
released: ${{ steps.semantic-release.outputs.released }}
|
|
43
|
+
version: ${{ steps.semantic-release.outputs.version }}
|
|
44
|
+
tag: ${{ steps.semantic-release.outputs.tag }}
|
|
45
|
+
steps:
|
|
46
|
+
- name: Checkout repository
|
|
47
|
+
uses: actions/checkout@v4
|
|
48
|
+
with:
|
|
49
|
+
fetch-depth: 0
|
|
50
|
+
- name: Semantic Release
|
|
51
|
+
id: semantic-release
|
|
52
|
+
uses: python-semantic-release/python-semantic-release@v10
|
|
53
|
+
with:
|
|
54
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
55
|
+
|
|
56
|
+
publish:
|
|
57
|
+
runs-on: ubuntu-latest
|
|
58
|
+
needs: [verify, release]
|
|
59
|
+
if: >-
|
|
60
|
+
always() &&
|
|
61
|
+
needs.verify.result == 'success' &&
|
|
62
|
+
(
|
|
63
|
+
(startsWith(github.ref, 'refs/tags/v') && needs.release.result == 'skipped') ||
|
|
64
|
+
(needs.release.result == 'success' && needs.release.outputs.released == 'true')
|
|
65
|
+
)
|
|
66
|
+
environment: pypi
|
|
67
|
+
permissions:
|
|
68
|
+
id-token: write
|
|
69
|
+
contents: read
|
|
70
|
+
steps:
|
|
71
|
+
- name: Checkout release ref
|
|
72
|
+
uses: actions/checkout@v4
|
|
73
|
+
with:
|
|
74
|
+
ref: ${{ needs.release.outputs.tag || github.ref }}
|
|
75
|
+
- uses: actions/setup-python@v5
|
|
76
|
+
with:
|
|
77
|
+
python-version: "3.12"
|
|
78
|
+
- name: Build distribution
|
|
79
|
+
run: |
|
|
80
|
+
python -m pip install --upgrade pip
|
|
81
|
+
pip install build twine
|
|
82
|
+
python -m build
|
|
83
|
+
twine check dist/*
|
|
84
|
+
- name: Publish package to PyPI
|
|
85
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*$py.class
|
|
4
|
+
.Python
|
|
5
|
+
.venv/
|
|
6
|
+
venv/
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
.eggs/
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.mypy_cache/
|
|
13
|
+
.ruff_cache/
|
|
14
|
+
.coverage
|
|
15
|
+
htmlcov/
|
|
16
|
+
.DS_Store
|
|
17
|
+
*.db
|
|
18
|
+
*.sqlite
|
|
19
|
+
*.sqlite3
|
|
20
|
+
*.sqlite-wal
|
|
21
|
+
*.sqlite-shm
|
|
22
|
+
models/
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# CHANGELOG
|
|
2
|
+
|
|
3
|
+
<!-- version list -->
|
|
4
|
+
|
|
5
|
+
## v0.2.0 (2026-12-02)
|
|
6
|
+
|
|
7
|
+
### Build System
|
|
8
|
+
|
|
9
|
+
- Update project dependencies in pyproject.toml
|
|
10
|
+
([`1490ddb`](https://github.com/hulistack/intent-router-py/commit/1490ddba9d0581ea58369185a3897be807d6976b))
|
|
11
|
+
|
|
12
|
+
### Chores
|
|
13
|
+
|
|
14
|
+
- Update dependencies in pyproject.toml
|
|
15
|
+
([`cbc5afc`](https://github.com/hulistack/intent-router-py/commit/cbc5afcf8203592137c4791f2e4e371cd86dabc1))
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
- Integrate python-semantic-release into CI workflow and use dynamic user agent
|
|
20
|
+
([`6ac8d19`](https://github.com/hulistack/intent-router-py/commit/6ac8d191bea05ca8f200518d80eb379406ed3d14))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
## v0.1.0 (2026-12-01)
|
|
24
|
+
|
|
25
|
+
- Initial Release
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hulistack
|
|
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.
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: intent-router
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Configuration-driven hybrid SQLite RAG intent router for AI agent tool selection.
|
|
5
|
+
Project-URL: Homepage, https://github.com/hulistack/intent-router-py
|
|
6
|
+
Project-URL: Repository, https://github.com/hulistack/intent-router-py
|
|
7
|
+
Project-URL: Issues, https://github.com/hulistack/intent-router-py/issues
|
|
8
|
+
Author: hulistack
|
|
9
|
+
License: MIT License
|
|
10
|
+
|
|
11
|
+
Copyright (c) 2026 hulistack
|
|
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
|
+
Keywords: ai-agents,embeddings,intent-router,mcp,pydantic-ai,rag,semantic-router,sqlite,tool-selection
|
|
32
|
+
Classifier: Development Status :: 3 - Alpha
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Operating System :: OS Independent
|
|
36
|
+
Classifier: Programming Language :: Python :: 3
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
41
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
42
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
43
|
+
Classifier: Typing :: Typed
|
|
44
|
+
Requires-Python: >=3.11
|
|
45
|
+
Requires-Dist: pydantic<3,>=2.7
|
|
46
|
+
Requires-Dist: pyyaml>=6.0
|
|
47
|
+
Provides-Extra: all
|
|
48
|
+
Requires-Dist: intent-router-py[http,onnx]; extra == 'all'
|
|
49
|
+
Provides-Extra: dev
|
|
50
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
51
|
+
Requires-Dist: httpx>=0.27; extra == 'dev'
|
|
52
|
+
Requires-Dist: mypy>=1.11; extra == 'dev'
|
|
53
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
54
|
+
Requires-Dist: ruff>=0.8; extra == 'dev'
|
|
55
|
+
Requires-Dist: twine>=5.0; extra == 'dev'
|
|
56
|
+
Requires-Dist: types-pyyaml>=6.0; extra == 'dev'
|
|
57
|
+
Provides-Extra: http
|
|
58
|
+
Requires-Dist: httpx>=0.27; extra == 'http'
|
|
59
|
+
Provides-Extra: onnx
|
|
60
|
+
Requires-Dist: numpy>=1.26; extra == 'onnx'
|
|
61
|
+
Requires-Dist: onnxruntime>=1.18; extra == 'onnx'
|
|
62
|
+
Requires-Dist: tokenizers>=0.19; extra == 'onnx'
|
|
63
|
+
Description-Content-Type: text/markdown
|
|
64
|
+
|
|
65
|
+
# intent-router-py
|
|
66
|
+
|
|
67
|
+
Configuration-driven hybrid SQLite RAG intent router for AI agent tool selection.
|
|
68
|
+
|
|
69
|
+
`intent-router-py` lets you declare your agent's tools and intent anchors in a single config file, index them into a portable SQLite database (combining `float32` embedding BLOBs with SQLite FTS5 BM25), and dynamically select only the most relevant tools for each user query.
|
|
70
|
+
|
|
71
|
+
## Features
|
|
72
|
+
|
|
73
|
+
- **Declarative Configuration**: Define tools, multilingual intent anchors (`en`, `cs`, etc.), tags, `always_include` rules, and retrieval thresholds in YAML, JSON, or TOML validated with Pydantic v2.
|
|
74
|
+
- **Hybrid SQLite RAG**: Combines cosine similarity over `float32` embedding BLOBs with SQLite FTS5 BM25 (`unicode61 remove_diacritics 2`) in a single file with WAL mode and zero external vector database services.
|
|
75
|
+
- **Fingerprint-Cached Indexing**: Computes a SHA-256 config + embedder fingerprint so persistent SQLite databases skip redundant re-embedding on startup.
|
|
76
|
+
- **Pluggable Embedders**:
|
|
77
|
+
- `[onnx]`: Offline CPU `Xenova/multilingual-e5-small` (384d, pinned HuggingFace revision with SHA-256 checksum verification).
|
|
78
|
+
- `[http]`: OpenAI-compatible (`/embeddings`) and Ollama (`/api/embed`) endpoints.
|
|
79
|
+
- Custom: Wrap any Python embedding function with `CallableEmbedder` or implement the `Embedder` protocol.
|
|
80
|
+
- Pure lexical mode (`mode: lexical`): Works out of the box with zero embedding models.
|
|
81
|
+
- **Framework-Agnostic Integration**: Bind Python callables with `@router.tool` or filter existing OpenAI, Anthropic, MCP, or PydanticAI tool lists via `router.filter_tools(...)` / `router.select_schemas(...)`.
|
|
82
|
+
|
|
83
|
+
## Minimum Hardware Requirements
|
|
84
|
+
|
|
85
|
+
`intent-router-py` is designed to run on modest CPU-only hardware with no GPU required:
|
|
86
|
+
|
|
87
|
+
| Mode / Extra | CPU | RAM | Disk | Notes |
|
|
88
|
+
|---|---|---|---|---|
|
|
89
|
+
| **Core (`mode: lexical`) or Hosted HTTP (`[http]`)** | 1 vCPU (no GPU) | ~64 MB | < 20 MB | Requires Python 3.11+ with SQLite FTS5 enabled (included in standard Python builds). |
|
|
90
|
+
| **Local CPU ONNX (`[onnx]` with `multilingual-e5-small` q8)** | 1 vCPU (`x86_64` / `arm64`, no GPU) | ~512 MB | ~200 MB | Single-threaded by default (`embedder.threads: 1`); ~135 MB for pinned ONNX + tokenizer files plus the SQLite index. |
|
|
91
|
+
| **Local Ollama (`[http]` with local Ollama)** | 1–2 vCPU (no GPU) | ~1 GB+ | ~300 MB+ | Depends on the Ollama embedding model (e.g., `nomic-embed-text`). |
|
|
92
|
+
|
|
93
|
+
## Installation
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# Core library (YAML/JSON/TOML config + SQLite FTS5 + custom embedders)
|
|
97
|
+
pip install intent-router-py
|
|
98
|
+
|
|
99
|
+
# With local CPU ONNX multilingual-e5-small support
|
|
100
|
+
pip install "intent-router-py[onnx]"
|
|
101
|
+
|
|
102
|
+
# With OpenAI / Ollama HTTP embedder support
|
|
103
|
+
pip install "intent-router-py[http]"
|
|
104
|
+
|
|
105
|
+
# Everything
|
|
106
|
+
pip install "intent-router-py[all]"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Quickstart
|
|
110
|
+
|
|
111
|
+
### 1. Define `tools.yaml`
|
|
112
|
+
|
|
113
|
+
```yaml
|
|
114
|
+
version: "1"
|
|
115
|
+
|
|
116
|
+
routing:
|
|
117
|
+
mode: hybrid # hybrid | semantic | lexical
|
|
118
|
+
threshold: 0.45
|
|
119
|
+
max_tools: 4
|
|
120
|
+
semantic_weight: 0.7
|
|
121
|
+
lexical_weight: 0.3
|
|
122
|
+
|
|
123
|
+
storage:
|
|
124
|
+
path: "./router_index.db"
|
|
125
|
+
|
|
126
|
+
embedder:
|
|
127
|
+
provider: onnx # onnx | openai | ollama | none | custom
|
|
128
|
+
model_dir: "./models"
|
|
129
|
+
|
|
130
|
+
tools:
|
|
131
|
+
- id: track_shipment
|
|
132
|
+
description: Track parcel delivery status, carrier updates, and estimated arrival dates.
|
|
133
|
+
tags: [orders, shipping]
|
|
134
|
+
anchors:
|
|
135
|
+
en:
|
|
136
|
+
- "Where is my package right now?"
|
|
137
|
+
- "Track shipment status and estimated delivery date for my order."
|
|
138
|
+
cs:
|
|
139
|
+
- "Kde se právě nachází můj balík?"
|
|
140
|
+
|
|
141
|
+
- id: process_refund
|
|
142
|
+
description: Initiate item returns, return labels, and check refund payout status.
|
|
143
|
+
tags: [orders, returns]
|
|
144
|
+
anchors:
|
|
145
|
+
en:
|
|
146
|
+
- "I want to return an item and get a refund."
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 2. Route queries in Python
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from intent_router import IntentRouter
|
|
153
|
+
|
|
154
|
+
with IntentRouter.from_config("tools.yaml") as router:
|
|
155
|
+
router.warm() # Syncs SQLite RAG index; no-op on subsequent runs when config is unchanged
|
|
156
|
+
|
|
157
|
+
result = router.route("Kde se právě nachází můj balík?")
|
|
158
|
+
print(result.tool_ids) # ['track_shipment']
|
|
159
|
+
for match in result.tools:
|
|
160
|
+
print(match.tool_id, match.score, match.matched_anchor)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 3. Filter callables or LLM tool schemas
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
@router.tool("track_shipment")
|
|
167
|
+
def track_shipment(order_id: str) -> dict:
|
|
168
|
+
...
|
|
169
|
+
|
|
170
|
+
# Callable selection
|
|
171
|
+
callables = router.select_callables("Where is my package?")
|
|
172
|
+
|
|
173
|
+
# OpenAI / Anthropic / MCP tool schema filtering
|
|
174
|
+
selected_openai_tools = router.select_schemas("Where is my package?", openai_tools)
|
|
175
|
+
selected_mcp_tools = router.filter_tools("Where is my package?", mcp_tools)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## CLI Usage
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
# Validate config
|
|
182
|
+
intent-router validate examples/ecommerce_support.yaml
|
|
183
|
+
|
|
184
|
+
# Build or verify persistent SQLite index
|
|
185
|
+
intent-router index examples/ecommerce_support.yaml
|
|
186
|
+
|
|
187
|
+
# Route a query
|
|
188
|
+
intent-router route examples/ecommerce_support.yaml \
|
|
189
|
+
"Kde se právě nachází můj balík?" --mode lexical --json
|
|
190
|
+
|
|
191
|
+
# Export JSON Schema for RouterConfig (for editor autocomplete & CI)
|
|
192
|
+
intent-router schema > router-config.schema.json
|
|
193
|
+
|
|
194
|
+
# Download and SHA-256 verify pinned multilingual-e5-small ONNX artifacts
|
|
195
|
+
intent-router provision-models --dir ./models
|
|
196
|
+
intent-router verify-models --dir ./models
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## SQLite RAG Architecture
|
|
200
|
+
|
|
201
|
+
`SQLiteToolStore` maintains four structures inside one `.db` file:
|
|
202
|
+
|
|
203
|
+
1. `router_meta`: Stores `schema_version`, SHA-256 `fingerprint`, `embedder_id`, `dimension`, and `synced_at`.
|
|
204
|
+
2. `tools`: Canonical tool definitions, tags, `always_include` flag, and JSON metadata.
|
|
205
|
+
3. `tool_anchors`: Each anchor utterance and tool description alongside its little-endian `float32` L2-normalized vector `BLOB`.
|
|
206
|
+
4. `tool_anchors_fts`: SQLite FTS5 virtual table using `unicode61 remove_diacritics 2` for diacritic-insensitive BM25 lexical scoring across anchor text and tags.
|
|
207
|
+
|
|
208
|
+
During `route()`, candidates are filtered by optional `tags` / `allowed_tools`, scored via cosine similarity and/or FTS5 BM25, combined with `semantic_weight` and `lexical_weight`, and gated by `threshold`, `min_tools`, `max_tools`, and `always_include`.
|
|
209
|
+
|
|
210
|
+
## Development & Publishing to PyPI
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
pip install -e ".[dev]"
|
|
214
|
+
pytest -v
|
|
215
|
+
python -m build
|
|
216
|
+
twine check dist/*
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Pushing [Conventional Commits](https://www.conventionalcommits.org/) to `main` automatically runs [`.github/workflows/publish.yml`](.github/workflows/publish.yml) using `python-semantic-release`:
|
|
220
|
+
|
|
221
|
+
- **Version calculation**: Determines the next semantic version from commits since the last tag (`fix:` / `perf:` -> patch, `feat:` -> minor, `BREAKING CHANGE:` / `!:` -> minor while on `0.x.y`, or major once `1.0.0+` / `major_on_zero = true`).
|
|
222
|
+
- **Release & PyPI publish**: Bumps `pyproject.toml` and `src/intent_router/__init__.py`, updates `CHANGELOG.md`, commits and tags `vX.Y.Z`, creates a GitHub Release, and publishes to PyPI via Trusted Publishing.
|
|
223
|
+
- **Manual tags**: Pushing a `v*` tag manually (e.g., `git tag v0.1.0 && git push --tags`) still triggers verification and PyPI publishing.
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# intent-router-py
|
|
2
|
+
|
|
3
|
+
Configuration-driven hybrid SQLite RAG intent router for AI agent tool selection.
|
|
4
|
+
|
|
5
|
+
`intent-router-py` lets you declare your agent's tools and intent anchors in a single config file, index them into a portable SQLite database (combining `float32` embedding BLOBs with SQLite FTS5 BM25), and dynamically select only the most relevant tools for each user query.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Declarative Configuration**: Define tools, multilingual intent anchors (`en`, `cs`, etc.), tags, `always_include` rules, and retrieval thresholds in YAML, JSON, or TOML validated with Pydantic v2.
|
|
10
|
+
- **Hybrid SQLite RAG**: Combines cosine similarity over `float32` embedding BLOBs with SQLite FTS5 BM25 (`unicode61 remove_diacritics 2`) in a single file with WAL mode and zero external vector database services.
|
|
11
|
+
- **Fingerprint-Cached Indexing**: Computes a SHA-256 config + embedder fingerprint so persistent SQLite databases skip redundant re-embedding on startup.
|
|
12
|
+
- **Pluggable Embedders**:
|
|
13
|
+
- `[onnx]`: Offline CPU `Xenova/multilingual-e5-small` (384d, pinned HuggingFace revision with SHA-256 checksum verification).
|
|
14
|
+
- `[http]`: OpenAI-compatible (`/embeddings`) and Ollama (`/api/embed`) endpoints.
|
|
15
|
+
- Custom: Wrap any Python embedding function with `CallableEmbedder` or implement the `Embedder` protocol.
|
|
16
|
+
- Pure lexical mode (`mode: lexical`): Works out of the box with zero embedding models.
|
|
17
|
+
- **Framework-Agnostic Integration**: Bind Python callables with `@router.tool` or filter existing OpenAI, Anthropic, MCP, or PydanticAI tool lists via `router.filter_tools(...)` / `router.select_schemas(...)`.
|
|
18
|
+
|
|
19
|
+
## Minimum Hardware Requirements
|
|
20
|
+
|
|
21
|
+
`intent-router-py` is designed to run on modest CPU-only hardware with no GPU required:
|
|
22
|
+
|
|
23
|
+
| Mode / Extra | CPU | RAM | Disk | Notes |
|
|
24
|
+
|---|---|---|---|---|
|
|
25
|
+
| **Core (`mode: lexical`) or Hosted HTTP (`[http]`)** | 1 vCPU (no GPU) | ~64 MB | < 20 MB | Requires Python 3.11+ with SQLite FTS5 enabled (included in standard Python builds). |
|
|
26
|
+
| **Local CPU ONNX (`[onnx]` with `multilingual-e5-small` q8)** | 1 vCPU (`x86_64` / `arm64`, no GPU) | ~512 MB | ~200 MB | Single-threaded by default (`embedder.threads: 1`); ~135 MB for pinned ONNX + tokenizer files plus the SQLite index. |
|
|
27
|
+
| **Local Ollama (`[http]` with local Ollama)** | 1–2 vCPU (no GPU) | ~1 GB+ | ~300 MB+ | Depends on the Ollama embedding model (e.g., `nomic-embed-text`). |
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
# Core library (YAML/JSON/TOML config + SQLite FTS5 + custom embedders)
|
|
33
|
+
pip install intent-router-py
|
|
34
|
+
|
|
35
|
+
# With local CPU ONNX multilingual-e5-small support
|
|
36
|
+
pip install "intent-router-py[onnx]"
|
|
37
|
+
|
|
38
|
+
# With OpenAI / Ollama HTTP embedder support
|
|
39
|
+
pip install "intent-router-py[http]"
|
|
40
|
+
|
|
41
|
+
# Everything
|
|
42
|
+
pip install "intent-router-py[all]"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Quickstart
|
|
46
|
+
|
|
47
|
+
### 1. Define `tools.yaml`
|
|
48
|
+
|
|
49
|
+
```yaml
|
|
50
|
+
version: "1"
|
|
51
|
+
|
|
52
|
+
routing:
|
|
53
|
+
mode: hybrid # hybrid | semantic | lexical
|
|
54
|
+
threshold: 0.45
|
|
55
|
+
max_tools: 4
|
|
56
|
+
semantic_weight: 0.7
|
|
57
|
+
lexical_weight: 0.3
|
|
58
|
+
|
|
59
|
+
storage:
|
|
60
|
+
path: "./router_index.db"
|
|
61
|
+
|
|
62
|
+
embedder:
|
|
63
|
+
provider: onnx # onnx | openai | ollama | none | custom
|
|
64
|
+
model_dir: "./models"
|
|
65
|
+
|
|
66
|
+
tools:
|
|
67
|
+
- id: track_shipment
|
|
68
|
+
description: Track parcel delivery status, carrier updates, and estimated arrival dates.
|
|
69
|
+
tags: [orders, shipping]
|
|
70
|
+
anchors:
|
|
71
|
+
en:
|
|
72
|
+
- "Where is my package right now?"
|
|
73
|
+
- "Track shipment status and estimated delivery date for my order."
|
|
74
|
+
cs:
|
|
75
|
+
- "Kde se právě nachází můj balík?"
|
|
76
|
+
|
|
77
|
+
- id: process_refund
|
|
78
|
+
description: Initiate item returns, return labels, and check refund payout status.
|
|
79
|
+
tags: [orders, returns]
|
|
80
|
+
anchors:
|
|
81
|
+
en:
|
|
82
|
+
- "I want to return an item and get a refund."
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 2. Route queries in Python
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from intent_router import IntentRouter
|
|
89
|
+
|
|
90
|
+
with IntentRouter.from_config("tools.yaml") as router:
|
|
91
|
+
router.warm() # Syncs SQLite RAG index; no-op on subsequent runs when config is unchanged
|
|
92
|
+
|
|
93
|
+
result = router.route("Kde se právě nachází můj balík?")
|
|
94
|
+
print(result.tool_ids) # ['track_shipment']
|
|
95
|
+
for match in result.tools:
|
|
96
|
+
print(match.tool_id, match.score, match.matched_anchor)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 3. Filter callables or LLM tool schemas
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
@router.tool("track_shipment")
|
|
103
|
+
def track_shipment(order_id: str) -> dict:
|
|
104
|
+
...
|
|
105
|
+
|
|
106
|
+
# Callable selection
|
|
107
|
+
callables = router.select_callables("Where is my package?")
|
|
108
|
+
|
|
109
|
+
# OpenAI / Anthropic / MCP tool schema filtering
|
|
110
|
+
selected_openai_tools = router.select_schemas("Where is my package?", openai_tools)
|
|
111
|
+
selected_mcp_tools = router.filter_tools("Where is my package?", mcp_tools)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## CLI Usage
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# Validate config
|
|
118
|
+
intent-router validate examples/ecommerce_support.yaml
|
|
119
|
+
|
|
120
|
+
# Build or verify persistent SQLite index
|
|
121
|
+
intent-router index examples/ecommerce_support.yaml
|
|
122
|
+
|
|
123
|
+
# Route a query
|
|
124
|
+
intent-router route examples/ecommerce_support.yaml \
|
|
125
|
+
"Kde se právě nachází můj balík?" --mode lexical --json
|
|
126
|
+
|
|
127
|
+
# Export JSON Schema for RouterConfig (for editor autocomplete & CI)
|
|
128
|
+
intent-router schema > router-config.schema.json
|
|
129
|
+
|
|
130
|
+
# Download and SHA-256 verify pinned multilingual-e5-small ONNX artifacts
|
|
131
|
+
intent-router provision-models --dir ./models
|
|
132
|
+
intent-router verify-models --dir ./models
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## SQLite RAG Architecture
|
|
136
|
+
|
|
137
|
+
`SQLiteToolStore` maintains four structures inside one `.db` file:
|
|
138
|
+
|
|
139
|
+
1. `router_meta`: Stores `schema_version`, SHA-256 `fingerprint`, `embedder_id`, `dimension`, and `synced_at`.
|
|
140
|
+
2. `tools`: Canonical tool definitions, tags, `always_include` flag, and JSON metadata.
|
|
141
|
+
3. `tool_anchors`: Each anchor utterance and tool description alongside its little-endian `float32` L2-normalized vector `BLOB`.
|
|
142
|
+
4. `tool_anchors_fts`: SQLite FTS5 virtual table using `unicode61 remove_diacritics 2` for diacritic-insensitive BM25 lexical scoring across anchor text and tags.
|
|
143
|
+
|
|
144
|
+
During `route()`, candidates are filtered by optional `tags` / `allowed_tools`, scored via cosine similarity and/or FTS5 BM25, combined with `semantic_weight` and `lexical_weight`, and gated by `threshold`, `min_tools`, `max_tools`, and `always_include`.
|
|
145
|
+
|
|
146
|
+
## Development & Publishing to PyPI
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pip install -e ".[dev]"
|
|
150
|
+
pytest -v
|
|
151
|
+
python -m build
|
|
152
|
+
twine check dist/*
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Pushing [Conventional Commits](https://www.conventionalcommits.org/) to `main` automatically runs [`.github/workflows/publish.yml`](.github/workflows/publish.yml) using `python-semantic-release`:
|
|
156
|
+
|
|
157
|
+
- **Version calculation**: Determines the next semantic version from commits since the last tag (`fix:` / `perf:` -> patch, `feat:` -> minor, `BREAKING CHANGE:` / `!:` -> minor while on `0.x.y`, or major once `1.0.0+` / `major_on_zero = true`).
|
|
158
|
+
- **Release & PyPI publish**: Bumps `pyproject.toml` and `src/intent_router/__init__.py`, updates `CHANGELOG.md`, commits and tags `vX.Y.Z`, creates a GitHub Release, and publishes to PyPI via Trusted Publishing.
|
|
159
|
+
- **Manual tags**: Pushing a `v*` tag manually (e.g., `git tag v0.1.0 && git push --tags`) still triggers verification and PyPI publishing.
|