以下代码示例演示了如何传递回调处理器

llm = OpenAI(temperature=0)
llm_math = LLMMathChain.from_llm(llm=llm)

@cl.on_message
async def main(message: cl.Message):

    res = await llm_math.acall(message.content, callbacks=[cl.LangchainCallbackHandler()])

    await cl.Message(content="Hello").send()

最终答案流式传输

如果在 LLM 级别启用了流式传输,Langchain 将仅流式传输中间步骤。您可以通过将 stream_final_answer=True 传递给回调处理器来启用最终答案流式传输。

# Optionally, you can also pass the prefix tokens that will be used to identify the final answer
answer_prefix_tokens=["FINAL", "ANSWER"]

cl.LangchainCallbackHandler(
        stream_final_answer=True,
        answer_prefix_tokens=answer_prefix_tokens,
    )

最终答案流式传输仅适用于具有一致最终答案模式的提示。它也不适用于 AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION