Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ Speed up method LangSmithTracer.add_trace by 18% in src/backend/base/langflow/services/tracing/langsmith.py #92

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 12, 2024

📄 LangSmithTracer.add_trace in src/backend/base/langflow/services/tracing/langsmith.py

✨ Performance Summary:

  • Speed Increase: 📈 18% (0.18x faster)
  • Runtime Reduction: ⏱️ From 3.20 microseconds down to 2.71 microseconds (best of 16 runs)

📝 Explanation and details

Here's the optimized version of the provided Python program.

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.

Correctness verification

The new optimized code was tested for correctness. The results are listed below:

Test Status Details
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 2 Passed See below
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Coverage 90.0%

🌀 Generated Regression Tests Details

Click to view details
from __future__ import annotations

from datetime import datetime, timezone
from typing import Any
from unittest.mock import MagicMock, patch
from uuid import UUID, uuid4

# imports
import pytest  # used for our unit tests
from langflow.graph.vertex.base import Vertex
from langflow.services.tracing.base import BaseTracer
from langflow.services.tracing.langsmith import LangSmithTracer
from loguru import logger

# unit tests

@pytest.fixture
def tracer():
    tracer = LangSmithTracer("test_trace", "simple", "test_project", uuid4())
    tracer._ready = True
    tracer._run_tree = MagicMock()
    tracer._run_tree.create_child = MagicMock()
    return tracer






def test_tracer_not_ready(tracer):
    # Test when tracer is not ready
    tracer._ready = False
    tracer.add_trace("6", "trace_not_ready", "simple", {}, None)
    tracer._run_tree.create_child.assert_not_called()










from __future__ import annotations

from datetime import datetime, timezone
from typing import Any
from unittest.mock import MagicMock, patch
from uuid import UUID

# imports
import pytest  # used for our unit tests
from langflow.graph.vertex.base import Vertex
from langflow.services.tracing.base import BaseTracer
from langflow.services.tracing.langsmith import LangSmithTracer
from loguru import logger


# Mock class for RunTree to be used in tests
class MockRunTree:
    def __init__(self):
        self.children = []
        self.events = []
        self.metadata = {}
    
    def add_event(self, event):
        self.events.append(event)
    
    def create_child(self, name, run_type, inputs):
        child = MockRunTree()
        self.children.append(child)
        return child
    
    def add_metadata(self, metadata):
        self.metadata.update(metadata)

# unit tests
@pytest.fixture
def tracer():
    tracer = LangSmithTracer("trace1", "type1", "project1", UUID("12345678123456781234567812345678"))
    tracer._ready = True
    tracer._run_tree = MockRunTree()
    return tracer







def test_add_trace_tracer_not_ready():
    tracer = LangSmithTracer("trace7", "type7", "project7", UUID("12345678123456781234567812345678"))
    tracer._ready = False
    tracer.add_trace("trace_id7", "not_ready", "type7", {"key1": "value1"}, {"meta1": "data1"})

📣 **Feedback**

If you have any feedback or need assistance, feel free to join our Discord community:

Discord

Here's the optimized version of the provided Python program.



### 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`.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 12, 2024
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 December 12, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡️ codeflash Optimization PR opened by Codeflash AI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants