From 149ec16d57778b73797599f2b8bcdccabe36883b Mon Sep 17 00:00:00 2001 From: SMKRV Date: Thu, 21 Nov 2024 13:56:22 +0300 Subject: [PATCH] Release v2.0.0 --- .gitignore | 37 +++++++++++-- custom_components/ha_text_ai/__init__.py | 4 +- custom_components/ha_text_ai/coordinator.py | 59 ++++++++++++++++++--- custom_components/ha_text_ai/services.yaml | 45 ++++++++-------- 4 files changed, 108 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 77eb614..0b3c370 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,37 @@ +# Python __pycache__/ *.py[cod] *$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# Home Assistant +.storage +.cloud +.google.token + +# IDE +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS .DS_Store -.env -.venv -venv/ -ENV/ +Thumbs.db diff --git a/custom_components/ha_text_ai/__init__.py b/custom_components/ha_text_ai/__init__.py index 29bd5b1..19d0933 100644 --- a/custom_components/ha_text_ai/__init__.py +++ b/custom_components/ha_text_ai/__init__.py @@ -204,8 +204,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: try: await coordinator.async_ask_question(question, **request_params) - except Exception as err: - _LOGGER.error("Error asking question: %s", str(err)) + except Exception as err: + _LOGGER.error("Error asking question: %s", str(err)) async def async_clear_history(call: ServiceCall) -> None: """Handle the clear_history service call.""" diff --git a/custom_components/ha_text_ai/coordinator.py b/custom_components/ha_text_ai/coordinator.py index 201e2a3..79cdf88 100644 --- a/custom_components/ha_text_ai/coordinator.py +++ b/custom_components/ha_text_ai/coordinator.py @@ -8,9 +8,11 @@ import ssl import certifi import aiohttp +from homeassistant.helpers import aiohttp_client from openai import AsyncOpenAI, APIError, AuthenticationError, RateLimitError from anthropic import AsyncAnthropic from homeassistant.core import HomeAssistant +from homeassistant.helpers import aiohttp_client from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from homeassistant.util import dt as dt_util import async_timeout @@ -28,7 +30,44 @@ _LOGGER = logging.getLogger(__name__) class HATextAICoordinator(DataUpdateCoordinator): """Class to manage fetching data from the API.""" - + def __init__( + self, + hass: HomeAssistant, + api_key: str, + endpoint: str, + model: str, + temperature: float, + max_tokens: int, + request_interval: float, + session: Optional[Any] = None, + is_anthropic: bool = False, + ) -> None: + """Initialize coordinator.""" + super().__init__( + hass, + _LOGGER, + name=DOMAIN, + update_interval=timedelta(seconds=request_interval), + ) + def __init__( + self, + hass: HomeAssistant, + api_key: str, + endpoint: str, + model: str, + temperature: float, + max_tokens: int, + request_interval: float, + session: Optional[Any] = None, + is_anthropic: bool = False, + ) -> None: + """Initialize coordinator.""" + super().__init__( + hass, + _LOGGER, + name=DOMAIN, + update_interval=timedelta(seconds=request_interval), + ) def __init__( self, hass: HomeAssistant, @@ -394,13 +433,17 @@ class HATextAICoordinator(DataUpdateCoordinator): def _update_final_metrics(self) -> None: """Update final metrics before shutdown.""" - if self._request_count > 0: - self._performance_metrics["final_success_rate"] = ( - (self._request_count - self._performance_metrics["total_errors"]) / - self._request_count * 100 - ) - self._performance_metrics["total_requests"] = self._request_count - self._performance_metrics["total_tokens"] = self._tokens_used + self._performance_metrics["final_success_rate"] = ( + (self._request_count - self._performance_metrics["total_errors"]) / + self._request_count * 100 if self._request_count > 0 else 0 + ) + self._performance_metrics["total_requests"] = self._request_count + self._performance_metrics["total_tokens"] = self._tokens_used + + async def async_initialize(self) -> None: + """Initialize coordinator.""" + self._is_ready = True + await self.async_refresh() @property def performance_metrics(self) -> Dict[str, Any]: diff --git a/custom_components/ha_text_ai/services.yaml b/custom_components/ha_text_ai/services.yaml index b7baf01..1953833 100644 --- a/custom_components/ha_text_ai/services.yaml +++ b/custom_components/ha_text_ai/services.yaml @@ -28,36 +28,35 @@ ask_question: model: name: Model - description: >- - Select an AI model to use (optional, overrides default setting). - Different models have different capabilities, context limits, and response characteristics: - - GPT-3.5 Turbo: Fast, efficient, good for most tasks - - GPT-4: More capable, better reasoning, slower - - Claude-3: Advanced capabilities, longer context - Note: More capable models may have longer response times and higher API costs. + description: "Select an AI model to use (optional, overrides default setting)." required: false - example: "gpt-3.5-turbo" - default: "gpt-3.5-turbo" + example: "gpt-4o-mini" + default: "gpt-4o-mini" selector: select: custom_value: true options: - - label: "GPT-3.5 Turbo (Fast & Efficient)" - value: "gpt-3.5-turbo" - - label: "GPT-3.5 Turbo 16K (Extended)" - value: "gpt-3.5-turbo-16k" - - label: "GPT-4 (Most Capable)" - value: "gpt-4" - - label: "GPT-4 32K (Extended Context)" - value: "gpt-4-32k" - - label: "GPT-4 Turbo (Latest)" - value: "gpt-4-1106-preview" - - label: "Claude-3 Sonnet (Balanced)" - value: "claude-3-sonnet" - - label: "Claude-3 Opus (Most Advanced)" - value: "claude-3-opus" + - label: "o1-preview" + value: "o1-preview" + - label: "o1-mini" + value: "o1-mini" + - label: "gpt-4o-mini" + value: "gpt-4o-mini" + - label: "gpt-4o" + value: "gpt-4o" + - label: "claude-3-5-haiku" + value: "claude-3-5-haiku" + - label: "claude-3.5-sonnet" + value: "claude-3.5-sonnet" + - label: "claude-3-haiku" + value: "claude-3-haiku" + - label: "anthropic/claude-3-5-haiku" + value: "anthropic/claude-3-5-haiku" + - label: "anthropic/claude-3.5-sonnet" + value: "anthropic/claude-3.5-sonnet" mode: dropdown + temperature: name: Temperature description: >-