Files
wehub-resource-sync 6b7e6b44f1
Python Build and Type Check / python-ci (ubuntu-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
gh-pages / build (push) Has been cancelled
Python Publish (pypi) / Upload release to PyPI (push) Has been cancelled
Spellcheck / spellcheck (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:37:31 +08:00

61 lines
1.5 KiB
Python

# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License
"""Retry middleware."""
from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from graphrag_llm.retry import Retry
from graphrag_llm.types import (
AsyncLLMFunction,
LLMFunction,
)
def with_retries(
*,
sync_middleware: "LLMFunction",
async_middleware: "AsyncLLMFunction",
retrier: "Retry",
) -> tuple[
"LLMFunction",
"AsyncLLMFunction",
]:
"""Wrap model functions with retry middleware.
Args
----
sync_middleware: LLMFunction
The synchronous model function to wrap.
Either a completion function or an embedding function.
async_middleware: AsyncLLMFunction
The asynchronous model function to wrap.
Either a completion function or an embedding function.
retrier: Retry
The retrier instance to use for retrying failed requests.
Returns
-------
tuple[LLMFunction, AsyncLLMFunction]
The synchronous and asynchronous model functions wrapped with retry middleware.
"""
def _retry_middleware(
**kwargs: Any,
):
return retrier.retry(
func=sync_middleware,
input_args=kwargs,
)
async def _retry_middleware_async(
**kwargs: Any,
):
return await retrier.retry_async(
func=async_middleware,
input_args=kwargs,
)
return (_retry_middleware, _retry_middleware_async) # type: ignore