gpt4 book ai didi

plotly-dash - 破折号 : Long-Running callback that streams tokens into a Markdown component

转载 作者:行者123 更新时间:2023-12-02 22:47:24 25 4
gpt4 key购买 nike

我正在使用 DASH 为聊天应用程序构建一个全 Python 客户端,该应用程序由某些 ChatGPT/OpenAI 模型(和附加处理)支持。计算用户的响应很容易需要 60 秒甚至更长的时间,但该协议(protocol)允许增量地传输 token (即单词),因此用户已经可以阅读回复的早期部分,而后面的部分仍在计算中。

我想知道如何最好地使用 DASH 来做到这一点...

感谢您的帮助!

最佳答案

经过一番修改,我最终使用了带有 background=Trueprogress=Output(....) 的回调,这似乎是为了更新进度条而设计的,但似乎对我的问题很有效。

下面是我正在使用的一个单独示例(使用延迟生成器模拟流式响应)。

我觉得我正在滥用一项功能,因此可能有更好的方法在 DASH 应用程序中实现 token 流,或者这种方法可能会出现迫在眉睫的问题 - 如果您有任何评论,请告诉我。

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)

结果如下所示: enter image description here

关于plotly-dash - 破折号 : Long-Running callback that streams tokens into a Markdown component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77060014/

25 4 0