langchain
langchain copied to clipboard
Issue:How to use callback functions in a Langchain sequential chain
Issue you'd like to raise.
How to use callback functions in a Langchain sequential chain, such as 1->2->3. I want to loop through the 2 function n times in the middle, where the output of the 2 function is its input. At the end of the loop, the output of the 2 function is input to the 3 function, and the final result is obtained
Suggestion:
No response
There are two types of sequential chains in LangChain:
SimpleSequentialChain: This is the simplest form of a sequential chain, in which each step has a single input/output, and the output of one step becomes the input of the next -1SimpleSequentialChain: The simplest form of sequential chains, where each step has a singular input/output, and the output of one step is the input to the next.\n\n * SequentialChain: A more general form of sequential chains, allowing for multiple inputs/outputs"}}.
SequentialChain: This is the more general form of sequential chain, which allows for multiple inputs/outputs -1SimpleSequentialChain: The simplest form of sequential chains, where each step has a singular input/output, and the output of one step is the input to the next.\n\n * SequentialChain: A more general form of sequential chains, allowing for multiple inputs/outputs"}}.
Here is an example of a SimpleSequentialChain:
python Copy code from langchain.llms import OpenAI from langchain.chains import LLMChain from langchain.prompts import PromptTemplate
This is an LLMChain to write a synopsis given a title of a play.
llm = OpenAI(temperature=.7) template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {title} Playwright: This is a synopsis for the above play:""" prompt_template = PromptTemplate(input_variables=["title"], template=template) synopsis_chain = LLMChain(llm=llm, prompt=prompt_template)
This is an LLMChain to write a review of a play given a synopsis.
llm = OpenAI(temperature=.7) template = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis: {synopsis} Review from a New York Times play critic of the above play:""" prompt_template = PromptTemplate(input_variables=["synopsis"], template=template) review_chain = LLMChain(llm=llm, prompt=prompt_template)
This is the overall chain where we run these two chains in sequence.
from langchain.chains import SimpleSequentialChain overall_chain = SimpleSequentialChain(chains=[synopsis_chain, review_chain], verbose=True)
review = overall_chain.run("Tragedy at sunset on the beach") In this example, we create two LLmchains, one for writing a script outline and one for writing a script review, and then we execute the two chains in sequence 1 in the SimpleSequentialChain.
i have the same question, when i split chain and run in a SequentialChain, the customized local function calling tool can't be used.
+1
I want to use different callbacks on different chains in the SequentialChain, but when i add those to the chains, the callback is not beeing called. Is there a way to get different chains to use different callbacks?
Hi, @vencent-debug! I'm Dosu, and I'm here to help the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.
From what I understand, you were seeking suggestions on how to loop through a specific function multiple times in a LangChain sequential chain, with the output of each iteration being the input for the next function in the chain. There have been comments from other users with similar questions, as well as a request for different chains to use different callbacks.
Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.
Thank you for your understanding and contribution to the LangChain community!
Why is it closed? Not solved yet.Please provide solution or reopen it.