我正在使用 DASH 为聊天应用程序构建一个全 Python 客户端,该应用程序由某些 ChatGPT/OpenAI 模型(和附加处理)支持。计算用户的响应很容易花费 60 秒甚至更长的时间,但协议允许增量地传输令牌(即单词),因此用户已经可以阅读回复的早期部分,而后面的部分仍在计算中。
我想知道如何用 DASH 最好地做到这一点...
感谢您的帮助!
经过一番修改,我最终使用了
background=True
和 progress=Output(....)
的回调,这似乎是为了更新进度条而设计的,但似乎对我的问题非常有效。
下面是我正在使用的一个单独的示例(使用延迟生成器模拟流响应)。
我觉得我正在滥用一项功能,因此可能有更好的方法来在 DASH 应用程序中实现令牌流,或者这种方法可能会出现迫在眉睫的问题 - 如果您有任何评论,请告诉我。
import time
import dash
from dash import html, dcc
from dash.long_callback import DiskcacheLongCallbackManager
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
## Diskcache
import diskcache
cache = diskcache.Cache("./cache")
long_callback_manager = DiskcacheLongCallbackManager(cache)
app = dash.Dash(__name__, long_callback_manager=long_callback_manager)
app.layout = html.Div([
dbc.Button("Start", id="start_button"),
dcc.Markdown(id="progress_output"),
]
)
@app.callback(
output=Output("progress_output", "children"),
inputs=[Input("start_button", "n_clicks")],
progress=Output("progress_output", "children"),
progress_default="",
background=True,
interval=100,
)
def read_message(set_progress, n_clicks):
if n_clicks is None:
raise dash.exceptions.PreventUpdate
state = ""
for chunk in read_chunk():
# append the chunk
state += chunk
# Update the progress component
set_progress(state)
# Return the final result
return state
def read_chunk():
reply = """
An h1 header
============
Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
look like:
* this one
* that one
* the other one
Note that --- not considering the asterisk --- the actual text
content starts at 4-columns in.
> Block quotes are
> written like so.
>
> They can span multiple paragraphs,
> if you like.
Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
Unicode is supported. ☺
"""
chunks = [word + " " for word in reply.split(" ")]
for chunk in chunks:
# Simulate some work
time.sleep(0.02)
yield chunk
if __name__ == "__main__":
app.run_server(debug=True)