-
-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Fix async example in readme #924
Conversation
Improve error handling in ChatgptLogin Fix async example in readme
Add create_async function in ChatCompletion Use SelectorEventLoop on windows
g4f/Provider/base_provider.py
Outdated
@@ -100,6 +108,11 @@ def create_async_generator( | |||
) -> AsyncGenerator: | |||
raise NotImplementedError() | |||
|
|||
# Don't create a new loop in a running loop | |||
def check_running_loop(): | |||
if asyncio.events._get_running_loop() is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the check, but you should be able to just use asyncio.get_running_loop() and catch it's exception if there isn't one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed
g4f/Provider/base_provider.py
Outdated
if asyncio.events._get_running_loop() is not None: | ||
raise RuntimeError( | ||
'Use "create_async" instead of "create" function in a async loop.') | ||
|
||
# Force use selector event loop on windows | ||
return asyncio.SelectorEventLoop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried something like
if sys.platform.startswith('win32'): asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
and use asyncio.run() in create() instead? Seems like it would abstract the platform better that way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If i use set_event_loop_policy, i change the loop method everywhere and not only in g4f?
Linux use the SelectorEventLoop by default too. There is a switch in the import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, didn't think of that
g4f/Provider/base_provider.py
Outdated
|
||
# Force use selector event loop on windows | ||
loop = asyncio.SelectorEventLoop() | ||
loop = get_new_event_loop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean create_event_loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are trying to do very similar things in our commits. Both are fine imo, I left some comments in your code, let me know what you think
g4f/__init__.py
Outdated
|
||
model, provider = get_model_and_provider(model, provider, False) | ||
|
||
if not issubclass(provider, AsyncProvider): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can create providers that inherit directly from BaseProvider too here, just need to handle the case.
Ofc you are more familiar with the codebase, so if you are planning to dismiss the use of purely synchronous providers, the change makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we use BaseProvider here, it will still executed synchronous like in create.
I changed already all working providers to "AsyncProvider".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for clarifying, all good then!
Add PerplexityAi Provider
Looks very good !, nice job. |
|
ChatgptLogin is currently not working. The website too |
Improve error handling in ChatgptLogin
Cache "snlm0e" in Bard
Fix issue: #913