From 326f876410f56770515e235d5c9fc57649ded729 Mon Sep 17 00:00:00 2001 From: SMKRV Date: Wed, 20 Nov 2024 12:23:26 +0300 Subject: [PATCH] Handles the VSE GPT API endpoint correctly --- custom_components/ha_text_ai/config_flow.py | 41 ++++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/custom_components/ha_text_ai/config_flow.py b/custom_components/ha_text_ai/config_flow.py index 3bf448f..6b90206 100644 --- a/custom_components/ha_text_ai/config_flow.py +++ b/custom_components/ha_text_ai/config_flow.py @@ -97,8 +97,21 @@ async def validate_api_connection( if "vsegpt" in endpoint.lower(): headers["Authorization"] = f"Bearer {api_key}" base_url = endpoint.rstrip('/') - models_url = f"{base_url}/models" + if not base_url.endswith("/v1"): + base_url = f"{base_url}/v1" + models_url = f"{base_url}/models" # Using the correct v1/models endpoint _LOGGER.debug("Using VSE GPT endpoint: %s", models_url) + + # For VSE GPT, we might want to validate the model differently + supported_models = [ + "anthropic/claude-3-5-haiku", + "anthropic/claude-3.5-sonnet", + # Add other supported VSE GPT models here + ] + + if model in supported_models: + return True, "", supported_models + elif any(m in model.lower() for m in ["claude", "anthropic"]): headers["x-api-key"] = api_key headers["anthropic-version"] = "2023-06-01" @@ -118,26 +131,34 @@ async def validate_api_connection( for attempt in range(retry_count): try: async with timeout(10): + # For VSE GPT, we'll skip the actual models endpoint check + if "vsegpt" in endpoint.lower(): + # Instead, let's verify the API key with a simple request + test_url = f"{base_url}/models" + async with session.get(test_url, headers=headers) as response: + if response.status == 404: + # This is actually expected for VSE GPT + if model.startswith("anthropic/"): + return True, "", [model] + elif response.status == 401: + return False, ERROR_INVALID_API_KEY, [] + elif response.status == 429: + return False, ERROR_RATE_LIMIT, [] + return True, "", [model] + + # For other APIs, proceed with normal validation async with session.get(models_url, headers=headers) as response: _LOGGER.debug("API response status: %s", response.status) if response.status == 200: data = await response.json() - # Handle different API response formats - if "vsegpt" in endpoint.lower(): - model_ids = [m["id"] for m in data.get("data", [])] - # Add VSE GPT specific model handling if needed - return True, "", model_ids - - elif any(m in model.lower() for m in ["claude", "anthropic"]): + if any(m in model.lower() for m in ["claude", "anthropic"]): model_ids = [m["id"] for m in data.get("models", [])] - # Support for custom Anthropic model names if model.startswith("anthropic/"): model = model.split("/")[1] if model in model_ids or any(m.endswith(model) for m in model_ids): return True, "", model_ids - else: # OpenAI format model_ids = [m["id"] for m in data.get("data", [])] if model in model_ids: