Skip to content

Commit

Permalink
Merge pull request #544 from GooeyAI/azure-form-reco-retry
Browse files Browse the repository at this point in the history
add retry_if fallback for Azure Form Recognizer requests
  • Loading branch information
milovate authored Dec 6, 2024
2 parents 77b6c47 + 8646946 commit 45b60a1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
13 changes: 13 additions & 0 deletions daras_ai_v2/azure_doc_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from daras_ai_v2.redis_cache import redis_cache_decorator
from daras_ai_v2.text_splitter import default_length_function

from aifail import retry_if

auth_headers = {"Ocp-Apim-Subscription-Key": settings.AZURE_FORM_RECOGNIZER_KEY}


Expand Down Expand Up @@ -54,7 +56,18 @@ def azure_form_recognizer_models() -> dict[str, str]:
return {value["modelId"]: value["description"] for value in r.json()["value"]}


def azure_should_retry(e: Exception) -> bool:
return isinstance(
e, (requests.exceptions.ConnectionError, requests.exceptions.Timeout)
) or (
isinstance(e, requests.exceptions.HTTPError)
and e.response is not None
and e.response.status_code in [408, 429, 502, 503, 504]
)


@redis_cache_decorator(ex=settings.REDIS_MODELS_CACHE_EXPIRY)
@retry_if(azure_should_retry, max_retries=2)
def azure_form_recognizer(url: str, model_id: str, params: dict = None) -> dict:
r = requests.post(
str(
Expand Down
45 changes: 44 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45b60a1

Please sign in to comment.