From b05c46074b200fc5cda93f30e9049a16f7406c5b Mon Sep 17 00:00:00 2001 From: Alexandre Dumont Date: Wed, 6 Dec 2023 05:08:17 +0100 Subject: [PATCH] OpenAIEmbeddings: retry_min_seconds/retry_max_seconds parameters (#13138) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - **Description:** new parameters in OpenAIEmbeddings() constructor (retry_min_seconds and retry_max_seconds) that allow parametrization by the user of the former min_seconds and max_seconds that were hidden in _create_retry_decorator() and _async_retry_decorator() - **Issue:** #9298, #12986 - **Dependencies:** none - **Tag maintainer:** @hwchase17 - **Twitter handle:** @adumont make format ✅ make lint ✅ make test ✅ Co-authored-by: Harrison Chase --- libs/langchain/langchain/embeddings/openai.py | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index 62aa549bd4430..265fdc2f6cad7 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -41,14 +41,19 @@ def _create_retry_decorator(embeddings: OpenAIEmbeddings) -> Callable[[Any], Any]: import openai - 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 + # retry_min_seconds seconds, then up to retry_max_seconds seconds, + # then retry_max_seconds seconds afterwards + # retry_min_seconds and retry_max_seconds are optional arguments of + # OpenAIEmbeddings return retry( reraise=True, stop=stop_after_attempt(embeddings.max_retries), - wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), + wait=wait_exponential( + multiplier=1, + min=embeddings.retry_min_seconds, + max=embeddings.retry_max_seconds, + ), retry=( retry_if_exception_type(openai.error.Timeout) | retry_if_exception_type(openai.error.APIError) @@ -63,14 +68,19 @@ def _create_retry_decorator(embeddings: OpenAIEmbeddings) -> Callable[[Any], Any def _async_retry_decorator(embeddings: OpenAIEmbeddings) -> Any: import openai - 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 + # retry_min_seconds seconds, then up to retry_max_seconds seconds, + # then retry_max_seconds seconds afterwards + # retry_min_seconds and retry_max_seconds are optional arguments of + # OpenAIEmbeddings async_retrying = AsyncRetrying( reraise=True, stop=stop_after_attempt(embeddings.max_retries), - wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), + wait=wait_exponential( + multiplier=1, + min=embeddings.retry_min_seconds, + max=embeddings.retry_max_seconds, + ), retry=( retry_if_exception_type(openai.error.Timeout) | retry_if_exception_type(openai.error.APIError) @@ -234,6 +244,10 @@ class OpenAIEmbeddings(BaseModel, Embeddings): default_query: Union[Mapping[str, object], None] = None # Configure a custom httpx client. See the # [httpx documentation](https://www.python-httpx.org/api/#client) for more details. + retry_min_seconds: int = 4 + """Min number of seconds to wait between retries""" + retry_max_seconds: int = 20 + """Max number of seconds to wait between retries""" http_client: Union[Any, None] = None """Optional httpx.Client."""