Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to fix Home Assistant Blocking Call warnings #221

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion custom_components/extended_openai_conversation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openai import AsyncAzureOpenAI, AsyncOpenAI
import voluptuous as vol
import yaml
import asyncio

from homeassistant.components import (
automation,
Expand Down Expand Up @@ -132,9 +133,25 @@ async def validate_authentication(
organization: str = None,
skip_authentication=False,
) -> None:
"""
Validate the authentication with OpenAI or Azure.

Parameters:
hass (HomeAssistant): The Home Assistant instance.
api_key (str): The API key for OpenAI or Azure.
base_url (str): The base URL for the API.
api_version (str): The API version to use.
organization (str): The organization ID for the API (optional).
skip_authentication (bool): If True, skip the authentication check.

Returns:
None
"""
# If skip_authentication is True, return immediately
if skip_authentication:
return

# Determine if the base URL is for Azure or OpenAI and create the appropriate client
if is_azure(base_url):
client = AsyncAzureOpenAI(
api_key=api_key,
Expand All @@ -147,7 +164,13 @@ async def validate_authentication(
api_key=api_key, base_url=base_url, organization=organization
)

await client.models.list(timeout=10)
# Define an asynchronous function that lists models with a timeout using asyncio.to_thread
async def list_models_with_timeout():
# Use asyncio.to_thread to run the blocking call in a separate thread
return await asyncio.to_thread(client.models.list, timeout=10)

# Await the execution of the list_models_with_timeout function
await list_models_with_timeout()
Comment on lines +167 to +173
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to following?

Suggested change
# Define an asynchronous function that lists models with a timeout using asyncio.to_thread
async def list_models_with_timeout():
# Use asyncio.to_thread to run the blocking call in a separate thread
return await asyncio.to_thread(client.models.list, timeout=10)
# Await the execution of the list_models_with_timeout function
await list_models_with_timeout()
await hass.async_add_executor_job(partial(client.models.list, timeout=10))

Also, importing partial is required at the top.

from functools import partial



class FunctionExecutor(ABC):
Expand Down