-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_text_streaming_with_set_props.py
67 lines (55 loc) · 1.94 KB
/
dash_text_streaming_with_set_props.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from openai import OpenAI
import dash
from dash import dcc, html, Input, Output, State, DiskcacheManager, set_props
import dash_bootstrap_components as dbc
from uuid import uuid4
import diskcache
import time
launch_uid = uuid4()
cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache, cache_by=[lambda: launch_uid], expire=60)
client = OpenAI(
api_key = 'YOUR_API_KEY'
)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True,
background_callback_manager=background_callback_manager)
app.config.suppress_callback_exceptions = True
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
dbc.Input(id='input-prompt', type='text', placeholder='Ask something...',
style={'marginBottom': '10px'}),
dbc.Button('Send', id='submit-button', n_clicks=0, color='primary')
], width=6)
]),
dbc.Row([
dbc.Col([
html.Div(id='output-response')
], width=12)
])
], className="mt-5")
@app.callback(
Input('submit-button', 'n_clicks'),
State('input-prompt', 'value'),
background=True,
interval=10,
prevent_initial_call=True
)
def update_output(n_clicks, prompt):
if n_clicks > 0:
response = client.chat.completions.create(
model='MODEL_NAME',
messages=[{"role": "user", "content": prompt}],
stream=True
)
result = ""
for chunk in response:
if len(chunk.choices) > 0:
text = chunk.choices[0].delta.content
if text:
result += text
set_props('output-response', {'children': [dcc.Markdown(result)]})
time.sleep(1)
set_props('output-response', {'children': [dcc.Markdown(result)]})
if __name__ == '__main__':
app.run_server(debug=False)