Chainlit supports streaming with any Python code. Here is an example with openai.

Streaming OpenAI response

OpenAI Chat Completion

Code Example
from openai import AsyncOpenAI
import chainlit as cl

client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")


settings = {
    "model": "gpt-3.5-turbo",
    "temperature": 0.7,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
}


@cl.on_chat_start
def start_chat():
    cl.user_session.set(
        "message_history",
        [{"role": "system", "content": "You are a helpful assistant."}],
    )


@cl.on_message
async def main(message: cl.Message):
    message_history = cl.user_session.get("message_history")
    message_history.append({"role": "user", "content": message.content})

    msg = cl.Message(content="")
    await msg.send()

    stream = await client.chat.completions.create(
        messages=message_history, stream=True, **settings
    )

    async for part in stream:
        if token := part.choices[0].delta.content or "":
            await msg.stream_token(token)

    message_history.append({"role": "assistant", "content": msg.content})
    await msg.update()

OpenAI Completion

Code Example
from openai import AsyncOpenAI
import chainlit as cl

client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY")


settings = {
    "model": "text-davinci-003",
    "temperature": 0.7,
    "max_tokens": 500,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "stop": ["```"],
}

prompt = """Answer the following question:
{question}
"""


@cl.on_message
async def main(message: cl.Message):
    fromatted_prompt = prompt.format(question=message.content)
    msg = cl.Message(
        content="",
    )

    stream = await client.completions.create(
        prompt=fromatted_prompt,
        stream=True,
        **settings,
    )

    async for part in stream:
        if token := part.choices[0].text or "":
            await msg.stream_token(token)

    await msg.send()