test_async.py 890 B

12345678910111213141516171819202122232425262728293031
  1. import sys
  2. from pathlib import Path
  3. import asyncio
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. sys.path.append(str(Path(__file__).parent.parent.parent))
  6. import g4f
  7. from testing._providers import get_providers
  8. from testing.log_time import log_time_async
  9. async def create_async(provider):
  10. try:
  11. response = await log_time_async(
  12. provider.create_async,
  13. model=g4f.models.default.name,
  14. messages=[{"role": "user", "content": "Hello, are you GPT 3.5?"}]
  15. )
  16. print(f"{provider.__name__}:", response)
  17. except Exception as e:
  18. print(f"{provider.__name__}: {e.__class__.__name__}: {e}")
  19. async def run_async():
  20. responses: list = [
  21. create_async(provider)
  22. for provider in get_providers()
  23. if provider.working
  24. ]
  25. await asyncio.gather(*responses)
  26. print("Total:", asyncio.run(log_time_async(run_async)))