mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-31 03:53:57 +08:00
Main changes:
Removed the validate_endpoint function Optimized the validate_api_connection function Simplified API connection verification Preserved all error handling and retry logic Improved exception handling The integration should now correctly verify the OpenAI API connection without false endpoint_not_available errors.
This commit is contained in:
@@ -62,24 +62,6 @@ STEP_USER_DATA_SCHEMA = vol.Schema({
|
|||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|
||||||
async def validate_endpoint(endpoint: str) -> Tuple[bool, str]:
|
|
||||||
"""Validate API endpoint accessibility."""
|
|
||||||
try:
|
|
||||||
parsed_url = urlparse(endpoint)
|
|
||||||
if parsed_url.scheme not in ('http', 'https'):
|
|
||||||
return False, "invalid_endpoint_scheme"
|
|
||||||
|
|
||||||
connector = aiohttp.TCPConnector(ssl=SSL_CONTEXT)
|
|
||||||
async with timeout(5):
|
|
||||||
async with aiohttp.ClientSession(connector=connector) as session:
|
|
||||||
async with session.get(endpoint) as response:
|
|
||||||
if response.status != 200:
|
|
||||||
return False, "endpoint_not_available"
|
|
||||||
return True, ""
|
|
||||||
except Exception as e:
|
|
||||||
_LOGGER.error("Error validating endpoint: %s", str(e))
|
|
||||||
return False, "endpoint_error"
|
|
||||||
|
|
||||||
async def validate_api_connection(
|
async def validate_api_connection(
|
||||||
api_key: str,
|
api_key: str,
|
||||||
endpoint: str,
|
endpoint: str,
|
||||||
@@ -87,21 +69,13 @@ async def validate_api_connection(
|
|||||||
retry_count: int = 3,
|
retry_count: int = 3,
|
||||||
retry_delay: float = 1.0
|
retry_delay: float = 1.0
|
||||||
) -> Tuple[bool, str, list]:
|
) -> Tuple[bool, str, list]:
|
||||||
"""Validate API connection with improved retry logic."""
|
"""Validate API connection with retry logic."""
|
||||||
# Validate endpoint first
|
|
||||||
endpoint_valid, endpoint_error = await validate_endpoint(endpoint)
|
|
||||||
if not endpoint_valid:
|
|
||||||
return False, endpoint_error, []
|
|
||||||
|
|
||||||
connector = aiohttp.TCPConnector(ssl=SSL_CONTEXT)
|
|
||||||
async with aiohttp.ClientSession(connector=connector) as session:
|
|
||||||
for attempt in range(retry_count):
|
for attempt in range(retry_count):
|
||||||
try:
|
try:
|
||||||
async with timeout(10):
|
async with timeout(10):
|
||||||
client = AsyncOpenAI(
|
client = AsyncOpenAI(
|
||||||
api_key=api_key,
|
api_key=api_key,
|
||||||
base_url=endpoint,
|
base_url=endpoint,
|
||||||
http_client=session
|
|
||||||
)
|
)
|
||||||
|
|
||||||
models = await client.models.list()
|
models = await client.models.list()
|
||||||
|
|||||||
Reference in New Issue
Block a user