Skip to content

Commit

Permalink
Set api_key param to litellm client based on providers api keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesev15 committed Nov 1, 2024
1 parent 830cad7 commit cd72d57
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libs/community/langchain_community/chat_models/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -264,13 +266,27 @@ 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."""
set_model_value = self.model
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,
Expand Down

0 comments on commit cd72d57

Please sign in to comment.