aidial-sdk 0.46.0.dev8__tar.gz → 0.47.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.
Files changed (77) hide show
  1. aidial_sdk-0.46.0.dev8/README.md → aidial_sdk-0.47.0/PKG-INFO +110 -0
  2. aidial_sdk-0.46.0.dev8/PKG-INFO → aidial_sdk-0.47.0/README.md +67 -40
  3. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/application.py +2 -2
  4. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/request.py +3 -3
  5. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/response.py +3 -3
  6. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/from_request_mixin.py +12 -9
  7. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/telemetry/init.py +19 -9
  8. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/__init__.py +28 -0
  9. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/app.py +134 -0
  10. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/cache_breakpoints.py +48 -0
  11. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/dial_extensions.py +66 -0
  12. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/from_chat_completions.py +237 -0
  13. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/reasoning.py +48 -0
  14. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/streaming.py +245 -0
  15. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/system_turns.py +26 -0
  16. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/chat_completions/to_chat_completions.py +695 -0
  17. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/common.py +61 -0
  18. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/core_client.py +64 -0
  19. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/errors.py +153 -0
  20. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/settings.py +21 -0
  21. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/sse_stream.py +209 -0
  22. aidial_sdk-0.47.0/aidial_sdk/translators/messages_to_chat_completions/translation_state.py +39 -0
  23. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/streaming.py +1 -2
  24. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/pyproject.toml +21 -14
  25. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/LICENSE +0 -0
  26. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/__init__.py +0 -0
  27. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/_errors.py +0 -0
  28. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/_pydantic/__init__.py +0 -0
  29. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/_pydantic/_compat.py +0 -0
  30. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/_pydantic/_model_config.py +0 -0
  31. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/__init__.py +0 -0
  32. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/_cache.py +0 -0
  33. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/_types.py +0 -0
  34. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/base.py +0 -0
  35. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/choice.py +0 -0
  36. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/choice_base.py +0 -0
  37. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/chunks.py +0 -0
  38. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/enums.py +0 -0
  39. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/form.py +0 -0
  40. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/function_call.py +0 -0
  41. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/function_tool_call.py +0 -0
  42. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/chat_completion/stage.py +0 -0
  43. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/__init__.py +0 -0
  44. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/_headers.py +0 -0
  45. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/configuration.py +0 -0
  46. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/rate.py +0 -0
  47. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/tokenize.py +0 -0
  48. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/deployment/truncate_prompt.py +0 -0
  49. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/embeddings/__init__.py +0 -0
  50. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/embeddings/base.py +0 -0
  51. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/embeddings/request.py +0 -0
  52. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/embeddings/response.py +0 -0
  53. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/exceptions.py +0 -0
  54. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/header_propagator.py +0 -0
  55. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/py.typed +0 -0
  56. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/pydantic/v2.py +0 -0
  57. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/pydantic_v1/__init__.py +0 -0
  58. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/response_headers.py +0 -0
  59. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/telemetry/__init__.py +0 -0
  60. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/telemetry/_context.py +0 -0
  61. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/telemetry/types.py +0 -0
  62. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/__init__.py +0 -0
  63. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_attachment.py +0 -0
  64. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_cancel_scope.py +0 -0
  65. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_content_stream.py +0 -0
  66. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_disconnect_middleware.py +0 -0
  67. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_indexed_list.py +0 -0
  68. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_json_log_formatter.py +0 -0
  69. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_logging.py +0 -0
  70. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/_reflection.py +0 -0
  71. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/env.py +0 -0
  72. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/errors.py +0 -0
  73. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/json.py +0 -0
  74. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/log_config.py +0 -0
  75. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/logging.py +0 -0
  76. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/merge_chunks.py +0 -0
  77. {aidial_sdk-0.46.0.dev8 → aidial_sdk-0.47.0}/aidial_sdk/utils/pydantic.py +0 -0
@@ -1,3 +1,45 @@
1
+ Metadata-Version: 2.4
2
+ Name: aidial-sdk
3
+ Version: 0.47.0
4
+ Summary: Framework to create applications and model adapters for AI DIAL
5
+ License-Expression: Apache-2.0
6
+ License-File: LICENSE
7
+ Author: EPAM RAIL
8
+ Author-email: SpecialEPM-DIALDevTeam@epam.com
9
+ Requires-Python: >=3.11,<4.0
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Provides-Extra: httpx
16
+ Provides-Extra: telemetry
17
+ Provides-Extra: translators
18
+ Requires-Dist: anthropic (>=0.105.0,<1.0) ; extra == "translators"
19
+ Requires-Dist: fastapi (>=0.51,<1.0)
20
+ Requires-Dist: httpx (>=0.25.0,<1.0) ; extra == "httpx"
21
+ Requires-Dist: httpx (>=0.25.0,<1.0) ; extra == "translators"
22
+ Requires-Dist: openai (>=2.46.0,<3.0) ; extra == "translators"
23
+ Requires-Dist: opentelemetry-api (>=1.22.0,<2.0) ; extra == "telemetry"
24
+ Requires-Dist: opentelemetry-exporter-otlp-proto-grpc (>=1.22.0,<2.0) ; extra == "telemetry"
25
+ Requires-Dist: opentelemetry-exporter-prometheus (>=0.64b0) ; extra == "telemetry"
26
+ Requires-Dist: opentelemetry-instrumentation-aiohttp-client (>=0.64b0) ; extra == "telemetry"
27
+ Requires-Dist: opentelemetry-instrumentation-fastapi (>=0.64b0) ; extra == "telemetry"
28
+ Requires-Dist: opentelemetry-instrumentation-httpx (>=0.64b0) ; extra == "telemetry"
29
+ Requires-Dist: opentelemetry-instrumentation-logging (>=0.64b0) ; extra == "telemetry"
30
+ Requires-Dist: opentelemetry-instrumentation-requests (>=0.64b0) ; extra == "telemetry"
31
+ Requires-Dist: opentelemetry-instrumentation-system-metrics (>=0.64b0) ; extra == "telemetry"
32
+ Requires-Dist: opentelemetry-instrumentation-urllib (>=0.64b0) ; extra == "telemetry"
33
+ Requires-Dist: opentelemetry-sdk (>=1.22.0,<2.0) ; extra == "telemetry"
34
+ Requires-Dist: prometheus-client (>=0.17.1,<1.0) ; extra == "telemetry"
35
+ Requires-Dist: pydantic (>=1.10.17,<3)
36
+ Requires-Dist: uvicorn (>=0.19,<1.0)
37
+ Requires-Dist: wrapt (>=1.10,<3)
38
+ Project-URL: Documentation, https://dialx.ai/dial_api
39
+ Project-URL: Homepage, https://dialx.ai
40
+ Project-URL: Repository, https://github.com/epam/ai-dial-sdk
41
+ Description-Content-Type: text/markdown
42
+
1
43
  <h1 align="center">
2
44
  AI DIAL Python SDK
3
45
  </h1>
@@ -23,6 +65,7 @@
23
65
  - [Run](#run)
24
66
  - [Check](#check)
25
67
  - [Endpoints](#endpoints)
68
+ - [Anthropic Messages to Chat Completions translator](#anthropic-messages-to-chat-completions-translator)
26
69
  - [Development](#development)
27
70
  - [Development Environment](#development-environment)
28
71
  - [Setup](#setup)
@@ -167,6 +210,72 @@ An `/openai/v1` request with none of the headers set fails with `500 Internal Se
167
210
 
168
211
  ---
169
212
 
213
+ ### Anthropic Messages to Chat Completions translator
214
+
215
+ The translator lets an Anthropic-native client (e.g. Claude Code) talk to a model that only speaks the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). It converts an Anthropic Messages request into a Chat Completions request to DIAL Core at `DIAL_URL`, and converts the response, streaming or not, back into the Anthropic Messages shape.
216
+
217
+ The translator requires the `translators` extra and Pydantic V2 models (`PYDANTIC_V2=True`):
218
+
219
+ ```sh
220
+ pip install "aidial-sdk[translators]"
221
+ ```
222
+
223
+ Mount it on your application:
224
+
225
+ ```python
226
+ import httpx
227
+
228
+ from aidial_sdk import DIALApp
229
+ from aidial_sdk.translators.messages_to_chat_completions import mount_translator
230
+
231
+ app = DIALApp()
232
+ mount_translator(app, httpx.AsyncClient())
233
+ ```
234
+
235
+ The second argument is the HTTP client the translator calls DIAL Core with: either an `httpx.AsyncClient` or an async function that takes the incoming `Request` and returns one. The `path` (`/openaiChatCompletions` by default) and `name` keyword arguments configure the mount.
236
+
237
+ |Endpoint|Calls on DIAL Core|
238
+ |---|---|
239
+ |`POST /openaiChatCompletions/anthropic/v1/messages`|`POST {DIAL_URL}/openai/v1/chat/completions`|
240
+ |`POST /openaiChatCompletions/legacy/anthropic/v1/messages`|`POST {DIAL_URL}/openai/deployments/{deployment}/chat/completions`|
241
+
242
+ `DIAL_URL` must be set, otherwise the endpoints answer `500`.
243
+
244
+ To expose a Chat Completions model under `/anthropic/v1/messages`, add an `anthropicMessages` interface to its DIAL Core config, pointing `base_url` at the mount path of the application that runs the translator (or at `/openaiChatCompletions/legacy` to call DIAL Core's legacy per-deployment endpoint):
245
+
246
+ <details>
247
+ <summary>DIAL Core config example</summary>
248
+
249
+ ```json
250
+ {
251
+ "models": {
252
+ "gpt-5.5": {
253
+ "type": "chat",
254
+ "interfaces": {
255
+ "openaiChatCompletions": {
256
+ "base_url": "${OPENAI_ADAPTER_ORIGIN}"
257
+ },
258
+ "anthropicMessages": {
259
+ "base_url": "${TRANSLATOR_ORIGIN}/openaiChatCompletions"
260
+ }
261
+ },
262
+ "upstreams": [
263
+ {
264
+ "endpoint": "https://api.openai.com/v1/chat/completions",
265
+ "key": "..."
266
+ }
267
+ ]
268
+ }
269
+ }
270
+ }
271
+ ```
272
+
273
+ </details>
274
+
275
+ See the [translation guide](docs/messages-to-chat-translation.md) for how request parameters, content blocks, tools, caching, usage, and errors are translated, and for the translator's limitations.
276
+
277
+ ---
278
+
170
279
  ## Development
171
280
 
172
281
  ### Development Environment
@@ -269,3 +378,4 @@ make publish
269
378
  ```
270
379
 
271
380
  ---
381
+
@@ -1,42 +1,3 @@
1
- Metadata-Version: 2.4
2
- Name: aidial-sdk
3
- Version: 0.46.0.dev8
4
- Summary: Framework to create applications and model adapters for AI DIAL
5
- License-Expression: Apache-2.0
6
- License-File: LICENSE
7
- Author: EPAM RAIL
8
- Author-email: SpecialEPM-DIALDevTeam@epam.com
9
- Requires-Python: >=3.10,<4.0
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.10
12
- Classifier: Programming Language :: Python :: 3.11
13
- Classifier: Programming Language :: Python :: 3.12
14
- Classifier: Programming Language :: Python :: 3.13
15
- Classifier: Programming Language :: Python :: 3.14
16
- Provides-Extra: httpx
17
- Provides-Extra: telemetry
18
- Requires-Dist: fastapi (>=0.51,<1.0)
19
- Requires-Dist: httpx (>=0.25.0,<1.0) ; extra == "httpx"
20
- Requires-Dist: opentelemetry-api (>=1.22.0,<2.0) ; extra == "telemetry"
21
- Requires-Dist: opentelemetry-exporter-otlp-proto-grpc (>=1.22.0,<2.0) ; extra == "telemetry"
22
- Requires-Dist: opentelemetry-exporter-prometheus (>=0.43b0) ; extra == "telemetry"
23
- Requires-Dist: opentelemetry-instrumentation-aiohttp-client (>=0.43b0) ; extra == "telemetry"
24
- Requires-Dist: opentelemetry-instrumentation-fastapi (>=0.43b0) ; extra == "telemetry"
25
- Requires-Dist: opentelemetry-instrumentation-httpx (>=0.43b0) ; extra == "telemetry"
26
- Requires-Dist: opentelemetry-instrumentation-logging (>=0.43b0) ; extra == "telemetry"
27
- Requires-Dist: opentelemetry-instrumentation-requests (>=0.43b0) ; extra == "telemetry"
28
- Requires-Dist: opentelemetry-instrumentation-system-metrics (>=0.43b0) ; extra == "telemetry"
29
- Requires-Dist: opentelemetry-instrumentation-urllib (>=0.43b0) ; extra == "telemetry"
30
- Requires-Dist: opentelemetry-sdk (>=1.22.0,<2.0) ; extra == "telemetry"
31
- Requires-Dist: prometheus-client (>=0.17.1,<1.0) ; extra == "telemetry"
32
- Requires-Dist: pydantic (>=1.10.17,<3)
33
- Requires-Dist: uvicorn (>=0.19,<1.0)
34
- Requires-Dist: wrapt (>=1.10,<3)
35
- Project-URL: Documentation, https://dialx.ai/dial_api
36
- Project-URL: Homepage, https://dialx.ai
37
- Project-URL: Repository, https://github.com/epam/ai-dial-sdk
38
- Description-Content-Type: text/markdown
39
-
40
1
  <h1 align="center">
41
2
  AI DIAL Python SDK
42
3
  </h1>
@@ -62,6 +23,7 @@ Description-Content-Type: text/markdown
62
23
  - [Run](#run)
63
24
  - [Check](#check)
64
25
  - [Endpoints](#endpoints)
26
+ - [Anthropic Messages to Chat Completions translator](#anthropic-messages-to-chat-completions-translator)
65
27
  - [Development](#development)
66
28
  - [Development Environment](#development-environment)
67
29
  - [Setup](#setup)
@@ -206,6 +168,72 @@ An `/openai/v1` request with none of the headers set fails with `500 Internal Se
206
168
 
207
169
  ---
208
170
 
171
+ ### Anthropic Messages to Chat Completions translator
172
+
173
+ The translator lets an Anthropic-native client (e.g. Claude Code) talk to a model that only speaks the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). It converts an Anthropic Messages request into a Chat Completions request to DIAL Core at `DIAL_URL`, and converts the response, streaming or not, back into the Anthropic Messages shape.
174
+
175
+ The translator requires the `translators` extra and Pydantic V2 models (`PYDANTIC_V2=True`):
176
+
177
+ ```sh
178
+ pip install "aidial-sdk[translators]"
179
+ ```
180
+
181
+ Mount it on your application:
182
+
183
+ ```python
184
+ import httpx
185
+
186
+ from aidial_sdk import DIALApp
187
+ from aidial_sdk.translators.messages_to_chat_completions import mount_translator
188
+
189
+ app = DIALApp()
190
+ mount_translator(app, httpx.AsyncClient())
191
+ ```
192
+
193
+ The second argument is the HTTP client the translator calls DIAL Core with: either an `httpx.AsyncClient` or an async function that takes the incoming `Request` and returns one. The `path` (`/openaiChatCompletions` by default) and `name` keyword arguments configure the mount.
194
+
195
+ |Endpoint|Calls on DIAL Core|
196
+ |---|---|
197
+ |`POST /openaiChatCompletions/anthropic/v1/messages`|`POST {DIAL_URL}/openai/v1/chat/completions`|
198
+ |`POST /openaiChatCompletions/legacy/anthropic/v1/messages`|`POST {DIAL_URL}/openai/deployments/{deployment}/chat/completions`|
199
+
200
+ `DIAL_URL` must be set, otherwise the endpoints answer `500`.
201
+
202
+ To expose a Chat Completions model under `/anthropic/v1/messages`, add an `anthropicMessages` interface to its DIAL Core config, pointing `base_url` at the mount path of the application that runs the translator (or at `/openaiChatCompletions/legacy` to call DIAL Core's legacy per-deployment endpoint):
203
+
204
+ <details>
205
+ <summary>DIAL Core config example</summary>
206
+
207
+ ```json
208
+ {
209
+ "models": {
210
+ "gpt-5.5": {
211
+ "type": "chat",
212
+ "interfaces": {
213
+ "openaiChatCompletions": {
214
+ "base_url": "${OPENAI_ADAPTER_ORIGIN}"
215
+ },
216
+ "anthropicMessages": {
217
+ "base_url": "${TRANSLATOR_ORIGIN}/openaiChatCompletions"
218
+ }
219
+ },
220
+ "upstreams": [
221
+ {
222
+ "endpoint": "https://api.openai.com/v1/chat/completions",
223
+ "key": "..."
224
+ }
225
+ ]
226
+ }
227
+ }
228
+ }
229
+ ```
230
+
231
+ </details>
232
+
233
+ See the [translation guide](docs/messages-to-chat-translation.md) for how request parameters, content blocks, tools, caching, usage, and errors are translated, and for the translator's limitations.
234
+
235
+ ---
236
+
209
237
  ## Development
210
238
 
211
239
  ### Development Environment
@@ -308,4 +336,3 @@ make publish
308
336
  ```
309
337
 
310
338
  ---
311
-
@@ -24,7 +24,7 @@ from aidial_sdk.chat_completion.response import (
24
24
  from aidial_sdk.deployment.configuration import ConfigurationRequest
25
25
  from aidial_sdk.deployment.from_request_mixin import (
26
26
  FromRequestMixin,
27
- resolve_deployment_id,
27
+ get_upstream_deployment_id,
28
28
  )
29
29
  from aidial_sdk.deployment.rate import RateRequest
30
30
  from aidial_sdk.deployment.tokenize import TokenizeRequest
@@ -288,7 +288,7 @@ class DIALApp(FastAPI):
288
288
 
289
289
  def _v1_dispatcher(self, endpoint: str) -> Handler:
290
290
  async def _handler(original_request: Request) -> Response:
291
- deployment_id = resolve_deployment_id(
291
+ deployment_id = get_upstream_deployment_id(
292
292
  original_request.headers, None
293
293
  )
294
294
  set_log_deployment(deployment_id)
@@ -1,8 +1,6 @@
1
1
  from collections.abc import Mapping
2
2
  from enum import Enum
3
- from typing import Annotated, Any, Literal
4
-
5
- from typing_extensions import assert_never
3
+ from typing import Annotated, Any, Literal, assert_never
6
4
 
7
5
  from aidial_sdk._pydantic import (
8
6
  PYDANTIC_V2,
@@ -279,6 +277,8 @@ class ReasoningEffort(str, Enum):
279
277
  LOW = "low"
280
278
  MEDIUM = "medium"
281
279
  HIGH = "high"
280
+ XHIGH = "xhigh"
281
+ MAX = "max"
282
282
 
283
283
 
284
284
  class AzureChatCompletionRequest(ExtraAllowModel):
@@ -1,11 +1,9 @@
1
1
  import asyncio
2
2
  from collections.abc import Callable, Coroutine
3
3
  from time import time
4
- from typing import Any
4
+ from typing import Any, assert_never
5
5
  from uuid import uuid4
6
6
 
7
- from typing_extensions import assert_never
8
-
9
7
  from aidial_sdk.chat_completion._cache import CacheBreakpointPath
10
8
  from aidial_sdk.chat_completion._types import ChunkQueue
11
9
  from aidial_sdk.chat_completion.choice import Choice
@@ -29,6 +27,7 @@ from aidial_sdk.deployment._headers import (
29
27
  DIAL_CACHE_EXPIRE_AT,
30
28
  DIAL_CACHE_EXTRA_METADATA,
31
29
  )
30
+ from aidial_sdk.deployment.from_request_mixin import get_client_deployment_id
32
31
  from aidial_sdk.exceptions import HTTPException as DIALException
33
32
  from aidial_sdk.exceptions import RequestValidationError, RuntimeServerError
34
33
  from aidial_sdk.utils._cancel_scope import CancelScope
@@ -71,6 +70,7 @@ class Response:
71
70
  if self.request.stream
72
71
  else "chat.completion"
73
72
  ),
73
+ model=get_client_deployment_id(request.headers),
74
74
  )
75
75
 
76
76
  self._headers = []
@@ -36,24 +36,27 @@ from aidial_sdk.utils.pydantic import ExtraAllowModel
36
36
  T = TypeVar("T", bound="FromRequestMixin")
37
37
 
38
38
 
39
- def resolve_deployment_id(
39
+ def get_upstream_deployment_id(
40
40
  headers: Mapping[str, str], deployment_id: str | None
41
41
  ) -> str:
42
- """The deployment id of a request comes either from the path it was
43
- sent to or, for the deployment-agnostic paths, from its headers."""
42
+ """The upstream deployment id that a DIAL application is expected to call."""
44
43
 
45
- effective_deployment_id = (
44
+ return (
46
45
  headers.get(DIAL_OVERRIDE_NAME)
47
46
  or deployment_id
48
- or headers.get(DIAL_DEPLOYMENT_ID)
47
+ or get_client_deployment_id(headers)
49
48
  )
50
49
 
51
- if effective_deployment_id is None:
50
+
51
+ def get_client_deployment_id(headers: Mapping[str, str]) -> str:
52
+ """The client-facing deployment id that a DIAL application is expected
53
+ to return as a model parameter in the response."""
54
+
55
+ if (deployment_id := headers.get(DIAL_DEPLOYMENT_ID)) is None:
52
56
  raise InternalServerError(
53
57
  f"The request headers are missing {DIAL_DEPLOYMENT_ID} header."
54
58
  )
55
-
56
- return effective_deployment_id
59
+ return deployment_id
57
60
 
58
61
 
59
62
  class FromRequestMixin(ABC, ExtraAllowModel):
@@ -235,7 +238,7 @@ class FromRequestDeploymentMixin(FromRequestMixin):
235
238
  bearer_token_secret=(
236
239
  SecretStr(bearer_token) if bearer_token else None
237
240
  ),
238
- deployment_id=resolve_deployment_id(headers, deployment_id),
241
+ deployment_id=get_upstream_deployment_id(headers, deployment_id),
239
242
  api_version=request.query_params.get("api-version"),
240
243
  headers=headers,
241
244
  original_request=request,
@@ -18,7 +18,7 @@ from opentelemetry.instrumentation.system_metrics import (
18
18
  )
19
19
  from opentelemetry.instrumentation.urllib import URLLibInstrumentor
20
20
  from opentelemetry.metrics import set_meter_provider
21
- from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler
21
+ from opentelemetry.sdk._logs import LoggerProvider
22
22
  from opentelemetry.sdk._logs.export import (
23
23
  BatchLogRecordProcessor,
24
24
  ConsoleLogRecordExporter,
@@ -86,13 +86,6 @@ def init_telemetry(app: FastAPI | None, config: TelemetryConfig):
86
86
  except ImportError:
87
87
  pass
88
88
 
89
- set_logging_format = config.tracing.logging
90
- if set_logging_format:
91
- # Remove any competing handlers to avoid duplicate logging
92
- remove_stream_handlers(logging.getLogger(), sys.stderr)
93
-
94
- LoggingInstrumentor().instrument(set_logging_format=set_logging_format)
95
-
96
89
  if config.logs is not None:
97
90
  # Adding a handler to the root logger which exports the logs to OTLP or to the console as JSON.
98
91
  provider = LoggerProvider(resource=resource)
@@ -120,7 +113,24 @@ def init_telemetry(app: FastAPI | None, config: TelemetryConfig):
120
113
  )
121
114
 
122
115
  set_logger_provider(provider)
123
- root.addHandler(LoggingHandler())
116
+
117
+ if config.tracing is not None or config.logs is not None:
118
+ # OTel's text format writes to stderr, which `console_export` takes over.
119
+ set_logging_format = (
120
+ config.tracing is not None
121
+ and config.tracing.logging
122
+ and not (config.logs is not None and config.logs.console_export)
123
+ )
124
+
125
+ if set_logging_format:
126
+ # Remove any competing handlers to avoid duplicate logging
127
+ remove_stream_handlers(logging.getLogger(), sys.stderr)
128
+
129
+ LoggingInstrumentor().instrument(
130
+ set_logging_format=set_logging_format,
131
+ inject_trace_context=config.tracing is not None,
132
+ enable_log_auto_instrumentation=config.logs is not None,
133
+ )
124
134
 
125
135
  if config.metrics is not None:
126
136
  metric_readers = []
@@ -0,0 +1,28 @@
1
+ import httpx
2
+ from fastapi import FastAPI
3
+
4
+ from aidial_sdk._pydantic import PYDANTIC_V2
5
+
6
+ if not PYDANTIC_V2:
7
+ raise ImportError(
8
+ "The Anthropic Messages API translator requires Pydantic v2: "
9
+ "install pydantic>=2 and set PYDANTIC_V2=True"
10
+ )
11
+
12
+ from aidial_sdk.translators.messages_to_chat_completions.chat_completions.app import ( # noqa: E402
13
+ ClientFactory,
14
+ create_anthropic_api_app,
15
+ )
16
+
17
+ __all__ = ["ClientFactory", "create_anthropic_api_app", "mount_translator"]
18
+
19
+
20
+ def mount_translator(
21
+ app: FastAPI,
22
+ get_client: ClientFactory[httpx.AsyncClient],
23
+ *,
24
+ path: str = "/openaiChatCompletions",
25
+ name: str = "Anthropic Messages API to Chat Completions API translator",
26
+ ) -> None:
27
+ translator = create_anthropic_api_app(get_client)
28
+ app.mount(path=path, app=translator, name=name)
@@ -0,0 +1,134 @@
1
+ import json
2
+ import logging
3
+ from collections.abc import Awaitable, Callable
4
+ from typing import TypeVar
5
+
6
+ import httpx
7
+ from anthropic.types.beta.message_create_params import MessageCreateParams
8
+ from fastapi import FastAPI, Request
9
+ from fastapi.responses import JSONResponse, Response, StreamingResponse
10
+ from openai import AsyncStream
11
+ from openai.types.chat import ChatCompletion, ChatCompletionChunk
12
+
13
+ from aidial_sdk.translators.messages_to_chat_completions.chat_completions.from_chat_completions import (
14
+ to_anthropic_response,
15
+ )
16
+ from aidial_sdk.translators.messages_to_chat_completions.chat_completions.streaming import (
17
+ translate_stream,
18
+ )
19
+ from aidial_sdk.translators.messages_to_chat_completions.chat_completions.to_chat_completions import (
20
+ to_chat_completions_request,
21
+ )
22
+ from aidial_sdk.translators.messages_to_chat_completions.common import (
23
+ not_found,
24
+ with_debug_logging,
25
+ )
26
+ from aidial_sdk.translators.messages_to_chat_completions.core_client import (
27
+ core_headers,
28
+ core_legacy_client,
29
+ core_v1_client,
30
+ )
31
+ from aidial_sdk.translators.messages_to_chat_completions.errors import (
32
+ AnthropicErrorType,
33
+ AnthropicHTTPError,
34
+ translator_error_handler,
35
+ )
36
+ from aidial_sdk.translators.messages_to_chat_completions.settings import (
37
+ get_dial_url,
38
+ )
39
+ from aidial_sdk.translators.messages_to_chat_completions.translation_state import (
40
+ TranslationState,
41
+ )
42
+ from aidial_sdk.utils.logging import logger as log
43
+
44
+ ClientT = TypeVar("ClientT")
45
+ ClientFactory = Callable[[Request], Awaitable[ClientT]] | ClientT
46
+
47
+
48
+ async def _handle_messages(
49
+ request: Request,
50
+ get_client: ClientFactory[httpx.AsyncClient],
51
+ legacy: bool,
52
+ ) -> Response:
53
+ dial_url = get_dial_url()
54
+ translation_state = TranslationState()
55
+
56
+ try:
57
+ req: MessageCreateParams = await request.json()
58
+ except ValueError as e:
59
+ raise AnthropicHTTPError(
60
+ AnthropicErrorType.INVALID_REQUEST,
61
+ f"Request body is not valid JSON: {e}",
62
+ ) from e
63
+
64
+ deployment = request.headers.get("x-dial-deployment-id") or req["model"]
65
+ chat_request = to_chat_completions_request(
66
+ req, deployment, translation_state
67
+ )
68
+
69
+ body = chat_request.model_dump(mode="json", exclude_none=True)
70
+ if log.isEnabledFor(logging.DEBUG):
71
+ log.debug(f"Translated request to Core: {json.dumps(body)}")
72
+
73
+ http_client = (
74
+ get_client
75
+ if isinstance(get_client, httpx.AsyncClient)
76
+ else await get_client(request)
77
+ )
78
+ client = (
79
+ core_legacy_client(dial_url, deployment, http_client)
80
+ if legacy
81
+ else core_v1_client(dial_url, http_client)
82
+ )
83
+ headers = core_headers(request)
84
+
85
+ if chat_request.stream:
86
+ events = await client.post(
87
+ "/chat/completions",
88
+ cast_to=ChatCompletion,
89
+ body=body,
90
+ options={"headers": headers},
91
+ stream=True,
92
+ stream_cls=AsyncStream[ChatCompletionChunk],
93
+ )
94
+ return StreamingResponse(
95
+ translate_stream(events, deployment, translation_state),
96
+ media_type="text/event-stream",
97
+ )
98
+
99
+ response = await client.post(
100
+ "/chat/completions",
101
+ cast_to=ChatCompletion,
102
+ body=body,
103
+ options={"headers": headers},
104
+ )
105
+ message = to_anthropic_response(response, deployment, translation_state)
106
+ return JSONResponse(
107
+ content=message.model_dump(mode="json", exclude_unset=True)
108
+ )
109
+
110
+
111
+ def create_anthropic_api_app(
112
+ get_client: ClientFactory[httpx.AsyncClient],
113
+ ) -> FastAPI:
114
+ app = FastAPI()
115
+
116
+ @app.post("/anthropic/v1/messages")
117
+ @with_debug_logging
118
+ @translator_error_handler
119
+ async def _handle_v1_messages(request: Request) -> Response:
120
+ return await _handle_messages(request, get_client, legacy=False)
121
+
122
+ @app.post("/legacy/anthropic/v1/messages")
123
+ @with_debug_logging
124
+ @translator_error_handler
125
+ async def _handle_legacy_messages(request: Request) -> Response:
126
+ return await _handle_messages(request, get_client, legacy=True)
127
+
128
+ app.add_api_route(
129
+ "/{full_path:path}",
130
+ not_found,
131
+ methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
132
+ )
133
+
134
+ return app
@@ -0,0 +1,48 @@
1
+ import re
2
+ from datetime import UTC, datetime, timedelta
3
+
4
+ from anthropic.types.beta import BetaCacheControlEphemeralParam as CacheControl
5
+
6
+ from aidial_sdk.chat_completion.request import CacheBreakpoint
7
+ from aidial_sdk.utils.logging import logger as log
8
+
9
+ _TTL = re.compile(r"^([1-9]\d*)([mh])$")
10
+
11
+ _UNITS = {
12
+ "m": timedelta(minutes=1),
13
+ "h": timedelta(hours=1),
14
+ }
15
+
16
+
17
+ def to_dial_cache_breakpoint(
18
+ controls: list[CacheControl],
19
+ ) -> CacheBreakpoint | None:
20
+ if not controls:
21
+ return None
22
+ expires_at: str | None = None
23
+ for control in controls:
24
+ try:
25
+ if (ttl := _ttl(control)) is not None:
26
+ candidate = _expire_at(ttl)
27
+ expires_at = (
28
+ max(expires_at, candidate) if expires_at else candidate
29
+ )
30
+ except (OverflowError, ValueError):
31
+ log.warning(
32
+ f"Ignoring unreadable cache_control.ttl: {control.get('ttl')}"
33
+ )
34
+ return CacheBreakpoint(expire_at=expires_at)
35
+
36
+
37
+ def _ttl(control: CacheControl) -> timedelta | None:
38
+ raw = control.get("ttl")
39
+ if raw is None:
40
+ return None
41
+ if isinstance(raw, str) and (match := _TTL.match(raw)):
42
+ return int(match.group(1)) * _UNITS[match.group(2)]
43
+ raise ValueError("Invalid ttl")
44
+
45
+
46
+ def _expire_at(ttl: timedelta) -> str:
47
+ expires_at = datetime.now(UTC) + ttl
48
+ return expires_at.replace(microsecond=0).isoformat().replace("+00:00", "Z")