mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
63343b4987
Description: adds support for langchain_cohere --------- Co-authored-by: Harry M <127103098+harry-cohere@users.noreply.github.com> Co-authored-by: Erick Friis <erick@langchain.dev>
37 lines
975 B
Python
37 lines
975 B
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any, Callable
|
|
|
|
import cohere
|
|
from tenacity import (
|
|
before_sleep_log,
|
|
retry,
|
|
retry_if_exception_type,
|
|
stop_after_attempt,
|
|
wait_exponential,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _create_retry_decorator(max_retries: int) -> Callable[[Any], Any]:
|
|
# support v4 and v5
|
|
retry_conditions = (
|
|
retry_if_exception_type(cohere.error.CohereError)
|
|
if hasattr(cohere, "error")
|
|
else retry_if_exception_type(Exception)
|
|
)
|
|
|
|
min_seconds = 4
|
|
max_seconds = 10
|
|
# Wait 2^x * 1 second between each retry starting with
|
|
# 4 seconds, then up to 10 seconds, then 10 seconds afterwards
|
|
return retry(
|
|
reraise=True,
|
|
stop=stop_after_attempt(max_retries),
|
|
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
|
|
retry=retry_conditions,
|
|
before_sleep=before_sleep_log(logger, logging.WARNING),
|
|
)
|