openaichat.py 802 B

1234567891011121314151617181920212223
  1. from g4f.client import Client
  2. from g4f.Provider import OpenaiChat, RetryProvider
  3. # compatible countries: https://pastebin.com/UK0gT9cn
  4. client = Client(
  5. proxies = {
  6. 'http': 'http://username:password@host:port', # MUST BE WORKING OPENAI COUNTRY PROXY ex: USA
  7. 'https': 'http://username:password@host:port' # MUST BE WORKING OPENAI COUNTRY PROXY ex: USA
  8. },
  9. provider = RetryProvider([OpenaiChat],
  10. single_provider_retry=True, max_retries=5)
  11. )
  12. messages = [
  13. {'role': 'user', 'content': 'Hello'}
  14. ]
  15. response = client.chat.completions.create(model='gpt-3.5-turbo',
  16. messages=messages,
  17. stream=True)
  18. for message in response:
  19. print(message.choices[0].delta.content or "")