diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-08-25 18:16:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-25 18:16:25 +0200 |
commit | d765551b97b671b7b532371b31f2da1dc22dedc1 (patch) | |
tree | 339f0a91136f6611c186f5730d3c4e58477df25e /testing | |
parent | ~ | Merge pull request #829 (diff) | |
parent | Add OpenaiChat and Hugchat Provider (diff) | |
download | gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar.gz gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar.bz2 gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar.lz gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar.xz gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.tar.zst gpt4free-d765551b97b671b7b532371b31f2da1dc22dedc1.zip |
Diffstat (limited to 'testing')
-rw-r--r-- | testing/test_needs_auth.py | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/testing/test_needs_auth.py b/testing/test_needs_auth.py new file mode 100644 index 00000000..d44ed1df --- /dev/null +++ b/testing/test_needs_auth.py @@ -0,0 +1,95 @@ +import sys +from pathlib import Path +import asyncio +from time import time + +sys.path.append(str(Path(__file__).parent.parent)) + +import g4f + +providers = [g4f.Provider.OpenaiChat, g4f.Provider.Bard, g4f.Provider.Bing] + +# Async support +async def log_time_async(method: callable, **kwargs): + start = time() + result = await method(**kwargs) + secs = f"{round(time() - start, 2)} secs" + if result: + return " ".join([result, secs]) + return secs + +def log_time_yield(method: callable, **kwargs): + start = time() + result = yield from method(**kwargs) + yield f" {round(time() - start, 2)} secs" + +def log_time(method: callable, **kwargs): + start = time() + result = method(**kwargs) + secs = f"{round(time() - start, 2)} secs" + if result: + return " ".join([result, secs]) + return secs + +async def run_async(): + responses = [] + for provider in providers: + responses.append(log_time_async( + provider.create_async, + model=None, + messages=[{"role": "user", "content": "Hello"}], + log_time=True + )) + responses = await asyncio.gather(*responses) + for idx, provider in enumerate(providers): + print(f"{provider.__name__}:", responses[idx]) +print("Async Total:", asyncio.run(log_time_async(run_async))) + +# Streaming support: +def run_stream(): + for provider in providers: + print(f"{provider.__name__}: ", end="") + for response in log_time_yield( + provider.create_completion, + model=None, + messages=[{"role": "user", "content": "Hello"}], + ): + print(response, end="") + print() +print("Stream Total:", log_time(run_stream)) + +# No streaming support: +def create_completion(): + for provider in providers: + print(f"{provider.__name__}:", end=" ") + for response in log_time_yield( + g4f.Provider.Bard.create_completion, + model=None, + messages=[{"role": "user", "content": "Hello"}], + ): + print(response, end="") + print() +print("No Stream Total:", log_time(create_completion)) + +for response in g4f.Provider.Hugchat.create_completion( + model=None, + messages=[{"role": "user", "content": "Hello, tell about you."}], +): + print("Hugchat:", response) + +""" +OpenaiChat: Hello! How can I assist you today? 2.0 secs +Bard: Hello! How can I help you today? 3.44 secs +Bing: Hello, this is Bing. How can I help? 😊 4.14 secs +Async Total: 4.25 secs + +OpenaiChat: Hello! How can I assist you today? 1.85 secs +Bard: Hello! How can I help you today? 3.38 secs +Bing: Hello, this is Bing. How can I help? 😊 6.14 secs +Stream Total: 11.37 secs + +OpenaiChat: Hello! How can I help you today? 3.28 secs +Bard: Hello there! How can I help you today? 3.58 secs +Bing: Hello! How can I help you today? 3.28 secs +No Stream Total: 10.14 secs +"""
\ No newline at end of file |