Skip to content

Commit

Permalink
Make anthropic_api_key a secret str (#10724)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshuasundance-swca authored Sep 23, 2023
1 parent 1b65779 commit d67b120
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions libs/langchain/langchain/llms/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
CallbackManagerForLLMRun,
)
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.output import GenerationChunk
from langchain.schema.prompt import PromptValue
Expand Down Expand Up @@ -45,7 +45,7 @@ class _AnthropicCommon(BaseLanguageModel):

anthropic_api_url: Optional[str] = None

anthropic_api_key: Optional[str] = None
anthropic_api_key: Optional[SecretStr] = None

HUMAN_PROMPT: Optional[str] = None
AI_PROMPT: Optional[str] = None
Expand All @@ -64,8 +64,8 @@ def build_extra(cls, values: Dict) -> Dict:
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
values["anthropic_api_key"] = get_from_dict_or_env(
values, "anthropic_api_key", "ANTHROPIC_API_KEY"
values["anthropic_api_key"] = SecretStr(
get_from_dict_or_env(values, "anthropic_api_key", "ANTHROPIC_API_KEY")
)
# Get custom api url from environment.
values["anthropic_api_url"] = get_from_dict_or_env(
Expand All @@ -81,12 +81,12 @@ def validate_environment(cls, values: Dict) -> Dict:
check_package_version("anthropic", gte_version="0.3")
values["client"] = anthropic.Anthropic(
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"],
)
values["async_client"] = anthropic.AsyncAnthropic(
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"],
)
values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT
Expand Down

0 comments on commit d67b120

Please sign in to comment.