From 3bfee4d4455af8ae18a947ca31785f7978e1384d Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 6 Jun 2023 10:05:01 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(graph):=20add=20from=5Fpayl?= =?UTF-8?q?oad=20class=20method=20to=20Graph=20class=20=F0=9F=9A=80=20feat?= =?UTF-8?q?(utils.py):=20import=20extract=5Finput=5Fvariables=5Ffrom=5Fpro?= =?UTF-8?q?mpt=20from=20langflow.interface.utils=20The=20`from=5Fpayload`?= =?UTF-8?q?=20class=20method=20is=20added=20to=20the=20`Graph`=20class=20t?= =?UTF-8?q?o=20create=20a=20graph=20from=20a=20payload.=20This=20method=20?= =?UTF-8?q?takes=20a=20dictionary=20as=20input=20and=20returns=20a=20`Grap?= =?UTF-8?q?h`=20object.=20The=20`extract=5Finput=5Fvariables=5Ffrom=5Fprom?= =?UTF-8?q?pt`=20function=20is=20imported=20from=20`langflow.interface.uti?= =?UTF-8?q?ls`=20to=20extract=20input=20variables=20from=20a=20prompt.=20T?= =?UTF-8?q?his=20function=20is=20used=20in=20other=20parts=20of=20the=20co?= =?UTF-8?q?debase=20to=20extract=20input=20variables=20from=20prompts.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/graph/graph/base.py | 21 +++++++++++++++++++++ src/backend/langflow/graph/graph/utils.py | 0 src/backend/langflow/graph/utils.py | 8 ++------ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/backend/langflow/graph/graph/utils.py diff --git a/src/backend/langflow/graph/graph/base.py b/src/backend/langflow/graph/graph/base.py index 020f539ec..5fd00d09b 100644 --- a/src/backend/langflow/graph/graph/base.py +++ b/src/backend/langflow/graph/graph/base.py @@ -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() diff --git a/src/backend/langflow/graph/graph/utils.py b/src/backend/langflow/graph/graph/utils.py new file mode 100644 index 000000000..e69de29bb diff --git a/src/backend/langflow/graph/utils.py b/src/backend/langflow/graph/utils.py index e22b27cf5..b78b2f961 100644 --- a/src/backend/langflow/graph/utils.py +++ b/src/backend/langflow/graph/utils.py @@ -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 = []