mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
feat(credential_accessor.py): support loading in credentials from credential_list
Resolves https://github.com/BerriAI/litellm/issues/9114
This commit is contained in:
parent
4bd4bb16fd
commit
fdd5ba3084
5 changed files with 46 additions and 13 deletions
15
litellm/litellm_core_utils/credential_accessor.py
Normal file
15
litellm/litellm_core_utils/credential_accessor.py
Normal file
|
@ -0,0 +1,15 @@
|
|||
"""Utils for accessing credentials."""
|
||||
|
||||
import litellm
|
||||
|
||||
|
||||
class CredentialAccessor:
|
||||
@staticmethod
|
||||
def get_credential_values(credential_name: str) -> dict:
|
||||
"""Safe accessor for credentials."""
|
||||
if not litellm.credential_list:
|
||||
return {}
|
||||
for credential in litellm.credential_list:
|
||||
if credential.credential_name == credential_name:
|
||||
return credential.credential_values.copy()
|
||||
return {}
|
|
@ -2,7 +2,7 @@ model_list:
|
|||
- model_name: gpt-4o
|
||||
litellm_params:
|
||||
model: azure/gpt-4o
|
||||
credential_name: default_azure_credential
|
||||
litellm_credential_name: default_azure_credential
|
||||
|
||||
credential_list:
|
||||
- credential_name: default_azure_credential
|
||||
|
|
|
@ -5370,18 +5370,18 @@ class Router:
|
|||
client = self.cache.get_cache(
|
||||
key=cache_key, local_only=True, parent_otel_span=parent_otel_span
|
||||
)
|
||||
if client is None:
|
||||
"""
|
||||
Re-initialize the client
|
||||
"""
|
||||
InitalizeOpenAISDKClient.set_client(
|
||||
litellm_router_instance=self, model=deployment
|
||||
)
|
||||
client = self.cache.get_cache(
|
||||
key=cache_key,
|
||||
local_only=True,
|
||||
parent_otel_span=parent_otel_span,
|
||||
)
|
||||
# if client is None:
|
||||
# """
|
||||
# Re-initialize the client
|
||||
# """
|
||||
# InitalizeOpenAISDKClient.set_client(
|
||||
# litellm_router_instance=self, model=deployment
|
||||
# )
|
||||
# client = self.cache.get_cache(
|
||||
# key=cache_key,
|
||||
# local_only=True,
|
||||
# parent_otel_span=parent_otel_span,
|
||||
# )
|
||||
return client
|
||||
else:
|
||||
if kwargs.get("stream") is True:
|
||||
|
|
|
@ -1815,6 +1815,7 @@ all_litellm_params = [
|
|||
"budget_duration",
|
||||
"use_in_pass_through",
|
||||
"merge_reasoning_content_in_choices",
|
||||
"litellm_credential_name",
|
||||
] + list(StandardCallbackDynamicParams.__annotations__.keys())
|
||||
|
||||
|
||||
|
|
|
@ -66,6 +66,7 @@ from litellm.litellm_core_utils.core_helpers import (
|
|||
map_finish_reason,
|
||||
process_response_headers,
|
||||
)
|
||||
from litellm.litellm_core_utils.credential_accessor import CredentialAccessor
|
||||
from litellm.litellm_core_utils.default_encoding import encoding
|
||||
from litellm.litellm_core_utils.exception_mapping_utils import (
|
||||
_get_response_headers,
|
||||
|
@ -141,6 +142,7 @@ from litellm.types.utils import (
|
|||
ChatCompletionMessageToolCall,
|
||||
Choices,
|
||||
CostPerToken,
|
||||
CredentialItem,
|
||||
CustomHuggingfaceTokenizer,
|
||||
Delta,
|
||||
Embedding,
|
||||
|
@ -455,6 +457,18 @@ def get_applied_guardrails(kwargs: Dict[str, Any]) -> List[str]:
|
|||
return applied_guardrails
|
||||
|
||||
|
||||
def load_credentials_from_list(kwargs: dict):
|
||||
"""
|
||||
Updates kwargs with the credentials if credential_name in kwarg
|
||||
"""
|
||||
credential_name = kwargs.get("litellm_credential_name")
|
||||
if credential_name and litellm.credential_list:
|
||||
credential_accessor = CredentialAccessor.get_credential_values(credential_name)
|
||||
for key, value in credential_accessor.items():
|
||||
if key not in kwargs:
|
||||
kwargs[key] = value
|
||||
|
||||
|
||||
def get_dynamic_callbacks(
|
||||
dynamic_callbacks: Optional[List[Union[str, Callable, CustomLogger]]]
|
||||
) -> List:
|
||||
|
@ -485,6 +499,9 @@ def function_setup( # noqa: PLR0915
|
|||
## GET APPLIED GUARDRAILS
|
||||
applied_guardrails = get_applied_guardrails(kwargs)
|
||||
|
||||
## LOAD CREDENTIALS
|
||||
load_credentials_from_list(kwargs)
|
||||
|
||||
## LOGGING SETUP
|
||||
function_id: Optional[str] = kwargs["id"] if "id" in kwargs else None
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue