readme_table.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import re
  2. from urllib.parse import urlparse
  3. import asyncio
  4. from g4f import models, ChatCompletion
  5. from g4f.providers.types import BaseRetryProvider, ProviderType
  6. from etc.testing._providers import get_providers
  7. from g4f import debug
  8. debug.logging = True
  9. async def test_async(provider: ProviderType):
  10. if not provider.working:
  11. return False
  12. messages = [{"role": "user", "content": "Hello Assistant!"}]
  13. try:
  14. if "webdriver" in provider.get_parameters():
  15. return False
  16. response = await asyncio.wait_for(ChatCompletion.create_async(
  17. model=models.default,
  18. messages=messages,
  19. provider=provider
  20. ), 30)
  21. return bool(response)
  22. except Exception as e:
  23. if debug.logging:
  24. print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
  25. return False
  26. def test_async_list(providers: list[ProviderType]):
  27. responses: list = [
  28. asyncio.run(test_async(_provider))
  29. for _provider in providers
  30. ]
  31. return responses
  32. def print_providers():
  33. providers = get_providers()
  34. responses = test_async_list(providers)
  35. for type in ("GPT-4", "GPT-3.5", "Other"):
  36. lines = [
  37. "",
  38. f"### {type}",
  39. "",
  40. "| Website | Provider | GPT-3.5 | GPT-4 | Stream | Status | Auth |",
  41. "| ------ | ------- | ------- | ----- | ------ | ------ | ---- |",
  42. ]
  43. for is_working in (True, False):
  44. for idx, _provider in enumerate(providers):
  45. if is_working != _provider.working:
  46. continue
  47. do_continue = False
  48. if type == "GPT-4" and _provider.supports_gpt_4:
  49. do_continue = True
  50. elif type == "GPT-3.5" and not _provider.supports_gpt_4 and _provider.supports_gpt_35_turbo:
  51. do_continue = True
  52. elif type == "Other" and not _provider.supports_gpt_4 and not _provider.supports_gpt_35_turbo:
  53. do_continue = True
  54. if not do_continue:
  55. continue
  56. netloc = urlparse(_provider.url).netloc.replace("www.", "")
  57. website = f"[{netloc}]({_provider.url})"
  58. provider_name = f"`g4f.Provider.{_provider.__name__}`"
  59. has_gpt_35 = "✔️" if _provider.supports_gpt_35_turbo else "❌"
  60. has_gpt_4 = "✔️" if _provider.supports_gpt_4 else "❌"
  61. stream = "✔️" if _provider.supports_stream else "❌"
  62. if _provider.working:
  63. status = '![Active](https://img.shields.io/badge/Active-brightgreen)'
  64. if responses[idx]:
  65. status = '![Active](https://img.shields.io/badge/Active-brightgreen)'
  66. else:
  67. status = '![Unknown](https://img.shields.io/badge/Unknown-grey)'
  68. else:
  69. status = '![Inactive](https://img.shields.io/badge/Inactive-red)'
  70. auth = "✔️" if _provider.needs_auth else "❌"
  71. lines.append(
  72. f"| {website} | {provider_name} | {has_gpt_35} | {has_gpt_4} | {stream} | {status} | {auth} |"
  73. )
  74. print("\n".join(lines))
  75. def print_models():
  76. base_provider_names = {
  77. "google": "Google",
  78. "openai": "OpenAI",
  79. "huggingface": "Huggingface",
  80. "anthropic": "Anthropic",
  81. "inflection": "Inflection",
  82. "meta": "Meta",
  83. }
  84. provider_urls = {
  85. "google": "https://gemini.google.com/",
  86. "openai": "https://openai.com/",
  87. "huggingface": "https://huggingface.co/",
  88. "anthropic": "https://www.anthropic.com/",
  89. "inflection": "https://inflection.ai/",
  90. "meta": "https://llama.meta.com/",
  91. }
  92. lines = [
  93. "| Model | Base Provider | Provider | Website |",
  94. "| ----- | ------------- | -------- | ------- |",
  95. ]
  96. for name, model in models.ModelUtils.convert.items():
  97. if name.startswith("gpt-3.5") or name.startswith("gpt-4"):
  98. if name not in ("gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"):
  99. continue
  100. name = re.split(r":|/", model.name)[-1]
  101. if model.base_provider not in base_provider_names:
  102. continue
  103. base_provider = base_provider_names[model.base_provider]
  104. if not isinstance(model.best_provider, BaseRetryProvider):
  105. provider_name = f"g4f.Provider.{model.best_provider.__name__}"
  106. else:
  107. provider_name = f"{len(model.best_provider.providers)}+ Providers"
  108. provider_url = provider_urls[model.base_provider]
  109. netloc = urlparse(provider_url).netloc.replace("www.", "")
  110. website = f"[{netloc}]({provider_url})"
  111. lines.append(f"| {name} | {base_provider} | {provider_name} | {website} |")
  112. print("\n".join(lines))
  113. def print_image_models():
  114. lines = [
  115. "| Label | Provider | Image Model | Vision Model | Website |",
  116. "| ----- | -------- | ----------- | ------------ | ------- |",
  117. ]
  118. from g4f.gui.server.api import Api
  119. for image_model in Api.get_image_models():
  120. provider_url = image_model["url"]
  121. netloc = urlparse(provider_url).netloc.replace("www.", "")
  122. website = f"[{netloc}]({provider_url})"
  123. label = image_model["provider"] if image_model["label"] is None else image_model["label"]
  124. if image_model["image_model"] is None:
  125. image_model["image_model"] = "❌"
  126. if image_model["vision_model"] is None:
  127. image_model["vision_model"] = "❌"
  128. lines.append(f'| {label} | `g4f.Provider.{image_model["provider"]}` | {image_model["image_model"]}| {image_model["vision_model"]} | {website} |')
  129. print("\n".join(lines))
  130. if __name__ == "__main__":
  131. #print_providers()
  132. #print("\n", "-" * 50, "\n")
  133. #print_models()
  134. print("\n", "-" * 50, "\n")
  135. print_image_models()