2023-08-25 04:41:32 +00:00
|
|
|
import sys
|
|
|
|
from pathlib import Path
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
|
|
|
|
|
|
import g4f
|
2023-08-27 23:43:45 +00:00
|
|
|
from testing.log_time import log_time, log_time_async, log_time_yield
|
2023-08-25 04:41:32 +00:00
|
|
|
|
|
|
|
|
2023-08-27 23:43:45 +00:00
|
|
|
_providers = [
|
|
|
|
g4f.Provider.H2o,
|
|
|
|
g4f.Provider.You,
|
|
|
|
g4f.Provider.HuggingChat,
|
|
|
|
g4f.Provider.OpenAssistant,
|
|
|
|
g4f.Provider.Bing,
|
|
|
|
g4f.Provider.Bard
|
|
|
|
]
|
2023-08-25 04:41:32 +00:00
|
|
|
|
2023-09-18 05:15:43 +00:00
|
|
|
_instruct = "Hello, are you GPT 4?."
|
2023-08-27 23:43:45 +00:00
|
|
|
|
|
|
|
_example = """
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2023-09-01 21:39:57 +00:00
|
|
|
print("Bing: ", end="")
|
2023-08-27 23:43:45 +00:00
|
|
|
for response in log_time_yield(
|
|
|
|
g4f.ChatCompletion.create,
|
2023-09-18 05:15:43 +00:00
|
|
|
model=g4f.models.default,
|
2023-08-27 23:43:45 +00:00
|
|
|
messages=[{"role": "user", "content": _instruct}],
|
2023-09-01 21:39:57 +00:00
|
|
|
provider=g4f.Provider.Bing,
|
2023-08-27 23:43:45 +00:00
|
|
|
#cookies=g4f.get_cookies(".huggingface.co"),
|
2023-09-18 05:15:43 +00:00
|
|
|
stream=True,
|
2023-08-27 23:43:45 +00:00
|
|
|
auth=True
|
|
|
|
):
|
2023-09-18 05:15:43 +00:00
|
|
|
print(response, end="", flush=True)
|
2023-08-27 23:43:45 +00:00
|
|
|
print()
|
|
|
|
print()
|
2023-08-25 04:41:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def run_async():
|
2023-08-27 23:43:45 +00:00
|
|
|
responses = [
|
|
|
|
log_time_async(
|
2023-08-25 04:41:32 +00:00
|
|
|
provider.create_async,
|
|
|
|
model=None,
|
2023-08-27 23:43:45 +00:00
|
|
|
messages=[{"role": "user", "content": _instruct}],
|
|
|
|
)
|
|
|
|
for provider in _providers
|
|
|
|
]
|
2023-08-25 04:41:32 +00:00
|
|
|
responses = await asyncio.gather(*responses)
|
2023-08-27 23:43:45 +00:00
|
|
|
for idx, provider in enumerate(_providers):
|
2023-08-25 04:41:32 +00:00
|
|
|
print(f"{provider.__name__}:", responses[idx])
|
|
|
|
print("Async Total:", asyncio.run(log_time_async(run_async)))
|
2023-08-27 23:43:45 +00:00
|
|
|
print()
|
|
|
|
|
2023-08-25 04:41:32 +00:00
|
|
|
|
|
|
|
def run_stream():
|
2023-08-27 23:43:45 +00:00
|
|
|
for provider in _providers:
|
2023-08-25 04:41:32 +00:00
|
|
|
print(f"{provider.__name__}: ", end="")
|
|
|
|
for response in log_time_yield(
|
|
|
|
provider.create_completion,
|
|
|
|
model=None,
|
2023-08-27 23:43:45 +00:00
|
|
|
messages=[{"role": "user", "content": _instruct}],
|
2023-08-25 04:41:32 +00:00
|
|
|
):
|
2023-09-18 05:15:43 +00:00
|
|
|
print(response, end="", flush=True)
|
2023-08-25 04:41:32 +00:00
|
|
|
print()
|
|
|
|
print("Stream Total:", log_time(run_stream))
|
2023-08-27 23:43:45 +00:00
|
|
|
print()
|
|
|
|
|
2023-08-25 04:41:32 +00:00
|
|
|
|
2023-08-27 23:43:45 +00:00
|
|
|
def create_no_stream():
|
|
|
|
for provider in _providers:
|
2023-08-25 04:41:32 +00:00
|
|
|
print(f"{provider.__name__}:", end=" ")
|
|
|
|
for response in log_time_yield(
|
2023-08-27 23:43:45 +00:00
|
|
|
provider.create_completion,
|
2023-08-25 04:41:32 +00:00
|
|
|
model=None,
|
2023-08-27 23:43:45 +00:00
|
|
|
messages=[{"role": "user", "content": _instruct}],
|
|
|
|
stream=False
|
2023-08-25 04:41:32 +00:00
|
|
|
):
|
|
|
|
print(response, end="")
|
|
|
|
print()
|
2023-08-27 23:43:45 +00:00
|
|
|
print("No Stream Total:", log_time(create_no_stream))
|
|
|
|
print()
|