From cddcd887338463930109512c2b661b53b39efcba Mon Sep 17 00:00:00 2001 From: kaijietti <43436010+kaijietti@users.noreply.github.com> Date: Tue, 9 Apr 2024 05:04:17 +0800 Subject: [PATCH] community: add request_timeout and max_retries to ChatAnthropic (#19402) This PR make `request_timeout` and `max_retries` configurable for ChatAnthropic. --------- Co-authored-by: Bagatur Co-authored-by: Erick Friis --- .../langchain_anthropic/chat_models.py | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index 20afaa8cef62d..7825a16518904 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -244,7 +244,11 @@ class Config: """Total probability mass of tokens to consider at each step.""" default_request_timeout: Optional[float] = Field(None, alias="timeout") - """Timeout for requests to Anthropic Completion API. Default is 600 seconds.""" + """Timeout for requests to Anthropic Completion API.""" + + # sdk default = 2: https://github.com/anthropics/anthropic-sdk-python?tab=readme-ov-file#retries + max_retries: int = 2 + """Number of retries allowed for requests sent to the Anthropic Completion API.""" anthropic_api_url: str = "https://api.anthropic.com" @@ -286,16 +290,23 @@ def validate_environment(cls, values: Dict) -> Dict: or "https://api.anthropic.com" ) values["anthropic_api_url"] = api_url - values["_client"] = anthropic.Client( - api_key=api_key, - base_url=api_url, - default_headers=values.get("default_headers"), - ) - values["_async_client"] = anthropic.AsyncClient( - api_key=api_key, - base_url=api_url, - default_headers=values.get("default_headers"), - ) + client_params = { + "api_key": api_key, + "base_url": api_url, + "max_retries": values["max_retries"], + "default_headers": values.get("default_headers"), + } + # value <= 0 indicates the param should be ignored. None is a meaningful value + # for Anthropic client and treated differently than not specifying the param at + # all. + if ( + values["default_request_timeout"] is None + or values["default_request_timeout"] > 0 + ): + client_params["timeout"] = values["default_request_timeout"] + + values["_client"] = anthropic.Client(**client_params) + values["_async_client"] = anthropic.AsyncClient(**client_params) return values def _format_params(