OpenAIEmbeddings: retry_min_seconds/retry_max_seconds parameters (#13138)

- **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 <hw.chase.17@gmail.com>
pull/13439/head^2
Alexandre Dumont 6 months ago committed by GitHub
parent 9e5d146409
commit b05c46074b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -41,14 +41,19 @@ logger = logging.getLogger(__name__)
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."""

Loading…
Cancel
Save