Replies: 1 comment 4 replies
-
Hello @shell-nlp! I'm here to help you with any bugs, questions, or contributions while you wait for a human maintainer. To modify your complex chain to use Assume you have a sequence of from langchain_core.runnables import RunnableLambda
# Define your functions
def add_one(x: int) -> int:
return x + 1
def mul_two(x: int) -> int:
return x * 2
def mul_three(x: int) -> int:
return x * 3
# Create the sequence
sequence = (
RunnableLambda(add_one) |
RunnableLambda(mul_two) |
RunnableLambda(mul_three)
)
# Modify the second step to use invoke instead of stream
class ModifiedRunnableLambda(RunnableLambda):
async def astream(self, input, config=None, **kwargs):
return self.invoke(input, config, **kwargs)
# Replace the second step with the modified version
sequence = (
RunnableLambda(add_one) |
ModifiedRunnableLambda(mul_two) |
RunnableLambda(mul_three)
)
# Now, when you invoke the sequence, the second step will use invoke instead of stream
result = sequence.invoke(1)
print(result) # Output will be 8 (1 + 1 = 2, 2 * 2 = 4, 4 * 2 = 8) In your specific case, you can modify the class ModifiedEventExtractionChain(RunnableLambda):
async def astream(self, input, config=None, **kwargs):
return self.invoke(input, config, **kwargs)
event_extraction_chain = ModifiedEventExtractionChain(event_extraction_prompt | llm)
qa_bot_with_docs = RunnableParallel(
{
"context": itemgetter("question") | contextual_rerank_retriever,
"question": itemgetter("question"),
}
).assign(answer=qa_bot, prompt=prompt, event=event_extraction_chain)
async for output in qa_bot_with_docs.astream({"question": query}, config={"callbacks": [token_callback]}):
if "prompt" in output:
...
if "context" in output:
...
if "event" in output:
...
# This step now uses invoke instead of stream In this example, the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
我实现了一个复杂的chain,我对chain进行stream操作,我希望中间的某一步是invoke输出,而不是stream输出,我应该怎么操作。
I have implemented a complex chain. I perform stream operations on the chain. I hope that one of the intermediate steps will be invoke output instead of stream output. How should I operate it?
System Info
System Information
Package Information
Packages not installed (Not Necessarily a Problem)
Beta Was this translation helpful? Give feedback.
All reactions