上几次我们已经将一些必备的内容进行了快速的梳理,让我们掌握了streanlit的凯快速上手,接下来我们将其它的一些基础函数再做简单的梳理,以顺便回顾我们未来可能用到的更丰富的函数来实现应用的制作。
st.write_stream
将生成器、迭代器或类似流的序列串流到应用程序中。
st.write_stream 对给定序列进行迭代,并将所有序列块写入应用程序。字符串块将使用打字机效果写入。其他数据类型将使用 st.write 写入。
st.write_stream(stream) | |
---|---|
Returns | |
(str or list) | The full response. If the streamed output only contains text, this is a string. Otherwise, this is a list of all the streamed objects. The return value is fully compatible as input for st.write. |
Parameters | |
stream (Callable, Generator, Iterable, OpenAI Stream, or LangChain Stream) | The generator or iterable to stream. Note To use additional LLM libraries, you can create a wrapper to manually define a generator function and include custom output parsing. |
示例
您可以按照我们的教程 "构建一个基本的 LLM 聊天应用程序 "所示,传递一个 OpenAI 数据流。或者,您也可以将通用生成器函数作为输入:
#导入必备的函数库import time
import numpy as np
import pandas as pd
import streamlit as st#输入指定的语句
_LOREM_IPSUM = """
Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
"""#定义一个函数来实现指定行列的展示
def stream_data():for word in _LOREM_IPSUM.split(" "):yield word + " "time.sleep(0.02)yield pd.DataFrame(np.random.randn(5, 10),columns=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"],)for word in _LOREM_IPSUM.split(" "):yield word + " "time.sleep(0.02)#点击按钮展示函数if st.button("Stream data"):st.write_stream(stream_data)
结果