From 669412cbc91fd11e502923c6d9dead62862d54a9 Mon Sep 17 00:00:00 2001 From: Dhaval Date: Thu, 2 Apr 2026 12:48:41 +0530 Subject: [PATCH] refactor: switch from requests to urllib for custom endpoint model fetching --- api/config.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/api/config.py b/api/config.py index 8c60b9b..84e023c 100644 --- a/api/config.py +++ b/api/config.py @@ -445,12 +445,13 @@ def get_available_models() -> dict: if all_env.get('DEEPSEEK_API_KEY'): 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: auto_detected_models = [] # Store models fetched from endpoint try: - import requests as _req import ipaddress + import urllib.request + import urllib.parse # Normalize the base_url base_url = cfg_base_url.strip() @@ -480,7 +481,7 @@ def get_available_models() -> dict: except ValueError: pass - # Get the API key for this provider + # Get the API key for this provider headers = {} # Try hermes-agent style API key resolution @@ -500,11 +501,19 @@ def get_available_models() -> dict: headers['Authorization'] = f'Bearer {api_key}' break - # Make the request + # Make the request using urllib.request try: - resp = _req.get(endpoint_url, headers=headers, timeout=10) - resp.raise_for_status() - data = resp.json() + # Build request URL + url = endpoint_url + + # 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 models_list = []