In Step 1, your app echoed messages back. Now let's connect a real AI model, stream each word live as it is generated, and remember earlier messages in the conversation.
What's new in Step 2: aiui.stream_token(), @aiui.on_chat_start, and per-chat memory with aiui.user_session.
aiui.stream_token()
@aiui.on_chat_start
aiui.user_session
<Steps>
<Step title="Install a model client and set your API key">
Install the OpenAI Python SDK (or any provider library you prefer — PraisonAIUI works with every model SDK):
Create a .env file in your project folder (do not commit this file to Git):
.env
aiui run automatically loads .env from your current folder.
aiui run
</Step>
<Step title="Stream tokens and keep chat memory">
Update app.py so every chat initializes its own message history and streams replies word by word:
app.py
<Step title="Run and try a follow-up question">
Ask *"Give me 3 ideas for a weekend project"*, then follow up with *"Expand on idea #2"*. Because aiui.user_session is isolated per chat, the assistant remembers the context and clicking Stop cleanly cancels a slow generation.
</Steps>
<Tip>
Prefer zero code? With pip install "aiui[praisonai]", a 3-line YAML file (name, instructions, model: gpt-4o-mini) run via aiui run chat.yaml streams replies and manages memory automatically.
pip install "aiui[praisonai]"
name
instructions
model: gpt-4o-mini
aiui run chat.yaml
</Tip>
await aiui.stream_token(text)
<CardGroup cols={2}>
<Card title="Step 3: Welcome, Starters & Profiles" icon="compass" href="03-welcome-and-starters.md">
Greet visitors, add one-click starter prompts and let users pick an assistant persona.
</Card>
<Card title="Step 1: Your First Chat App" icon="arrow-left" href="01-first-chat.md">
Review Step 1.
</CardGroup>