example_gpt-4.py 681 B

123456789101112131415161718192021222324252627282930313233
  1. import g4f
  2. import sys
  3. # Provider selection
  4. provider=g4f.Provider.Phind
  5. # Streaming is not supported by these providers
  6. if provider in {g4f.Provider.Aws, g4f.Provider.Ora,
  7. g4f.Provider.Bard, g4f.Provider.Aichat}:
  8. stream=False
  9. else:
  10. stream=True
  11. print(provider.params) # supported args
  12. # Getting the response
  13. response = g4f.ChatCompletion.create(model='gpt-4',
  14. messages=[{"role": "user",
  15. "content": "Write a poem about a tree."}],
  16. stream=stream,
  17. provider=provider)
  18. # Printing the response
  19. if stream:
  20. for message in response:
  21. print(message, end="")
  22. sys.stdout.flush()
  23. print("\n")
  24. else:
  25. print(response)