radlab-llm-router-plugins 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.
- radlab_llm_router_plugins-0.1.0/.version +1 -0
- radlab_llm_router_plugins-0.1.0/LICENSE +201 -0
- radlab_llm_router_plugins-0.1.0/MANIFEST.in +4 -0
- radlab_llm_router_plugins-0.1.0/PKG-INFO +420 -0
- radlab_llm_router_plugins-0.1.0/README.md +393 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/cli/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/cli/plugins/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/cli/plugins/utils/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/cli/plugins/utils/rag/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/cli/plugins/utils/rag/langchain.py +171 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/constants.py +2 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/core/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/core/utils.py +61 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/guardrails_base.py +36 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/nask/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/nask/nask_guard_plugin.py +73 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/pipeline.py +53 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/plugin_registrator.py +70 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/registry.py +32 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/speakleash/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/guardrails/speakleash/sojka_guard_plugin.py +61 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/__init__.py +8 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/core/__init__.py +18 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/core/masker.py +463 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/core/rule_interface.py +38 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/fast_masker_plugin.py +61 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/bank_account_rule.py +431 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/base_rule.py +65 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/car_plate_rule.py +88 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/credit_card_rule.py +88 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/date_number_rule.py +54 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/date_word_rule.py +120 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/email_rule.py +31 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/eu_vat_rule.py +73 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/health_id_rule.py +27 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/id_card_rule.py +26 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/invoice_number_rule.py +26 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/ip_rule.py +130 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/jwt_rule.py +71 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/krs_rule.py +74 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/mac_address_rule.py +74 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/money_rule.py +124 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/nip_rule.py +72 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/nrb_rule.py +86 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/order_number_rule.py +26 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/passport_rule.py +26 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/pesel_rule.py +57 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/pesel_tagged_rule.py +51 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/phone_international_rule.py +37 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/phone_rule.py +67 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/postal_code_rule.py +72 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/regon_rule.py +82 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/sim_card_rule.py +66 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/social_id_rule.py +33 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/ssl_cert_rule.py +78 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/ssn_rule.py +74 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/time_rule.py +38 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/transaction_ref_rule.py +86 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/url_rule.py +49 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/rules/vin_rule.py +82 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/utils/__init__.py +3 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/fast_masker/utils/validators.py +468 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/maskers_http_base.py +36 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/payload_interface.py +183 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/pii/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/pii/pii_masker_plugin.py +29 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/pipeline.py +43 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/plugin_registrator.py +91 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/maskers/registry.py +39 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/plugin_interface.py +206 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/resources/routing/semantic_biencoder-pk.json +128 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/resources/routing/semantic_biencoder.json +128 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/resources/routing/simple_semantic.json +477 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/pipeline.py +56 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/plugin_registrator.py +106 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/rag/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/rag/engine/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/rag/engine/langchain.py +363 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/rag/langchain_plugin.py +133 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/registry.py +22 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/__init__.py +0 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/constants.py +11 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/semantic_biencoder/__init__.py +7 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/semantic_biencoder/config.py +307 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/semantic_biencoder/embedder.py +512 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/semantic_biencoder/semantic_biencoder_routing.py +345 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/simple_semantic/__init__.py +7 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/simple_semantic/config.py +179 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/routing/simple_semantic/simple_semantic_routing.py +572 -0
- radlab_llm_router_plugins-0.1.0/llm_router_plugins/utils/text_extractor.py +54 -0
- radlab_llm_router_plugins-0.1.0/pyproject.toml +19 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/PKG-INFO +420 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/SOURCES.txt +111 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/dependency_links.txt +1 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/entry_points.txt +2 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/requires.txt +1 -0
- radlab_llm_router_plugins-0.1.0/radlab_llm_router_plugins.egg-info/top_level.txt +1 -0
- radlab_llm_router_plugins-0.1.0/setup.cfg +12 -0
- radlab_llm_router_plugins-0.1.0/setup.py +37 -0
- radlab_llm_router_plugins-0.1.0/tests/test_bank_account_rule.py +323 -0
- radlab_llm_router_plugins-0.1.0/tests/test_iban_debug.py +141 -0
- radlab_llm_router_plugins-0.1.0/tests/test_iban_full.py +92 -0
- radlab_llm_router_plugins-0.1.0/tests/test_iban_masking.py +180 -0
- radlab_llm_router_plugins-0.1.0/tests/test_nrb_krs_rules.py +230 -0
- radlab_llm_router_plugins-0.1.0/tests/test_phone_rules.py +303 -0
- radlab_llm_router_plugins-0.1.0/tests/test_semantic_biencoder_routing.py +502 -0
- radlab_llm_router_plugins-0.1.0/tests/test_simple_semantic_routing.py +388 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: radlab-llm-router-plugins
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Plugins for the LLM Router (guardrails, maskers, etc.)
|
|
5
|
+
Home-page: https://github.com/radlab-dev-group/llm-router-plugins
|
|
6
|
+
Author: RadLab.dev Team
|
|
7
|
+
Author-email: hello@radlab.dev
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: radlab-pii-classification>=0.1.0
|
|
16
|
+
Dynamic: author
|
|
17
|
+
Dynamic: author-email
|
|
18
|
+
Dynamic: classifier
|
|
19
|
+
Dynamic: description
|
|
20
|
+
Dynamic: description-content-type
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: license
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
Dynamic: requires-dist
|
|
25
|
+
Dynamic: requires-python
|
|
26
|
+
Dynamic: summary
|
|
27
|
+
|
|
28
|
+
## Overview
|
|
29
|
+
|
|
30
|
+
The **LLM‑Router** project ships with a modular plugin system that lets you plug‑in **anonymizers** (also called
|
|
31
|
+
*maskers*) and **guardrails** into request‑processing pipelines.
|
|
32
|
+
Each plugin implements a tiny, well‑defined interface (`apply`) and can be composed in an ordered list to form a *
|
|
33
|
+
*pipeline**. Pipelines are instantiated by the `MaskerPipeline` and `GuardrailPipeline` classes and are driven
|
|
34
|
+
automatically by the endpoint logic in `endpoint_i.py`.
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 1. Anonymizers (Maskers)
|
|
39
|
+
|
|
40
|
+
### 1.1 What they do
|
|
41
|
+
|
|
42
|
+
* **Goal** – Remove or replace personally‑identifiable information (PII) from a payload before it reaches the LLM or an
|
|
43
|
+
external service.
|
|
44
|
+
* **Typical strategy** – Run a pipeline of maskers that locate spans corresponding to IDs, emails, IPs, etc., and
|
|
45
|
+
replace each span with a placeholder such as `{{MASKED_ITEM}}`.
|
|
46
|
+
|
|
47
|
+
### 1.2 Built‑in anonymizer plugins
|
|
48
|
+
|
|
49
|
+
| Plugin | Description | Technical notes |
|
|
50
|
+
|------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
51
|
+
| **FastMaskerPlugin** (`fast_masker_plugin.py`) | Thin wrapper around the `FastMasker` utility class. Receives a JSON‑compatible payload and returns the same payload with all detected PII masked. | Implements `PluginInterface`. The heavy lifting is delegated to `FastMasker.mask_payload(payload)`. No extra I/O; the `FastMasker` instance is created once in `__init__`. |
|
|
52
|
+
|
|
53
|
+
### 1.3 How a masker is used
|
|
54
|
+
|
|
55
|
+
1. The endpoint (e.g. `EndpointI._do_masking_if_needed`) checks the global flag `FORCE_MASKING`.
|
|
56
|
+
2. If enabled, it creates a `MaskerPipeline` with the list of masker plugin identifiers (e.g. `["fast_masker"]`).
|
|
57
|
+
3. The pipeline calls each plugin’s `apply` method sequentially, feeding the output of one as the input of the next.
|
|
58
|
+
4. The final payload – now stripped of PII – proceeds to the rest of the request flow (guardrails, model dispatch,
|
|
59
|
+
etc.).
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## 2. Guardrails
|
|
64
|
+
|
|
65
|
+
### 2.1 What they do
|
|
66
|
+
|
|
67
|
+
* **Goal** – Verify that a request (or its response) complies with policy rules (e.g. no hateful, illegal, or unsafe
|
|
68
|
+
content).
|
|
69
|
+
* **Typical strategy** – Split the payload into manageable text chunks, run a pipeline of guardrails, aggregate
|
|
70
|
+
per‑chunk scores, and decide whether the overall request is safe.
|
|
71
|
+
|
|
72
|
+
### 2.2 Built‑in guardrail plugins
|
|
73
|
+
|
|
74
|
+
| Plugin | Description | Technical notes |
|
|
75
|
+
|----------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
76
|
+
| **NASKGuardPlugin** (`nask_guard_plugin.py`) | HTTP‑based guardrail that forwards the payload to the external NASK guardrail service (`/nask_guard` endpoint) and returns a boolean *safe* flag together with the raw response. | Inherits from `HttpPluginInterface`. The `apply` method calls `_request(payload)` (provided by the base class) and extracts `results["safe"]`. Errors are caught and logged; on failure the plugin returns `(False, {})`. |
|
|
77
|
+
| **SojkaGuardPlugin** (`sojka_guard_plugin.py`) | HTTP‑based guardrail that forwards the payload to the **Sójka** guardrail service (`/sojka_guard` endpoint) and returns a safety flag. | Mirrors the design of `NASKGuardPlugin`. The `endpoint_url` is built from the `LLM_ROUTER_GUARDRAIL_SOJKA_GUARD_HOST` environment variable. On success it returns `(True, response)`, otherwise `(False, {})`. |
|
|
78
|
+
| **(Implicit) GuardrailProcessor** (`processor.py`) | Core logic used by the internal NASK guardrail Flask route (`nask_guardrail`). Tokenises the payload, creates overlapping chunks, runs a Hugging‑Face `text‑classification` pipeline, and produces a detailed safety report. | Handles model loading (`AutoTokenizer`, `pipeline("text‑classification")`), chunking (`_chunk_text`), and scoring thresholds (`MIN_SCORE_FOR_SAFE`, `MIN_SCORE_FOR_NOT_SAFE`). Returns a dict: `{"safe": <bool>, "detailed": [...]}`. |
|
|
79
|
+
|
|
80
|
+
### 2.3 How a guardrail is used
|
|
81
|
+
|
|
82
|
+
1. The endpoint calls `_is_request_guardrail_safe(payload)` (or the analogous response guardrail).
|
|
83
|
+
2. If `FORCE_GUARDRAIL_REQUEST` is true, a `GuardrailPipeline` is built from the configured plugin IDs (e.g.
|
|
84
|
+
`["nask_guard", "sojka_guard"]`).
|
|
85
|
+
3. The pipeline iterates over each guardrail plugin; each `apply` returns `(is_safe, message)`.
|
|
86
|
+
4. The first plugin that reports `is_safe=False` short‑circuits the pipeline and the request is rejected with a 400/500
|
|
87
|
+
error payload.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## 2.5 ML-Based PII Classification
|
|
92
|
+
|
|
93
|
+
For cases where regex patterns alone are insufficient (e.g. context-dependent PII detection), the project integrates
|
|
94
|
+
with the **anonymizer-model** repository:
|
|
95
|
+
|
|
96
|
+
**Repository**: [radlab-dev-group/anonymizer-model](https://github.com/radlab-dev-group/anonymizer-model)
|
|
97
|
+
|
|
98
|
+
| Feature | Description |
|
|
99
|
+
|-----------------------|-------------------------------------------------------------------------------------------------|
|
|
100
|
+
| **Approach** | NER (Named Entity Recognition) model based on Hugging Face `AutoModelForTokenClassification` |
|
|
101
|
+
| **What it does** | Identifies PII entities in Polish text with context-aware detection (not just pattern matching) |
|
|
102
|
+
| **Training pipeline** | Configurable via JSON, logs to W&B, exports best model (F1 macro) to `final_model/` |
|
|
103
|
+
| **REST API** | Flask service supporting multiple model versions with optional dynamic quantization |
|
|
104
|
+
| **CLI** | `pii-classifier convert` / `generalise` / `report` for data prep and analysis |
|
|
105
|
+
| **Inference** | Sub-token merging, punctuation cleaning, gap preservation for human-readable entity spans |
|
|
106
|
+
| **Web tester** | HTML/JS interface for real-time PII detection testing |
|
|
107
|
+
|
|
108
|
+
### How it complements regex maskers
|
|
109
|
+
|
|
110
|
+
| Approach | Strength | Best for |
|
|
111
|
+
|---------------------------------------|--------------------------------------------------------------------|-------------------------------------------------------------|
|
|
112
|
+
| **Regex maskers** (FastMasker) | Deterministic, zero false negatives for known formats, fast | Structured IDs (KRS, NIP, PESEL, NRB, REGON, VIN, etc.) |
|
|
113
|
+
| **PII classifier** (anonymizer-model) | Context-aware, handles unknown formats, generalizes across domains | Free-text entities, names, addresses, context-dependent PII |
|
|
114
|
+
|
|
115
|
+
### Quick start
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Clone and install
|
|
119
|
+
git clone https://github.com/radlab-dev-group/anonymizer-model.git
|
|
120
|
+
cd anonymizer-model
|
|
121
|
+
pip install .
|
|
122
|
+
|
|
123
|
+
# Run the API (serves multiple model versions)
|
|
124
|
+
python3 -m pii_classification.api.app
|
|
125
|
+
|
|
126
|
+
# API endpoints
|
|
127
|
+
# GET /models — list available models + default
|
|
128
|
+
# POST /predict — { "text": "...", "model": "optional_name" }
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## 2.6 Polish Identification Regex Patterns
|
|
134
|
+
|
|
135
|
+
The **FastMaskerPlugin** ships with a comprehensive set of rules for detecting Polish business and personal identifiers.
|
|
136
|
+
These rules use regex matching + checksum/form validation to minimize false positives.
|
|
137
|
+
|
|
138
|
+
### Available Polish rules
|
|
139
|
+
|
|
140
|
+
| Rule | Placeholder | Format | Validation |
|
|
141
|
+
|---------------------|--------------------|---------------------------------------------------|------------------------------------------------------|
|
|
142
|
+
| **KrsRule** | `{{KRS}}` | `1234567890` or `123-456-78-90` / `123 456 78 90` | 10 digits (format-only) |
|
|
143
|
+
| **NrbRule** | `{{NRB}}` | `PL58105012981000009062923173` (26 digits) | 26 digits |
|
|
144
|
+
| **NipRule** | `{{NIP}}` | `1234567890` or `123-456-78-90` | Checksum with weights `[6,5,7,2,3,4,5,6,7]` mod 11 |
|
|
145
|
+
| **PeselRule** | `{{PESEL}}` | 11 digits | Checksum with weights `[1,3,7,9,1,3,7,9,1,3]` mod 10 |
|
|
146
|
+
| **RegonRule** | `{{REGON}}` | 9 or 14 digits | Checksum (different weights per form) |
|
|
147
|
+
| **BankAccountRule** | `{{BANK_ACCOUNT}}` | `PL58 1050 1298 1000 0090 6292 3173` | Polish IBAN (28 chars), supports masked `XX` groups |
|
|
148
|
+
|
|
149
|
+
### How they work
|
|
150
|
+
|
|
151
|
+
Each rule follows the same pattern:
|
|
152
|
+
|
|
153
|
+
1. **Regex match** — detects candidate strings (e.g. 10-digit sequences for KRS)
|
|
154
|
+
2. **Checksum validation** — validates the candidate against the official algorithm
|
|
155
|
+
3. **Placeholder replacement** — valid matches are replaced with `{{PLACEHOLDER}}`; invalid ones are left untouched
|
|
156
|
+
4. **Optional anonymizer** — if an `anonymizer_fn` is provided, it's called with `(original, tag_type)` and its result (
|
|
157
|
+
wrapped in `{}`) is used instead
|
|
158
|
+
|
|
159
|
+
### Adding rules to the pipeline
|
|
160
|
+
|
|
161
|
+
To enable Polish rules in your masking pipeline, add their plugin IDs to `MASKING_STRATEGY_PIPELINE`:
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
MASKING_STRATEGY_PIPELINE = ["fast_masker"]
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
All Polish rules are included in the FastMasker plugin by default. For a full list of available rules, see
|
|
168
|
+
the [fast_masker README](llm_router_plugins/maskers/fast_masker/README.md).
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## 2.7 Semantic Routing (Model Selection)
|
|
173
|
+
|
|
174
|
+
Two routing plugins are available for model selection. Both activate when
|
|
175
|
+
`payload["model"] == "auto"`.
|
|
176
|
+
|
|
177
|
+
### 2.7.1 Simple Semantic Routing (Heuristic)
|
|
178
|
+
|
|
179
|
+
The **Simple Semantic Routing plugin** (`simple_semantic_routing`) performs
|
|
180
|
+
two-stage heuristic model selection: it classifies the user's intent
|
|
181
|
+
(code, math, creative, general) via weighted keywords, multi-word phrases, and
|
|
182
|
+
regex patterns, then estimates input complexity (token count) to pick the most
|
|
183
|
+
appropriate model from a configured pool.
|
|
184
|
+
|
|
185
|
+
No embedding model is required — routing is a fast, pure-text classification.
|
|
186
|
+
|
|
187
|
+
**1. Intent scoring — each intent accumulates a score from three sources:**
|
|
188
|
+
|
|
189
|
+
| Source | How it works |
|
|
190
|
+
|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
191
|
+
| **Keywords** | Each keyword from the JSON has an optional weight. If the keyword is found in the lower-cased input text the score increases by that weight (default 1). |
|
|
192
|
+
| **Phrases** | Multi-word expressions like `"write code:5"` or `"debug:2"`. If the phrase is found the score increases by the specified weight (default 2.0). |
|
|
193
|
+
| **Patterns** | Regex patterns — if a pattern matches the input each match adds **3.0** to the score. |
|
|
194
|
+
|
|
195
|
+
The intent with the **highest total score** wins. If no intent score exceeds zero, the intent is classified as `"none"`.
|
|
196
|
+
|
|
197
|
+
**2. Complexity estimation — token count:**
|
|
198
|
+
|
|
199
|
+
The plugin estimates the number of tokens using a simple word-count heuristic:
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
token_estimate = len(input_text.split()) × 1.25
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
This is compared against two thresholds from the config (`simple`, `medium`):
|
|
206
|
+
|
|
207
|
+
| Tokens | Complexity |
|
|
208
|
+
|----------------------|------------|
|
|
209
|
+
| ≤ `simple` threshold | `simple` |
|
|
210
|
+
| ≤ `medium` threshold | `medium` |
|
|
211
|
+
| > `medium` threshold | `complex` |
|
|
212
|
+
|
|
213
|
+
**3. Model selection:**
|
|
214
|
+
|
|
215
|
+
The pool of models is a list defined in `default_models` (e.g. `["gpt-oss:120b", "qwen3.6:35b"]`). The complexity and
|
|
216
|
+
intent together determine the index into this pool:
|
|
217
|
+
|
|
218
|
+
- Complexity maps to a base index: `simple → 0` (first / smallest model), `medium → n // 2` (middle), `complex → n-1` (
|
|
219
|
+
last / largest).
|
|
220
|
+
- Intent-adjustment from the config (e.g.
|
|
221
|
+
`{"code": "medium", "math": "medium", "creative": "simple", "general": "simple"}`) can **increase** the base index but
|
|
222
|
+
never decrease it.
|
|
223
|
+
- The final index is clamped to `[0, n-1]` and the model at that index is selected.
|
|
224
|
+
- If no text is found or the intent is `"none"` the `default_models["simple"]` model is used as fallback.
|
|
225
|
+
|
|
226
|
+
Configuration is entirely JSON-driven in
|
|
227
|
+
[simple_semantic.json](llm_router_plugins/resources/routing/simple_semantic.json)
|
|
228
|
+
with intent definitions (keywords, phrases, patterns, weights) and two
|
|
229
|
+
complexity thresholds (`simple` / `medium`).
|
|
230
|
+
|
|
231
|
+
**Environment variable overrides:**
|
|
232
|
+
|
|
233
|
+
| Env variable | Purpose |
|
|
234
|
+
|--------------------------------------------|------------------------------------------------|
|
|
235
|
+
| `LLM_ROUTER_ROUTING_COMPLEXITY_THRESHOLDS` | Pipe-separated `simple |medium` token thresholds |
|
|
236
|
+
| `LLM_ROUTER_ROUTING_MODELS` | Pipe-separated model names for the pool |
|
|
237
|
+
| `LLM_ROUTER_ROUTING_DEFAULT_MODEL` | Fallback model when no text or no intent match |
|
|
238
|
+
| `LLM_ROUTER_ROUTING_INTENT_<name>` | Override intent keywords (pipe-separated) |
|
|
239
|
+
|
|
240
|
+
### 2.7.2 Bi-Encoder Semantic Routing (Model Selection)
|
|
241
|
+
|
|
242
|
+
The **Bi-Encoder routing plugin** (`semantic_biencoder_routing`) uses a neural embedding model
|
|
243
|
+
(**radlab/semantic-euro-bert-encoder-v1**) to compute semantic embeddings for a set of pre-configured routing targets.
|
|
244
|
+
Each target has a `name`, a `model_name` (the model to route to), a `description`, and a list of `examples`.
|
|
245
|
+
At query time the user message is embedded and matched against all stored target embeddings using FAISS
|
|
246
|
+
(`IndexFlatIP` on L2-normalised vectors = cosine similarity). The best-matching target determines the selected model.
|
|
247
|
+
|
|
248
|
+
**1. Index building (on first load or when the persist directory is missing):**
|
|
249
|
+
|
|
250
|
+
- For each target, its `description` and `examples` are combined into text.
|
|
251
|
+
- The text is split into overlapping **token chunks** using a sliding window (`chunk_size` tokens, `chunk_overlap`
|
|
252
|
+
tokens overlap).
|
|
253
|
+
- Each chunk is embedded via the BiEncoder model (e.g. `radlab/semantic-euro-bert-encoder-v1`).
|
|
254
|
+
- All embedding vectors are **L2-normalised** to unit length.
|
|
255
|
+
- Vectors are inserted into a `faiss.IndexFlatIP` index (inner product).
|
|
256
|
+
- A docstore maps each FAISS doc ID to its target name (for reverse lookup).
|
|
257
|
+
|
|
258
|
+
**2. Routing (query):**
|
|
259
|
+
|
|
260
|
+
- The user message is embedded and L2-normalised.
|
|
261
|
+
- FAISS performs a nearest-neighbor search returning the `top_k` closest chunks.
|
|
262
|
+
- Scores are **aggregated per target**: the mean cosine similarity of all chunks belonging to the same target is
|
|
263
|
+
computed.
|
|
264
|
+
- The target with the **highest mean similarity** wins and its `model_name` is returned.
|
|
265
|
+
|
|
266
|
+
**3. Persistence:**
|
|
267
|
+
|
|
268
|
+
The FAISS index and docstore are saved to disk (files `index.faiss` and `docstore.pkl`) under the configured persist
|
|
269
|
+
directory.
|
|
270
|
+
On subsequent starts the index is loaded from disk — embeddings are **not recomputed**.
|
|
271
|
+
If the embedding model changes (different output dimension) the index is automatically rebuilt.
|
|
272
|
+
|
|
273
|
+
Configuration is loaded from
|
|
274
|
+
[semantic_biencoder.json](llm_router_plugins/resources/routing/semantic_biencoder.json). The embedding model, chunk
|
|
275
|
+
size, overlap, and persist directory can all be overridden via environment variables:
|
|
276
|
+
|
|
277
|
+
| Env variable | Purpose |
|
|
278
|
+
|-------------------------------------------------------|---------------------------------------|
|
|
279
|
+
| `LLM_ROUTER_ROUTING_SEMANTIC_BIENCODER_MODEL` | Override the embedding model name |
|
|
280
|
+
| `LLM_ROUTER_ROUTING_SEMANTIC_BIENCODER_TARGETS` | Pipe-separated list of target names |
|
|
281
|
+
| `LLM_ROUTER_ROUTING_SEMANTIC_BIENCODER_CHUNK_SIZE` | Override chunk size |
|
|
282
|
+
| `LLM_ROUTER_ROUTING_SEMANTIC_BIENCODER_CHUNK_OVERLAP` | Override chunk overlap |
|
|
283
|
+
| `LLM_ROUTER_ROUTING_SEMANTIC_BIENCODER_PERSIST_DIR` | Directory for FAISS index persistence |
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
287
|
+
## 3. Pipelines
|
|
288
|
+
|
|
289
|
+
Both masker and guardrail pipelines share the same design pattern:
|
|
290
|
+
|
|
291
|
+
| Class | Purpose |
|
|
292
|
+
|-----------------------------------------------------------|------------------------------------------------------------------------------------|
|
|
293
|
+
| **MaskerPipeline** (`pipeline.py` – masker version) | Executes a list of masker plugins in order, transforming the payload step‑by‑step. |
|
|
294
|
+
| **GuardrailPipeline** (`pipeline.py` – guardrail version) | Executes guardrail plugins sequentially, stopping on the first failure. |
|
|
295
|
+
|
|
296
|
+
### 3.1 Registration
|
|
297
|
+
|
|
298
|
+
* Plugins are registered lazily via `MaskerRegistry.register(name, logger)` or
|
|
299
|
+
`GuardrailRegistry.register(name, logger)`.
|
|
300
|
+
* The registry maps a string identifier (e.g. `"fast_masker"`) to a concrete plugin class, allowing pipelines to resolve
|
|
301
|
+
the classes at runtime.
|
|
302
|
+
|
|
303
|
+
### 3.2 Configuration
|
|
304
|
+
|
|
305
|
+
All plugin identifiers are stored in environment variables or constants such as:
|
|
306
|
+
|
|
307
|
+
```python
|
|
308
|
+
MASKING_STRATEGY_PIPELINE = ["fast_masker"]
|
|
309
|
+
GUARDRAIL_STRATEGY_PIPELINE_REQUEST = ["nask_guard", "sojka_guard"]
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
These lists are consumed by the endpoint initialization (`EndpointI._prepare_masker_pipeline`,
|
|
313
|
+
`EndpointI._prepare_guardrails_pipeline`).
|
|
314
|
+
|
|
315
|
+
---
|
|
316
|
+
|
|
317
|
+
## 4. Adding a New Plugin
|
|
318
|
+
|
|
319
|
+
1. **Create a subclass** of either `PluginInterface` (for maskers) or `HttpPluginInterface` / a custom guardrail base.
|
|
320
|
+
2. **Define a `name` class attribute** – this is the identifier used in pipeline configuration.
|
|
321
|
+
3. **Implement `apply(self, payload: Dict) -> Dict`** (masker) **or `apply(self, payload: Dict) -> Tuple[bool, Dict]`
|
|
322
|
+
** (guardrail).
|
|
323
|
+
4. **Register the plugin** – either automatically via the registry’s `register` call in the pipeline constructor, or
|
|
324
|
+
manually by calling `MaskerRegistry.register(name=MyPlugin.name, logger=logger)`.
|
|
325
|
+
|
|
326
|
+
*Example stub for a new masker:*
|
|
327
|
+
|
|
328
|
+
```python
|
|
329
|
+
# my_custom_masker.py
|
|
330
|
+
from llm_router_plugins.maskers.plugin_interface import PluginInterface
|
|
331
|
+
import logging
|
|
332
|
+
from typing import Dict, Optional
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
class MyCustomMasker(PluginInterface):
|
|
336
|
+
name = "my_custom_masker"
|
|
337
|
+
|
|
338
|
+
def __init__(self, logger: Optional[logging.Logger] = None):
|
|
339
|
+
super().__init__(logger=logger)
|
|
340
|
+
# Load any heavy resources here (e.g., a spaCy model)
|
|
341
|
+
|
|
342
|
+
def apply(self, payload: Dict) -> Dict:
|
|
343
|
+
# Perform your masking logic and return the modified payload
|
|
344
|
+
return payload
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
After placing the file in `llm_router_plugins/maskers/plugins/`, enable it by adding `"my_custom_masker"` to
|
|
348
|
+
`MASKING_STRATEGY_PIPELINE`.
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## 5. Retrieval‑Augmented Generation (RAG) Support
|
|
353
|
+
|
|
354
|
+
The project now includes a **LangChain‑based RAG plugin** that enables semantic search over user‑provided documents. The
|
|
355
|
+
implementation lives in `llm_router_plugins/utils/rag/langchain_plugin.py` and is driven by the helper CLI scripts
|
|
356
|
+
located in `scripts/`.
|
|
357
|
+
|
|
358
|
+
### 5.1 What the plugin does
|
|
359
|
+
|
|
360
|
+
| Feature | Description |
|
|
361
|
+
|-------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
362
|
+
| **Indexing** | Reads a directory of text‑like files (`.txt`, `.md`, `.html`, `.js`, …), splits them into token‑based windows, embeds each chunk with a configurable transformer model, and stores the vectors in a FAISS (or compatible) vector store. |
|
|
363
|
+
| **Searching** | Given a user query, retrieves the most similar chunks and injects them into the payload (e.g., appends to the last user message) so that downstream LLM calls can use the retrieved context. |
|
|
364
|
+
| **Configuration** | All parameters (collection name, embedder model, device, chunk size, overlap, persistence directory) are driven by environment variables prefixed with `LLM_ROUTER_`. See the table below for the full list. |
|
|
365
|
+
| **CLI helpers** | Two ready‑to‑use scripts: `scripts/llm-router-rag-langchain-index.sh` (indexes a repository) and `scripts/llm-router-rag-langchain-search.sh` (runs a search or starts an interactive REPL). |
|
|
366
|
+
|
|
367
|
+
### 5.2 Environment variables
|
|
368
|
+
|
|
369
|
+
| Variable | Default | Meaning |
|
|
370
|
+
|------------------------------------------|--------------------------------------------------------------------------------|-------------------------------------------------------------|
|
|
371
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_COLLECTION` | *must be set* | Name of the FAISS collection (e.g. `sample_collection`). |
|
|
372
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_EMBEDDER` | `/mnt/data2/llms/models/community/google/embeddinggemma-300m` | Path or Hugging‑Face identifier of the embedding model. |
|
|
373
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_DEVICE` | `cuda:2` | Torch device (`cpu`, `cuda:0`, …). |
|
|
374
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_CHUNK_SIZE` | `1024` | Number of tokens per chunk. |
|
|
375
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_CHUNK_OVERLAP` | `100` | Number of overlapping tokens between consecutive chunks. |
|
|
376
|
+
| `LLM_ROUTER_LANGCHAIN_RAG_PERSIST_DIR` | `./workdir/plugins/utils/rag/langchain/${LLM_ROUTER_LANGCHAIN_RAG_COLLECTION}` | Directory where the FAISS index and docstore are persisted. |
|
|
377
|
+
|
|
378
|
+
#### Example export block (add to your shell profile or a `.env` file)
|
|
379
|
+
|
|
380
|
+
```shell script
|
|
381
|
+
export LLM_ROUTER_LANGCHAIN_RAG_COLLECTION="${LLM_ROUTER_LANGCHAIN_RAG_COLLECTION:-sample_collection}"
|
|
382
|
+
export LLM_ROUTER_LANGCHAIN_RAG_EMBEDDER="${LLM_ROUTER_LANGCHAIN_RAG_EMBEDDER:-/mnt/data2/llms/models/community/google/embeddinggemma-300m}"
|
|
383
|
+
export LLM_ROUTER_LANGCHAIN_RAG_DEVICE="${LLM_ROUTER_LANGCHAIN_RAG_DEVICE:-cuda:2}"
|
|
384
|
+
export LLM_ROUTER_LANGCHAIN_RAG_CHUNK_SIZE="${LLM_ROUTER_LANGCHAIN_RAG_CHUNK_SIZE:-1024}"
|
|
385
|
+
export LLM_ROUTER_LANGCHAIN_RAG_CHUNK_OVERLAP="${LLM_ROUTER_LANGCHAIN_RAG_CHUNK_OVERLAP:-100}"
|
|
386
|
+
export LLM_ROUTER_LANGCHAIN_RAG_PERSIST_DIR="${LLM_ROUTER_LANGCHAIN_RAG_PERSIST_DIR:-./workdir/plugins/utils/rag/langchain/${LLM_ROUTER_LANGCHAIN_RAG_COLLECTION}}"
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### 5.3 Using the CLI scripts
|
|
390
|
+
|
|
391
|
+
**Index a repository** (example for the documentation site):
|
|
392
|
+
|
|
393
|
+
```shell script
|
|
394
|
+
scripts/llm-router-rag-langchain-index.sh
|
|
395
|
+
# Internally runs:
|
|
396
|
+
# llm-router-rag-langchain index --path "../.github/pages/llmrouter.cloud/" --ext .html .js .md
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**Search** (interactive REPL):
|
|
400
|
+
|
|
401
|
+
```shell script
|
|
402
|
+
scripts/llm-router-rag-langchain-search.sh
|
|
403
|
+
# Internally runs:
|
|
404
|
+
# llm-router-rag-langchain search
|
|
405
|
+
# (you will be prompted for a query, type “exit” to quit)
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
**One‑shot search**:
|
|
409
|
+
|
|
410
|
+
```shell script
|
|
411
|
+
llm-router-rag-langchain search --query "What is Retrieval‑Augmented Generation?" --top_n 5
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
The CLI returns the raw matching chunks together with similarity scores. The `LangchainRAGPlugin` automatically formats
|
|
415
|
+
the retrieved text and appends it to the user’s last message, prefixed with:
|
|
416
|
+
|
|
417
|
+
```
|
|
418
|
+
If the context below will help answer the above question, use it.
|
|
419
|
+
Context separated with double enter
|
|
420
|
+
```
|