-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Fixes #140182 by checking file status before sending the prompt. #144131
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ | |
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
import mimetypes | ||
from pathlib import Path | ||
|
||
from google.genai import Client | ||
from google.genai.errors import APIError, ClientError | ||
from google.genai.types import File, FileState | ||
from requests.exceptions import Timeout | ||
import voluptuous as vol | ||
|
||
|
@@ -32,6 +34,8 @@ | |
CONF_CHAT_MODEL, | ||
CONF_PROMPT, | ||
DOMAIN, | ||
FILE_POLLING_INTERVAL_SECONDS, | ||
LOGGER, | ||
RECOMMENDED_CHAT_MODEL, | ||
TIMEOUT_MILLIS, | ||
) | ||
|
@@ -91,8 +95,40 @@ def append_files_to_prompt(): | |
) | ||
prompt_parts.append(uploaded_file) | ||
|
||
async def wait_for_file_processing(uploaded_file: File) -> None: | ||
"""Wait for file processing to complete.""" | ||
while True: | ||
uploaded_file = await client.aio.files.get( | ||
name=uploaded_file.name, | ||
config={"http_options": {"timeout": TIMEOUT_MILLIS}}, | ||
) | ||
if uploaded_file.state not in ( | ||
FileState.STATE_UNSPECIFIED, | ||
FileState.PROCESSING, | ||
): | ||
break | ||
LOGGER.debug( | ||
"Waiting for file `%s` to be processed, current state: %s", | ||
uploaded_file.name, | ||
uploaded_file.state, | ||
) | ||
await asyncio.sleep(FILE_POLLING_INTERVAL_SECONDS) | ||
|
||
if uploaded_file.state == FileState.FAILED: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could be here if PROCESSING or STATE_UNSPECIFIED. Shouldn't we raise an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't no? the loop above only breaks if it state is not UNSPECIFIED or PROCESSING, ergo ACTIVE or FAILED. It can also break if the timeout is reached, however that would be handled on line 129, and an unhandled Timeout Exception would be thrown. |
||
raise HomeAssistantError( | ||
f"File `{uploaded_file.name}` processing failed, reason: {uploaded_file.error.message}" | ||
) | ||
|
||
await hass.async_add_executor_job(append_files_to_prompt) | ||
|
||
tasks = [ | ||
asyncio.create_task(wait_for_file_processing(part)) | ||
for part in prompt_parts | ||
if isinstance(part, File) and part.state != FileState.ACTIVE | ||
] | ||
async with asyncio.timeout(TIMEOUT_MILLIS / 1000): | ||
await asyncio.gather(*tasks) | ||
|
||
try: | ||
response = await client.aio.models.generate_content( | ||
model=RECOMMENDED_CHAT_MODEL, contents=prompt_parts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ | |
RECOMMENDED_USE_GOOGLE_SEARCH_TOOL = False | ||
|
||
TIMEOUT_MILLIS = 10000 | ||
FILE_POLLING_INTERVAL_SECONDS = 0.05 |
Uh oh!
There was an error while loading. Please reload this page.