From aade9bfde55c068648ab55a1a9f4098327d01be7 Mon Sep 17 00:00:00 2001 From: Han Sol Park Date: Fri, 19 Jul 2024 11:25:38 +0900 Subject: [PATCH] Mask API key for ChatOpenAI based chat_models (#14293) - **Description**: Mask API key for ChatOpenAi based chat_models (openai, azureopenai, anyscale, everlyai). Made changes to all chat_models that are based on ChatOpenAI since all of them assumes that openai_api_key is str rather than SecretStr. - **Issue:**: #12165 - **Dependencies:** N/A - **Tag maintainer:** @eyurtsev - **Twitter handle:** N/A --------- Co-authored-by: Chester Curme --- .../langchain_community/chat_models/everlyai.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/chat_models/everlyai.py b/libs/community/langchain_community/chat_models/everlyai.py index c85d4c9e9a..4122dae280 100644 --- a/libs/community/langchain_community/chat_models/everlyai.py +++ b/libs/community/langchain_community/chat_models/everlyai.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING, Dict, Optional, Set from langchain_core.messages import BaseMessage from langchain_core.pydantic_v1 import Field, root_validator -from langchain_core.utils import get_from_dict_or_env +from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env from langchain_community.adapters.openai import convert_message_to_dict from langchain_community.chat_models.openai import ( @@ -79,10 +79,12 @@ class ChatEverlyAI(ChatOpenAI): @root_validator(pre=True) def validate_environment_override(cls, values: dict) -> dict: """Validate that api key and python package exists in environment.""" - values["openai_api_key"] = get_from_dict_or_env( - values, - "everlyai_api_key", - "EVERLYAI_API_KEY", + values["openai_api_key"] = convert_to_secret_str( + get_from_dict_or_env( + values, + "everlyai_api_key", + "EVERLYAI_API_KEY", + ) ) values["openai_api_base"] = DEFAULT_API_BASE