aidial-interceptors-sdk 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.
- aidial_interceptors_sdk-0.1.0/LICENSE +201 -0
- aidial_interceptors_sdk-0.1.0/PKG-INFO +247 -0
- aidial_interceptors_sdk-0.1.0/README.md +218 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/__init__.py +3 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/adapter.py +104 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/annotated_chunk.py +8 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/base.py +50 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/element_path.py +46 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/helpers.py +155 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/index_mapper.py +37 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/request_handler.py +46 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/request_message_handler.py +108 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/response_handler.py +145 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/chat_completion/response_message_handler.py +111 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/dial_client.py +53 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/embeddings/__init__.py +2 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/embeddings/adapter.py +37 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/embeddings/base.py +48 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/error.py +4 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/app.py +28 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/__init__.py +24 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/blacklisted_words.py +42 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/cache.py +67 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/image_watermark.py +54 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/pii_anonymiser/__init__.py +3 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/pii_anonymiser/impl.py +124 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/pii_anonymiser/spacy_anonymizer.py +123 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/pirate.py +18 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/reject_external_links.py +28 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/replicator.py +183 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/chat_completion/statistics_reporter.py +174 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/embeddings/__init__.py +9 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/embeddings/blacklisted_words.py +19 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/embeddings/normalize_vector.py +27 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/embeddings/project_vector.py +34 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/registry.py +50 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/dict.py +161 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/embedding_encoding.py +15 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/log_config.py +40 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/lru_cache.py +25 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/markdown.py +66 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/watermark/generate.py +96 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/examples/utils/watermark/stamp.py +57 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_debug.py +32 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_dial_sdk.py +31 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_env.py +18 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_exceptions.py +45 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_http_client.py +20 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/_reflection.py +43 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/not_given.py +22 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/storage.py +71 -0
- aidial_interceptors_sdk-0.1.0/aidial_interceptors_sdk/utils/streaming.py +79 -0
- aidial_interceptors_sdk-0.1.0/pyproject.toml +101 -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 2023 EPAM Systems, Inc.
|
|
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,247 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: aidial-interceptors-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Framework for creating interceptors for AI DIAL
|
|
5
|
+
Home-page: https://epam-rail.com
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Keywords: ai
|
|
8
|
+
Author: EPAM RAIL
|
|
9
|
+
Author-email: SpecialEPM-DIALDevTeam@epam.com
|
|
10
|
+
Requires-Python: >=3.11,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Provides-Extra: examples
|
|
17
|
+
Requires-Dist: aidial-sdk[telemetry] (>=0.13.0,<0.14.0)
|
|
18
|
+
Requires-Dist: aiohttp (>=3.8.3,<4.0.0)
|
|
19
|
+
Requires-Dist: aiostream (>=0.6.2,<0.7.0)
|
|
20
|
+
Requires-Dist: fastapi (>=0.51,<1.0)
|
|
21
|
+
Requires-Dist: httpx (>=0.25.0,<1.0)
|
|
22
|
+
Requires-Dist: numpy (>=1.26.1,<2.0.0) ; extra == "examples"
|
|
23
|
+
Requires-Dist: openai (>=1.32.0,<2.0.0)
|
|
24
|
+
Requires-Dist: pillow (>=10.4.0,<11.0.0) ; extra == "examples"
|
|
25
|
+
Requires-Dist: spacy (>=3.7.5,<4.0.0) ; extra == "examples"
|
|
26
|
+
Project-URL: Documentation, https://epam-rail.com/dial_api
|
|
27
|
+
Project-URL: Repository, https://github.com/epam/ai-dial-interceptors-sdk
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# AI DIAL Interceptors Python SDK
|
|
31
|
+
|
|
32
|
+
> [!IMPORTANT]
|
|
33
|
+
> This package is in early development and subject to rapid changes. Breaking changes between versions are likely as the project evolves.
|
|
34
|
+
|
|
35
|
+
## Overview
|
|
36
|
+
|
|
37
|
+
The framework provides useful classes and helpers for creating DIAL Interceptors in Python for chat completion and embedding models.
|
|
38
|
+
|
|
39
|
+
An interceptor could be thought of as a middleware that
|
|
40
|
+
|
|
41
|
+
1. modifies an incoming DIAL request received from the client *(or it may leave it as is)*
|
|
42
|
+
2. calls upstream DIAL application *(**the upstream** for short)* with the modified request
|
|
43
|
+
3. modifies the response from the upstream *(or it may leave it as is)*
|
|
44
|
+
4. returns the modified response to the client
|
|
45
|
+
|
|
46
|
+
The upstream is encapsulated behind a special deployment id `interceptor`. This deployment id is resolved by the DIAL Core into an appropriate deployment id.
|
|
47
|
+
|
|
48
|
+
Interceptors could be classified into the following categories:
|
|
49
|
+
|
|
50
|
+
1. **Pre-interceptors** that only modify the incoming request from the client *(e.g. rejecting requests following certain criteria)*
|
|
51
|
+
2. **Post-interceptors** that only modify the response received from the upstream *(e.g. censoring the response)*
|
|
52
|
+
3. **Generic interceptors** that modify both the incoming request and the response from the upstream *(e.g. caching the responses)*
|
|
53
|
+
|
|
54
|
+
To create chat completion interceptor one needs to implement instance of the class [ChatCompletionInterceptor](aidial_interceptors_sdk/chat_completion/base.py) and for embedding interceptor - [EmbeddingsInterceptor](aidial_interceptors_sdk/embeddings/base.py).
|
|
55
|
+
|
|
56
|
+
See [example](aidial_interceptors_sdk/examples/interceptor/registry.py) interceptor implementations for more details.
|
|
57
|
+
|
|
58
|
+
## Environment Variables
|
|
59
|
+
|
|
60
|
+
Copy `.env.example` to `.env` and customize it for your environment:
|
|
61
|
+
|
|
62
|
+
|Variable|Default|Description|
|
|
63
|
+
|---|---|---|
|
|
64
|
+
|LOG_LEVEL|INFO|Log level. Use DEBUG for dev purposes and INFO in prod|
|
|
65
|
+
|WEB_CONCURRENCY|1|Number of workers for the server|
|
|
66
|
+
|DIAL_URL||The URL of the DIAL Core server|
|
|
67
|
+
|
|
68
|
+
## Development
|
|
69
|
+
|
|
70
|
+
This project uses [Python>=3.11](https://www.python.org/downloads/) and [Poetry>=1.6.1](https://python-poetry.org/) as a dependency manager.
|
|
71
|
+
|
|
72
|
+
Check out Poetry's [documentation on how to install it](https://python-poetry.org/docs/#installation) on your system before proceeding.
|
|
73
|
+
|
|
74
|
+
To install requirements:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
poetry install
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This will install all requirements for running the package, linting, formatting and tests.
|
|
81
|
+
|
|
82
|
+
### IDE configuration
|
|
83
|
+
|
|
84
|
+
The recommended IDE is [VSCode](https://code.visualstudio.com/).
|
|
85
|
+
Open the project in VSCode and install the recommended extensions.
|
|
86
|
+
|
|
87
|
+
The VSCode is configured to use PEP-8 compatible formatter [Black](https://black.readthedocs.io/en/stable/index.html).
|
|
88
|
+
|
|
89
|
+
Alternatively you can use [PyCharm](https://www.jetbrains.com/pycharm/).
|
|
90
|
+
|
|
91
|
+
Set-up the Black formatter for PyCharm [manually](https://black.readthedocs.io/en/stable/integrations/editors.html#pycharm-intellij-idea) or
|
|
92
|
+
install PyCharm>=2023.2 with [built-in Black support](https://blog.jetbrains.com/pycharm/2023/07/2023-2/#black).
|
|
93
|
+
|
|
94
|
+
### Make on Windows
|
|
95
|
+
|
|
96
|
+
As of now, Windows distributions do not include the make tool. To run make commands, the tool can be installed using
|
|
97
|
+
the following command (since [Windows 10](https://learn.microsoft.com/en-us/windows/package-manager/winget/)):
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
winget install GnuWin32.Make
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
For convenience, the tool folder can be added to the PATH environment variable as `C:\Program Files (x86)\GnuWin32\bin`.
|
|
104
|
+
The command definitions inside Makefile should be cross-platform to keep the development environment setup simple.
|
|
105
|
+
|
|
106
|
+
### Lint
|
|
107
|
+
|
|
108
|
+
To run the linting before committing:
|
|
109
|
+
|
|
110
|
+
```sh
|
|
111
|
+
make lint
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
To auto-fix formatting issues run:
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
make format
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Test
|
|
121
|
+
|
|
122
|
+
To run unit tests:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
make test
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Clean
|
|
129
|
+
|
|
130
|
+
To remove the virtual environment and build artifacts:
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
make clean
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Examples
|
|
137
|
+
|
|
138
|
+
The repository also provides examples of various DIAL Interceptors all packed into a DIAL service.
|
|
139
|
+
|
|
140
|
+
Keep in mind, that the following example interceptors aren't ready for a production use.
|
|
141
|
+
They are provided solely as examples to demonstrate basic use cases of interceptors. Use at your discretion.
|
|
142
|
+
|
|
143
|
+
### Chat completion interceptors
|
|
144
|
+
|
|
145
|
+
|Interceptor name|Category|Description|
|
|
146
|
+
|---|---|---|
|
|
147
|
+
|reply-as-pirate|Pre|Injects systems prompt `Reply as a pirate` to the request|
|
|
148
|
+
|reject-external-links|Pre|Rejects any URL in DIAL attachments which do not point to DIAL Core storage|
|
|
149
|
+
|reject-blacklisted-words|Generic|Rejects the request if it contains any blacklisted words|
|
|
150
|
+
|image-watermark|Post|Stamps "EPAM DIAL" watermark on all image attachments in the response. Demonstrates how to work with files stored on DIAL File Storage.|
|
|
151
|
+
|statistics-reporter|Post|Collects statistics on the response stream *(tokens/sec, finish reason, completion tokens etc)* and reports it in a new stage when response is finished|
|
|
152
|
+
|pii-anonymizer|Generic|Anonymizes any PII in the request, calls the upstream, deanonymizes the response|
|
|
153
|
+
|replicator:N|Generic|Calls the upstream N times and combines the N response into a single response. Could be useful for stabilization of model's output, since certain models aren't deterministic.|
|
|
154
|
+
|cache|Generic|Caches incoming chat completion requests. **Not ready for production use. Use at your discretion**|
|
|
155
|
+
|no-op|Generic|No-op interceptor - does not modify the request or the response, simply proxies the upstream|
|
|
156
|
+
|
|
157
|
+
### Embeddings interceptors
|
|
158
|
+
|
|
159
|
+
|Interceptor name|Category|Description|
|
|
160
|
+
|---|---|---|
|
|
161
|
+
|reject-blacklisted-words|Pre|Rejects the request if it contains any blacklisted words|
|
|
162
|
+
|normalize-vector|Post|Normalizes the vector in the response|
|
|
163
|
+
|project-vector:N|Post|Changes the dimensionality of the vectors in the response to N, where N is an integer path parameter.|
|
|
164
|
+
|no-op|Generic|No-op interceptor - does not modify the request or the response, simply proxies the upstream|
|
|
165
|
+
|
|
166
|
+
### Environment variables
|
|
167
|
+
|
|
168
|
+
Copy `.env.example` to `.env` and customize it for your environment:
|
|
169
|
+
|
|
170
|
+
|Variable|Default|Description|
|
|
171
|
+
|---|---|---|
|
|
172
|
+
|PII_ANONYMIZER_LABELS_TO_REDACT|PERSON,ORG,GPE,PRODUCT|Comma-separated list of spaCy entity types to redact. Find the full list of entities [here](https://github.com/explosion/spacy-models/blob/e46017f5c8241096c1b30fae080f0e0709c8038c/meta/en_core_web_sm-3.7.0.json#L121-L140).|
|
|
173
|
+
|
|
174
|
+
### Running interceptor as a DIAL service
|
|
175
|
+
|
|
176
|
+
To run dev application locally run:
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
make examples_serve
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
To run from Docker container:
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
make examples_docker_serve
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Either of the commands will start the server on `http://localhost:5000` exposing endpoints for each of the interceptors like these:
|
|
189
|
+
|
|
190
|
+
- `http://localhost:5000/openai/deployments/pii-anonymizer/chat/completions`
|
|
191
|
+
- `http://localhost:5000/openai/deployments/normalize-vector/embeddings`
|
|
192
|
+
|
|
193
|
+
### DIAL Core configuration
|
|
194
|
+
|
|
195
|
+
The interceptor endpoints are defined in the `interceptors` section of the DIAL Core configuration like this:
|
|
196
|
+
|
|
197
|
+
```json
|
|
198
|
+
{
|
|
199
|
+
"interceptors": {
|
|
200
|
+
"chat-reply-as-pirate": {
|
|
201
|
+
"endpoint": "${INTERCEPTOR_SERVICE_URL}/openai/deployments/reply-as-pirate/chat/completions"
|
|
202
|
+
},
|
|
203
|
+
"chat-statistics-reporter": {
|
|
204
|
+
"endpoint": "${INTERCEPTOR_SERVICE_URL}/openai/deployments/statistics-reporter/chat/completions"
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
where `INTERCEPTOR_SERVICE_URL` is the URL of the interceptor service, which is `http://localhost:5000` when run locally, or the interceptor service URL when deployed within Kubernetes.
|
|
211
|
+
|
|
212
|
+
The declared interceptors could be then attached to particular models and applications:
|
|
213
|
+
|
|
214
|
+
```json
|
|
215
|
+
{
|
|
216
|
+
"models": {
|
|
217
|
+
"anthropic.claude-v3-haiku": {
|
|
218
|
+
"type": "chat",
|
|
219
|
+
"iconUrl": "anthropic.svg",
|
|
220
|
+
"endpoint": "${BEDROCK_ADAPTER_SERVICE_URL}/openai/deployments/anthropic.claude-3-haiku-20240307-v1:0/chat/completions",
|
|
221
|
+
"interceptors": [
|
|
222
|
+
"chat-statistics-reporter",
|
|
223
|
+
"chat-reply-as-pirate"
|
|
224
|
+
]
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Make sure that
|
|
231
|
+
|
|
232
|
+
1. chat completion interceptors are only used in chat models or application,
|
|
233
|
+
2. embeddings interceptors are only used in embeddings models.
|
|
234
|
+
|
|
235
|
+
The stack of interceptors in DIAL works similarly to a stack of middlewares in Express.js or Django:
|
|
236
|
+
|
|
237
|
+
```txt
|
|
238
|
+
Client -> (original request) ->
|
|
239
|
+
Interceptor 1 -> (modified request #1) ->
|
|
240
|
+
Interceptor 2 -> (modified request #2) ->
|
|
241
|
+
Upstream -> (original response) ->
|
|
242
|
+
Interceptor 2 -> (modified response #1) ->
|
|
243
|
+
Interceptor 1 -> (modified response #2) ->
|
|
244
|
+
Client
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Every** request/response in the diagram above goes through the DIAL Core. This is hidden from the diagram for brevity.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# AI DIAL Interceptors Python SDK
|
|
2
|
+
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> This package is in early development and subject to rapid changes. Breaking changes between versions are likely as the project evolves.
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
The framework provides useful classes and helpers for creating DIAL Interceptors in Python for chat completion and embedding models.
|
|
9
|
+
|
|
10
|
+
An interceptor could be thought of as a middleware that
|
|
11
|
+
|
|
12
|
+
1. modifies an incoming DIAL request received from the client *(or it may leave it as is)*
|
|
13
|
+
2. calls upstream DIAL application *(**the upstream** for short)* with the modified request
|
|
14
|
+
3. modifies the response from the upstream *(or it may leave it as is)*
|
|
15
|
+
4. returns the modified response to the client
|
|
16
|
+
|
|
17
|
+
The upstream is encapsulated behind a special deployment id `interceptor`. This deployment id is resolved by the DIAL Core into an appropriate deployment id.
|
|
18
|
+
|
|
19
|
+
Interceptors could be classified into the following categories:
|
|
20
|
+
|
|
21
|
+
1. **Pre-interceptors** that only modify the incoming request from the client *(e.g. rejecting requests following certain criteria)*
|
|
22
|
+
2. **Post-interceptors** that only modify the response received from the upstream *(e.g. censoring the response)*
|
|
23
|
+
3. **Generic interceptors** that modify both the incoming request and the response from the upstream *(e.g. caching the responses)*
|
|
24
|
+
|
|
25
|
+
To create chat completion interceptor one needs to implement instance of the class [ChatCompletionInterceptor](aidial_interceptors_sdk/chat_completion/base.py) and for embedding interceptor - [EmbeddingsInterceptor](aidial_interceptors_sdk/embeddings/base.py).
|
|
26
|
+
|
|
27
|
+
See [example](aidial_interceptors_sdk/examples/interceptor/registry.py) interceptor implementations for more details.
|
|
28
|
+
|
|
29
|
+
## Environment Variables
|
|
30
|
+
|
|
31
|
+
Copy `.env.example` to `.env` and customize it for your environment:
|
|
32
|
+
|
|
33
|
+
|Variable|Default|Description|
|
|
34
|
+
|---|---|---|
|
|
35
|
+
|LOG_LEVEL|INFO|Log level. Use DEBUG for dev purposes and INFO in prod|
|
|
36
|
+
|WEB_CONCURRENCY|1|Number of workers for the server|
|
|
37
|
+
|DIAL_URL||The URL of the DIAL Core server|
|
|
38
|
+
|
|
39
|
+
## Development
|
|
40
|
+
|
|
41
|
+
This project uses [Python>=3.11](https://www.python.org/downloads/) and [Poetry>=1.6.1](https://python-poetry.org/) as a dependency manager.
|
|
42
|
+
|
|
43
|
+
Check out Poetry's [documentation on how to install it](https://python-poetry.org/docs/#installation) on your system before proceeding.
|
|
44
|
+
|
|
45
|
+
To install requirements:
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
poetry install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This will install all requirements for running the package, linting, formatting and tests.
|
|
52
|
+
|
|
53
|
+
### IDE configuration
|
|
54
|
+
|
|
55
|
+
The recommended IDE is [VSCode](https://code.visualstudio.com/).
|
|
56
|
+
Open the project in VSCode and install the recommended extensions.
|
|
57
|
+
|
|
58
|
+
The VSCode is configured to use PEP-8 compatible formatter [Black](https://black.readthedocs.io/en/stable/index.html).
|
|
59
|
+
|
|
60
|
+
Alternatively you can use [PyCharm](https://www.jetbrains.com/pycharm/).
|
|
61
|
+
|
|
62
|
+
Set-up the Black formatter for PyCharm [manually](https://black.readthedocs.io/en/stable/integrations/editors.html#pycharm-intellij-idea) or
|
|
63
|
+
install PyCharm>=2023.2 with [built-in Black support](https://blog.jetbrains.com/pycharm/2023/07/2023-2/#black).
|
|
64
|
+
|
|
65
|
+
### Make on Windows
|
|
66
|
+
|
|
67
|
+
As of now, Windows distributions do not include the make tool. To run make commands, the tool can be installed using
|
|
68
|
+
the following command (since [Windows 10](https://learn.microsoft.com/en-us/windows/package-manager/winget/)):
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
winget install GnuWin32.Make
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
For convenience, the tool folder can be added to the PATH environment variable as `C:\Program Files (x86)\GnuWin32\bin`.
|
|
75
|
+
The command definitions inside Makefile should be cross-platform to keep the development environment setup simple.
|
|
76
|
+
|
|
77
|
+
### Lint
|
|
78
|
+
|
|
79
|
+
To run the linting before committing:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
make lint
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
To auto-fix formatting issues run:
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
make format
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Test
|
|
92
|
+
|
|
93
|
+
To run unit tests:
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
make test
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Clean
|
|
100
|
+
|
|
101
|
+
To remove the virtual environment and build artifacts:
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
make clean
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Examples
|
|
108
|
+
|
|
109
|
+
The repository also provides examples of various DIAL Interceptors all packed into a DIAL service.
|
|
110
|
+
|
|
111
|
+
Keep in mind, that the following example interceptors aren't ready for a production use.
|
|
112
|
+
They are provided solely as examples to demonstrate basic use cases of interceptors. Use at your discretion.
|
|
113
|
+
|
|
114
|
+
### Chat completion interceptors
|
|
115
|
+
|
|
116
|
+
|Interceptor name|Category|Description|
|
|
117
|
+
|---|---|---|
|
|
118
|
+
|reply-as-pirate|Pre|Injects systems prompt `Reply as a pirate` to the request|
|
|
119
|
+
|reject-external-links|Pre|Rejects any URL in DIAL attachments which do not point to DIAL Core storage|
|
|
120
|
+
|reject-blacklisted-words|Generic|Rejects the request if it contains any blacklisted words|
|
|
121
|
+
|image-watermark|Post|Stamps "EPAM DIAL" watermark on all image attachments in the response. Demonstrates how to work with files stored on DIAL File Storage.|
|
|
122
|
+
|statistics-reporter|Post|Collects statistics on the response stream *(tokens/sec, finish reason, completion tokens etc)* and reports it in a new stage when response is finished|
|
|
123
|
+
|pii-anonymizer|Generic|Anonymizes any PII in the request, calls the upstream, deanonymizes the response|
|
|
124
|
+
|replicator:N|Generic|Calls the upstream N times and combines the N response into a single response. Could be useful for stabilization of model's output, since certain models aren't deterministic.|
|
|
125
|
+
|cache|Generic|Caches incoming chat completion requests. **Not ready for production use. Use at your discretion**|
|
|
126
|
+
|no-op|Generic|No-op interceptor - does not modify the request or the response, simply proxies the upstream|
|
|
127
|
+
|
|
128
|
+
### Embeddings interceptors
|
|
129
|
+
|
|
130
|
+
|Interceptor name|Category|Description|
|
|
131
|
+
|---|---|---|
|
|
132
|
+
|reject-blacklisted-words|Pre|Rejects the request if it contains any blacklisted words|
|
|
133
|
+
|normalize-vector|Post|Normalizes the vector in the response|
|
|
134
|
+
|project-vector:N|Post|Changes the dimensionality of the vectors in the response to N, where N is an integer path parameter.|
|
|
135
|
+
|no-op|Generic|No-op interceptor - does not modify the request or the response, simply proxies the upstream|
|
|
136
|
+
|
|
137
|
+
### Environment variables
|
|
138
|
+
|
|
139
|
+
Copy `.env.example` to `.env` and customize it for your environment:
|
|
140
|
+
|
|
141
|
+
|Variable|Default|Description|
|
|
142
|
+
|---|---|---|
|
|
143
|
+
|PII_ANONYMIZER_LABELS_TO_REDACT|PERSON,ORG,GPE,PRODUCT|Comma-separated list of spaCy entity types to redact. Find the full list of entities [here](https://github.com/explosion/spacy-models/blob/e46017f5c8241096c1b30fae080f0e0709c8038c/meta/en_core_web_sm-3.7.0.json#L121-L140).|
|
|
144
|
+
|
|
145
|
+
### Running interceptor as a DIAL service
|
|
146
|
+
|
|
147
|
+
To run dev application locally run:
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
make examples_serve
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
To run from Docker container:
|
|
154
|
+
|
|
155
|
+
```sh
|
|
156
|
+
make examples_docker_serve
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Either of the commands will start the server on `http://localhost:5000` exposing endpoints for each of the interceptors like these:
|
|
160
|
+
|
|
161
|
+
- `http://localhost:5000/openai/deployments/pii-anonymizer/chat/completions`
|
|
162
|
+
- `http://localhost:5000/openai/deployments/normalize-vector/embeddings`
|
|
163
|
+
|
|
164
|
+
### DIAL Core configuration
|
|
165
|
+
|
|
166
|
+
The interceptor endpoints are defined in the `interceptors` section of the DIAL Core configuration like this:
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{
|
|
170
|
+
"interceptors": {
|
|
171
|
+
"chat-reply-as-pirate": {
|
|
172
|
+
"endpoint": "${INTERCEPTOR_SERVICE_URL}/openai/deployments/reply-as-pirate/chat/completions"
|
|
173
|
+
},
|
|
174
|
+
"chat-statistics-reporter": {
|
|
175
|
+
"endpoint": "${INTERCEPTOR_SERVICE_URL}/openai/deployments/statistics-reporter/chat/completions"
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
where `INTERCEPTOR_SERVICE_URL` is the URL of the interceptor service, which is `http://localhost:5000` when run locally, or the interceptor service URL when deployed within Kubernetes.
|
|
182
|
+
|
|
183
|
+
The declared interceptors could be then attached to particular models and applications:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"models": {
|
|
188
|
+
"anthropic.claude-v3-haiku": {
|
|
189
|
+
"type": "chat",
|
|
190
|
+
"iconUrl": "anthropic.svg",
|
|
191
|
+
"endpoint": "${BEDROCK_ADAPTER_SERVICE_URL}/openai/deployments/anthropic.claude-3-haiku-20240307-v1:0/chat/completions",
|
|
192
|
+
"interceptors": [
|
|
193
|
+
"chat-statistics-reporter",
|
|
194
|
+
"chat-reply-as-pirate"
|
|
195
|
+
]
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Make sure that
|
|
202
|
+
|
|
203
|
+
1. chat completion interceptors are only used in chat models or application,
|
|
204
|
+
2. embeddings interceptors are only used in embeddings models.
|
|
205
|
+
|
|
206
|
+
The stack of interceptors in DIAL works similarly to a stack of middlewares in Express.js or Django:
|
|
207
|
+
|
|
208
|
+
```txt
|
|
209
|
+
Client -> (original request) ->
|
|
210
|
+
Interceptor 1 -> (modified request #1) ->
|
|
211
|
+
Interceptor 2 -> (modified request #2) ->
|
|
212
|
+
Upstream -> (original response) ->
|
|
213
|
+
Interceptor 2 -> (modified response #1) ->
|
|
214
|
+
Interceptor 1 -> (modified response #2) ->
|
|
215
|
+
Client
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**Every** request/response in the diagram above goes through the DIAL Core. This is hidden from the diagram for brevity.
|