Make anthropic_api_key a secret str (#10724)

This PR makes `ChatAnthropic.anthropic_api_key` a `pydantic.SecretStr`
to avoid inadvertently exposing API keys when the `ChatAnthropic` object
is represented as a str.
This commit is contained in:
Joshua Sundance Bailey 2023-09-22 22:06:20 -04:00 committed by GitHub
parent 1b65779905
commit d67b120a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ from langchain.callbacks.manager import (
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
) )
from langchain.llms.base import LLM from langchain.llms.base import LLM
from langchain.pydantic_v1 import Field, root_validator from langchain.pydantic_v1 import Field, SecretStr, root_validator
from langchain.schema.language_model import BaseLanguageModel from langchain.schema.language_model import BaseLanguageModel
from langchain.schema.output import GenerationChunk from langchain.schema.output import GenerationChunk
from langchain.schema.prompt import PromptValue from langchain.schema.prompt import PromptValue
@ -45,7 +45,7 @@ class _AnthropicCommon(BaseLanguageModel):
anthropic_api_url: Optional[str] = None anthropic_api_url: Optional[str] = None
anthropic_api_key: Optional[str] = None anthropic_api_key: Optional[SecretStr] = None
HUMAN_PROMPT: Optional[str] = None HUMAN_PROMPT: Optional[str] = None
AI_PROMPT: Optional[str] = None AI_PROMPT: Optional[str] = None
@ -64,8 +64,8 @@ class _AnthropicCommon(BaseLanguageModel):
@root_validator() @root_validator()
def validate_environment(cls, values: Dict) -> Dict: def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment.""" """Validate that api key and python package exists in environment."""
values["anthropic_api_key"] = get_from_dict_or_env( values["anthropic_api_key"] = SecretStr(
values, "anthropic_api_key", "ANTHROPIC_API_KEY" get_from_dict_or_env(values, "anthropic_api_key", "ANTHROPIC_API_KEY")
) )
# Get custom api url from environment. # Get custom api url from environment.
values["anthropic_api_url"] = get_from_dict_or_env( values["anthropic_api_url"] = get_from_dict_or_env(
@ -81,12 +81,12 @@ class _AnthropicCommon(BaseLanguageModel):
check_package_version("anthropic", gte_version="0.3") check_package_version("anthropic", gte_version="0.3")
values["client"] = anthropic.Anthropic( values["client"] = anthropic.Anthropic(
base_url=values["anthropic_api_url"], base_url=values["anthropic_api_url"],
api_key=values["anthropic_api_key"], api_key=values["anthropic_api_key"].get_secret_value(),
timeout=values["default_request_timeout"], timeout=values["default_request_timeout"],
) )
values["async_client"] = anthropic.AsyncAnthropic( values["async_client"] = anthropic.AsyncAnthropic(
base_url=values["anthropic_api_url"], base_url=values["anthropic_api_url"],
api_key=values["anthropic_api_key"], api_key=values["anthropic_api_key"].get_secret_value(),
timeout=values["default_request_timeout"], timeout=values["default_request_timeout"],
) )
values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT