refactor: switch from requests to urllib for custom endpoint model fetching

This commit is contained in:
Dhaval
2026-04-02 12:48:41 +05:30
committed by Nathan Esquenazi
parent c1a9324f35
commit 669412cbc9

View File

@@ -445,12 +445,13 @@ def get_available_models() -> dict:
if all_env.get('DEEPSEEK_API_KEY'): if all_env.get('DEEPSEEK_API_KEY'):
detected_providers.add('deepseek') detected_providers.add('deepseek')
# 3. Fetch models from custom endpoint if base_url is configured # 3. Fetch models from custom endpoint if base_url is configured
if cfg_base_url: if cfg_base_url:
auto_detected_models = [] # Store models fetched from endpoint auto_detected_models = [] # Store models fetched from endpoint
try: try:
import requests as _req
import ipaddress import ipaddress
import urllib.request
import urllib.parse
# Normalize the base_url # Normalize the base_url
base_url = cfg_base_url.strip() base_url = cfg_base_url.strip()
@@ -480,7 +481,7 @@ def get_available_models() -> dict:
except ValueError: except ValueError:
pass pass
# Get the API key for this provider # Get the API key for this provider
headers = {} headers = {}
# Try hermes-agent style API key resolution # Try hermes-agent style API key resolution
@@ -500,11 +501,19 @@ def get_available_models() -> dict:
headers['Authorization'] = f'Bearer {api_key}' headers['Authorization'] = f'Bearer {api_key}'
break break
# Make the request # Make the request using urllib.request
try: try:
resp = _req.get(endpoint_url, headers=headers, timeout=10) # Build request URL
resp.raise_for_status() url = endpoint_url
data = resp.json()
# Prepare request
req = urllib.request.Request(url, method='GET')
for key, value in headers.items():
req.add_header(key, value)
# Send request with timeout
with urllib.request.urlopen(req, timeout=10) as response:
data = json.loads(response.read().decode('utf-8'))
# Parse the response - handle both OpenAI-compatible and llama.cpp formats # Parse the response - handle both OpenAI-compatible and llama.cpp formats
models_list = [] models_list = []