Skip to content

Commit

Permalink
Fix API not passing input_value to Chat Input (#1749)
Browse files Browse the repository at this point in the history
* Fix API not returning values when stream is True in a component

Fixes #1744

* Update Graph.from_payload method to include user_id parameter
  • Loading branch information
ogabrielluiz committed Apr 18, 2024
1 parent b04caf2 commit 3ad2919
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async def simplified_run_flow(
This endpoint provides a powerful interface for executing flows with enhanced flexibility and efficiency, supporting a wide range of applications by allowing for dynamic input and output configuration along with performance optimizations through session management and caching.
"""
session_id = input_request.session_id

try:
task_result: List[RunOutputs] = []
artifacts = {}
Expand All @@ -127,8 +128,9 @@ async def simplified_run_flow(
if flow.data is None:
raise ValueError(f"Flow {flow_id} has no data")
graph_data = flow.data
graph_data = process_tweaks(graph_data, input_request.tweaks or {})
graph = Graph.from_payload(graph_data, flow_id=flow_id)

graph_data = process_tweaks(graph_data, input_request.tweaks or {}, stream=stream)
graph = Graph.from_payload(graph_data, flow_id=flow_id, user_id=api_key_user.id)
inputs = [
InputValueRequest(components=[], input_value=input_request.input_value, type=input_request.input_type)
]
Expand Down
8 changes: 5 additions & 3 deletions src/backend/base/langflow/graph/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
nodes: List[Dict],
edges: List[Dict[str, str]],
flow_id: Optional[str] = None,
user_id: Optional[str] = None,
) -> None:
"""
Initializes a new instance of the Graph class.
Expand All @@ -47,6 +48,7 @@ def __init__(
self._runs = 0
self._updates = 0
self.flow_id = flow_id
self.user_id = user_id
self._is_input_vertices: List[str] = []
self._is_output_vertices: List[str] = []
self._is_state_vertices: List[str] = []
Expand Down Expand Up @@ -450,7 +452,7 @@ def __setstate__(self, state):
self.__init__(**state)

@classmethod
def from_payload(cls, payload: Dict, flow_id: Optional[str] = None) -> "Graph":
def from_payload(cls, payload: Dict, flow_id: Optional[str] = None, user_id: Optional[str] = None) -> "Graph":
"""
Creates a graph from a payload.
Expand All @@ -465,7 +467,7 @@ def from_payload(cls, payload: Dict, flow_id: Optional[str] = None) -> "Graph":
try:
vertices = payload["nodes"]
edges = payload["edges"]
return cls(vertices, edges, flow_id)
return cls(vertices, edges, flow_id, user_id)
except KeyError as exc:
logger.exception(exc)
if "nodes" not in payload and "edges" not in payload:
Expand Down Expand Up @@ -772,7 +774,7 @@ async def process(self, start_component_id: Optional[str] = None) -> "Graph":
lock=lock,
set_cache_coro=set_cache_coro,
vertex_id=vertex_id,
user_id=None,
user_id=self.user_id,
inputs_dict={},
),
name=f"{vertex.display_name} Run {vertex_task_run_count.get(vertex_id, 0)}",
Expand Down
12 changes: 7 additions & 5 deletions src/backend/base/langflow/processing/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ def apply_tweaks(node: Dict[str, Any], node_tweaks: Dict[str, Any]) -> None:

for tweak_name, tweak_value in node_tweaks.items():
if tweak_name not in template_data:
logger.warning(f"Node {node.get('id')} does not have a tweak named {tweak_name}")
continue
if tweak_name in template_data:
key = "file_path" if template_data[tweak_name]["type"] == "file" else "value"
Expand All @@ -256,21 +255,24 @@ def apply_tweaks_on_vertex(vertex: Vertex, node_tweaks: Dict[str, Any]) -> None:
vertex.params[tweak_name] = tweak_value


def process_tweaks(graph_data: Dict[str, Any], tweaks: Union["Tweaks", Dict[str, Dict[str, Any]]]) -> Dict[str, Any]:
def process_tweaks(
graph_data: Dict[str, Any], tweaks: Union["Tweaks", Dict[str, Dict[str, Any]]], stream: bool = False
) -> Dict[str, Any]:
"""
This function is used to tweak the graph data using the node id and the tweaks dict.
:param graph_data: The dictionary containing the graph data. It must contain a 'data' key with
'nodes' as its child or directly contain 'nodes' key. Each node should have an 'id' and 'data'.
:param tweaks: The dictionary containing the tweaks. The keys can be the node id or the name of the tweak.
The values can be a dictionary containing the tweaks for the node or the value of the tweak.
The values can be a dictionary containing the tweaks for the node or the value of the tweak.
:param stream: A boolean flag indicating whether streaming should be deactivated across all components or not. Default is False.
:return: The modified graph_data dictionary.
:raises ValueError: If the input is not in the expected format.
"""
if not isinstance(tweaks, dict):
tweaks = tweaks.model_dump()

if "stream" not in tweaks:
tweaks["stream"] = stream
nodes = validate_input(graph_data, tweaks)
nodes_map = {node.get("id"): node for node in nodes}
nodes_display_name_map = {node.get("data", {}).get("node", {}).get("display_name"): node for node in nodes}
Expand Down

0 comments on commit 3ad2919

Please sign in to comment.