| 15 | |
| 16 | |
| 17 | class VLMessageClient: |
| 18 | def __init__( |
| 19 | self, |
| 20 | api_url, |
| 21 | *, |
| 22 | timeout_base=60, |
| 23 | max_retries=10, |
| 24 | backoff_base=2, |
| 25 | backoff_cap=10, |
| 26 | pool_maxsize=8, |
| 27 | max_cache_items=1024, |
| 28 | ): |
| 29 | self.api_url = api_url |
| 30 | self.timeout_base = timeout_base |
| 31 | self.max_retries = max_retries |
| 32 | self.backoff_base = backoff_base |
| 33 | self.backoff_cap = backoff_cap |
| 34 | self.pool_maxsize = pool_maxsize |
| 35 | self.max_cache_items = max_cache_items |
| 36 | self._local = threading.local() |
| 37 | self._cache_lock = threading.Lock() |
| 38 | self._encode_cache = {} |
| 39 | |
| 40 | def _get_session(self): |
| 41 | session = getattr(self._local, "session", None) |
| 42 | if session is None: |
| 43 | session = requests.Session() |
| 44 | adapter = HTTPAdapter( |
| 45 | pool_connections=self.pool_maxsize, |
| 46 | pool_maxsize=self.pool_maxsize, |
| 47 | ) |
| 48 | session.mount("http://", adapter) |
| 49 | session.mount("https://", adapter) |
| 50 | self._local.session = session |
| 51 | return session |
| 52 | |
| 53 | def _encode_image(self, image): |
| 54 | with Image.open(image) as img: |
| 55 | img = img.convert("RGB") |
| 56 | buffered = BytesIO() |
| 57 | img.save(buffered, format="JPEG", quality=95) |
| 58 | return base64.b64encode(buffered.getvalue()).decode("utf-8") |
| 59 | |
| 60 | def _get_cached_base64(self, image_path): |
| 61 | with self._cache_lock: |
| 62 | cached = self._encode_cache.get(image_path) |
| 63 | if cached is not None: |
| 64 | return cached |
| 65 | encoded = self._encode_image(image_path) |
| 66 | with self._cache_lock: |
| 67 | if len(self._encode_cache) >= self.max_cache_items: |
| 68 | self._encode_cache.clear() |
| 69 | self._encode_cache[image_path] = encoded |
| 70 | return encoded |
| 71 | |
| 72 | def build_messages(self, item, image_root=None): |
| 73 | content = [] |
| 74 | images = list(item.get("images", [])) |