From d4b7d8b469d08ae9ef890f919eab57c051f2b07d Mon Sep 17 00:00:00 2001 From: italojohnny Date: Wed, 26 Jun 2024 17:48:42 -0300 Subject: [PATCH 01/12] create an abstract class for an existing class --- .../base/langflow/services/tracing/base.py | 33 +++++++++++++++++++ .../base/langflow/services/tracing/service.py | 5 +-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/backend/base/langflow/services/tracing/base.py diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py new file mode 100644 index 000000000..2a5d609c9 --- /dev/null +++ b/src/backend/base/langflow/services/tracing/base.py @@ -0,0 +1,33 @@ +from abc import ABC, abstractmethod +from typing import Dict, Any + + +class BaseTrace(ABC): + + @abstractmethod + def ready(self): + raise NotImplementedError + + @abstractmethod + def setup_langsmith(self): + raise NotImplementedError + + @abstractmethod + def add_trace(self, trace_name: str, trace_type: str, inputs: Dict[str, Any], metadata: Dict[str, Any] = None): + raise NotImplementedError + + @abstractmethod + def _convert_to_langchain_types(self, io_dict: Dict[str, Any]): + raise NotImplementedError + + @abstractmethod + def _convert_to_langchain_type(self, value): + raise NotImplementedError + + @abstractmethod + def end_trace(self, trace_name: str, outputs: Dict[str, Any] = None, error: str = None): + raise NotImplementedError + + @abstractmethod + def end(self, outputs: Dict[str, Any], error: str | None = None): + raise NotImplementedError diff --git a/src/backend/base/langflow/services/tracing/service.py b/src/backend/base/langflow/services/tracing/service.py index 95795c988..393b127f9 100644 --- a/src/backend/base/langflow/services/tracing/service.py +++ b/src/backend/base/langflow/services/tracing/service.py @@ -13,6 +13,7 @@ from loguru import logger from langflow.schema.data import Data from langflow.services.base import Service from langflow.services.tracing.schema import Log +from langflow.services.tracing.base import BaseTrace if TYPE_CHECKING: from langflow.services.monitor.service import MonitorService @@ -180,8 +181,8 @@ class TracingService(Service): self.outputs_metadata[trace_name] |= output_metadata or {} -class LangSmithTracer: - def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: UUID): +class LangSmithTracer(BaseTrace): + def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: str): from langsmith.run_trees import RunTree self.trace_name = trace_name From 4212b2780085cbc073c67c561bc64a35c4fa0b82 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 21 Jun 2024 19:57:49 -0300 Subject: [PATCH 02/12] refactor: Update logs to outputs in CustomComponent and TracingService classes --- .../langflow/custom/custom_component/custom_component.py | 9 +++------ src/backend/base/langflow/services/tracing/service.py | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/backend/base/langflow/custom/custom_component/custom_component.py b/src/backend/base/langflow/custom/custom_component/custom_component.py index 24df42894..779618e9a 100644 --- a/src/backend/base/langflow/custom/custom_component/custom_component.py +++ b/src/backend/base/langflow/custom/custom_component/custom_component.py @@ -83,7 +83,7 @@ class CustomComponent(BaseComponent): _flows_data: Optional[List[Data]] = None _outputs: List[OutputLog] = [] _logs: List[Log] = [] - _tracing_service: "TracingService" + tracing_service: Optional["TracingService"] = None def update_state(self, name: str, value: Any): if not self.vertex: @@ -489,13 +489,10 @@ class CustomComponent(BaseComponent): message (LoggableType | list[LoggableType]): The message to log. """ if name is None: - name = self.display_name if self.display_name else self.__class__.__name__ - if hasattr(message, "model_dump") and isinstance(message, BaseModel): - message = message.model_dump() + name = self.display_name log = Log(message=message, type=get_artifact_type(message), name=name) self._logs.append(log) - if self.vertex: - self._tracing_service.add_log(trace_name=self.vertex.id, log=log) + self.tracing_service.add_log(trace_name=self.vertex.id, log=log) def post_code_processing(self, new_build_config: dict, current_build_config: dict): """ diff --git a/src/backend/base/langflow/services/tracing/service.py b/src/backend/base/langflow/services/tracing/service.py index 393b127f9..40af5a1a7 100644 --- a/src/backend/base/langflow/services/tracing/service.py +++ b/src/backend/base/langflow/services/tracing/service.py @@ -12,8 +12,8 @@ from loguru import logger from langflow.schema.data import Data from langflow.services.base import Service -from langflow.services.tracing.schema import Log from langflow.services.tracing.base import BaseTrace +from langflow.services.tracing.schema import Log if TYPE_CHECKING: from langflow.services.monitor.service import MonitorService From e456e70bc0b74fb41106efd5c3f6a275c5b40899 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 09:22:30 -0300 Subject: [PATCH 03/12] refactor: Remove unused methods and abstract class from BaseTracer --- .../base/langflow/services/tracing/base.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py index 2a5d609c9..c96388227 100644 --- a/src/backend/base/langflow/services/tracing/base.py +++ b/src/backend/base/langflow/services/tracing/base.py @@ -1,29 +1,16 @@ from abc import ABC, abstractmethod -from typing import Dict, Any +from typing import Any, Dict -class BaseTrace(ABC): - +class BaseTracer(ABC): @abstractmethod def ready(self): raise NotImplementedError - @abstractmethod - def setup_langsmith(self): - raise NotImplementedError - @abstractmethod def add_trace(self, trace_name: str, trace_type: str, inputs: Dict[str, Any], metadata: Dict[str, Any] = None): raise NotImplementedError - @abstractmethod - def _convert_to_langchain_types(self, io_dict: Dict[str, Any]): - raise NotImplementedError - - @abstractmethod - def _convert_to_langchain_type(self, value): - raise NotImplementedError - @abstractmethod def end_trace(self, trace_name: str, outputs: Dict[str, Any] = None, error: str = None): raise NotImplementedError From 7c0b77505d658d1a68d5127629d403081abd2f59 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 09:22:41 -0300 Subject: [PATCH 04/12] refactor: Update LangSmithTracer to use convert_to_langchain_types from utils.py --- .../base/langflow/services/tracing/utils.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/backend/base/langflow/services/tracing/utils.py diff --git a/src/backend/base/langflow/services/tracing/utils.py b/src/backend/base/langflow/services/tracing/utils.py new file mode 100644 index 000000000..a90c94c99 --- /dev/null +++ b/src/backend/base/langflow/services/tracing/utils.py @@ -0,0 +1,34 @@ +from typing import Any, Dict + +from langflow.schema.data import Data + + +def convert_to_langchain_type(value): + from langflow.schema.message import Message + + if isinstance(value, dict): + for key, _value in value.copy().items(): + _value = convert_to_langchain_type(_value) + value[key] = _value + elif isinstance(value, list): + value = [convert_to_langchain_type(v) for v in value] + elif isinstance(value, Message): + if "prompt" in value: + value = value.load_lc_prompt() + elif value.sender: + value = value.to_lc_message() + else: + value = value.to_lc_document() + elif isinstance(value, Data): + if "text" in value.data: + value = value.to_lc_document() + else: + value = value.data + return value + + +def convert_to_langchain_types(io_dict: Dict[str, Any]): + converted = {} + for key, value in io_dict.items(): + converted[key] = convert_to_langchain_type(value) + return converted From c6961539ee6cc1dd9badf6f7ed26bf3723160d37 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 14:24:25 -0300 Subject: [PATCH 05/12] chore: Update LangSmithTracer import in tracing service --- src/backend/base/langflow/services/tracing/service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/services/tracing/service.py b/src/backend/base/langflow/services/tracing/service.py index 40af5a1a7..fd96b3eb9 100644 --- a/src/backend/base/langflow/services/tracing/service.py +++ b/src/backend/base/langflow/services/tracing/service.py @@ -12,7 +12,7 @@ from loguru import logger from langflow.schema.data import Data from langflow.services.base import Service -from langflow.services.tracing.base import BaseTrace +from langflow.services.tracing.base import BaseTracer from langflow.services.tracing.schema import Log if TYPE_CHECKING: @@ -181,7 +181,7 @@ class TracingService(Service): self.outputs_metadata[trace_name] |= output_metadata or {} -class LangSmithTracer(BaseTrace): +class LangSmithTracer(BaseTracer): def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: str): from langsmith.run_trees import RunTree From c95036c9ac171046638d726adf07e0a3d66617b5 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 14:36:02 -0300 Subject: [PATCH 06/12] Refactor: handle error when retrieving messages from monitor service This commit refactors the code in the `migrate_messages_from_monitor_service_to_database` function to handle errors that may occur when retrieving messages from the monitor service. If an exception is raised, the error is logged and the function returns `False`. This ensures that the migration process can continue even if there is an issue with retrieving the messages. --- src/backend/base/langflow/services/database/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/services/database/utils.py b/src/backend/base/langflow/services/database/utils.py index fa40c725f..9500bcc50 100644 --- a/src/backend/base/langflow/services/database/utils.py +++ b/src/backend/base/langflow/services/database/utils.py @@ -18,8 +18,12 @@ def migrate_messages_from_monitor_service_to_database(session: Session) -> bool: from langflow.schema.message import Message from langflow.services.database.models.message import MessageTable - monitor_service = get_monitor_service() - messages_df = monitor_service.get_messages() + try: + monitor_service = get_monitor_service() + messages_df = monitor_service.get_messages() + except Exception as e: + logger.error(f"Error retrieving messages from monitor service: {e}") + return False if messages_df.empty: logger.info("No messages to migrate.") From ce0efda9902110c53cf01d39b6a2b08717041ed2 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:19:19 -0300 Subject: [PATCH 07/12] chore: Add metadata parameter to end method in BaseTracer This commit adds a new optional `metadata` parameter to the `end` method in the `BaseTracer` class. The `metadata` parameter allows for passing additional information related to the tracing process. This enhancement provides more flexibility and extensibility to the tracing functionality. --- src/backend/base/langflow/services/tracing/base.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py index c96388227..4ef640889 100644 --- a/src/backend/base/langflow/services/tracing/base.py +++ b/src/backend/base/langflow/services/tracing/base.py @@ -16,5 +16,10 @@ class BaseTracer(ABC): raise NotImplementedError @abstractmethod - def end(self, outputs: Dict[str, Any], error: str | None = None): + def end( + self, + outputs: Dict[str, Any], + error: str | None = None, + metadata: dict[str, Any] | None = None, + ): raise NotImplementedError From df2015160e10be2558e5a6d94e0cf15b97876a2b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:20:09 -0300 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=93=9D=20(base.py):=20add=20abstrac?= =?UTF-8?q?t=20method=20=5F=5Finit=5F=5F=20to=20BaseTracer=20class=20with?= =?UTF-8?q?=20required=20parameters=20to=20enforce=20implementation=20in?= =?UTF-8?q?=20subclasses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/base/langflow/services/tracing/base.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py index 4ef640889..3d855436d 100644 --- a/src/backend/base/langflow/services/tracing/base.py +++ b/src/backend/base/langflow/services/tracing/base.py @@ -3,6 +3,10 @@ from typing import Any, Dict class BaseTracer(ABC): + @abstractmethod + def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: UUID): + raise NotImplementedError + @abstractmethod def ready(self): raise NotImplementedError From d37ae29a3b280e61d2708d5e8deac5c9cd0f7d73 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:20:20 -0300 Subject: [PATCH 09/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(base.py):=20Refacto?= =?UTF-8?q?r=20add=5Ftrace=20and=20end=5Ftrace=20methods=20to=20use=20Unio?= =?UTF-8?q?n=20type=20for=20optional=20arguments=20to=20improve=20code=20r?= =?UTF-8?q?eadability=20and=20type=20safety?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/base/langflow/services/tracing/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py index 3d855436d..5b4605382 100644 --- a/src/backend/base/langflow/services/tracing/base.py +++ b/src/backend/base/langflow/services/tracing/base.py @@ -1,5 +1,6 @@ from abc import ABC, abstractmethod from typing import Any, Dict +from uuid import UUID class BaseTracer(ABC): @@ -12,11 +13,13 @@ class BaseTracer(ABC): raise NotImplementedError @abstractmethod - def add_trace(self, trace_name: str, trace_type: str, inputs: Dict[str, Any], metadata: Dict[str, Any] = None): + def add_trace( + self, trace_name: str, trace_type: str, inputs: Dict[str, Any], metadata: Dict[str, Any] | None = None + ): raise NotImplementedError @abstractmethod - def end_trace(self, trace_name: str, outputs: Dict[str, Any] = None, error: str = None): + def end_trace(self, trace_name: str, outputs: Dict[str, Any] | None = None, error: str | None = None): raise NotImplementedError @abstractmethod From 6b74d364cadb71b05b2a633524ebe6dccb013235 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:20:42 -0300 Subject: [PATCH 10/12] =?UTF-8?q?=F0=9F=90=9B=20(custom=5Fcomponent.py):?= =?UTF-8?q?=20fix=20issue=20where=20name=20was=20not=20being=20set=20corre?= =?UTF-8?q?ctly=20when=20name=20is=20None=20and=20self.display=5Fname=20is?= =?UTF-8?q?=20True?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/custom/custom_component/custom_component.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/custom/custom_component/custom_component.py b/src/backend/base/langflow/custom/custom_component/custom_component.py index 779618e9a..ee8192496 100644 --- a/src/backend/base/langflow/custom/custom_component/custom_component.py +++ b/src/backend/base/langflow/custom/custom_component/custom_component.py @@ -488,11 +488,14 @@ class CustomComponent(BaseComponent): Args: message (LoggableType | list[LoggableType]): The message to log. """ - if name is None: + if name is None and self.display_name: name = self.display_name + else: + name = self.__class__.__name__ log = Log(message=message, type=get_artifact_type(message), name=name) self._logs.append(log) - self.tracing_service.add_log(trace_name=self.vertex.id, log=log) + if self.tracing_service and self.vertex: + self.tracing_service.add_log(trace_name=self.vertex.id, log=log) def post_code_processing(self, new_build_config: dict, current_build_config: dict): """ From fa53def2d840bfa49fbddc055a00f77ce1f954c0 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:20:51 -0300 Subject: [PATCH 11/12] chore: Update LangSmithTracer constructor parameter type to UUID --- src/backend/base/langflow/services/tracing/service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/services/tracing/service.py b/src/backend/base/langflow/services/tracing/service.py index fd96b3eb9..4bdf17fa8 100644 --- a/src/backend/base/langflow/services/tracing/service.py +++ b/src/backend/base/langflow/services/tracing/service.py @@ -182,7 +182,7 @@ class TracingService(Service): class LangSmithTracer(BaseTracer): - def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: str): + def __init__(self, trace_name: str, trace_type: str, project_name: str, trace_id: UUID): from langsmith.run_trees import RunTree self.trace_name = trace_name @@ -293,7 +293,7 @@ class LangSmithTracer(BaseTracer): inputs: dict[str, Any], outputs: Dict[str, Any], error: str | None = None, - metadata: Optional[dict[str, Any]] = None, + metadata: dict[str, Any] | None = None, ): self._run_tree.add_metadata({"inputs": inputs, "metadata": metadata or {}}) self._run_tree.end(outputs=outputs, error=error) From 10e4a6606d9bfa7d3dbbae41c1f7d60a84addd84 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 27 Jun 2024 15:37:16 -0300 Subject: [PATCH 12/12] chore: Add inputs parameter to end method in BaseTracer --- src/backend/base/langflow/services/tracing/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/base/langflow/services/tracing/base.py b/src/backend/base/langflow/services/tracing/base.py index 5b4605382..2ec2eea26 100644 --- a/src/backend/base/langflow/services/tracing/base.py +++ b/src/backend/base/langflow/services/tracing/base.py @@ -25,6 +25,7 @@ class BaseTracer(ABC): @abstractmethod def end( self, + inputs: dict[str, Any], outputs: Dict[str, Any], error: str | None = None, metadata: dict[str, Any] | None = None,