mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-07 22:11:51 +00:00
Refactor: use SecretStr for tongyi chat-model (#15102)
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user