Skip to content

The agent engineering platform.

Refactor: use SecretStr for tongyi chat-model (#15102)

chyroc37ad6ec248e1
diff --git a/libs/community/langchain_community/chat_models/tongyi.py b/libs/community/langchain_community/chat_models/tongyi.py
index 5e4f4c1ab9..ab2958d5c7 100644
--- a/libs/community/langchain_community/chat_models/tongyi.py
+++ b/libs/community/langchain_community/chat_models/tongyi.py
@@ -37,8 +37,8 @@ from langchain_core.outputs import (
ChatGenerationChunk,
ChatResult,
)
-from langchain_core.pydantic_v1 import Field, root_validator
-from langchain_core.utils import get_from_dict_or_env
+from langchain_core.pydantic_v1 import Field, SecretStr, root_validator
+from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
from requests.exceptions import HTTPError
from tenacity import (
before_sleep_log,
@@ -153,7 +153,7 @@ class ChatTongyi(BaseChatModel):
top_p: float = 0.8
"""Total probability mass of tokens to consider at each step."""
- dashscope_api_key: Optional[str] = None
+ dashscope_api_key: Optional[SecretStr] = None
"""Dashscope api key provide by Alibaba Cloud."""
streaming: bool = False
@@ -170,8 +170,8 @@ class ChatTongyi(BaseChatModel):
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
- values["dashscope_api_key"] = get_from_dict_or_env(
- values, "dashscope_api_key", "DASHSCOPE_API_KEY"
+ values["dashscope_api_key"] = convert_to_secret_str(
+ get_from_dict_or_env(values, "dashscope_api_key", "DASHSCOPE_API_KEY")
)
try:
import dashscope
@@ -197,7 +197,7 @@ class ChatTongyi(BaseChatModel):
return {
"model": self.model_name,
"top_p": self.top_p,
- "api_key": self.dashscope_api_key,
+ "api_key": self.dashscope_api_key.get_secret_value(),
"result_format": "message",
**self.model_kwargs,
}
diff --git a/libs/community/tests/integration_tests/chat_models/test_tongyi.py b/libs/community/tests/integration_tests/chat_models/test_tongyi.py
index ebb92b24b2..b01f3d00fd 100644
--- a/libs/community/tests/integration_tests/chat_models/test_tongyi.py
+++ b/libs/community/tests/integration_tests/chat_models/test_tongyi.py
@@ -3,11 +3,28 @@
from langchain_core.callbacks import CallbackManager
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.outputs import ChatGeneration, LLMResult
+from langchain_core.pydantic_v1 import SecretStr
+from pytest import CaptureFixture
from langchain_community.chat_models.tongyi import ChatTongyi
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
+def test_api_key_is_string() -> None:
+ llm = ChatTongyi(dashscope_api_key="secret-api-key")
+ assert isinstance(llm.dashscope_api_key, SecretStr)
+
+
+def test_api_key_masked_when_passed_via_constructor(
+ capsys: CaptureFixture,
+) -> None:
+ llm = ChatTongyi(dashscope_api_key="secret-api-key")
+ print(llm.dashscope_api_key, end="")
+ captured = capsys.readouterr()
+
+ assert captured.out == "**********"
+
+
def test_default_call() -> None:
"""Test default model call."""
chat = ChatTongyi()