readme_table.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import re
  2. import sys
  3. from pathlib import Path
  4. from urllib.parse import urlparse
  5. sys.path.append(str(Path(__file__).parent.parent.parent))
  6. import asyncio
  7. from g4f import models
  8. from g4f import ChatCompletion
  9. from g4f.Provider.base_provider import BaseProvider
  10. from etc.testing._providers import get_providers
  11. from g4f import debug
  12. debug.logging = True
  13. async def test_async(provider: type[BaseProvider]):
  14. if not provider.working:
  15. return False
  16. messages = [{"role": "user", "content": "Hello Assistant!"}]
  17. try:
  18. response = await asyncio.wait_for(ChatCompletion.create_async(
  19. model=models.default,
  20. messages=messages,
  21. provider=provider
  22. ), 30)
  23. return bool(response)
  24. except Exception as e:
  25. if debug.logging:
  26. print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
  27. return False
  28. async def test_async_list(providers: list[type[BaseProvider]]):
  29. responses: list = [
  30. test_async(_provider)
  31. for _provider in providers
  32. ]
  33. return await asyncio.gather(*responses)
  34. def print_providers():
  35. providers = get_providers()
  36. responses = asyncio.run(test_async_list(providers))
  37. for type in ("GPT-4", "GPT-3.5", "Other"):
  38. lines = [
  39. "",
  40. f"### {type}",
  41. "",
  42. "| Website | Provider | GPT-3.5 | GPT-4 | Stream | Status | Auth |",
  43. "| ------ | ------- | ------- | ----- | ------ | ------ | ---- |",
  44. ]
  45. for is_working in (True, False):
  46. for idx, _provider in enumerate(providers):
  47. if is_working != _provider.working:
  48. continue
  49. do_continue = False
  50. if type == "GPT-4" and _provider.supports_gpt_4:
  51. do_continue = True
  52. elif type == "GPT-3.5" and not _provider.supports_gpt_4 and _provider.supports_gpt_35_turbo:
  53. do_continue = True
  54. elif type == "Other" and not _provider.supports_gpt_4 and not _provider.supports_gpt_35_turbo:
  55. do_continue = True
  56. if not do_continue:
  57. continue
  58. netloc = urlparse(_provider.url).netloc
  59. website = f"[{netloc}]({_provider.url})"
  60. provider_name = f"`g4f.Provider.{_provider.__name__}`"
  61. has_gpt_35 = "✔️" if _provider.supports_gpt_35_turbo else "❌"
  62. has_gpt_4 = "✔️" if _provider.supports_gpt_4 else "❌"
  63. stream = "✔️" if _provider.supports_stream else "❌"
  64. if _provider.working:
  65. status = '![Active](https://img.shields.io/badge/Active-brightgreen)'
  66. if responses[idx]:
  67. status = '![Active](https://img.shields.io/badge/Active-brightgreen)'
  68. else:
  69. status = '![Unknown](https://img.shields.io/badge/Unknown-grey)'
  70. else:
  71. status = '![Inactive](https://img.shields.io/badge/Inactive-red)'
  72. auth = "✔️" if _provider.needs_auth else "❌"
  73. lines.append(
  74. f"| {website} | {provider_name} | {has_gpt_35} | {has_gpt_4} | {stream} | {status} | {auth} |"
  75. )
  76. print("\n".join(lines))
  77. def print_models():
  78. base_provider_names = {
  79. "cohere": "Cohere",
  80. "google": "Google",
  81. "openai": "OpenAI",
  82. "anthropic": "Anthropic",
  83. "replicate": "Replicate",
  84. "huggingface": "Huggingface",
  85. }
  86. provider_urls = {
  87. "Bard": "https://bard.google.com/",
  88. "H2o": "https://www.h2o.ai/",
  89. "Vercel": "https://sdk.vercel.ai/",
  90. }
  91. lines = [
  92. "| Model | Base Provider | Provider | Website |",
  93. "| ----- | ------------- | -------- | ------- |",
  94. ]
  95. _models = get_models()
  96. for model in _models:
  97. if not model.best_provider or model.best_provider.__name__ not in provider_urls:
  98. continue
  99. name = re.split(r":|/", model.name)[-1]
  100. base_provider = base_provider_names[model.base_provider]
  101. provider_name = f"g4f.provider.{model.best_provider.__name__}"
  102. provider_url = provider_urls[model.best_provider.__name__]
  103. netloc = urlparse(provider_url).netloc
  104. website = f"[{netloc}]({provider_url})"
  105. lines.append(f"| {name} | {base_provider} | {provider_name} | {website} |")
  106. print("\n".join(lines))
  107. def get_models():
  108. _models = [item[1] for item in models.__dict__.items()]
  109. _models = [model for model in _models if type(model) is models.Model]
  110. return [model for model in _models if model.name not in ["gpt-3.5-turbo", "gpt-4"]]
  111. if __name__ == "__main__":
  112. print_providers()
  113. print("\n", "-" * 50, "\n")
  114. print_models()