From cfcdb67ed9f4b2d250a99ab5092a4820b51bcd4f Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 29 Jun 2023 22:05:22 -0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=9A=80=20feat(base.py):=20add=20defau?= =?UTF-8?q?lt=20values=20for=20string=20and=20prompt=20type=20parameters?= =?UTF-8?q?=20in=20=5Fbuild=5Fparams=20method=20The=20unused=20imports=20h?= =?UTF-8?q?ave=20been=20removed=20to=20improve=20code=20cleanliness.=20The?= =?UTF-8?q?=20code=20in=20the=20=5Fbuild=5Fparams=20method=20has=20been=20?= =?UTF-8?q?simplified=20by=20removing=20unnecessary=20conditions=20and=20l?= =?UTF-8?q?oops.=20Default=20values=20are=20now=20added=20for=20string=20a?= =?UTF-8?q?nd=20prompt=20type=20parameters=20to=20ensure=20that=20they=20h?= =?UTF-8?q?ave=20a=20value=20even=20if=20not=20explicitly=20set.=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(base.py):=20remove=20unused=20imports=20and?= =?UTF-8?q?=20simplify=20code=20in=20=5Fbuild=5Fparams=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/graph/vertex/base.py | 58 ++++++----------------- 1 file changed, 15 insertions(+), 43 deletions(-) diff --git a/src/backend/langflow/graph/vertex/base.py b/src/backend/langflow/graph/vertex/base.py index 79c3270fb..a7fcf88b5 100644 --- a/src/backend/langflow/graph/vertex/base.py +++ b/src/backend/langflow/graph/vertex/base.py @@ -1,14 +1,11 @@ -from langflow.utils.constants import DIRECT_TYPES from langflow.interface.initialize import loading from langflow.interface.listing import ALL_TYPES_DICT from langflow.utils.logger import logger from langflow.utils.util import sync_to_async -import contextlib import inspect import types -import warnings from typing import Any, Dict, List, Optional from typing import TYPE_CHECKING @@ -69,6 +66,7 @@ class Vertex: break def _build_params(self): + # sourcery skip: merge-list-append, remove-redundant-if # Some params are required, some are optional # but most importantly, some params are python base classes # like str and others are LangChain objects like LLMChain, BasePromptTemplate @@ -89,8 +87,19 @@ class Vertex: if isinstance(value, dict) } params = {} + + for edge in self.edges: + param_key = edge.target_param + if param_key in template_dict: + if template_dict[param_key]["list"]: + if param_key not in params: + params[param_key] = [] + params[param_key].append(edge.source) + else: + params[param_key] = edge.source + for key, value in template_dict.items(): - if key == "_type": + if key == "_type" or not value.get("show"): continue # If the type is not transformable to a python base class # then we need to get the edge that connects to this node @@ -101,45 +110,8 @@ class Vertex: file_path = value.get("file_path") params[key] = file_path - - elif value.get("type") not in DIRECT_TYPES: - # Get the edge that connects to this node - edges = [ - edge - for edge in self.edges - if edge.target == self and edge.matched_type in value["type"] - ] - - # Get the output of the node that the edge connects to - # if the value['list'] is True, then there will be more - # than one time setting to params[key] - # so we need to append to a list if it exists - # or create a new list if it doesn't - - if value["required"] and not edges: - # If a required parameter is not found, raise an error - raise ValueError( - f"Required input {key} for module {self.vertex_type} not found" - ) - elif value["list"]: - # If this is a list parameter, append all sources to a list - params[key] = [edge.source for edge in edges] - elif edges: - # If a single parameter is found, use its source - params[key] = edges[0].source - - elif value["required"] or value.get("value"): - # If value does not have value this still passes - # but then gives a keyError - # so we need to check if value has value - new_value = value.get("value") - if new_value is None: - warnings.warn(f"Value for {key} in {self.vertex_type} is None. ") - if value.get("type") == "int": - with contextlib.suppress(TypeError, ValueError): - new_value = int(new_value) # type: ignore - params[key] = new_value - + elif value.get("type") in ["str", "prompt"] and params.get(key) is None: + params[key] = value.get("value") # Add _type to params self.params = params From 766ef28a0c1d278f554f41e06c65810f0371d996 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 29 Jun 2023 22:06:57 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=94=A7=20refactor(base.py):=20add=20s?= =?UTF-8?q?ource=5Fhandle,=20target=5Fhandle,=20and=20target=5Fparam=20att?= =?UTF-8?q?ributes=20to=20Edge=20class=20The=20Edge=20class=20constructor?= =?UTF-8?q?=20now=20takes=20an=20additional=20parameter=20'edge'=20which?= =?UTF-8?q?=20is=20a=20dictionary=20containing=20additional=20information?= =?UTF-8?q?=20about=20the=20edge.=20This=20information=20is=20used=20to=20?= =?UTF-8?q?set=20the=20source=5Fhandle,=20target=5Fhandle,=20and=20target?= =?UTF-8?q?=5Fparam=20attributes=20of=20the=20Edge=20class.=20This=20chang?= =?UTF-8?q?e=20allows=20for=20more=20flexibility=20and=20extensibility=20w?= =?UTF-8?q?hen=20working=20with=20edges=20in=20the=20graph.=20=F0=9F=94=A7?= =?UTF-8?q?=20refactor(base.py):=20add=20edge=20parameter=20to=20Edge=20cl?= =?UTF-8?q?ass=20constructor=20to=20pass=20additional=20edge=20information?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/graph/edge/base.py | 8 +++++++- src/backend/langflow/graph/graph/base.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/graph/edge/base.py b/src/backend/langflow/graph/edge/base.py index 08f084a5c..341c3c78f 100644 --- a/src/backend/langflow/graph/edge/base.py +++ b/src/backend/langflow/graph/edge/base.py @@ -6,9 +6,15 @@ if TYPE_CHECKING: class Edge: - def __init__(self, source: "Vertex", target: "Vertex"): + def __init__(self, source: "Vertex", target: "Vertex", edge: dict): self.source: "Vertex" = source self.target: "Vertex" = target + self.source_handle = edge.get("sourceHandle", "") + self.target_handle = edge.get("targetHandle", "") + # 'BaseLoader;BaseOutputParser|documents|PromptTemplate-zmTlD' + # target_param is documents + self.target_param = self.target_handle.split("|")[1] + self.validate_edge() def validate_edge(self) -> None: diff --git a/src/backend/langflow/graph/graph/base.py b/src/backend/langflow/graph/graph/base.py index 46425ddf6..65fff3239 100644 --- a/src/backend/langflow/graph/graph/base.py +++ b/src/backend/langflow/graph/graph/base.py @@ -179,7 +179,7 @@ class Graph: raise ValueError(f"Source node {edge['source']} not found") if target is None: raise ValueError(f"Target node {edge['target']} not found") - edges.append(Edge(source, target)) + edges.append(Edge(source, target, edge)) return edges def _get_vertex_class(self, node_type: str, node_lc_type: str) -> Type[Vertex]: From 9a69a71ea1ff9561743afc3c3e57e1e39eb0d946 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 29 Jun 2023 22:07:23 -0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20fix(chat.py):=20add=20except?= =?UTF-8?q?ion=20logging=20to=20stream=5Fbuild=20function=20to=20improve?= =?UTF-8?q?=20error=20handling=20The=20`stream=5Fbuild`=20function=20now?= =?UTF-8?q?=20logs=20the=20exception=20that=20occurred=20during=20the=20fl?= =?UTF-8?q?ow=20building=20process=20using=20the=20`logger.exception`=20me?= =?UTF-8?q?thod.=20This=20improves=20error=20handling=20and=20provides=20m?= =?UTF-8?q?ore=20detailed=20information=20about=20the=20error=20in=20the?= =?UTF-8?q?=20logs.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index cdffcdf4f..b394afc99 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -139,6 +139,7 @@ async def stream_build(flow_id: str): # We need to reset the chat history chat_manager.chat_history.empty_history(flow_id) except Exception as exc: + logger.exception(exc) logger.error("Error while building the flow: %s", exc) yield str(StreamData(event="error", data={"error": str(exc)})) finally: