test_chat_completion.py 677 B

123456789101112131415161718192021222324252627
  1. import sys
  2. from pathlib import Path
  3. sys.path.append(str(Path(__file__).parent.parent.parent))
  4. import g4f, asyncio
  5. print("create:", end=" ", flush=True)
  6. for response in g4f.ChatCompletion.create(
  7. model=g4f.models.default,
  8. #provider=g4f.Provider.Bing,
  9. messages=[{"role": "user", "content": "write a poem about a tree"}],
  10. stream=True
  11. ):
  12. print(response, end="", flush=True)
  13. print()
  14. async def run_async():
  15. response = await g4f.ChatCompletion.create_async(
  16. model=g4f.models.default,
  17. #provider=g4f.Provider.Bing,
  18. messages=[{"role": "user", "content": "hello!"}],
  19. )
  20. print("create_async:", response)
  21. asyncio.run(run_async())