From becb8dd55942dc99c5c06f0a209a57827df6fe36 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:46:05 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Speed=20up=20method=20`Lan?= =?UTF-8?q?gSmithTracer.add=5Ftrace`=20by=2018%=20Here's=20the=20optimized?= =?UTF-8?q?=20version=20of=20the=20provided=20Python=20program.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Changes Made. 1. **Removed redundant import statement**: Moved `RunTree` import inside the outer try block to the module scope as it is imported unconditionally if `setup_langsmith` is ready. 2. **Optimized `_convert_to_langchain_types` method**: Used dictionary comprehension instead of a loop for conversion. 3. **Eliminated redundant step in `add_trace` method**: Direct dictionary comprehension in `processed_inputs`. --- src/backend/base/langflow/graph/vertex/base.py | 6 ++++-- .../base/langflow/services/tracing/langsmith.py | 15 ++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/backend/base/langflow/graph/vertex/base.py b/src/backend/base/langflow/graph/vertex/base.py index 4c012e7aacd4..ff1c19a5c2ec 100644 --- a/src/backend/base/langflow/graph/vertex/base.py +++ b/src/backend/base/langflow/graph/vertex/base.py @@ -14,14 +14,16 @@ from loguru import logger from langflow.exceptions.component import ComponentBuildError -from langflow.graph.schema import INPUT_COMPONENTS, OUTPUT_COMPONENTS, InterfaceComponentTypes, ResultData +from langflow.graph.schema import (INPUT_COMPONENTS, OUTPUT_COMPONENTS, + InterfaceComponentTypes, ResultData) from langflow.graph.utils import UnbuiltObject, UnbuiltResult, log_transaction from langflow.interface import initialize from langflow.interface.listing import lazy_load_dict from langflow.schema.artifact import ArtifactType from langflow.schema.data import Data from langflow.schema.message import Message -from langflow.schema.schema import INPUT_FIELD_NAME, OutputValue, build_output_logs +from langflow.schema.schema import (INPUT_FIELD_NAME, OutputValue, + build_output_logs) from langflow.services.deps import get_storage_service from langflow.utils.constants import DIRECT_TYPES from langflow.utils.schemas import ChatOutputResponse diff --git a/src/backend/base/langflow/services/tracing/langsmith.py b/src/backend/base/langflow/services/tracing/langsmith.py index dff06f675a2e..7ee662a2c55c 100644 --- a/src/backend/base/langflow/services/tracing/langsmith.py +++ b/src/backend/base/langflow/services/tracing/langsmith.py @@ -5,9 +5,11 @@ import types from datetime import datetime, timezone from typing import TYPE_CHECKING, Any +from uuid import UUID from loguru import logger +from langflow.graph.vertex.base import Vertex from langflow.schema.data import Data from langflow.services.tracing.base import BaseTracer @@ -73,24 +75,23 @@ def add_trace( ) -> None: if not self._ready or not self._run_tree: return - processed_inputs = {} - if inputs: - processed_inputs = self._convert_to_langchain_types(inputs) + + processed_inputs = self._convert_to_langchain_types(inputs) if inputs else {} + child = self._run_tree.create_child( name=trace_name, run_type=trace_type, # type: ignore[arg-type] inputs=processed_inputs, ) + if metadata: child.add_metadata(self._convert_to_langchain_types(metadata)) + self._children[trace_name] = child self._child_link: dict[str, str] = {} def _convert_to_langchain_types(self, io_dict: dict[str, Any]): - converted = {} - for key, value in io_dict.items(): - converted[key] = self._convert_to_langchain_type(value) - return converted + return {key: self._convert_to_langchain_type(value) for key, value in io_dict.items()} def _convert_to_langchain_type(self, value): from langflow.schema.message import Message