From 82bd6f91808a383781807262c4ae1f3de9740531 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Wed, 20 Sep 2023 06:12:34 +0200 Subject: Cache "snlm0e" in Bard Improve error handling in ChatgptLogin Fix async example in readme --- README.md | 65 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index bf15f3a6..dc148e7d 100644 --- a/README.md +++ b/README.md @@ -238,43 +238,42 @@ response = g4f.ChatCompletion.create( ##### Async Support: -To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution. +To enhance speed and overall performance, execute providers asynchronously. +The total execution time will be determined by the duration of the slowest provider's execution. ```py import g4f, asyncio -async def run_async(): - _providers = [ - g4f.Provider.AItianhu, - g4f.Provider.Acytoo, - g4f.Provider.Aichat, - g4f.Provider.Ails, - g4f.Provider.Aivvm, - g4f.Provider.ChatBase, - g4f.Provider.ChatgptAi, - g4f.Provider.ChatgptLogin, - g4f.Provider.CodeLinkAva, - g4f.Provider.DeepAi, - g4f.Provider.Opchatgpts, - g4f.Provider.Vercel, - g4f.Provider.Vitalentum, - g4f.Provider.Wewordle, - g4f.Provider.Ylokh, - g4f.Provider.You, - g4f.Provider.Yqcloud, - ] - responses = [ - provider.create_async( - model=g4f.models.default, - messages=[{"role": "user", "content": "Hello"}], - ) - for provider in _providers - ] - responses = await asyncio.gather(*responses) - for idx, provider in enumerate(_providers): - print(f"{provider.__name__}:", responses[idx]) - -asyncio.run(run_async()) +_providers = [ + g4f.Provider.Aichat, + g4f.Provider.Aivvm, + g4f.Provider.ChatBase, + g4f.Provider.Bing, + g4f.Provider.CodeLinkAva, + g4f.Provider.DeepAi, + g4f.Provider.GptGo, + g4f.Provider.Wewordle, + g4f.Provider.You, + g4f.Provider.Yqcloud, +] + +async def run_provider(provider: g4f.Provider.AsyncProvider): + try: + response = await provider.create_async( + model=g4f.models.default.name, + messages=[{"role": "user", "content": "Hello"}], + ) + print(f"{provider.__name__}:", response) + except Exception as e: + print(f"{provider.__name__}:", e) + +async def run_all(): + calls = [ + run_provider(provider) for provider in _providers + ] + await asyncio.gather(*calls) + +asyncio.run(run_all()) ``` ### interference openai-proxy api (use with openai python package) -- cgit v1.2.3