mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Initialize google.genai.Client in the executor (#141432)
* Intialize the client on an executor thread * Fix MyPy error * MyPy error * Exception error * Fix ruff * Update __init__.py --------- Co-authored-by: tronikos <tronikos@users.noreply.github.com>
This commit is contained in:
parent
b1ee5a76e1
commit
f4c0eb4189
@ -5,7 +5,7 @@ from __future__ import annotations
|
|||||||
import mimetypes
|
import mimetypes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from google import genai # type: ignore[attr-defined]
|
from google.genai import Client
|
||||||
from google.genai.errors import APIError, ClientError
|
from google.genai.errors import APIError, ClientError
|
||||||
from requests.exceptions import Timeout
|
from requests.exceptions import Timeout
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -43,7 +43,7 @@ CONF_FILENAMES = "filenames"
|
|||||||
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
||||||
PLATFORMS = (Platform.CONVERSATION,)
|
PLATFORMS = (Platform.CONVERSATION,)
|
||||||
|
|
||||||
type GoogleGenerativeAIConfigEntry = ConfigEntry[genai.Client]
|
type GoogleGenerativeAIConfigEntry = ConfigEntry[Client]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
@ -139,7 +139,11 @@ async def async_setup_entry(
|
|||||||
"""Set up Google Generative AI Conversation from a config entry."""
|
"""Set up Google Generative AI Conversation from a config entry."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
client = genai.Client(api_key=entry.data[CONF_API_KEY])
|
|
||||||
|
def _init_client() -> Client:
|
||||||
|
return Client(api_key=entry.data[CONF_API_KEY])
|
||||||
|
|
||||||
|
client = await hass.async_add_executor_job(_init_client)
|
||||||
await client.aio.models.get(
|
await client.aio.models.get(
|
||||||
model=entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL),
|
model=entry.options.get(CONF_CHAT_MODEL, RECOMMENDED_CHAT_MODEL),
|
||||||
config={"http_options": {"timeout": TIMEOUT_MILLIS}},
|
config={"http_options": {"timeout": TIMEOUT_MILLIS}},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user