Release v2.0.0

This commit is contained in:
SMKRV
2024-11-25 02:05:13 +03:00
parent 351a8b18dd
commit 7f46380054
3 changed files with 60 additions and 101 deletions
+9 -36
View File
@@ -1,13 +1,7 @@
# api_client.py
"""API Client for HA Text AI.
This file contains the implementation of an API client for the HA Text AI integration compatible with both OpenAI and Anthropic service providers.
The client handles API requests, manages connection sessions, implements retry logic, and validates parameters for requests to ensure the correct functionality of the AI service integration within Home Assistant.
"""
"""API Client for HA Text AI."""
import logging
import asyncio
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional
from aiohttp import ClientSession, ClientTimeout
from async_timeout import timeout
@@ -26,10 +20,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
class APIClient:
"""API Client for OpenAI and Anthropic.
This client handles requests to OpenAI and Anthropic services to create text-based AI completions.
"""
"""API Client for OpenAI and Anthropic."""
def __init__(
self,
@@ -39,10 +30,7 @@ class APIClient:
api_provider: str,
model: str,
) -> None:
"""Initialize API client.
Initialize with session, endpoint URL, request headers, type of API provider (OpenAI or Anthropic), and the AI model in use.
"""
"""Initialize API client."""
self.session = session
self.endpoint = endpoint
self.headers = headers
@@ -55,10 +43,7 @@ class APIClient:
temperature: float,
max_tokens: int,
) -> None:
"""Validate API parameters.
Check that the temperature and max_tokens are within their respective allowed ranges.
"""
"""Validate API parameters."""
if not MIN_TEMPERATURE <= temperature <= MAX_TEMPERATURE:
raise ValueError(
f"Temperature must be between {MIN_TEMPERATURE} and {MAX_TEMPERATURE}"
@@ -73,10 +58,7 @@ class APIClient:
url: str,
payload: Dict[str, Any],
) -> Dict[str, Any]:
"""Make API request with retry logic.
Attempt the API request a pre-defined number of times in case of failure, with an exponential backoff strategy for timeouts.
"""
"""Make API request with retry logic."""
for attempt in range(API_RETRY_COUNT):
try:
async with timeout(API_TIMEOUT):
@@ -107,10 +89,7 @@ class APIClient:
temperature: float,
max_tokens: int,
) -> Dict[str, Any]:
"""Create completion using appropriate API.
Make a completion request using either OpenAI or Anthropic API based on the provider specified during initialization.
"""
"""Create completion using appropriate API."""
try:
self._validate_parameters(temperature, max_tokens)
@@ -133,10 +112,7 @@ class APIClient:
temperature: float,
max_tokens: int,
) -> Dict[str, Any]:
"""Create completion using OpenAI API.
Send details of the completion request to the OpenAI API and process the response.
"""
"""Create completion using OpenAI API."""
url = f"{self.endpoint}/chat/completions"
payload = {
"model": model,
@@ -168,10 +144,7 @@ class APIClient:
temperature: float,
max_tokens: int,
) -> Dict[str, Any]:
"""Create completion using Anthropic API.
Adapt messages format to Anthropic's requirement and make the API request to generate text completion.
"""
"""Create completion using Anthropic API."""
url = f"{self.endpoint}/v1/messages"
# Convert messages to Anthropic format