generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

Asynchronous file upload (`google.generativeai.upload_file`) and other blocking methods?

Open zavocc opened this issue 1 year ago • 4 comments

Description of the feature request:

Currently google.generativeai.upload_file is a blocking function, it would be nice if there are async versions of these methods along with other I/O related methods like get_file and delete_file though upload_file can be made asynchronous if uploading lets say 100mb of file.

What problem are you trying to solve with this feature?

Adding asynchronous versions of upload file methods is good in applications where it is async-driven (e.g. Discord bots, web) which currently using this to utilize multimodal features can potentially block the entire script which is not good for such applications

Any other information you'd like to share?

Right now I'm using something like asyncio.to_thread to make it asynchronous, but offering a native async methods would be nice

Right now there are only two async methods with this SDK are generate_content and send_message Although it would also be a good idea if adding async variant to SDK as well

zavocc avatar Sep 21 '24 13:09 zavocc

asyncio.to_thread, interesting. That's new to me.

The implementation of that upload function is kind of messy (see https://github.com/google-gemini/generative-ai-python/issues/327). When I was writing it there just didn't seem to be a clear way to simply make an async implementation.

Am I reading this right? (Once 327 is fixed) we can make the implementation for upload_file_async just:

async def upload_file_async(*args, **kwargs)
  return await asyncio.to_thread(blocking_function, *args, **kwargs)

MarkDaoust avatar Oct 02 '24 23:10 MarkDaoust

Either https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor or https://docs.python.org/3/library/asyncio-task.html#asyncio.to_thread can be used... Both makes methods blocking, but ngl the python SDK is quite messy to deal with and I'm not entirely sure if this is the correct way/workaround for making it async but it works

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Mark Daoust @.> Sent: Thursday, October 3, 2024 8:00:09 AM To: google-gemini/generative-ai-python @.> Cc: WMCB Tech (marcusz) @.>; Author @.> Subject: Re: [google-gemini/generative-ai-python] Asynchronous file upload (google.generativeai.upload_file) and other blocking methods? (Issue #564)

asyncio.to_thread, interesting. That's new to me.

The implementation of that upload function is kind of messy (see #327https://github.com/google-gemini/generative-ai-python/issues/327). When I was writing it there just didn't seem to be a clear way to simply make an async implementation.

Am I reading this right? (Once 327 is fixed) we can make the implementation for upload_file_async just:

async def upload_file_async(*args, **kwargs) return await asyncio.to_thread(blocking_function, *args, **kwargs)

— Reply to this email directly, view it on GitHubhttps://github.com/google-gemini/generative-ai-python/issues/564#issuecomment-2390047543, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A4QQBQM3OSFCXFIKOZXWRFLZZSCITAVCNFSM6AAAAABOTSP2SKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJQGA2DONJUGM. You are receiving this because you authored the thread.Message ID: @.***>

zavocc avatar Oct 03 '24 00:10 zavocc

https://github.com/google-gemini/generative-ai-python/pull/586

MarkDaoust avatar Oct 29 '24 23:10 MarkDaoust

I occasionally get the following error when using asyncio.to_thread I tried hard to diagnose what exactly is triggering this but in vain, it sometimes works and sometimes breaks for the same image. Eventually mitigated it with a retry mechanism. Posting this because it might be related @MarkDaoust

 Encountered error: 503 IOCP/Socket: Connection reset (An existing connection was forcibly closed by the remote host.

 -- 10054

gokturkDev avatar Nov 06 '24 16:11 gokturkDev