Replies: 2 comments 4 replies
-
Hey @Eskiii, great to see you diving deeper into the LangChain world! Facing a new challenge, I see? Let's tackle this one together. To limit the number of iterations for specific loops within your graph, you can create custom chain classes that inherit from Here's a step-by-step guide to achieve this:
Here's an example to illustrate this approach: from langchain.chains import SequentialChain
class LimitedIterationChain(SequentialChain):
def __init__(self, components, max_iterations):
super().__init__(components)
self.max_iterations = max_iterations
self.current_iteration = 0
def __call__(self, *args, **kwargs):
while self.current_iteration < self.max_iterations:
super().__call__(*args, **kwargs)
self.current_iteration += 1
self.current_iteration = 0 # Reset for potential reuse
# Example usage with your specific loops
sequence_1_chain = LimitedIterationChain(components_sequence_1, max_iterations=3)
sequence_2_chain = LimitedIterationChain(components_sequence_2, max_iterations=5) Replace This method requires you to adjust the logic based on your specific implementation details and how components are called within each iteration. For further insights on handling iterations with Sources
|
Beta Was this translation helpful? Give feedback.
-
Hi @Eskiii You can pass chain.invoke(...,{"recursion_limit": 15}) |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
in the self-rag graph: https://github.com/langchain-ai/langgraph/blob/main/examples/rag/langgraph_self_rag.ipynb,which contain three loops, i wonder how to limit the maximum number of times of one of the loops.
For example, limit the loop: retrive - grade_documents - decide_to_generate - retrieve to 3, and limit the other loop: generate - grade_generation_v_documents_and_question - generate to 5.
Looking forward to reply, thank you!
System Info
langchain-experimental == 0.0.49
langchain-openai == 0.1.1
langchain == 0.1.13
langchain-community == 0.0.29
langchain-core == 0.1.34
langchain-text-splitters == 0.0.1
python == 3.10.12
Beta Was this translation helpful? Give feedback.
All reactions