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

Fix async example in readme #924

Merged
merged 6 commits into from
Sep 22, 2023
Merged

Fix async example in readme #924

merged 6 commits into from
Sep 22, 2023

Conversation

hlohaus
Copy link
Collaborator

@hlohaus hlohaus commented Sep 20, 2023

Improve error handling in ChatgptLogin
Cache "snlm0e" in Bard
Fix issue: #913

Improve error handling in ChatgptLogin
Fix async example in readme
Add create_async function in ChatCompletion
Use SelectorEventLoop on windows
@@ -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:
Copy link

@adettori adettori Sep 20, 2023

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed

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()

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

Copy link
Collaborator Author

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.

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


# Force use selector event loop on windows
loop = asyncio.SelectorEventLoop()
loop = get_new_event_loop()

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes

Copy link

@adettori adettori left a 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):

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.

Copy link
Collaborator Author

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".

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!

@xtekky
Copy link
Owner

xtekky commented Sep 22, 2023

Looks very good !, nice job.

@xtekky xtekky merged commit ba287e8 into xtekky:main Sep 22, 2023
@johnd0e
Copy link
Contributor

johnd0e commented Sep 24, 2023

  File "D:\AI\gpt4free\g4f\Provider\ChatgptLogin.py", line 59, in create_async
    raise RuntimeError(data["msg"])
RuntimeError: Your account is not active, please check your billing details on our website.

@hlohaus
Copy link
Collaborator Author

hlohaus commented Sep 25, 2023

ChatgptLogin is currently not working. The website too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants