Ví dụ xích phức tạp (tiếp tục)
Trong khi cái này làm việc, chúng ta đã mất story gốc trong kết quả của chúng ta – chúng ta chỉ nhận
phân tích!. Trong các ứng dụng sản xuất, chúng ta điển hình muốn bảo lưu bối cảnh xuyên suốt xích:
from langchain_core.runnables import RunnablePassthrough
# Using RunnablePassthrough.assign to preserve data
enhanced_chain = RunnablePassthrough.assign(story=story_chain # Add ‘story’ key with generated content
).assign(
analysis=analysis_chain # Add ‘analysis’ key with analysis of the
story
)
# Execute the chain
result = enhanced_chain.invoke({“topic”: “a rainy day”})
print(result.keys()) # Output: dict_keys([‘topic’, ‘story’, ‘analysis’])
# dict_keys([‘topic’, ‘story’, ‘analysis’])
Cho kiểm soát hơn trên cấu trúc đầu ra, chúng ta cũng có thể xây dựng các dictionaries thủ công:
Chúng ta có thể đơn giản hóa cái này với chuyển đổi dictionary sử dụng một LCEL shorthand:
# Simplified dictionary construction
simple_dict_chain = story_chain | {“analysis”: analysis_chain}
result = simple_dict_chain.invoke({“topic”: “a rainy day”}) print(result.
keys()) # Output: dict_keys([‘analysis’, ‘output’])

