test_needs_auth.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import sys
  2. from pathlib import Path
  3. import asyncio
  4. sys.path.append(str(Path(__file__).parent.parent))
  5. import g4f
  6. from testing.log_time import log_time, log_time_async, log_time_yield
  7. _providers = [
  8. g4f.Provider.H2o,
  9. g4f.Provider.You,
  10. g4f.Provider.HuggingChat,
  11. g4f.Provider.OpenAssistant,
  12. g4f.Provider.Bing,
  13. g4f.Provider.Bard
  14. ]
  15. _instruct = "Hello, are you GPT 4?."
  16. _example = """
  17. OpenaiChat: Hello! How can I assist you today? 2.0 secs
  18. Bard: Hello! How can I help you today? 3.44 secs
  19. Bing: Hello, this is Bing. How can I help? 😊 4.14 secs
  20. Async Total: 4.25 secs
  21. OpenaiChat: Hello! How can I assist you today? 1.85 secs
  22. Bard: Hello! How can I help you today? 3.38 secs
  23. Bing: Hello, this is Bing. How can I help? 😊 6.14 secs
  24. Stream Total: 11.37 secs
  25. OpenaiChat: Hello! How can I help you today? 3.28 secs
  26. Bard: Hello there! How can I help you today? 3.58 secs
  27. Bing: Hello! How can I help you today? 3.28 secs
  28. No Stream Total: 10.14 secs
  29. """
  30. print("Bing: ", end="")
  31. for response in log_time_yield(
  32. g4f.ChatCompletion.create,
  33. model=g4f.models.default,
  34. messages=[{"role": "user", "content": _instruct}],
  35. provider=g4f.Provider.Bing,
  36. #cookies=g4f.get_cookies(".huggingface.co"),
  37. stream=True,
  38. auth=True
  39. ):
  40. print(response, end="", flush=True)
  41. print()
  42. print()
  43. async def run_async():
  44. responses = [
  45. log_time_async(
  46. provider.create_async,
  47. model=None,
  48. messages=[{"role": "user", "content": _instruct}],
  49. )
  50. for provider in _providers
  51. ]
  52. responses = await asyncio.gather(*responses)
  53. for idx, provider in enumerate(_providers):
  54. print(f"{provider.__name__}:", responses[idx])
  55. print("Async Total:", asyncio.run(log_time_async(run_async)))
  56. print()
  57. def run_stream():
  58. for provider in _providers:
  59. print(f"{provider.__name__}: ", end="")
  60. for response in log_time_yield(
  61. provider.create_completion,
  62. model=None,
  63. messages=[{"role": "user", "content": _instruct}],
  64. ):
  65. print(response, end="", flush=True)
  66. print()
  67. print("Stream Total:", log_time(run_stream))
  68. print()
  69. def create_no_stream():
  70. for provider in _providers:
  71. print(f"{provider.__name__}:", end=" ")
  72. for response in log_time_yield(
  73. provider.create_completion,
  74. model=None,
  75. messages=[{"role": "user", "content": _instruct}],
  76. stream=False
  77. ):
  78. print(response, end="")
  79. print()
  80. print("No Stream Total:", log_time(create_no_stream))
  81. print()