RunnableConfig
Runnables | LangChain Reference
Bind arguments to a Runnable, returning a new Runnable. Useful when a Runnable in a chain requires an argument that is not in the output of the previous Runnable or included in the user input. PARAMETER DESCRIPTION **kwargs The arguments to bind to the Run
reference.langchain.com
에이전트의 툴 처리에 대한 루프가 끝나고, AIMessage를 스트리밍 하는 방법에 대한 글입니다.
에이전트 호출 내역을 스트리밍 해버리면 Tool 선택 과정까지도 스트리밍 범위로 잡히게 됩니다.
[create_agent에서 구분하는 기능을 지원해주면 좋겠는데.. 아직까지는 확인된 게 없습니다.]
스트림 모드가 messages일 때 토큰과 메타 데이터 정보를 관리하게 되는데,
메타 데이터를 기반으로 응답을 필터링할 수 있다면 고객에게 뿌려질 스트림 토큰만 따로 전달할 수 있습니다.
해당 설정은 간단한데, RunnableConfig (agent실행에 대한 run타임 설정 정보)
tags에 값을 넘기면 token, metadata로 처리되는 스트림 메시지 정보에서, metadata 쪽에 tags 정보가 들어오게 됩니다.
async for _, channel, event in dof_agent_graph.astream(
input=state,
context=context,
stream_mode=["custom", "messages"],
subgraphs=True
):
if channel == "messages":
token, meta_data = event
tags = meta_data.get('tags') if 'tags' in meta_data else None
if tags and "streaming_token" in tags:
yield f"token: {token}"
elif channel == "custom":
yield f"커스텀: {event}\n"

메타데이터를 까서 tags정보를 체크하면서 UI로 전달시킬 스트리밍 데이터를 선택할 수 있습니다.
나머지는 커스텀 데이터에 담아서 클라이언트와 약속한 방식으로(주로 json) 내려주면 됩니다.