Skip to content

Commit c2546d6

Browse files
author
James Zhu
committed
fix: litellm_models.py to use correct endpoint from config and handle SSL for private IPs
- Modified litellm_models.py to read endpoint URL from environment variables or config file instead of hardcoded IP - Added automatic SSL verification bypass for private/internal IPs to handle self-signed certificates - Updated EndpointManager to properly pass environment variables to internal modules - Fixed the script getting stuck by using the correct Litellm server endpoint (10.189.8.10 instead of 192.168.1.100)
1 parent 947d789 commit c2546d6

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

code_assistant_manager/endpoints.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def _execute_model_command(
374374

375375
# Try internal module execution
376376
if self._is_internal_module_command(tokens):
377-
return self._execute_internal_module(tokens[2])
377+
return self._execute_internal_module(tokens[2], env)
378378

379379
# Try literal model list (command not found on PATH)
380380
if shutil.which(tokens[0]) is None:
@@ -391,14 +391,32 @@ def _is_internal_module_command(self, tokens: List[str]) -> bool:
391391
and tokens[2] in self.INTERNAL_MODEL_MODULES
392392
)
393393

394-
def _execute_internal_module(self, module_name: str) -> Optional[str]:
394+
def _execute_internal_module(self, module_name: str, env: Dict[str, str] = None) -> Optional[str]:
395395
"""Execute an internal Python module for model listing."""
396396
try:
397397
mod = importlib.import_module(module_name)
398-
buf = io.StringIO()
399-
with contextlib.redirect_stdout(buf):
400-
mod.list_models()
401-
return buf.getvalue().strip()
398+
399+
# Temporarily set environment variables if provided
400+
old_env = {}
401+
if env:
402+
for key, value in env.items():
403+
if key in os.environ:
404+
old_env[key] = os.environ[key]
405+
os.environ[key] = value
406+
407+
try:
408+
buf = io.StringIO()
409+
with contextlib.redirect_stdout(buf):
410+
mod.list_models()
411+
return buf.getvalue().strip()
412+
finally:
413+
# Restore original environment variables
414+
for key, value in old_env.items():
415+
os.environ[key] = value
416+
for key in env.keys():
417+
if key not in old_env and key in os.environ:
418+
del os.environ[key]
419+
402420
except Exception as e:
403421
print(f"Warning: Failed to load module {module_name}: {e}")
404422
return None

code_assistant_manager/litellm_models.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import requests
88

99
from .env_loader import load_env
10+
from .config import ConfigManager
1011

1112
logger = logging.getLogger(__name__)
1213

@@ -24,8 +25,30 @@ def fetch_litellm_models(api_key: str, base_url: str = "https://192.168.1.100:41
2425
"accept": "application/json",
2526
"x-litellm-api-key": api_key,
2627
}
27-
# Security: Enable SSL certificate verification for secure connections
28-
r = requests.get(url, params=params, headers=headers, timeout=30, verify=True)
28+
29+
# Determine if SSL verification should be enabled
30+
# For internal/private IPs, skip verification as they often use self-signed certificates
31+
import ipaddress
32+
from urllib.parse import urlparse
33+
34+
verify_ssl = True
35+
try:
36+
parsed = urlparse(base_url)
37+
if parsed.hostname:
38+
try:
39+
ip = ipaddress.ip_address(parsed.hostname)
40+
# Skip verification for private IPs (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8)
41+
if ip.is_private or ip.is_loopback:
42+
verify_ssl = False
43+
except ValueError:
44+
# Not an IP address, keep verification enabled
45+
pass
46+
except Exception:
47+
# If parsing fails, keep verification enabled
48+
pass
49+
50+
# Make the request
51+
r = requests.get(url, params=params, headers=headers, timeout=30, verify=verify_ssl)
2952
r.raise_for_status()
3053
return r.json()
3154

@@ -42,9 +65,21 @@ def list_models():
4265
logger.error("API_KEY_LITELLM environment variable is required but not found")
4366
raise SystemExit("API_KEY_LITELLM environment variable is required")
4467

68+
# Get base URL from environment (set by EndpointManager) or from config
69+
base_url = os.environ.get("endpoint")
70+
if not base_url:
71+
# Try to get from config
72+
try:
73+
config = ConfigManager()
74+
litellm_config = config.get_endpoint_config("litellm")
75+
base_url = litellm_config.get("endpoint", "https://192.168.1.100:4142")
76+
except Exception as e:
77+
logger.debug(f"Could not load config, using default URL: {e}")
78+
base_url = "https://192.168.1.100:4142"
79+
4580
logger.debug("Fetching Litellm models")
4681
try:
47-
models_data = fetch_litellm_models(api_key)
82+
models_data = fetch_litellm_models(api_key, base_url)
4883
model_count = len(models_data.get("data", []))
4984
logger.debug(f"Found {model_count} models")
5085

0 commit comments

Comments
 (0)