Adjust Redis connection logic for SSL-first approach, add Hugging Face provider debugging, and update requirements
Browse files- core/providers/huggingface.py +14 -4
- core/redis_client.py +35 -12
- requirements.txt +1 -1
core/providers/huggingface.py
CHANGED
|
@@ -19,6 +19,10 @@ class HuggingFaceProvider(LLMProvider):
|
|
| 19 |
def __init__(self, model_name: str, timeout: int = 30, max_retries: int = 3):
|
| 20 |
super().__init__(model_name, timeout, max_retries)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not HUGGINGFACE_SDK_AVAILABLE:
|
| 23 |
raise ImportError("Hugging Face provider requires 'openai' package")
|
| 24 |
|
|
@@ -26,10 +30,16 @@ class HuggingFaceProvider(LLMProvider):
|
|
| 26 |
raise ValueError("HF_TOKEN not set - required for Hugging Face provider")
|
| 27 |
|
| 28 |
# Make sure NO proxies parameter is included
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def generate(self, prompt: str, conversation_history: List[Dict]) -> Optional[str]:
|
| 35 |
"""Generate a response synchronously"""
|
|
|
|
| 19 |
def __init__(self, model_name: str, timeout: int = 30, max_retries: int = 3):
|
| 20 |
super().__init__(model_name, timeout, max_retries)
|
| 21 |
|
| 22 |
+
logger.info(f"Initializing HuggingFaceProvider with:")
|
| 23 |
+
logger.info(f" HF_API_URL: {config.hf_api_url}")
|
| 24 |
+
logger.info(f" HF_TOKEN SET: {bool(config.hf_token)}")
|
| 25 |
+
|
| 26 |
if not HUGGINGFACE_SDK_AVAILABLE:
|
| 27 |
raise ImportError("Hugging Face provider requires 'openai' package")
|
| 28 |
|
|
|
|
| 30 |
raise ValueError("HF_TOKEN not set - required for Hugging Face provider")
|
| 31 |
|
| 32 |
# Make sure NO proxies parameter is included
|
| 33 |
+
try:
|
| 34 |
+
self.client = OpenAI(
|
| 35 |
+
base_url=config.hf_api_url,
|
| 36 |
+
api_key=config.hf_token
|
| 37 |
+
)
|
| 38 |
+
logger.info("HuggingFaceProvider initialized successfully")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
logger.error(f"Failed to initialize HuggingFaceProvider: {e}")
|
| 41 |
+
logger.error(f"Error type: {type(e)}")
|
| 42 |
+
raise
|
| 43 |
|
| 44 |
def generate(self, prompt: str, conversation_history: List[Dict]) -> Optional[str]:
|
| 45 |
"""Generate a response synchronously"""
|
core/redis_client.py
CHANGED
|
@@ -36,13 +36,37 @@ class RedisClient:
|
|
| 36 |
logger.info("Redis not configured, skipping connection")
|
| 37 |
return None
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
# Parse host and port if port is included in host
|
| 41 |
host, port = self._parse_host_port(config.redis_host, config.redis_port)
|
|
|
|
| 42 |
|
| 43 |
-
logger.info(f"Connecting to Redis at {host}:{port}")
|
| 44 |
-
|
| 45 |
-
# Try connection WITHOUT SSL first for Redis Cloud (most common setup)
|
| 46 |
self._redis_client = redis.Redis(
|
| 47 |
host=host,
|
| 48 |
port=port,
|
|
@@ -51,18 +75,19 @@ class RedisClient:
|
|
| 51 |
decode_responses=True,
|
| 52 |
socket_connect_timeout=5,
|
| 53 |
socket_timeout=5,
|
|
|
|
|
|
|
| 54 |
retry_on_timeout=True,
|
| 55 |
health_check_interval=30
|
| 56 |
)
|
| 57 |
|
| 58 |
-
# Test connection
|
| 59 |
self._redis_client.ping()
|
| 60 |
-
logger.info("Successfully connected to Redis
|
|
|
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
-
logger.warning(f"
|
| 64 |
try:
|
| 65 |
-
# Fallback to SSL connection
|
| 66 |
host, port = self._parse_host_port(config.redis_host, config.redis_port)
|
| 67 |
|
| 68 |
self._redis_client = redis.Redis(
|
|
@@ -73,15 +98,13 @@ class RedisClient:
|
|
| 73 |
decode_responses=True,
|
| 74 |
socket_connect_timeout=5,
|
| 75 |
socket_timeout=5,
|
| 76 |
-
ssl=True,
|
| 77 |
-
ssl_cert_reqs=None, # Skip cert verification for Redis Cloud
|
| 78 |
retry_on_timeout=True,
|
| 79 |
health_check_interval=30
|
| 80 |
)
|
| 81 |
|
| 82 |
-
# Test connection
|
| 83 |
self._redis_client.ping()
|
| 84 |
-
logger.info("Successfully connected to Redis
|
|
|
|
| 85 |
|
| 86 |
except Exception as e2:
|
| 87 |
logger.error(f"Could not connect to Redis: {e2}")
|
|
|
|
| 36 |
logger.info("Redis not configured, skipping connection")
|
| 37 |
return None
|
| 38 |
|
| 39 |
+
# If SSL is explicitly disabled, try non-SSL only
|
| 40 |
+
if config.redis_disable_ssl:
|
| 41 |
+
try:
|
| 42 |
+
host, port = self._parse_host_port(config.redis_host, config.redis_port)
|
| 43 |
+
logger.info(f"Connecting to Redis at {host}:{port} (SSL disabled)")
|
| 44 |
+
|
| 45 |
+
self._redis_client = redis.Redis(
|
| 46 |
+
host=host,
|
| 47 |
+
port=port,
|
| 48 |
+
username=config.redis_username or None,
|
| 49 |
+
password=config.redis_password or None,
|
| 50 |
+
decode_responses=True,
|
| 51 |
+
socket_connect_timeout=5,
|
| 52 |
+
socket_timeout=5,
|
| 53 |
+
retry_on_timeout=True,
|
| 54 |
+
health_check_interval=30
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
self._redis_client.ping()
|
| 58 |
+
logger.info("Successfully connected to Redis without SSL")
|
| 59 |
+
return
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error(f"Non-SSL connection failed: {e}")
|
| 62 |
+
self._redis_client = None
|
| 63 |
+
return
|
| 64 |
+
|
| 65 |
+
# Try SSL connection first (default for Redis Cloud)
|
| 66 |
try:
|
|
|
|
| 67 |
host, port = self._parse_host_port(config.redis_host, config.redis_port)
|
| 68 |
+
logger.info(f"Connecting to Redis at {host}:{port} with SSL")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
self._redis_client = redis.Redis(
|
| 71 |
host=host,
|
| 72 |
port=port,
|
|
|
|
| 75 |
decode_responses=True,
|
| 76 |
socket_connect_timeout=5,
|
| 77 |
socket_timeout=5,
|
| 78 |
+
ssl=True,
|
| 79 |
+
ssl_cert_reqs=None, # Skip cert verification for Redis Cloud
|
| 80 |
retry_on_timeout=True,
|
| 81 |
health_check_interval=30
|
| 82 |
)
|
| 83 |
|
|
|
|
| 84 |
self._redis_client.ping()
|
| 85 |
+
logger.info("Successfully connected to Redis with SSL")
|
| 86 |
+
return
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
+
logger.warning(f"SSL connection failed: {e}, trying without SSL")
|
| 90 |
try:
|
|
|
|
| 91 |
host, port = self._parse_host_port(config.redis_host, config.redis_port)
|
| 92 |
|
| 93 |
self._redis_client = redis.Redis(
|
|
|
|
| 98 |
decode_responses=True,
|
| 99 |
socket_connect_timeout=5,
|
| 100 |
socket_timeout=5,
|
|
|
|
|
|
|
| 101 |
retry_on_timeout=True,
|
| 102 |
health_check_interval=30
|
| 103 |
)
|
| 104 |
|
|
|
|
| 105 |
self._redis_client.ping()
|
| 106 |
+
logger.info("Successfully connected to Redis without SSL")
|
| 107 |
+
return
|
| 108 |
|
| 109 |
except Exception as e2:
|
| 110 |
logger.error(f"Could not connect to Redis: {e2}")
|
requirements.txt
CHANGED
|
@@ -3,7 +3,7 @@ fastapi==0.95.0
|
|
| 3 |
uvicorn==0.21.1
|
| 4 |
redis==5.0.3
|
| 5 |
python-dotenv==1.0.0
|
| 6 |
-
openai
|
| 7 |
tavily-python>=0.1.0,<1.0.0
|
| 8 |
requests==2.31.0
|
| 9 |
docker==6.1.3
|
|
|
|
| 3 |
uvicorn==0.21.1
|
| 4 |
redis==5.0.3
|
| 5 |
python-dotenv==1.0.0
|
| 6 |
+
openai>=1.35.6
|
| 7 |
tavily-python>=0.1.0,<1.0.0
|
| 8 |
requests==2.31.0
|
| 9 |
docker==6.1.3
|