Skip to content

Commit

Permalink
community: add request_timeout and max_retries to ChatAnthropic (#19402)
Browse files Browse the repository at this point in the history
This PR make `request_timeout` and `max_retries` configurable for
ChatAnthropic.

---------

Co-authored-by: Bagatur <[email protected]>
Co-authored-by: Erick Friis <[email protected]>
  • Loading branch information
3 people authored and hinthornw committed Apr 26, 2024
1 parent f0cc968 commit cddcd88
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions libs/partners/anthropic/langchain_anthropic/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cddcd88

Please sign in to comment.