From 12d14f89476ae7bf65211c51cf0ebcaf6488b372 Mon Sep 17 00:00:00 2001 From: David Duong Date: Thu, 6 Jul 2023 22:57:12 +0200 Subject: [PATCH] Fix secrets serialisation for ChatAnthropic (#7300) --- langchain/chat_models/anthropic.py | 4 ++++ langchain/llms/anthropic.py | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/langchain/chat_models/anthropic.py b/langchain/chat_models/anthropic.py index 17e456dc3a..ce770c9494 100644 --- a/langchain/chat_models/anthropic.py +++ b/langchain/chat_models/anthropic.py @@ -34,6 +34,10 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon): model = ChatAnthropic(model="", anthropic_api_key="my-api-key") """ + @property + def lc_secrets(self) -> Dict[str, str]: + return {"anthropic_api_key": "ANTHROPIC_API_KEY"} + @property def _llm_type(self) -> str: """Return type of chat model.""" diff --git a/langchain/llms/anthropic.py b/langchain/llms/anthropic.py index bbddd95cb8..25509b6c41 100644 --- a/langchain/llms/anthropic.py +++ b/langchain/llms/anthropic.py @@ -50,11 +50,11 @@ class _AnthropicCommon(BaseModel): @root_validator() def validate_environment(cls, values: Dict) -> Dict: """Validate that api key and python package exists in environment.""" - anthropic_api_key = get_from_dict_or_env( + values["anthropic_api_key"] = get_from_dict_or_env( values, "anthropic_api_key", "ANTHROPIC_API_KEY" ) # Get custom api url from environment. - anthropic_api_url = get_from_dict_or_env( + values["anthropic_api_url"] = get_from_dict_or_env( values, "anthropic_api_url", "ANTHROPIC_API_URL", @@ -72,13 +72,13 @@ class _AnthropicCommon(BaseModel): f"`pip install -U anthropic`" ) values["client"] = anthropic.Anthropic( - base_url=anthropic_api_url, - api_key=anthropic_api_key, + base_url=values["anthropic_api_url"], + api_key=values["anthropic_api_key"], timeout=values["default_request_timeout"], ) values["async_client"] = anthropic.AsyncAnthropic( - base_url=anthropic_api_url, - api_key=anthropic_api_key, + base_url=values["anthropic_api_url"], + api_key=values["anthropic_api_key"], timeout=values["default_request_timeout"], ) values["HUMAN_PROMPT"] = anthropic.HUMAN_PROMPT