|
| 1 | +from pathlib import Path |
| 2 | +from typing import Mapping, Optional |
| 3 | +from urllib.parse import urlparse |
| 4 | + |
| 5 | +import aiohttp |
| 6 | +import requests |
| 7 | + |
| 8 | +from vllm.version import __version__ as VLLM_VERSION |
| 9 | + |
| 10 | + |
| 11 | +class HTTPConnection: |
| 12 | + """Helper class to send HTTP requests.""" |
| 13 | + |
| 14 | + def __init__(self, *, reuse_client: bool = True) -> None: |
| 15 | + super().__init__() |
| 16 | + |
| 17 | + self.reuse_client = reuse_client |
| 18 | + |
| 19 | + self._sync_client: Optional[requests.Session] = None |
| 20 | + self._async_client: Optional[aiohttp.ClientSession] = None |
| 21 | + |
| 22 | + def get_sync_client(self) -> requests.Session: |
| 23 | + if self._sync_client is None or not self.reuse_client: |
| 24 | + self._sync_client = requests.Session() |
| 25 | + |
| 26 | + return self._sync_client |
| 27 | + |
| 28 | + # NOTE: We intentionally use an async function even though it is not |
| 29 | + # required, so that the client is only accessible inside async event loop |
| 30 | + async def get_async_client(self) -> aiohttp.ClientSession: |
| 31 | + if self._async_client is None or not self.reuse_client: |
| 32 | + self._async_client = aiohttp.ClientSession() |
| 33 | + |
| 34 | + return self._async_client |
| 35 | + |
| 36 | + def _validate_http_url(self, url: str): |
| 37 | + parsed_url = urlparse(url) |
| 38 | + |
| 39 | + if parsed_url.scheme not in ("http", "https"): |
| 40 | + raise ValueError("Invalid HTTP URL: A valid HTTP URL " |
| 41 | + "must have scheme 'http' or 'https'.") |
| 42 | + |
| 43 | + def _headers(self, **extras: str) -> Mapping[str, str]: |
| 44 | + return {"User-Agent": f"vLLM/{VLLM_VERSION}", **extras} |
| 45 | + |
| 46 | + def get_response( |
| 47 | + self, |
| 48 | + url: str, |
| 49 | + *, |
| 50 | + stream: bool = False, |
| 51 | + timeout: Optional[float] = None, |
| 52 | + extra_headers: Optional[Mapping[str, str]] = None, |
| 53 | + ): |
| 54 | + self._validate_http_url(url) |
| 55 | + |
| 56 | + client = self.get_sync_client() |
| 57 | + extra_headers = extra_headers or {} |
| 58 | + |
| 59 | + return client.get(url, |
| 60 | + headers=self._headers(**extra_headers), |
| 61 | + stream=stream, |
| 62 | + timeout=timeout) |
| 63 | + |
| 64 | + async def get_async_response( |
| 65 | + self, |
| 66 | + url: str, |
| 67 | + *, |
| 68 | + timeout: Optional[float] = None, |
| 69 | + extra_headers: Optional[Mapping[str, str]] = None, |
| 70 | + ): |
| 71 | + self._validate_http_url(url) |
| 72 | + |
| 73 | + client = await self.get_async_client() |
| 74 | + extra_headers = extra_headers or {} |
| 75 | + |
| 76 | + return client.get(url, |
| 77 | + headers=self._headers(**extra_headers), |
| 78 | + timeout=timeout) |
| 79 | + |
| 80 | + def get_bytes(self, url: str, *, timeout: Optional[float] = None) -> bytes: |
| 81 | + with self.get_response(url, timeout=timeout) as r: |
| 82 | + r.raise_for_status() |
| 83 | + |
| 84 | + return r.content |
| 85 | + |
| 86 | + async def async_get_bytes( |
| 87 | + self, |
| 88 | + url: str, |
| 89 | + *, |
| 90 | + timeout: Optional[float] = None, |
| 91 | + ) -> bytes: |
| 92 | + async with await self.get_async_response(url, timeout=timeout) as r: |
| 93 | + r.raise_for_status() |
| 94 | + |
| 95 | + return await r.read() |
| 96 | + |
| 97 | + def get_text(self, url: str, *, timeout: Optional[float] = None) -> str: |
| 98 | + with self.get_response(url, timeout=timeout) as r: |
| 99 | + r.raise_for_status() |
| 100 | + |
| 101 | + return r.text |
| 102 | + |
| 103 | + async def async_get_text( |
| 104 | + self, |
| 105 | + url: str, |
| 106 | + *, |
| 107 | + timeout: Optional[float] = None, |
| 108 | + ) -> str: |
| 109 | + async with await self.get_async_response(url, timeout=timeout) as r: |
| 110 | + r.raise_for_status() |
| 111 | + |
| 112 | + return await r.text() |
| 113 | + |
| 114 | + def get_json(self, url: str, *, timeout: Optional[float] = None) -> str: |
| 115 | + with self.get_response(url, timeout=timeout) as r: |
| 116 | + r.raise_for_status() |
| 117 | + |
| 118 | + return r.json() |
| 119 | + |
| 120 | + async def async_get_json( |
| 121 | + self, |
| 122 | + url: str, |
| 123 | + *, |
| 124 | + timeout: Optional[float] = None, |
| 125 | + ) -> str: |
| 126 | + async with await self.get_async_response(url, timeout=timeout) as r: |
| 127 | + r.raise_for_status() |
| 128 | + |
| 129 | + return await r.json() |
| 130 | + |
| 131 | + def download_file( |
| 132 | + self, |
| 133 | + url: str, |
| 134 | + save_path: Path, |
| 135 | + *, |
| 136 | + timeout: Optional[float] = None, |
| 137 | + chunk_size: int = 128, |
| 138 | + ) -> Path: |
| 139 | + with self.get_response(url, timeout=timeout) as r: |
| 140 | + r.raise_for_status() |
| 141 | + |
| 142 | + with save_path.open("wb") as f: |
| 143 | + for chunk in r.iter_content(chunk_size): |
| 144 | + f.write(chunk) |
| 145 | + |
| 146 | + return save_path |
| 147 | + |
| 148 | + async def async_download_file( |
| 149 | + self, |
| 150 | + url: str, |
| 151 | + save_path: Path, |
| 152 | + *, |
| 153 | + timeout: Optional[float] = None, |
| 154 | + chunk_size: int = 128, |
| 155 | + ) -> Path: |
| 156 | + async with await self.get_async_response(url, timeout=timeout) as r: |
| 157 | + r.raise_for_status() |
| 158 | + |
| 159 | + with save_path.open("wb") as f: |
| 160 | + async for chunk in r.content.iter_chunked(chunk_size): |
| 161 | + f.write(chunk) |
| 162 | + |
| 163 | + return save_path |
| 164 | + |
| 165 | + |
| 166 | +global_http_connection = HTTPConnection() |
| 167 | +"""The global :class:`HTTPConnection` instance used by vLLM.""" |
0 commit comments