Skip to content

Commit

Permalink
OpenAIEmbeddings: retry_min_seconds/retry_max_seconds parameters (#13138
Browse files Browse the repository at this point in the history
)

- **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 <[email protected]>
  • Loading branch information
adumont and hwchase17 authored Dec 6, 2023
1 parent 9e5d146 commit b05c460
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions libs/langchain/langchain/embeddings/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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."""

Expand Down

0 comments on commit b05c460

Please sign in to comment.