|
1 | 1 | import os |
| 2 | +from dataclasses import dataclass |
2 | 3 | from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar, Union, overload |
3 | 4 | from urllib.parse import parse_qs, urlencode, urlparse, urlunparse |
4 | 5 | from warnings import warn |
|
15 | 16 | from redis.sentinel import Sentinel |
16 | 17 |
|
17 | 18 | from redisvl import __version__ |
18 | | -from redisvl.redis.constants import REDIS_URL_ENV_VAR |
| 19 | +from redisvl.redis.constants import ( |
| 20 | + REDIS_URL_ENV_VAR, |
| 21 | + SVS_MIN_REDIS_VERSION, |
| 22 | + SVS_REQUIRED_MODULES, |
| 23 | +) |
19 | 24 | from redisvl.redis.utils import convert_bytes, is_cluster_url |
20 | 25 | from redisvl.types import AsyncRedisClient, RedisClient, SyncRedisClient |
21 | 26 | from redisvl.utils.utils import deprecated_function |
@@ -101,6 +106,152 @@ def unpack_redis_modules(module_list: List[Dict[str, Any]]) -> Dict[str, Any]: |
101 | 106 | return {module["name"]: module["ver"] for module in module_list} |
102 | 107 |
|
103 | 108 |
|
| 109 | +@dataclass |
| 110 | +class VectorSupport: |
| 111 | + """Redis server capabilities for vector operations.""" |
| 112 | + |
| 113 | + redis_version: str |
| 114 | + search_version: int |
| 115 | + searchlight_version: int |
| 116 | + svs_vamana_supported: bool |
| 117 | + |
| 118 | + @property |
| 119 | + def search_version_str(self) -> str: |
| 120 | + """Format search module version as string.""" |
| 121 | + return format_module_version(self.search_version) |
| 122 | + |
| 123 | + @property |
| 124 | + def searchlight_version_str(self) -> str: |
| 125 | + """Format searchlight module version as string.""" |
| 126 | + return format_module_version(self.searchlight_version) |
| 127 | + |
| 128 | + |
| 129 | +def format_module_version(version: int) -> str: |
| 130 | + """Format module version from integer (20810) to string (2.8.10).""" |
| 131 | + if version == 0: |
| 132 | + return "not installed" |
| 133 | + major = version // 10000 |
| 134 | + minor = (version % 10000) // 100 |
| 135 | + patch = version % 100 |
| 136 | + return f"{major}.{minor}.{patch}" |
| 137 | + |
| 138 | + |
| 139 | +def check_vector_capabilities(client: SyncRedisClient) -> VectorSupport: |
| 140 | + """Check Redis server capabilities for vector features. |
| 141 | +
|
| 142 | + Args: |
| 143 | + client: Sync Redis client instance |
| 144 | +
|
| 145 | + Returns: |
| 146 | + VectorSupport with version info and supported features |
| 147 | + """ |
| 148 | + info = client.info("server") |
| 149 | + redis_version = info.get("redis_version", "0.0.0") |
| 150 | + |
| 151 | + modules = RedisConnectionFactory.get_modules(client) |
| 152 | + search_ver = modules.get("search", 0) |
| 153 | + searchlight_ver = modules.get("searchlight", 0) |
| 154 | + |
| 155 | + # Check if SVS-VAMANA requirements are met |
| 156 | + redis_ok = compare_versions(redis_version, SVS_MIN_REDIS_VERSION) |
| 157 | + modules_ok = search_ver >= 20810 or searchlight_ver >= 20810 |
| 158 | + |
| 159 | + return VectorSupport( |
| 160 | + redis_version=redis_version, |
| 161 | + search_version=search_ver, |
| 162 | + searchlight_version=searchlight_ver, |
| 163 | + svs_vamana_supported=redis_ok and modules_ok, |
| 164 | + ) |
| 165 | + |
| 166 | + |
| 167 | +async def check_vector_capabilities_async(client: AsyncRedisClient) -> VectorSupport: |
| 168 | + """Async version of check_vector_capabilities. |
| 169 | +
|
| 170 | + Args: |
| 171 | + client: Async Redis client instance |
| 172 | +
|
| 173 | + Returns: |
| 174 | + VectorSupport with version info and supported features |
| 175 | + """ |
| 176 | + info = await client.info("server") |
| 177 | + redis_version = info.get("redis_version", "0.0.0") |
| 178 | + |
| 179 | + modules = await RedisConnectionFactory.get_modules_async(client) |
| 180 | + search_ver = modules.get("search", 0) |
| 181 | + searchlight_ver = modules.get("searchlight", 0) |
| 182 | + |
| 183 | + # Check if SVS-VAMANA requirements are met |
| 184 | + redis_ok = compare_versions(redis_version, SVS_MIN_REDIS_VERSION) |
| 185 | + modules_ok = search_ver >= 20810 or searchlight_ver >= 20810 |
| 186 | + |
| 187 | + return VectorSupport( |
| 188 | + redis_version=redis_version, |
| 189 | + search_version=search_ver, |
| 190 | + searchlight_version=searchlight_ver, |
| 191 | + svs_vamana_supported=redis_ok and modules_ok, |
| 192 | + ) |
| 193 | + |
| 194 | + |
| 195 | +def supports_svs_vamana(client: SyncRedisClient) -> bool: |
| 196 | + """Check if Redis server supports SVS-VAMANA algorithm. |
| 197 | +
|
| 198 | + SVS-Vamana requires: |
| 199 | + - Redis version >= 8.2.0 |
| 200 | + - RediSearch version >= 2.8.10 (20810) |
| 201 | +
|
| 202 | + Args: |
| 203 | + client: Sync Redis client instance |
| 204 | +
|
| 205 | + Returns: |
| 206 | + bool: True if SVS-VAMANA is supported, False otherwise |
| 207 | + """ |
| 208 | + info = client.info() |
| 209 | + redis_version = info.get("redis_version", "0.0.0") |
| 210 | + if not compare_versions(redis_version, SVS_MIN_REDIS_VERSION): |
| 211 | + return False |
| 212 | + |
| 213 | + # Check module versions |
| 214 | + modules = unpack_redis_modules(convert_bytes(client.module_list())) |
| 215 | + for module in SVS_REQUIRED_MODULES: |
| 216 | + module_name = module["name"] |
| 217 | + required_version = module["ver"] |
| 218 | + if module_name not in modules: |
| 219 | + return False |
| 220 | + if modules[module_name] < required_version: |
| 221 | + return False |
| 222 | + return True |
| 223 | + |
| 224 | + |
| 225 | +async def async_supports_svs_vamana(client: AsyncRedisClient) -> bool: |
| 226 | + """Check if Redis server supports SVS-VAMANA algorithm asynchronously. |
| 227 | +
|
| 228 | + SVS-Vamana requires: |
| 229 | + - Redis version >= 8.2.0 |
| 230 | + - RediSearch version >= 2.8.10 (20810) |
| 231 | +
|
| 232 | + Args: |
| 233 | + client: Sync Redis client instance |
| 234 | +
|
| 235 | + Returns: |
| 236 | + bool: True if SVS-VAMANA is supported, False otherwise |
| 237 | + """ |
| 238 | + info = await client.info() |
| 239 | + redis_version = info.get("redis_version", "0.0.0") |
| 240 | + if not compare_versions(redis_version, SVS_MIN_REDIS_VERSION): |
| 241 | + return False |
| 242 | + |
| 243 | + # Check module versions |
| 244 | + modules = unpack_redis_modules(convert_bytes(await client.module_list())) |
| 245 | + for module in SVS_REQUIRED_MODULES: |
| 246 | + module_name = module["name"] |
| 247 | + required_version = module["ver"] |
| 248 | + if module_name not in modules: |
| 249 | + return False |
| 250 | + if modules[module_name] < required_version: |
| 251 | + return False |
| 252 | + return True |
| 253 | + |
| 254 | + |
104 | 255 | def get_address_from_env() -> str: |
105 | 256 | """Get Redis URL from environment variable.""" |
106 | 257 | redis_url = os.getenv(REDIS_URL_ENV_VAR) |
|
0 commit comments