diff --git a/fastdeploy/entrypoints/chat_utils.py b/fastdeploy/entrypoints/chat_utils.py index eb25122e1..9850430a5 100644 --- a/fastdeploy/entrypoints/chat_utils.py +++ b/fastdeploy/entrypoints/chat_utils.py @@ -15,6 +15,7 @@ """ import os +import time import uuid from copy import deepcopy from pathlib import Path @@ -32,6 +33,7 @@ from typing_extensions import Required, TypeAlias, TypedDict from fastdeploy.multimodal.image import ImageMediaIO from fastdeploy.multimodal.video import VideoMediaIO +from fastdeploy.utils import api_server_logger class VideoURL(TypedDict, total=False): @@ -90,12 +92,32 @@ class MultiModalPartParser: """Parse Video""" return self.load_from_url(video_url, self.video_io) + def http_get_with_retry(self, url, max_retries=3, retry_delay=1, backoff_factor=2): + """HTTP GET retry""" + + retry_cnt = 0 + delay = retry_delay + + while retry_cnt < max_retries: + try: + response = requests.get(url) + response.raise_for_status() + return response.content + except Exception as e: + retry_cnt += 1 + if retry_cnt >= max_retries: + api_server_logger.error(f"HTTP GET failed: {e}. Max retries reached") + raise + api_server_logger.info(f"HTTP GET failed: {e}. Start retry {retry_cnt}") + time.sleep(delay) + delay *= backoff_factor + def load_from_url(self, url, media_io): """Load media from URL""" parsed = urlparse(url) if parsed.scheme.startswith("http"): - media_bytes = requests.get(url).content + media_bytes = self.http_get_with_retry(url) return media_io.load_bytes(media_bytes) if parsed.scheme.startswith("data"):