rasa-pro 3.11.0a4.dev3__py3-none-any.whl → 3.11.0rc2__py3-none-any.whl
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.
Potentially problematic release.
This version of rasa-pro might be problematic. Click here for more details.
- rasa/__main__.py +22 -12
- rasa/api.py +1 -1
- rasa/cli/arguments/default_arguments.py +1 -2
- rasa/cli/arguments/shell.py +5 -1
- rasa/cli/e2e_test.py +1 -1
- rasa/cli/evaluate.py +8 -8
- rasa/cli/inspect.py +6 -4
- rasa/cli/llm_fine_tuning.py +1 -1
- rasa/cli/project_templates/calm/config.yml +5 -7
- rasa/cli/project_templates/calm/endpoints.yml +8 -0
- rasa/cli/project_templates/tutorial/config.yml +8 -5
- rasa/cli/project_templates/tutorial/data/flows.yml +1 -1
- rasa/cli/project_templates/tutorial/data/patterns.yml +5 -0
- rasa/cli/project_templates/tutorial/domain.yml +14 -0
- rasa/cli/project_templates/tutorial/endpoints.yml +7 -7
- rasa/cli/run.py +1 -1
- rasa/cli/scaffold.py +4 -2
- rasa/cli/studio/studio.py +18 -8
- rasa/cli/utils.py +5 -0
- rasa/cli/x.py +8 -8
- rasa/constants.py +1 -1
- rasa/core/actions/action_repeat_bot_messages.py +17 -0
- rasa/core/channels/channel.py +20 -0
- rasa/core/channels/inspector/dist/assets/{arc-6852c607.js → arc-bc141fb2.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{c4Diagram-d0fbc5ce-acc952b2.js → c4Diagram-d0fbc5ce-be2db283.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{classDiagram-936ed81e-848a7597.js → classDiagram-936ed81e-55366915.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{classDiagram-v2-c3cb15f1-a73d3e68.js → classDiagram-v2-c3cb15f1-bb529518.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{createText-62fc7601-e5ee049d.js → createText-62fc7601-b0ec81d6.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{edges-f2ad444c-771e517e.js → edges-f2ad444c-6166330c.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{erDiagram-9d236eb7-aa347178.js → erDiagram-9d236eb7-5ccc6a8e.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{flowDb-1972c806-651fc57d.js → flowDb-1972c806-fca3bfe4.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{flowDiagram-7ea5b25a-ca67804f.js → flowDiagram-7ea5b25a-4739080f.js} +1 -1
- rasa/core/channels/inspector/dist/assets/flowDiagram-v2-855bc5b3-736177bf.js +1 -0
- rasa/core/channels/inspector/dist/assets/{flowchart-elk-definition-abe16c3d-2dbc568d.js → flowchart-elk-definition-abe16c3d-7c1b0e0f.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{ganttDiagram-9b5ea136-25a65bd8.js → ganttDiagram-9b5ea136-772fd050.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{gitGraphDiagram-99d0ae7c-fdc7378d.js → gitGraphDiagram-99d0ae7c-8eae1dc9.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{index-2c4b9a3b-6f1fd606.js → index-2c4b9a3b-f55afcdf.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{index-efdd30c1.js → index-e7cef9de.js} +68 -68
- rasa/core/channels/inspector/dist/assets/{infoDiagram-736b4530-cb1a041a.js → infoDiagram-736b4530-124d4a14.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{journeyDiagram-df861f2b-14609879.js → journeyDiagram-df861f2b-7c4fae44.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{layout-2490f52b.js → layout-b9885fb6.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{line-40186f1f.js → line-7c59abb6.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{linear-08814e93.js → linear-4776f780.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{mindmap-definition-beec6740-1a534584.js → mindmap-definition-beec6740-2332c46c.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{pieDiagram-dbbf0591-72397b61.js → pieDiagram-dbbf0591-8fb39303.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{quadrantDiagram-4d7f4fd6-3bb0b6a3.js → quadrantDiagram-4d7f4fd6-3c7180a2.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{requirementDiagram-6fc4c22a-57334f61.js → requirementDiagram-6fc4c22a-e910bcb8.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{sankeyDiagram-8f13d901-111e1297.js → sankeyDiagram-8f13d901-ead16c89.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{sequenceDiagram-b655622a-10bcfe62.js → sequenceDiagram-b655622a-29a02a19.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{stateDiagram-59f0c015-acaf7513.js → stateDiagram-59f0c015-042b3137.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{stateDiagram-v2-2b26beab-3ec2a235.js → stateDiagram-v2-2b26beab-2178c0f3.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{styles-080da4f6-62730289.js → styles-080da4f6-23ffa4fc.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{styles-3dcbcfbf-5284ee76.js → styles-3dcbcfbf-94f59763.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{styles-9c745c82-642435e3.js → styles-9c745c82-78a6bebc.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{svgDrawCommon-4835440b-b250a350.js → svgDrawCommon-4835440b-eae2a6f6.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{timeline-definition-5b62e21b-c2b147ed.js → timeline-definition-5b62e21b-5c968d92.js} +1 -1
- rasa/core/channels/inspector/dist/assets/{xychartDiagram-2b33534f-f92cfea9.js → xychartDiagram-2b33534f-fd3db0d5.js} +1 -1
- rasa/core/channels/inspector/dist/index.html +1 -1
- rasa/core/channels/inspector/src/App.tsx +1 -1
- rasa/core/channels/inspector/src/helpers/audiostream.ts +77 -16
- rasa/core/channels/socketio.py +2 -1
- rasa/core/channels/telegram.py +1 -1
- rasa/core/channels/twilio.py +1 -1
- rasa/core/channels/voice_ready/audiocodes.py +12 -0
- rasa/core/channels/voice_ready/jambonz.py +15 -4
- rasa/core/channels/voice_ready/twilio_voice.py +6 -21
- rasa/core/channels/voice_stream/asr/asr_event.py +5 -0
- rasa/core/channels/voice_stream/asr/azure.py +122 -0
- rasa/core/channels/voice_stream/asr/deepgram.py +16 -6
- rasa/core/channels/voice_stream/audio_bytes.py +1 -0
- rasa/core/channels/voice_stream/browser_audio.py +31 -8
- rasa/core/channels/voice_stream/call_state.py +23 -0
- rasa/core/channels/voice_stream/tts/azure.py +6 -2
- rasa/core/channels/voice_stream/tts/cartesia.py +10 -6
- rasa/core/channels/voice_stream/tts/tts_engine.py +1 -0
- rasa/core/channels/voice_stream/twilio_media_streams.py +27 -18
- rasa/core/channels/voice_stream/util.py +4 -4
- rasa/core/channels/voice_stream/voice_channel.py +189 -39
- rasa/core/featurizers/single_state_featurizer.py +22 -1
- rasa/core/featurizers/tracker_featurizers.py +115 -18
- rasa/core/nlg/contextual_response_rephraser.py +32 -30
- rasa/core/persistor.py +86 -39
- rasa/core/policies/enterprise_search_policy.py +119 -60
- rasa/core/policies/flows/flow_executor.py +7 -4
- rasa/core/policies/intentless_policy.py +78 -22
- rasa/core/policies/ted_policy.py +58 -33
- rasa/core/policies/unexpected_intent_policy.py +15 -7
- rasa/core/processor.py +25 -0
- rasa/core/training/interactive.py +34 -35
- rasa/core/utils.py +8 -3
- rasa/dialogue_understanding/coexistence/llm_based_router.py +39 -12
- rasa/dialogue_understanding/commands/change_flow_command.py +6 -0
- rasa/dialogue_understanding/commands/user_silence_command.py +59 -0
- rasa/dialogue_understanding/commands/utils.py +5 -0
- rasa/dialogue_understanding/generator/constants.py +2 -0
- rasa/dialogue_understanding/generator/flow_retrieval.py +49 -4
- rasa/dialogue_understanding/generator/llm_based_command_generator.py +37 -23
- rasa/dialogue_understanding/generator/multi_step/multi_step_llm_command_generator.py +57 -10
- rasa/dialogue_understanding/generator/nlu_command_adapter.py +19 -1
- rasa/dialogue_understanding/generator/single_step/single_step_llm_command_generator.py +71 -11
- rasa/dialogue_understanding/patterns/default_flows_for_patterns.yml +39 -0
- rasa/dialogue_understanding/patterns/user_silence.py +37 -0
- rasa/dialogue_understanding/processor/command_processor.py +21 -1
- rasa/e2e_test/e2e_test_case.py +85 -6
- rasa/e2e_test/e2e_test_runner.py +4 -2
- rasa/e2e_test/utils/io.py +1 -1
- rasa/engine/validation.py +316 -10
- rasa/model_manager/config.py +15 -3
- rasa/model_manager/model_api.py +15 -7
- rasa/model_manager/runner_service.py +8 -6
- rasa/model_manager/socket_bridge.py +6 -3
- rasa/model_manager/trainer_service.py +7 -5
- rasa/model_manager/utils.py +28 -7
- rasa/model_service.py +9 -2
- rasa/model_training.py +2 -0
- rasa/nlu/classifiers/diet_classifier.py +38 -25
- rasa/nlu/classifiers/logistic_regression_classifier.py +22 -9
- rasa/nlu/classifiers/sklearn_intent_classifier.py +37 -16
- rasa/nlu/extractors/crf_entity_extractor.py +93 -50
- rasa/nlu/featurizers/sparse_featurizer/count_vectors_featurizer.py +45 -16
- rasa/nlu/featurizers/sparse_featurizer/lexical_syntactic_featurizer.py +52 -17
- rasa/nlu/featurizers/sparse_featurizer/regex_featurizer.py +5 -3
- rasa/nlu/tokenizers/whitespace_tokenizer.py +3 -14
- rasa/server.py +3 -1
- rasa/shared/constants.py +36 -3
- rasa/shared/core/constants.py +7 -0
- rasa/shared/core/domain.py +26 -0
- rasa/shared/core/flows/flow.py +5 -0
- rasa/shared/core/flows/flows_list.py +5 -1
- rasa/shared/core/flows/flows_yaml_schema.json +10 -0
- rasa/shared/core/flows/utils.py +39 -0
- rasa/shared/core/flows/validation.py +96 -0
- rasa/shared/core/slots.py +5 -0
- rasa/shared/nlu/training_data/features.py +120 -2
- rasa/shared/providers/_configs/azure_openai_client_config.py +5 -3
- rasa/shared/providers/_configs/litellm_router_client_config.py +200 -0
- rasa/shared/providers/_configs/model_group_config.py +167 -0
- rasa/shared/providers/_configs/openai_client_config.py +1 -1
- rasa/shared/providers/_configs/rasa_llm_client_config.py +73 -0
- rasa/shared/providers/_configs/self_hosted_llm_client_config.py +1 -0
- rasa/shared/providers/_configs/utils.py +16 -0
- rasa/shared/providers/embedding/_base_litellm_embedding_client.py +18 -29
- rasa/shared/providers/embedding/azure_openai_embedding_client.py +54 -21
- rasa/shared/providers/embedding/litellm_router_embedding_client.py +135 -0
- rasa/shared/providers/llm/_base_litellm_client.py +37 -31
- rasa/shared/providers/llm/azure_openai_llm_client.py +50 -29
- rasa/shared/providers/llm/litellm_router_llm_client.py +127 -0
- rasa/shared/providers/llm/rasa_llm_client.py +112 -0
- rasa/shared/providers/llm/self_hosted_llm_client.py +1 -1
- rasa/shared/providers/mappings.py +19 -0
- rasa/shared/providers/router/__init__.py +0 -0
- rasa/shared/providers/router/_base_litellm_router_client.py +149 -0
- rasa/shared/providers/router/router_client.py +73 -0
- rasa/shared/utils/common.py +8 -0
- rasa/shared/utils/health_check/__init__.py +0 -0
- rasa/shared/utils/health_check/embeddings_health_check_mixin.py +31 -0
- rasa/shared/utils/health_check/health_check.py +256 -0
- rasa/shared/utils/health_check/llm_health_check_mixin.py +31 -0
- rasa/shared/utils/io.py +28 -6
- rasa/shared/utils/llm.py +353 -46
- rasa/shared/utils/yaml.py +111 -73
- rasa/studio/auth.py +3 -5
- rasa/studio/config.py +13 -4
- rasa/studio/constants.py +1 -0
- rasa/studio/data_handler.py +10 -3
- rasa/studio/upload.py +81 -26
- rasa/telemetry.py +92 -17
- rasa/tracing/config.py +2 -0
- rasa/tracing/instrumentation/attribute_extractors.py +94 -17
- rasa/tracing/instrumentation/instrumentation.py +121 -0
- rasa/utils/common.py +5 -0
- rasa/utils/io.py +7 -81
- rasa/utils/log_utils.py +9 -2
- rasa/utils/sanic_error_handler.py +32 -0
- rasa/utils/tensorflow/feature_array.py +366 -0
- rasa/utils/tensorflow/model_data.py +2 -193
- rasa/validator.py +70 -0
- rasa/version.py +1 -1
- {rasa_pro-3.11.0a4.dev3.dist-info → rasa_pro-3.11.0rc2.dist-info}/METADATA +11 -10
- {rasa_pro-3.11.0a4.dev3.dist-info → rasa_pro-3.11.0rc2.dist-info}/RECORD +183 -163
- rasa/core/channels/inspector/dist/assets/flowDiagram-v2-855bc5b3-587d82d8.js +0 -1
- {rasa_pro-3.11.0a4.dev3.dist-info → rasa_pro-3.11.0rc2.dist-info}/NOTICE +0 -0
- {rasa_pro-3.11.0a4.dev3.dist-info → rasa_pro-3.11.0rc2.dist-info}/WHEEL +0 -0
- {rasa_pro-3.11.0a4.dev3.dist-info → rasa_pro-3.11.0rc2.dist-info}/entry_points.txt +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
from typing import Any, Dict, List, Optional
|
|
2
1
|
import os
|
|
2
|
+
from typing import Any, Dict, List, Optional
|
|
3
|
+
|
|
3
4
|
import structlog
|
|
4
5
|
|
|
5
6
|
from rasa.shared.constants import (
|
|
@@ -42,6 +43,7 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
42
43
|
If not provided, it will be set via environment variable.
|
|
43
44
|
kwargs (Optional[Dict[str, Any]]): Optional configuration parameters specific
|
|
44
45
|
to the embedding model deployment.
|
|
46
|
+
|
|
45
47
|
Raises:
|
|
46
48
|
ProviderClientValidationError: If validation of the client setup fails.
|
|
47
49
|
DeprecationWarning: If deprecated environment variables are used for
|
|
@@ -60,6 +62,7 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
60
62
|
super().__init__() # type: ignore
|
|
61
63
|
self._deployment = deployment
|
|
62
64
|
self._model = model
|
|
65
|
+
self._extra_parameters = kwargs or {}
|
|
63
66
|
|
|
64
67
|
# Set api_base with the following priority:
|
|
65
68
|
# parameter -> Azure Env Var -> (deprecated) OpenAI Env Var
|
|
@@ -81,17 +84,55 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
81
84
|
# Litellm does not support use of OPENAI_API_KEY, so we need to map it
|
|
82
85
|
# because of backward compatibility. However, we're first looking at
|
|
83
86
|
# AZURE_API_KEY.
|
|
84
|
-
self.
|
|
85
|
-
OPENAI_API_KEY_ENV_VAR
|
|
86
|
-
)
|
|
87
|
+
self._api_key_env_var = self._resolve_api_key_env_var()
|
|
87
88
|
|
|
88
|
-
self._extra_parameters = kwargs or {}
|
|
89
89
|
self.validate_client_setup()
|
|
90
90
|
|
|
91
|
+
def _resolve_api_key_env_var(self) -> str:
|
|
92
|
+
"""Resolves the environment variable to use for the API key.
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
str: The env variable in dollar syntax format to use for the API key.
|
|
96
|
+
"""
|
|
97
|
+
if API_KEY in self._extra_parameters:
|
|
98
|
+
# API key is set to an env var in the config itself
|
|
99
|
+
# in case the model is defined in the endpoints.yml
|
|
100
|
+
return self._extra_parameters[API_KEY]
|
|
101
|
+
|
|
102
|
+
if os.getenv(AZURE_API_KEY_ENV_VAR) is not None:
|
|
103
|
+
return "${AZURE_API_KEY}"
|
|
104
|
+
|
|
105
|
+
if os.getenv(OPENAI_API_KEY_ENV_VAR) is not None:
|
|
106
|
+
# API key can be set through OPENAI_API_KEY too,
|
|
107
|
+
# because of the backward compatibility
|
|
108
|
+
raise_deprecation_warning(
|
|
109
|
+
message=(
|
|
110
|
+
f"Usage of '{OPENAI_API_KEY_ENV_VAR}' environment variable "
|
|
111
|
+
"for setting the API key of "
|
|
112
|
+
"Azure OpenAI client is deprecated and will "
|
|
113
|
+
"be removed in 4.0.0. Please "
|
|
114
|
+
f"use '{AZURE_API_KEY_ENV_VAR}' instead."
|
|
115
|
+
)
|
|
116
|
+
)
|
|
117
|
+
return "${OPENAI_API_KEY}"
|
|
118
|
+
|
|
119
|
+
structlogger.error(
|
|
120
|
+
"azure_openai_embedding_client.api_key_not_set",
|
|
121
|
+
event_info=(
|
|
122
|
+
"API key not set, it is required for API calls. "
|
|
123
|
+
f"Set it either via the environment variable "
|
|
124
|
+
f"'{AZURE_API_KEY_ENV_VAR}' or directly"
|
|
125
|
+
f"via the config key '{API_KEY}'."
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
raise ProviderClientValidationError(
|
|
129
|
+
f"Missing required environment variable/config key '{API_KEY}' for "
|
|
130
|
+
f"API calls."
|
|
131
|
+
)
|
|
132
|
+
|
|
91
133
|
@classmethod
|
|
92
134
|
def from_config(cls, config: Dict[str, Any]) -> "AzureOpenAIEmbeddingClient":
|
|
93
|
-
"""
|
|
94
|
-
Initializes the client from given configuration.
|
|
135
|
+
"""Initializes the client from given configuration.
|
|
95
136
|
|
|
96
137
|
Args:
|
|
97
138
|
config (Dict[str, Any]): Configuration.
|
|
@@ -142,8 +183,7 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
142
183
|
|
|
143
184
|
@property
|
|
144
185
|
def model(self) -> Optional[str]:
|
|
145
|
-
"""
|
|
146
|
-
Returns the name of the model deployed on Azure. If model name is not
|
|
186
|
+
"""Returns the name of the model deployed on Azure. If model name is not
|
|
147
187
|
provided, returns "N/A".
|
|
148
188
|
"""
|
|
149
189
|
return self._model
|
|
@@ -170,8 +210,7 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
170
210
|
|
|
171
211
|
@property
|
|
172
212
|
def _litellm_extra_parameters(self) -> Dict[str, Any]:
|
|
173
|
-
"""
|
|
174
|
-
Returns the model parameters for the azure openai embedding client.
|
|
213
|
+
"""Returns the model parameters for the azure openai embedding client.
|
|
175
214
|
|
|
176
215
|
Returns:
|
|
177
216
|
Dictionary containing the model parameters.
|
|
@@ -186,7 +225,7 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
186
225
|
"api_base": self.api_base,
|
|
187
226
|
"api_type": self.api_type,
|
|
188
227
|
"api_version": self.api_version,
|
|
189
|
-
"api_key": self.
|
|
228
|
+
"api_key": self._api_key_env_var,
|
|
190
229
|
}
|
|
191
230
|
|
|
192
231
|
@property
|
|
@@ -197,8 +236,9 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
197
236
|
return self.deployment
|
|
198
237
|
|
|
199
238
|
def validate_client_setup(self) -> None:
|
|
200
|
-
"""Perform client validation.
|
|
201
|
-
|
|
239
|
+
"""Perform client validation.
|
|
240
|
+
|
|
241
|
+
By default, only environment variables are validated.
|
|
202
242
|
|
|
203
243
|
Raises:
|
|
204
244
|
ProviderClientValidationError if validation fails.
|
|
@@ -214,13 +254,6 @@ class AzureOpenAIEmbeddingClient(_BaseLiteLLMEmbeddingClient):
|
|
|
214
254
|
"current_value": self.api_base,
|
|
215
255
|
"new_env_key": AZURE_API_BASE_ENV_VAR,
|
|
216
256
|
},
|
|
217
|
-
{
|
|
218
|
-
"param_name": "API key",
|
|
219
|
-
"config_key": API_KEY,
|
|
220
|
-
"deprecated_env_key": OPENAI_API_KEY_ENV_VAR,
|
|
221
|
-
"current_value": self._api_key,
|
|
222
|
-
"new_env_key": AZURE_API_KEY_ENV_VAR,
|
|
223
|
-
},
|
|
224
257
|
{
|
|
225
258
|
"param_name": "API version",
|
|
226
259
|
"config_key": API_VERSION_CONFIG_KEY,
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
from typing import Any, Dict, List
|
|
2
|
+
import logging
|
|
3
|
+
import structlog
|
|
4
|
+
|
|
5
|
+
from rasa.shared.exceptions import ProviderClientAPIException
|
|
6
|
+
from rasa.shared.providers._configs.litellm_router_client_config import (
|
|
7
|
+
LiteLLMRouterClientConfig,
|
|
8
|
+
)
|
|
9
|
+
from rasa.shared.providers.embedding._base_litellm_embedding_client import (
|
|
10
|
+
_BaseLiteLLMEmbeddingClient,
|
|
11
|
+
)
|
|
12
|
+
from rasa.shared.providers.embedding.embedding_response import EmbeddingResponse
|
|
13
|
+
from rasa.shared.providers.router._base_litellm_router_client import (
|
|
14
|
+
_BaseLiteLLMRouterClient,
|
|
15
|
+
)
|
|
16
|
+
from rasa.shared.utils.io import suppress_logs
|
|
17
|
+
|
|
18
|
+
structlogger = structlog.get_logger()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class LiteLLMRouterEmbeddingClient(
|
|
22
|
+
_BaseLiteLLMRouterClient, _BaseLiteLLMEmbeddingClient
|
|
23
|
+
):
|
|
24
|
+
"""A client for interfacing with LiteLLM Router Embedding endpoints.
|
|
25
|
+
|
|
26
|
+
Parameters:
|
|
27
|
+
model_group_id (str): The model group ID.
|
|
28
|
+
model_configurations (List[Dict[str, Any]]): The list of model configurations.
|
|
29
|
+
router_settings (Dict[str, Any]): The router settings.
|
|
30
|
+
kwargs (Optional[Dict[str, Any]]): Additional configuration parameters.
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
ProviderClientValidationError: If validation of the client setup fails.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
model_group_id: str,
|
|
39
|
+
model_configurations: List[Dict[str, Any]],
|
|
40
|
+
router_settings: Dict[str, Any],
|
|
41
|
+
**kwargs: Any,
|
|
42
|
+
):
|
|
43
|
+
super().__init__(
|
|
44
|
+
model_group_id, model_configurations, router_settings, **kwargs
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def from_config(cls, config: Dict[str, Any]) -> "LiteLLMRouterEmbeddingClient":
|
|
49
|
+
"""Instantiates a LiteLLM Router Embedding client from a configuration dict.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
config: (Dict[str, Any]) The configuration dictionary.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
LiteLLMRouterLLMClient: The instantiated LiteLLM Router LLM client.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
ValueError: If the configuration is invalid.
|
|
59
|
+
"""
|
|
60
|
+
try:
|
|
61
|
+
client_config = LiteLLMRouterClientConfig.from_dict(config)
|
|
62
|
+
except (KeyError, ValueError) as e:
|
|
63
|
+
message = "Cannot instantiate a client from the passed configuration."
|
|
64
|
+
structlogger.error(
|
|
65
|
+
"litellm_router_llm_client.from_config.error",
|
|
66
|
+
message=message,
|
|
67
|
+
config=config,
|
|
68
|
+
original_error=e,
|
|
69
|
+
)
|
|
70
|
+
raise
|
|
71
|
+
|
|
72
|
+
return cls(
|
|
73
|
+
model_group_id=client_config.model_group_id,
|
|
74
|
+
model_configurations=client_config.litellm_model_list,
|
|
75
|
+
router_settings=client_config.router,
|
|
76
|
+
**client_config.extra_parameters,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
@suppress_logs(log_level=logging.WARNING)
|
|
80
|
+
def embed(self, documents: List[str]) -> EmbeddingResponse:
|
|
81
|
+
"""
|
|
82
|
+
Embeds a list of documents synchronously.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
documents: List of documents to be embedded.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
List of embedding vectors.
|
|
89
|
+
|
|
90
|
+
Raises:
|
|
91
|
+
ProviderClientAPIException: If API calls raised an error.
|
|
92
|
+
"""
|
|
93
|
+
self.validate_documents(documents)
|
|
94
|
+
try:
|
|
95
|
+
response = self.router_client.embedding(
|
|
96
|
+
input=documents, **self._embedding_fn_args
|
|
97
|
+
)
|
|
98
|
+
return self._format_response(response)
|
|
99
|
+
except Exception as e:
|
|
100
|
+
raise ProviderClientAPIException(
|
|
101
|
+
message="Failed to embed documents", original_exception=e
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
@suppress_logs(log_level=logging.WARNING)
|
|
105
|
+
async def aembed(self, documents: List[str]) -> EmbeddingResponse:
|
|
106
|
+
"""
|
|
107
|
+
Embeds a list of documents asynchronously.
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
documents: List of documents to be embedded.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
List of embedding vectors.
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
ProviderClientAPIException: If API calls raised an error.
|
|
117
|
+
"""
|
|
118
|
+
self.validate_documents(documents)
|
|
119
|
+
try:
|
|
120
|
+
response = await self.router_client.aembedding(
|
|
121
|
+
input=documents, **self._embedding_fn_args
|
|
122
|
+
)
|
|
123
|
+
return self._format_response(response)
|
|
124
|
+
except Exception as e:
|
|
125
|
+
raise ProviderClientAPIException(
|
|
126
|
+
message="Failed to embed documents", original_exception=e
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
@property
|
|
130
|
+
def _embedding_fn_args(self) -> Dict[str, Any]:
|
|
131
|
+
"""Returns the arguments to be passed to the embedding function."""
|
|
132
|
+
return {
|
|
133
|
+
**self._litellm_extra_parameters,
|
|
134
|
+
"model": self._model_group_id,
|
|
135
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
from abc import abstractmethod
|
|
2
3
|
from typing import Dict, List, Any, Union
|
|
3
4
|
|
|
4
|
-
import logging
|
|
5
5
|
import structlog
|
|
6
6
|
from litellm import (
|
|
7
7
|
completion,
|
|
@@ -9,6 +9,7 @@ from litellm import (
|
|
|
9
9
|
validate_environment,
|
|
10
10
|
)
|
|
11
11
|
|
|
12
|
+
from rasa.shared.constants import API_BASE_CONFIG_KEY, API_KEY
|
|
12
13
|
from rasa.shared.exceptions import (
|
|
13
14
|
ProviderClientAPIException,
|
|
14
15
|
ProviderClientValidationError,
|
|
@@ -18,7 +19,7 @@ from rasa.shared.providers._ssl_verification_utils import (
|
|
|
18
19
|
ensure_ssl_certificates_for_litellm_openai_based_clients,
|
|
19
20
|
)
|
|
20
21
|
from rasa.shared.providers.llm.llm_response import LLMResponse, LLMUsage
|
|
21
|
-
from rasa.shared.utils.io import suppress_logs
|
|
22
|
+
from rasa.shared.utils.io import suppress_logs, resolve_environment_variables
|
|
22
23
|
|
|
23
24
|
structlogger = structlog.get_logger()
|
|
24
25
|
|
|
@@ -29,8 +30,7 @@ logging.getLogger("LiteLLM").setLevel(logging.WARNING)
|
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
class _BaseLiteLLMClient:
|
|
32
|
-
"""
|
|
33
|
-
An abstract base class for LiteLLM clients.
|
|
33
|
+
"""An abstract base class for LiteLLM clients.
|
|
34
34
|
|
|
35
35
|
This class defines the interface and common functionality for all clients
|
|
36
36
|
based on LiteLLM.
|
|
@@ -99,11 +99,14 @@ class _BaseLiteLLMClient:
|
|
|
99
99
|
ProviderClientValidationError if validation fails.
|
|
100
100
|
"""
|
|
101
101
|
self._validate_environment_variables()
|
|
102
|
-
self._validate_api_key_not_in_config()
|
|
103
102
|
|
|
104
103
|
def _validate_environment_variables(self) -> None:
|
|
105
104
|
"""Validate that the required environment variables are set."""
|
|
106
|
-
validation_info = validate_environment(
|
|
105
|
+
validation_info = validate_environment(
|
|
106
|
+
self._litellm_model_name,
|
|
107
|
+
api_key=self._litellm_extra_parameters.get(API_KEY),
|
|
108
|
+
api_base=self._litellm_extra_parameters.get(API_BASE_CONFIG_KEY),
|
|
109
|
+
)
|
|
107
110
|
if missing_environment_variables := validation_info.get(
|
|
108
111
|
_VALIDATE_ENVIRONMENT_MISSING_KEYS_KEY
|
|
109
112
|
):
|
|
@@ -118,61 +121,65 @@ class _BaseLiteLLMClient:
|
|
|
118
121
|
)
|
|
119
122
|
raise ProviderClientValidationError(event_info)
|
|
120
123
|
|
|
121
|
-
def _validate_api_key_not_in_config(self) -> None:
|
|
122
|
-
if "api_key" in self._litellm_extra_parameters:
|
|
123
|
-
event_info = (
|
|
124
|
-
"API Key is set through `api_key` extra parameter."
|
|
125
|
-
"Set API keys through environment variables."
|
|
126
|
-
)
|
|
127
|
-
structlogger.error(
|
|
128
|
-
"base_litellm_client.validate_api_key_not_in_config",
|
|
129
|
-
event_info=event_info,
|
|
130
|
-
)
|
|
131
|
-
raise ProviderClientValidationError(event_info)
|
|
132
|
-
|
|
133
124
|
@suppress_logs(log_level=logging.WARNING)
|
|
134
125
|
def completion(self, messages: Union[List[str], str]) -> LLMResponse:
|
|
135
|
-
"""
|
|
136
|
-
Synchronously generate completions for given list of messages.
|
|
126
|
+
"""Synchronously generate completions for given list of messages.
|
|
137
127
|
|
|
138
128
|
Args:
|
|
139
129
|
messages: List of messages or a single message to generate the
|
|
140
130
|
completion for.
|
|
131
|
+
|
|
141
132
|
Returns:
|
|
142
133
|
List of message completions.
|
|
134
|
+
|
|
143
135
|
Raises:
|
|
144
136
|
ProviderClientAPIException: If the API request fails.
|
|
145
137
|
"""
|
|
146
138
|
try:
|
|
147
139
|
formatted_messages = self._format_messages(messages)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
)
|
|
140
|
+
arguments = resolve_environment_variables(self._completion_fn_args)
|
|
141
|
+
response = completion(messages=formatted_messages, **arguments)
|
|
151
142
|
return self._format_response(response)
|
|
152
143
|
except Exception as e:
|
|
153
144
|
raise ProviderClientAPIException(e)
|
|
154
145
|
|
|
155
146
|
@suppress_logs(log_level=logging.WARNING)
|
|
156
147
|
async def acompletion(self, messages: Union[List[str], str]) -> LLMResponse:
|
|
157
|
-
"""
|
|
158
|
-
Asynchronously generate completions for given list of messages.
|
|
148
|
+
"""Asynchronously generate completions for given list of messages.
|
|
159
149
|
|
|
160
150
|
Args:
|
|
161
151
|
messages: List of messages or a single message to generate the
|
|
162
152
|
completion for.
|
|
153
|
+
|
|
163
154
|
Returns:
|
|
164
155
|
List of message completions.
|
|
156
|
+
|
|
165
157
|
Raises:
|
|
166
158
|
ProviderClientAPIException: If the API request fails.
|
|
167
159
|
"""
|
|
168
160
|
try:
|
|
169
161
|
formatted_messages = self._format_messages(messages)
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
)
|
|
162
|
+
arguments = resolve_environment_variables(self._completion_fn_args)
|
|
163
|
+
response = await acompletion(messages=formatted_messages, **arguments)
|
|
173
164
|
return self._format_response(response)
|
|
174
165
|
except Exception as e:
|
|
175
|
-
|
|
166
|
+
message = ""
|
|
167
|
+
from rasa.shared.providers.llm.self_hosted_llm_client import (
|
|
168
|
+
SelfHostedLLMClient,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
if isinstance(self, SelfHostedLLMClient):
|
|
172
|
+
message = (
|
|
173
|
+
"If you are using 'provider=self-hosted' to call a hosted vllm "
|
|
174
|
+
"server make sure your config is correctly setup. You should have "
|
|
175
|
+
"the following mandatory keys in your config: "
|
|
176
|
+
"provider=self-hosted; "
|
|
177
|
+
"model='<your-vllm-model-name>'; "
|
|
178
|
+
"api_base='your-hosted-vllm-serv'."
|
|
179
|
+
"In case you are getting OpenAI connection errors, such as missing "
|
|
180
|
+
"API key, your configuration is incorrect."
|
|
181
|
+
)
|
|
182
|
+
raise ProviderClientAPIException(e, message)
|
|
176
183
|
|
|
177
184
|
def _format_messages(self, messages: Union[List[str], str]) -> List[Dict[str, str]]:
|
|
178
185
|
"""Formats messages (or a single message) to OpenAI format."""
|
|
@@ -216,8 +223,7 @@ class _BaseLiteLLMClient:
|
|
|
216
223
|
|
|
217
224
|
@staticmethod
|
|
218
225
|
def _ensure_certificates() -> None:
|
|
219
|
-
"""
|
|
220
|
-
Configures SSL certificates for LiteLLM. This method is invoked during
|
|
226
|
+
"""Configures SSL certificates for LiteLLM. This method is invoked during
|
|
221
227
|
client initialization.
|
|
222
228
|
|
|
223
229
|
LiteLLM may utilize `openai` clients or other providers that require
|
|
@@ -17,6 +17,7 @@ from rasa.shared.constants import (
|
|
|
17
17
|
OPENAI_API_KEY_ENV_VAR,
|
|
18
18
|
AZURE_API_TYPE_ENV_VAR,
|
|
19
19
|
AZURE_OPENAI_PROVIDER,
|
|
20
|
+
API_KEY,
|
|
20
21
|
)
|
|
21
22
|
from rasa.shared.exceptions import ProviderClientValidationError
|
|
22
23
|
from rasa.shared.providers._configs.azure_openai_client_config import (
|
|
@@ -29,8 +30,7 @@ structlogger = structlog.get_logger()
|
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
32
|
-
"""
|
|
33
|
-
A client for interfacing with Azure's OpenAI LLM deployments.
|
|
33
|
+
"""A client for interfacing with Azure's OpenAI LLM deployments.
|
|
34
34
|
|
|
35
35
|
Parameters:
|
|
36
36
|
deployment (str): The deployment name.
|
|
@@ -80,11 +80,7 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
80
80
|
or os.getenv(OPENAI_API_VERSION_ENV_VAR)
|
|
81
81
|
)
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
# because of the backward compatibility
|
|
85
|
-
self._api_key = os.getenv(AZURE_API_KEY_ENV_VAR) or os.getenv(
|
|
86
|
-
OPENAI_API_KEY_ENV_VAR
|
|
87
|
-
)
|
|
83
|
+
self._api_key_env_var = self._resolve_api_key_env_var()
|
|
88
84
|
|
|
89
85
|
# Not used by LiteLLM, here for backward compatibility
|
|
90
86
|
self._api_type = (
|
|
@@ -117,11 +113,6 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
117
113
|
"env_var": AZURE_API_VERSION_ENV_VAR,
|
|
118
114
|
"deprecated_var": OPENAI_API_VERSION_ENV_VAR,
|
|
119
115
|
},
|
|
120
|
-
"API Key": {
|
|
121
|
-
"current_value": self._api_key,
|
|
122
|
-
"env_var": AZURE_API_KEY_ENV_VAR,
|
|
123
|
-
"deprecated_var": OPENAI_API_KEY_ENV_VAR,
|
|
124
|
-
},
|
|
125
116
|
}
|
|
126
117
|
|
|
127
118
|
deprecation_warning_message = (
|
|
@@ -154,10 +145,51 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
154
145
|
)
|
|
155
146
|
raise_deprecation_warning(message=message)
|
|
156
147
|
|
|
148
|
+
def _resolve_api_key_env_var(self) -> str:
|
|
149
|
+
"""Resolves the environment variable to use for the API key.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
str: The env variable in dollar syntax format to use for the API key.
|
|
153
|
+
"""
|
|
154
|
+
if API_KEY in self._extra_parameters:
|
|
155
|
+
# API key is set to an env var in the config itself
|
|
156
|
+
# in case the model is defined in the endpoints.yml
|
|
157
|
+
return self._extra_parameters[API_KEY]
|
|
158
|
+
|
|
159
|
+
if os.getenv(AZURE_API_KEY_ENV_VAR) is not None:
|
|
160
|
+
return "${AZURE_API_KEY}"
|
|
161
|
+
|
|
162
|
+
if os.getenv(OPENAI_API_KEY_ENV_VAR) is not None:
|
|
163
|
+
# API key can be set through OPENAI_API_KEY too,
|
|
164
|
+
# because of the backward compatibility
|
|
165
|
+
raise_deprecation_warning(
|
|
166
|
+
message=(
|
|
167
|
+
f"Usage of '{OPENAI_API_KEY_ENV_VAR}' environment variable "
|
|
168
|
+
"for setting the API key for Azure OpenAI "
|
|
169
|
+
"client is deprecated and will be removed "
|
|
170
|
+
f"in 4.0.0. Please use '{AZURE_API_KEY_ENV_VAR}' "
|
|
171
|
+
"environment variable."
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
return "${OPENAI_API_KEY}"
|
|
175
|
+
|
|
176
|
+
structlogger.error(
|
|
177
|
+
"azure_openai_llm_client.api_key_not_set",
|
|
178
|
+
event_info=(
|
|
179
|
+
"API key not set, it is required for API calls. "
|
|
180
|
+
f"Set it either via the environment variable"
|
|
181
|
+
f"'{AZURE_API_KEY_ENV_VAR}' or directly"
|
|
182
|
+
f"via the config key '{API_KEY}'."
|
|
183
|
+
),
|
|
184
|
+
)
|
|
185
|
+
raise ProviderClientValidationError(
|
|
186
|
+
f"Missing required environment variable/config key '{API_KEY}' for "
|
|
187
|
+
f"API calls."
|
|
188
|
+
)
|
|
189
|
+
|
|
157
190
|
@classmethod
|
|
158
191
|
def from_config(cls, config: Dict[str, Any]) -> "AzureOpenAILLMClient":
|
|
159
|
-
"""
|
|
160
|
-
Initializes the client from given configuration.
|
|
192
|
+
"""Initializes the client from given configuration.
|
|
161
193
|
|
|
162
194
|
Args:
|
|
163
195
|
config (Dict[str, Any]): Configuration.
|
|
@@ -212,23 +244,17 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
212
244
|
|
|
213
245
|
@property
|
|
214
246
|
def model(self) -> Optional[str]:
|
|
215
|
-
"""
|
|
216
|
-
Returns the name of the model deployed on Azure.
|
|
217
|
-
"""
|
|
247
|
+
"""Returns the name of the model deployed on Azure."""
|
|
218
248
|
return self._model
|
|
219
249
|
|
|
220
250
|
@property
|
|
221
251
|
def api_base(self) -> Optional[str]:
|
|
222
|
-
"""
|
|
223
|
-
Returns the API base URL for the Azure OpenAI llm client.
|
|
224
|
-
"""
|
|
252
|
+
"""Returns the API base URL for the Azure OpenAI llm client."""
|
|
225
253
|
return self._api_base
|
|
226
254
|
|
|
227
255
|
@property
|
|
228
256
|
def api_version(self) -> Optional[str]:
|
|
229
|
-
"""
|
|
230
|
-
Returns the API version for the Azure OpenAI llm client.
|
|
231
|
-
"""
|
|
257
|
+
"""Returns the API version for the Azure OpenAI llm client."""
|
|
232
258
|
return self._api_version
|
|
233
259
|
|
|
234
260
|
@property
|
|
@@ -261,7 +287,7 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
261
287
|
{
|
|
262
288
|
"api_base": self.api_base,
|
|
263
289
|
"api_version": self.api_version,
|
|
264
|
-
"api_key": self.
|
|
290
|
+
"api_key": self._api_key_env_var,
|
|
265
291
|
}
|
|
266
292
|
)
|
|
267
293
|
return fn_args
|
|
@@ -305,11 +331,6 @@ class AzureOpenAILLMClient(_BaseLiteLLMClient):
|
|
|
305
331
|
"env_var": None,
|
|
306
332
|
"config_key": DEPLOYMENT_CONFIG_KEY,
|
|
307
333
|
},
|
|
308
|
-
"API Key": {
|
|
309
|
-
"current_value": self._api_key,
|
|
310
|
-
"env_var": AZURE_API_KEY_ENV_VAR,
|
|
311
|
-
"config_key": None,
|
|
312
|
-
},
|
|
313
334
|
}
|
|
314
335
|
|
|
315
336
|
missing_settings = [
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
from typing import Any, Dict, List, Union
|
|
2
|
+
import logging
|
|
3
|
+
import structlog
|
|
4
|
+
|
|
5
|
+
from rasa.shared.exceptions import ProviderClientAPIException
|
|
6
|
+
from rasa.shared.providers._configs.litellm_router_client_config import (
|
|
7
|
+
LiteLLMRouterClientConfig,
|
|
8
|
+
)
|
|
9
|
+
from rasa.shared.providers.llm._base_litellm_client import _BaseLiteLLMClient
|
|
10
|
+
from rasa.shared.providers.llm.llm_response import LLMResponse
|
|
11
|
+
from rasa.shared.providers.router._base_litellm_router_client import (
|
|
12
|
+
_BaseLiteLLMRouterClient,
|
|
13
|
+
)
|
|
14
|
+
from rasa.shared.utils.io import suppress_logs
|
|
15
|
+
|
|
16
|
+
structlogger = structlog.get_logger()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class LiteLLMRouterLLMClient(_BaseLiteLLMRouterClient, _BaseLiteLLMClient):
|
|
20
|
+
"""A client for interfacing with LiteLLM Router LLM endpoints.
|
|
21
|
+
|
|
22
|
+
Parameters:
|
|
23
|
+
model_group_id (str): The model group ID.
|
|
24
|
+
model_configurations (List[Dict[str, Any]]): The list of model configurations.
|
|
25
|
+
router_settings (Dict[str, Any]): The router settings.
|
|
26
|
+
kwargs (Optional[Dict[str, Any]]): Additional configuration parameters.
|
|
27
|
+
|
|
28
|
+
Raises:
|
|
29
|
+
ProviderClientValidationError: If validation of the client setup fails.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
model_group_id: str,
|
|
35
|
+
model_configurations: List[Dict[str, Any]],
|
|
36
|
+
router_settings: Dict[str, Any],
|
|
37
|
+
**kwargs: Any,
|
|
38
|
+
):
|
|
39
|
+
super().__init__(
|
|
40
|
+
model_group_id, model_configurations, router_settings, **kwargs
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_config(cls, config: Dict[str, Any]) -> "LiteLLMRouterLLMClient":
|
|
45
|
+
"""Instantiates a LiteLLM Router LLM client from a configuration dict.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
config: (Dict[str, Any]) The configuration dictionary.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
LiteLLMRouterLLMClient: The instantiated LiteLLM Router LLM client.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
ValueError: If the configuration is invalid.
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
client_config = LiteLLMRouterClientConfig.from_dict(config)
|
|
58
|
+
except (KeyError, ValueError) as e:
|
|
59
|
+
message = "Cannot instantiate a client from the passed configuration."
|
|
60
|
+
structlogger.error(
|
|
61
|
+
"litellm_router_llm_client.from_config.error",
|
|
62
|
+
message=message,
|
|
63
|
+
config=config,
|
|
64
|
+
original_error=e,
|
|
65
|
+
)
|
|
66
|
+
raise
|
|
67
|
+
|
|
68
|
+
return cls(
|
|
69
|
+
model_group_id=client_config.model_group_id,
|
|
70
|
+
model_configurations=client_config.litellm_model_list,
|
|
71
|
+
router_settings=client_config.router,
|
|
72
|
+
**client_config.extra_parameters,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
@suppress_logs(log_level=logging.WARNING)
|
|
76
|
+
def completion(self, messages: Union[List[str], str]) -> LLMResponse:
|
|
77
|
+
"""
|
|
78
|
+
Synchronously generate completions for given list of messages.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
messages: List of messages or a single message to generate the
|
|
82
|
+
completion for.
|
|
83
|
+
Returns:
|
|
84
|
+
List of message completions.
|
|
85
|
+
Raises:
|
|
86
|
+
ProviderClientAPIException: If the API request fails.
|
|
87
|
+
"""
|
|
88
|
+
try:
|
|
89
|
+
formatted_messages = self._format_messages(messages)
|
|
90
|
+
response = self.router_client.completion(
|
|
91
|
+
messages=formatted_messages, **self._completion_fn_args
|
|
92
|
+
)
|
|
93
|
+
return self._format_response(response)
|
|
94
|
+
except Exception as e:
|
|
95
|
+
raise ProviderClientAPIException(e)
|
|
96
|
+
|
|
97
|
+
@suppress_logs(log_level=logging.WARNING)
|
|
98
|
+
async def acompletion(self, messages: Union[List[str], str]) -> LLMResponse:
|
|
99
|
+
"""
|
|
100
|
+
Asynchronously generate completions for given list of messages.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
messages: List of messages or a single message to generate the
|
|
104
|
+
completion for.
|
|
105
|
+
Returns:
|
|
106
|
+
List of message completions.
|
|
107
|
+
Raises:
|
|
108
|
+
ProviderClientAPIException: If the API request fails.
|
|
109
|
+
"""
|
|
110
|
+
try:
|
|
111
|
+
formatted_messages = self._format_messages(messages)
|
|
112
|
+
response = await self.router_client.acompletion(
|
|
113
|
+
messages=formatted_messages, **self._completion_fn_args
|
|
114
|
+
)
|
|
115
|
+
return self._format_response(response)
|
|
116
|
+
except Exception as e:
|
|
117
|
+
raise ProviderClientAPIException(e)
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def _completion_fn_args(self) -> Dict[str, Any]:
|
|
121
|
+
"""Returns the completion arguments for invoking a call through
|
|
122
|
+
LiteLLM's completion functions.
|
|
123
|
+
"""
|
|
124
|
+
return {
|
|
125
|
+
**self._litellm_extra_parameters,
|
|
126
|
+
"model": self.model_group_id,
|
|
127
|
+
}
|