_providers.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import sys
  2. from pathlib import Path
  3. from colorama import Fore, Style
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. from g4f import Provider, ProviderType, models
  6. from g4f.Provider import __providers__
  7. def main():
  8. providers = get_providers()
  9. failed_providers = []
  10. for provider in providers:
  11. if provider.needs_auth:
  12. continue
  13. print("Provider:", provider.__name__)
  14. result = test(provider)
  15. print("Result:", result)
  16. if provider.working and not result:
  17. failed_providers.append(provider)
  18. print()
  19. if failed_providers:
  20. print(f"{Fore.RED + Style.BRIGHT}Failed providers:{Style.RESET_ALL}")
  21. for _provider in failed_providers:
  22. print(f"{Fore.RED}{_provider.__name__}")
  23. else:
  24. print(f"{Fore.GREEN + Style.BRIGHT}All providers are working")
  25. def get_providers() -> list[ProviderType]:
  26. return [
  27. provider
  28. for provider in __providers__
  29. if provider.__name__ not in dir(Provider.deprecated)
  30. and provider.url is not None
  31. ]
  32. def create_response(provider: ProviderType) -> str:
  33. response = provider.create_completion(
  34. model=models.default.name,
  35. messages=[{"role": "user", "content": "Hello, who are you? Answer in detail much as possible."}],
  36. stream=False,
  37. )
  38. return "".join(response)
  39. def test(provider: ProviderType) -> bool:
  40. try:
  41. response = create_response(provider)
  42. assert type(response) is str
  43. assert len(response) > 0
  44. return response
  45. except Exception:
  46. return False
  47. if __name__ == "__main__":
  48. main()