参考官方文档
1、Streaming:流,实时显示输出的进度,对最终用户的体验、感知有着关键作用
langchain中重要的组件(LLMs, parsers, prompts, retrievers, and agents)都有相关接口,以下是两种常规的方法实现流输出
(1)sync stream and async astream:输出最终结果
(2)async astream_events and async astream_log:输出中间过程和最终结果
2、Using Stream
所有的Runnable对象都提供一个同步方法(stream),和一个异步方法(astream);通过chunks(分块)的方式输出最终结果
Streaming只能是当所有的环节都知道如何去处理输入流,也就是一次处理一个输入chunk并产出一个对应的输出chunk
"Streaming is only possible if all steps in the program know how to process an input stream; i.e., process an input chunk one at a time, and yield a corresponding output chunk."
通过LLMs来了解streaming是有效的方式
3、LLMs and Chat Models
大模型通常需要数秒去给出一个完整的回答,这样最终用户对于应用的灵敏感知是不友好的。一个关键的策略就是streaming
from langchain_openai import ChatOpenAI
import asyncio
model = ChatOpenAI()
chunks = []
async def astreaming():
async for chunk in model.astream("写一首关于元宵的词"):
chunks.append(chunk)
print(chunk.content, end="|", flush=True)
def main():
loop = asyncio.get_event_loop()
task = astreaming()
loop.run_until_complete(task)
待学习补充