Подключение
Базовый URL, учётные данные и доступный сервер.
Рассмотрите image_candidate.py как явное правило ширины, а не воспроизведение всех правил выбора изображения браузером.
Базовый URL, учётные данные и доступный сервер.
Модель, которую предоставляет настроенный endpoint.
Объём контекста и поддержка инструментов определяются моделью и сервером.
import math
def image_candidate(candidates, width, ratio):
if type(width) is not int or not 1 <= width <= 10000 or type(ratio) not in (int, float) or not 0.25 <= ratio <= 8 or not math.isfinite(ratio):
raise ValueError("image.viewport")
if type(candidates) is not list or not 1 <= len(candidates) <= 64:
raise ValueError("image.candidates")
sizes, seen = [], set()
for pair in candidates:
if type(pair) not in (list, tuple) or len(pair) != 2:
raise ValueError("image.pair")
url, pixels = pair
if type(url) is not str or not 1 <= len(url) <= 2048 or any(ord(c) < 32 or ord(c) == 127 for c in url):
raise ValueError("image.url")
if type(pixels) is not int or not 1 <= pixels <= 100000 or pixels in seen:
raise ValueError("image.size")
seen.add(pixels)
sizes.append((pixels, url))
sizes.sort()
target = width*ratio
return next((url for pixels, url in sizes if pixels >= target), sizes[-1][1])