from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import PromptTemplate from langchain_openai import ChatOpenAI
prompt_template = "What is a good name for a company that makes {product}?" prompt = PromptTemplate(template=prompt_template, input_variables=["product"])
如果熟悉 linux 命令行的话,我们会知道,其实 linux 中的管道操作符也是
|。与之类似的,langchain 重载 |
操作符也是为了抽象管道这种操作。 在这行代码中,prompt
的输出会作为 llm 的输入,同时,llm
的输出也会作为 StrOutputParser()
的输入。然后最终得到多个管道处理后的结果。
invoke 实现管道操作的源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# invoke all steps in sequence try: for i, step inenumerate(self.steps): # mark each step as a child run config = patch_config( config, callbacks=run_manager.get_child(f"seq:step:{i+1}") ) if i == 0: input = step.invoke(input, config, **kwargs) else: input = step.invoke(input, config) # finish the root run except BaseException as e: run_manager.on_chain_error(e) raise
from langchain.chains.llm import LLMChain from langchain_community.chains.llm_requests import LLMRequestsChain from langchain_core.prompts import PromptTemplate from langchain_openai import ChatOpenAI
from langchain.chains.llm import LLMChain from langchain.chains.sequential import SequentialChain from langchain_core.prompts import PromptTemplate from langchain_openai import ChatOpenAI