| import asyncio | |
| from g4f.client import AsyncClient | |
| async def main(): | |
| client = AsyncClient() | |
| stream = client.chat.completions.create( | |
| model="gpt-4", | |
| messages=[{"role": "user", "content": "Say hello there!"}], | |
| stream=True, | |
| ) | |
| accumulated_text = "" | |
| try: | |
| async for chunk in stream: | |
| if chunk.choices and chunk.choices[0].delta.content: | |
| content = chunk.choices[0].delta.content | |
| accumulated_text += content | |
| print(content, end="", flush=True) | |
| except Exception as e: | |
| print(f"\nError occurred: {e}") | |
| finally: | |
| print("\n\nFinal accumulated text:", accumulated_text) | |
| asyncio.run(main()) | |