Async
Important Chat
methods such as .chat()
, .stream()
, etc., are synchronous, but are also available in an asynchronous form. Most important amongst these is .stream_async()
– the recommended way to stream in a production environment where multiple users may be streaming at the same time.
The next article uses .stream_async()
to build performant chatbot apps, but below is a more minimal example of how to it works. Note that, in order to use async methods, you need to await
the result inside an async
function.
import asyncio
import chatlas as ctl
= ctl.ChatOpenAI(
chat ="gpt-4o-mini",
model="You are a helpful assistant.",
system_prompt
)
async def do_stream(prompt: str):
= await chat.stream_async(prompt)
stream async for chunk in stream:
print(chunk)
"My name is Chatlas.")) asyncio.run(do_stream(
Hello
Chatlas.
How
can
Ihelp?