From 03349bf999bf259ad72d5d08942a3dbb11424ffc Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 17:26:57 -0300 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20fix(vertex/base.py):=20add?= =?UTF-8?q?=20artifacts=20attribute=20to=20Vertex=20class=20to=20store=20a?= =?UTF-8?q?dditional=20data=20=F0=9F=90=9B=20fix(vertex/base.py):=20update?= =?UTF-8?q?=20instantiation=20logic=20to=20handle=20tuple=20result=20from?= =?UTF-8?q?=20loading.instantiate=5Fclass()=20=F0=9F=90=9B=20fix(loading.p?= =?UTF-8?q?y):=20update=20return=20value=20of=20instantiate=5Fprompt()=20t?= =?UTF-8?q?o=20return=20a=20tuple=20of=20prompt=20and=20format=5Fkwargs=20?= =?UTF-8?q?The=20Vertex=20class=20now=20has=20a=20new=20attribute=20called?= =?UTF-8?q?=20artifacts,=20which=20is=20a=20dictionary=20used=20to=20store?= =?UTF-8?q?=20additional=20data=20related=20to=20the=20vertex.=20The=20ins?= =?UTF-8?q?tantiation=20logic=20in=20the=20Vertex=20class=20has=20been=20u?= =?UTF-8?q?pdated=20to=20handle=20the=20case=20where=20loading.instantiate?= =?UTF-8?q?=5Fclass()=20returns=20a=20tuple=20containing=20the=20built=20o?= =?UTF-8?q?bject=20and=20additional=20artifacts.=20The=20loading.instantia?= =?UTF-8?q?te=5Fprompt()=20function=20now=20returns=20a=20tuple=20containi?= =?UTF-8?q?ng=20the=20prompt=20and=20format=5Fkwargs.=20These=20changes=20?= =?UTF-8?q?fix=20issues=20related=20to=20storing=20and=20handling=20additi?= =?UTF-8?q?onal=20data=20in=20the=20vertex=20and=20loading=20modules.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/graph/vertex/base.py | 9 ++++++++- src/backend/langflow/interface/initialize/loading.py | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/langflow/graph/vertex/base.py b/src/backend/langflow/graph/vertex/base.py index 61c50eda5..5e723b989 100644 --- a/src/backend/langflow/graph/vertex/base.py +++ b/src/backend/langflow/graph/vertex/base.py @@ -25,6 +25,7 @@ class Vertex: self._parse_data() self._built_object = None self._built = False + self.artifacts: Dict[str, Any] = {} def _parse_data(self) -> None: self.data = self._data["data"] @@ -195,11 +196,17 @@ class Vertex: # and return the instance try: - self._built_object = loading.instantiate_class( + result = loading.instantiate_class( node_type=self.vertex_type, base_type=self.base_type, params=self.params, ) + # Result could be the _built_object or + # (_built_object, dict) tuple + if isinstance(result, tuple): + self._built_object, self.artifacts = result + else: + self._built_object = result except Exception as exc: raise ValueError( f"Error building node {self.vertex_type}: {str(exc)}" diff --git a/src/backend/langflow/interface/initialize/loading.py b/src/backend/langflow/interface/initialize/loading.py index bbaa1f131..1a075184e 100644 --- a/src/backend/langflow/interface/initialize/loading.py +++ b/src/backend/langflow/interface/initialize/loading.py @@ -124,7 +124,7 @@ def instantiate_prompt(node_type, class_object, params): if format_kwargs: prompt = prompt.partial(**format_kwargs) - return prompt + return prompt, format_kwargs def instantiate_tool(node_type, class_object, params): From d0da0bb1ca5d07609346b74e06a77dcaa5f2c739 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 17:27:18 -0300 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=90=9B=20fix(chat.py):=20add=20suppor?= =?UTF-8?q?t=20for=20artifacts=20in=20stream=5Fbuild=20function=20to=20pas?= =?UTF-8?q?s=20prompt=20variables=20to=20build=5Finput=5Fkeys=5Fresponse?= =?UTF-8?q?=20=E2=9C=A8=20feat(chat.py):=20update=20build=5Finput=5Fkeys?= =?UTF-8?q?=5Fresponse=20function=20to=20accept=20artifacts=20parameter=20?= =?UTF-8?q?to=20set=20input=5Fkeys=20values=20The=20stream=5Fbuild=20funct?= =?UTF-8?q?ion=20now=20supports=20the=20artifacts=20parameter,=20which=20a?= =?UTF-8?q?llows=20passing=20prompt=20variables=20to=20the=20build=5Finput?= =?UTF-8?q?=5Fkeys=5Fresponse=20function.=20This=20ensures=20that=20the=20?= =?UTF-8?q?input=5Fkeys=20values=20are=20correctly=20set=20based=20on=20th?= =?UTF-8?q?e=20provided=20artifacts.=20The=20build=5Finput=5Fkeys=5Frespon?= =?UTF-8?q?se=20function=20has=20been=20updated=20to=20accept=20the=20arti?= =?UTF-8?q?facts=20parameter=20and=20use=20it=20to=20set=20the=20input=5Fk?= =?UTF-8?q?eys=20values.=20This=20improves=20the=20functionality=20of=20th?= =?UTF-8?q?e=20chat=20module=20by=20allowing=20more=20flexibility=20in=20h?= =?UTF-8?q?andling=20prompt=20variables.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index de3c33e01..cdffcdf4f 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -72,6 +72,7 @@ async def stream_build(flow_id: str): async def event_stream(flow_id): final_response = {"end_of_stream": True} + artifacts = {} try: if flow_id not in flow_data_store: error_message = "Invalid session ID" @@ -108,6 +109,11 @@ async def stream_build(flow_id: str): logger.debug( f"Building node {params[:50]}{'...' if len(params) > 50 else ''}" ) + if vertex.artifacts: + # The artifacts will be prompt variables + # passed to build_input_keys_response + # to set the input_keys values + artifacts.update(vertex.artifacts) except Exception as exc: params = str(exc) valid = False @@ -124,7 +130,9 @@ async def stream_build(flow_id: str): langchain_object = graph.build() # Now we need to check the input_keys to send them to the client if hasattr(langchain_object, "input_keys"): - input_keys_response = build_input_keys_response(langchain_object) + input_keys_response = build_input_keys_response( + langchain_object, artifacts + ) yield str(StreamData(event="message", data=input_keys_response)) chat_manager.set_cache(flow_id, langchain_object) From bb377d4935153f2df98f6d73a76b698f120442b2 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 17:27:28 -0300 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20set=20input?= =?UTF-8?q?=20keys=20values=20from=20artifacts=20in=20build=5Finput=5Fkeys?= =?UTF-8?q?=5Fresponse=20function=20=E2=9C=A8=20feat(utils.py):=20add=20su?= =?UTF-8?q?pport=20for=20setting=20input=20keys=20values=20from=20artifact?= =?UTF-8?q?s=20in=20build=5Finput=5Fkeys=5Fresponse=20function=20The=20bui?= =?UTF-8?q?ld=5Finput=5Fkeys=5Fresponse=20function=20now=20takes=20an=20ad?= =?UTF-8?q?ditional=20parameter,=20artifacts,=20which=20is=20a=20dictionar?= =?UTF-8?q?y=20containing=20key-value=20pairs.=20The=20function=20sets=20t?= =?UTF-8?q?he=20values=20of=20the=20input=20keys=20in=20the=20input=5Fkeys?= =?UTF-8?q?=5Fresponse=20dictionary=20based=20on=20the=20corresponding=20k?= =?UTF-8?q?eys=20in=20the=20artifacts=20dictionary.=20This=20allows=20for?= =?UTF-8?q?=20more=20flexibility=20in=20setting=20the=20input=20keys=20val?= =?UTF-8?q?ues=20dynamically=20based=20on=20the=20provided=20artifacts.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/api/utils.py b/src/backend/langflow/api/utils.py index bd25d74f4..2df86eb7f 100644 --- a/src/backend/langflow/api/utils.py +++ b/src/backend/langflow/api/utils.py @@ -24,13 +24,18 @@ def remove_api_keys(flow: dict): return flow -def build_input_keys_response(langchain_object): +def build_input_keys_response(langchain_object, artifacts): """Build the input keys response.""" input_keys_response = { "input_keys": {key: "" for key in langchain_object.input_keys}, "memory_keys": [], } + + # Set the input keys values from artifacts + for key, value in artifacts.items(): + if key in input_keys_response["input_keys"]: + input_keys_response["input_keys"][key] = value # If the object has memory, that memory will have a memory_variables attribute # memory variables should be removed from the input keys if hasattr(langchain_object, "memory") and hasattr(