Sure! Here is a minimal example of how to stream a response.
Try asking it to write a story.
import dash
from dash import dcc, html, Input, Output, State, ctx, ALL, MATCH,clientside_callback,DiskcacheManager
import dash_bootstrap_components as dbc
import dash_mantine_components as dmc
import diskcache
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)
sys_prompt = f"You are a chatbot assistant. Today is {datetime.datetime.now().strftime('%Y-%m-%d')}"
app = dash.Dash(__name__, background_callback_manager=background_callback_manager,external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title="Streaming Simple Example App"
app.layout = dmc.Paper([
html.Div(id='new_response_temp'),
dmc.Paper(
dmc.Grid(
[
dmc.Col(
dmc.Textarea(id="text_input",placeholder="Ask me anything...",minRows=1,value='',maxRows=8,autosize=True,radius='md',persistence=True,
rightSection=dmc.Center(dmc.ActionIcon(DashIconify(icon="bi:send"),id='submit')),
style={
"overflow": "hidden",
"width": "100%",
"boxSizing": "border-box",
}),style={'width':'100%'},span=12),
]),
radius='lg',
withBorder=True,
p='xs',
style={
'backgroundColor':'#f5f7f7',
"position": "fixed",
"bottom": "10px",
"width": "60%",
"left":"50%",
'transform': 'translateX(-50%)',
"boxSizing": "border-box",
"height":"auto"
}
)
])
@app.callback(
Input("submit","n_clicks"),
State("text_input","value"),
background = True,
progress=[Output('new_response_temp','children')]
)
def response(set_progress,n_clicks,text_input):
if n_clicks:
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{"role":"system","content":sys_prompt},
{"role":"user","content":text_input},
],
temperature = 0,
)
chunks = []
for chunk in response:
if len(chunk.choices)>0:
if chunk.choices[0].delta.content is not None:
new_chunk=chunk.choices[0].delta.content.split(" ")
chunks.append(' '.join(new_chunk))
set_progress(dcc.Markdown(''.join(chunks),style={'font-size':15}))
if __name__ == "__main__":
app.run_server(debug=False)