-
Notifications
You must be signed in to change notification settings - Fork 29
feat: support parallel calls #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
andreitava-uip
wants to merge
1
commit into
main
Choose a base branch
from
feat/parallel-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """Aggregator node for merging substates back into main state.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from langchain_core.messages import AIMessage, AnyMessage | ||
| from langgraph.types import Overwrite | ||
|
|
||
| from uipath_langchain.agent.react.types import AgentGraphState, InnerAgentGraphState | ||
|
|
||
|
|
||
| def _aggregate_messages( | ||
| original_messages: list[AnyMessage], substate_messages: dict[str, list[AnyMessage]] | ||
| ) -> list[AnyMessage]: | ||
| aggregated_by_id: dict[str, AnyMessage] = {} | ||
| original_order: list[str] = [] | ||
|
|
||
| for msg in original_messages: | ||
| aggregated_by_id[msg.id] = msg | ||
| original_order.append(msg.id) | ||
|
|
||
| new_messages: list[AnyMessage] = [] | ||
|
|
||
| for tool_call_id, substate_msgs in substate_messages.items(): | ||
| for msg in substate_msgs: | ||
| if msg.id in aggregated_by_id: | ||
| # existing message | ||
| original_msg = aggregated_by_id[msg.id] | ||
| if ( | ||
| isinstance(msg, AIMessage) | ||
| and msg.tool_calls | ||
| and len(msg.tool_calls) > 0 | ||
| ): | ||
| updated_tool_call = next( | ||
| (tc for tc in msg.tool_calls if tc["id"] == tool_call_id), None | ||
| ) | ||
| if updated_tool_call: | ||
| # update the specific tool call in the original message | ||
| new_tool_calls = [ | ||
| updated_tool_call if tc["id"] == tool_call_id else tc | ||
| for tc in original_msg.tool_calls | ||
| ] | ||
| aggregated_by_id[msg.id].tool_calls = new_tool_calls | ||
| else: | ||
| # new message, add it | ||
| new_messages.append(msg) | ||
|
|
||
| result = [] | ||
| for msg_id in original_order: | ||
| result.append(aggregated_by_id[msg_id]) | ||
| result.extend(new_messages) | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def create_aggregator_node() -> callable: | ||
| """Create an aggregator node that merges substates back into main state.""" | ||
|
|
||
| def aggregator_node(state: AgentGraphState) -> dict[str, Any] | Overwrite: | ||
| """ | ||
| Aggregate substates back into main state. | ||
|
|
||
| If substates is empty, no-op and continue. | ||
| If substates is non-empty: | ||
| - for messages, leave placeholder for message aggregation logic | ||
| - for each field in inner state, get its reducer and apply updates | ||
| - lastly, overwrite the state and clear substates | ||
| """ | ||
| if not state.substates: | ||
| return {} | ||
|
|
||
| # message aggregation | ||
| substate_messages = {} | ||
| for tool_call_id, substate in state.substates.items(): | ||
| if "messages" in substate: | ||
| substate_messages[tool_call_id] = substate["messages"] | ||
|
|
||
| aggregated_messages = _aggregate_messages(state.messages, substate_messages) | ||
|
|
||
| # inner state fields aggregation | ||
| aggregated_inner_dict = state.inner_state.model_dump() | ||
|
|
||
| inner_state_fields = InnerAgentGraphState.model_fields | ||
| for substate in state.substates.values(): | ||
| if "inner_state" in substate: | ||
| substate_inner_data = substate["inner_state"] | ||
|
|
||
| if isinstance(substate_inner_data, InnerAgentGraphState): | ||
| substate_inner_dict = substate_inner_data.model_dump() | ||
| else: | ||
| substate_inner_dict = substate_inner_data | ||
|
|
||
| # for each field, apply reducer if defined | ||
| for field_name, field_info in inner_state_fields.items(): | ||
| if field_name in substate_inner_dict: | ||
| substate_field_value = substate_inner_dict[field_name] | ||
| current_field_value = aggregated_inner_dict[field_name] | ||
|
|
||
| if field_info.metadata and callable(field_info.metadata[-1]): | ||
| reducer_func = field_info.metadata[-1] | ||
| merged_value = reducer_func( | ||
| current_field_value, substate_field_value | ||
| ) | ||
| else: | ||
| # no reducer, just replace | ||
| merged_value = substate_field_value | ||
|
|
||
| aggregated_inner_dict[field_name] = merged_value | ||
|
|
||
| aggregated_inner_state = InnerAgentGraphState.model_validate( | ||
| aggregated_inner_dict | ||
| ) | ||
|
|
||
| state.messages = aggregated_messages | ||
| state.inner_state = aggregated_inner_state | ||
| state.substates = {} | ||
|
|
||
| # return overwrite command to replace the state | ||
| return { | ||
| **state.model_dump(exclude={"messages", "inner_state", "substates"}), | ||
| "messages": Overwrite(aggregated_messages), | ||
| "inner_state": Overwrite(aggregated_inner_state), | ||
| "substates": Overwrite({}), | ||
| } | ||
|
|
||
| return aggregator_node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is generic for any scope (agent, llm or tool). Won't your changes add the TOOL_CALL_STATE_HANDLER for agent and llm guardrail subgraphs as well?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would, but the llm and agent subgraphs are expected to have tool_call_id=None, which will make the node a no-op.
We can of course also conditionally add this node only for tool guardrails, that would make it more optimized.