diff options
author | Heiner Lohaus <heiner@lohaus.eu> | 2023-09-12 04:40:10 +0200 |
---|---|---|
committer | Heiner Lohaus <heiner@lohaus.eu> | 2023-09-12 04:40:10 +0200 |
commit | 618addd001077d9e4ed4d2a7aa03769a0ee0901b (patch) | |
tree | 0a1f1eb3639c34472884d8d3d7bd102b12c891ff /tool | |
parent | Update Readme (diff) | |
download | gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar.gz gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar.bz2 gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar.lz gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar.xz gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.tar.zst gpt4free-618addd001077d9e4ed4d2a7aa03769a0ee0901b.zip |
Diffstat (limited to 'tool')
-rw-r--r-- | tool/readme_table.py | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/tool/readme_table.py b/tool/readme_table.py index 3e2a6e64..9e43b0ae 100644 --- a/tool/readme_table.py +++ b/tool/readme_table.py @@ -5,9 +5,13 @@ from urllib.parse import urlparse sys.path.append(str(Path(__file__).parent.parent)) +import asyncio from g4f import models -from g4f.Provider.base_provider import AsyncProvider -from testing.test_providers import test, get_providers +from g4f.Provider.base_provider import AsyncProvider, BaseProvider +from testing.test_providers import get_providers + +logging = False + def print_imports(): print("##### Providers:") @@ -28,21 +32,50 @@ def print_async(): print("```py") print("_providers = [") for _provider in get_providers(): - if issubclass(_provider, AsyncProvider): + if _provider.working and issubclass(_provider, AsyncProvider): print(f" g4f.Provider.{_provider.__name__},") print("]") print("```") print() print() + +async def test_async(provider: type[BaseProvider]): + if not provider.working: + return False + model = models.gpt_35_turbo.name if provider.supports_gpt_35_turbo else models.default.name + messages = [{"role": "user", "content": "Hello Assistant!"}] + try: + if issubclass(provider, AsyncProvider): + response = await provider.create_async(model=model, messages=messages) + else: + response = provider.create_completion(model=model, messages=messages, stream=False) + return True if response else False + except Exception as e: + if logging: + print(f"{provider.__name__}: {e.__class__.__name__}: {e}") + return False + + +async def test_async_list(providers: list[type[BaseProvider]]): + responses: list = [ + test_async(_provider) + for _provider in providers + ] + return await asyncio.gather(*responses) + + def print_providers(): lines = [ - "| Website| Provider| gpt-3.5 | gpt-4 | Streaming | Status | Auth |", - "| ------ | ------- | ------- | ----- | --------- | ------ | ---- |", + "| Website| Provider| gpt-3.5 | gpt-4 | Streaming | Asynchron | Status | Auth |", + "| ------ | ------- | ------- | ----- | --------- | --------- | ------ | ---- |", ] + providers = get_providers() + responses = asyncio.run(test_async_list(providers)) + for is_working in (True, False): - for _provider in providers: + for idx, _provider in enumerate(providers): if is_working != _provider.working: continue netloc = urlparse(_provider.url).netloc @@ -53,8 +86,9 @@ def print_providers(): has_gpt_35 = "✔️" if _provider.supports_gpt_35_turbo else "❌" has_gpt_4 = "✔️" if _provider.supports_gpt_4 else "❌" stream = "✔️" if _provider.supports_stream else "❌" + can_async = "✔️" if issubclass(_provider, AsyncProvider) else "❌" if _provider.working: - if test(_provider): + if responses[idx]: status = '![Active](https://img.shields.io/badge/Active-brightgreen)' else: status = '![Unknown](https://img.shields.io/badge/Unknown-grey)' @@ -63,7 +97,7 @@ def print_providers(): auth = "✔️" if _provider.needs_auth else "❌" lines.append( - f"| {website} | {provider_name} | {has_gpt_35} | {has_gpt_4} | {stream} | {status} | {auth} |" + f"| {website} | {provider_name} | {has_gpt_35} | {has_gpt_4} | {stream} | {can_async} | {status} | {auth} |" ) print("\n".join(lines)) @@ -89,17 +123,16 @@ def print_models(): _models = get_models() for model in _models: - if model.best_provider.__name__ not in provider_urls: + if not model.best_provider or model.best_provider.__name__ not in provider_urls: continue - split_name = re.split(r":|/", model.name) - name = split_name[-1] + name = re.split(r":|/", model.name)[-1] base_provider = base_provider_names[model.base_provider] provider_name = f"g4f.provider.{model.best_provider.__name__}" - provider_url = provider_urls[model.best_provider.__name__] netloc = urlparse(provider_url).netloc website = f"[{netloc}]({provider_url})" + lines.append(f"| {name} | {base_provider} | {provider_name} | {website} |") print("\n".join(lines)) |