text_completions_streaming.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import asyncio
  2. from g4f.client import Client, AsyncClient
  3. question = """
  4. Hey! How can I recursively list all files in a directory in Python?
  5. """
  6. # Synchronous streaming function
  7. def sync_stream():
  8. client = Client()
  9. stream = client.chat.completions.create(
  10. model="gpt-4",
  11. messages=[
  12. {"role": "user", "content": question}
  13. ],
  14. stream=True,
  15. )
  16. for chunk in stream:
  17. if chunk.choices[0].delta.content:
  18. print(chunk.choices[0].delta.content or "", end="")
  19. # Asynchronous streaming function
  20. async def async_stream():
  21. client = AsyncClient()
  22. stream = client.chat.completions.create(
  23. model="gpt-4",
  24. messages=[
  25. {"role": "user", "content": question}
  26. ],
  27. stream=True,
  28. )
  29. async for chunk in stream:
  30. if chunk.choices and chunk.choices[0].delta.content:
  31. print(chunk.choices[0].delta.content, end="")
  32. # Main function to run both streams
  33. def main():
  34. print("Synchronous Stream:")
  35. sync_stream()
  36. print("\n\nAsynchronous Stream:")
  37. asyncio.run(async_stream())
  38. if __name__ == "__main__":
  39. try:
  40. main()
  41. except Exception as e:
  42. print(f"An error occurred: {str(e)}")