Transformation(转换链)是 LangChain 中用于“自定义数据处理”的链式工具,允许你在链路中插入任意 Python 代码,对输入或中间结果进行灵活处理。常用于:
- 对输入/输出做格式化、过滤、摘要、拆分等自定义操作
- 作为 LLMChain 等链的前置或后置处理环节
典型用法举例
假设你有一段长文本,想先用 Python 代码提取前3段,再交给大模型总结:
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain.chains import SimpleSequentialChain# 读取待处理文本内容
with open("letter.txt", "r") as f:letters = f.read()from langchain.chains import (LLMChain,SimpleSequentialChain,TransformChain
)# 定义转换函数:只取前三个段落
def transform_func(inputs:dict) -> dict:text = inputs["content"]trans_text = "\n\n".join(text.split("\n\n")[:3])return {"summary": trans_text}# 构建文档转换链,将原始内容转换为摘要内容
transform_chain = TransformChain(input_variables=["content"],output_variables=["summary"],transform=transform_func
)# 总结用的prompt模板
template = """总结下面的内容:
{summary}
总结:"""prompt = PromptTemplate(input_variables=["summary"],template=template
)print(prompt) # 打印prompt模板# 使用腾讯混元大模型作为LLM
llm = ChatOpenAI(api_key=SecretStr(os.environ.get("HUNYUAN_API_KEY", "")),base_url="https://api.hunyuan.cloud.tencent.com/v1",model="hunyuan-t1-latest",temperature=0,
)# 构建LLM链,输入为summary,输出为总结
llm_chain = LLMChain(llm=llm,prompt=prompt
)# 顺序链:先transform,再总结
sequential_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain],verbose=True
)# 执行顺序链,输出最终结果
print(sequential_chain.run(letters))
输出:
input_variables=['summary'] template='总结下面的内容:\n{summary}\n总结:'> Entering new SimpleSequentialChain chain...
Dear Dr. Smith,I hope this letter finds you well. I am writing to express my sincere gratitude for your guidance and support throughout my recent research project on "Deep Learning-Based Image Recognition for Medical Diagnosis." Specifically, my research focused on applying convolutional neural networks (CNNs) to the automatic detection and classification of medical images, such as identifying pathological features in X-rays and MRI scans. I explored methods to improve model accuracy and robustness, including data augmentation, transfer learning, and the integration of attention mechanisms to enhance feature extraction. Your expertise and encouragement have been invaluable to my academic growth, and I truly appreciate the time and effort you have dedicated to mentoring me.The insights you provided, especially regarding the optimization of convolutional neural network architectures and the interpretation of experimental results, not only helped me overcome several challenges but also inspired me to explore new directions in my work. I am especially grateful for your constructive feedback during our discussions, which greatly improved the quality of my research.
这是一封致Dr. Smith的感谢信,作者主要表达对其在“基于深度学习的医学诊断图像识别”研究项目中的指导与支持的诚挚谢意。研究聚焦于应用卷积神经网络(CNN)实现医学影像(如X光、MRI)的自动检测与分类,并探索了数据增强、迁移学习及注意力机制等方法以提升模型准确性与鲁棒性。作者特别感谢Dr. Smith在优化CNN架构、解读实验结果等方面的专业建议,以及讨论中提供的建设性反馈,这些帮助其克服挑战并提升了研究质量,对其学术成长意义重大。> Finished chain.
这是一封致Dr. Smith的感谢信,作者主要表达对其在“基于深度学习的医学诊断图像识别”研究项目中的指导与支持的诚挚谢意。研究聚焦于应用卷积神经网络(CNN)实现医学影像(如X光、MRI)的自动检测与分类,并探索了数据增强、迁移学习及注意力机制等方法以提升模型准确性与鲁棒性。作者特别感谢Dr. Smith在优化CNN架构、解读实验结果等方面的专业建议,以及讨论中提供的建设性反馈,这些帮助其克服挑战并提升了研究质量,对其学术成长意义重大。
关键点说明
- TransformChain 允许插入任意 Python 逻辑,极大增强链式流程的灵活性。
- 可与 LLMChain、SimpleSequentialChain 等组合,实现“预处理-大模型-后处理”流水线。
- 适合数据清洗、格式转换、摘要、实体提取等场景。