From cd72d57a8a45687a4760732198dedd45df1f25ae Mon Sep 17 00:00:00 2001 From: James Espichan Vilca <84110446+jamesev15@users.noreply.github.com> Date: Fri, 1 Nov 2024 23:33:46 +0000 Subject: [PATCH] Set api_key param to litellm client based on providers api keys --- .../langchain_community/chat_models/litellm.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libs/community/langchain_community/chat_models/litellm.py b/libs/community/langchain_community/chat_models/litellm.py index d6c9557339857..7e88fd363163a 100644 --- a/libs/community/langchain_community/chat_models/litellm.py +++ b/libs/community/langchain_community/chat_models/litellm.py @@ -225,6 +225,8 @@ class ChatLiteLLM(BaseChatModel): replicate_api_key: Optional[str] = None cohere_api_key: Optional[str] = None openrouter_api_key: Optional[str] = None + together_ai_api_key: Optional[str] = None + huggingface_api_key: Optional[str] = None streaming: bool = False api_base: Optional[str] = None organization: Optional[str] = None @@ -264,6 +266,19 @@ def _default_params(self) -> Dict[str, Any]: **self.model_kwargs, } + def get_api_key(self) -> str: + return ( + self.openai_api_key + or self.azure_api_key + or self.anthropic_api_key + or self.replicate_api_key + or self.openrouter_api_key + or self.cohere_api_key + or self.huggingface_api_key + or self.together_ai_api_key + or "" + ) + @property def _client_params(self) -> Dict[str, Any]: """Get the parameters used for the openai client.""" @@ -271,6 +286,7 @@ def _client_params(self) -> Dict[str, Any]: if self.model_name is not None: set_model_value = self.model_name self.client.api_base = self.api_base + self.client.api_key = self.get_api_key() self.client.organization = self.organization creds: Dict[str, Any] = { "model": set_model_value,