🚀 feat(graph): add from_payload class method to Graph class

🚀 feat(utils.py): import extract_input_variables_from_prompt from langflow.interface.utils
The `from_payload` class method is added to the `Graph` class to create a graph from a payload. This method takes a dictionary as input and returns a `Graph` object. The `extract_input_variables_from_prompt` function is imported from `langflow.interface.utils` to extract input variables from a prompt. This function is used in other parts of the codebase to extract input variables from prompts.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-06 10:05:01 -03:00
commit 3bfee4d445
3 changed files with 23 additions and 6 deletions

View file

@ -24,6 +24,27 @@ class Graph:
self._edges = edges
self._build_graph()
@classmethod
@classmethod
def from_payload(cls, payload: Dict) -> "Graph":
"""
Creates a graph from a payload.
Args:
payload (Dict): The payload to create the graph from.
Returns:
Graph: The created graph.
"""
if "data" in payload:
payload = payload["data"]
try:
nodes = payload["nodes"]
edges = payload["edges"]
return cls(nodes, edges)
except KeyError as exc:
raise ValueError("Invalid payload") from exc
def _build_graph(self) -> None:
"""Builds the graph from the nodes and edges."""
self.nodes = self._build_vertices()

View file

@ -1,6 +1,7 @@
import re
from typing import Any, Union
from langflow.interface.utils import extract_input_variables_from_prompt
def validate_prompt(prompt: str):
"""Validate prompt."""
@ -15,11 +16,6 @@ def fix_prompt(prompt: str):
return prompt + " {input}"
def extract_input_variables_from_prompt(prompt: str) -> list[str]:
"""Extract input variables from prompt."""
return re.findall(r"{(.*?)}", prompt)
def flatten_list(list_of_lists: list[Union[list, Any]]) -> list:
"""Flatten list of lists."""
new_list = []