mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 10:40:43 -08:00
25 lines
720 B
Python
Executable file
25 lines
720 B
Python
Executable file
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())
|