Skip to content

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Feb 6, 2025

⚡️ This pull request contains optimizations for PR #6028

If you approve this dependent PR, these changes will be merged into the original PR branch PlaygroundPage.

This PR will be automatically closed if the original PR is merged.


📄 11,770% (117.70x) speedup for TavilySearchComponent.fetch_content in src/backend/base/langflow/components/tools/tavily.py

⏱️ Runtime : 613 milliseconds 5.16 milliseconds (best of 13 runs)

📝 Explanation and details

Here's the optimized version of the original program.

Key Changes.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 27 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage undefined
🌀 Generated Regression Tests Details
from unittest.mock import Mock, patch

import httpx
# imports
import pytest  # used for our unit tests
from langflow.components.tools.tavily import TavilySearchComponent
from langflow.custom import Component
from langflow.schema import Data
from loguru import logger


# unit tests
@pytest.fixture
def component():
    component = TavilySearchComponent()
    component.api_key = "test_api_key"
    component.query = "test_query"
    component.search_depth = 3
    component.topic = "test_topic"
    component.max_results = 5
    component.include_images = False
    component.include_answer = False
    component.time_range = "last_week"
    return component

def test_successful_single_result(component):
    """Test successful API response with a single result."""
    response_data = {
        "results": [
            {
                "title": "Test Title",
                "url": "http://example.com",
                "content": "Test content",
                "score": 0.9
            }
        ]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_successful_multiple_results(component):
    """Test successful API response with multiple results."""
    response_data = {
        "results": [
            {
                "title": "Test Title 1",
                "url": "http://example.com/1",
                "content": "Test content 1",
                "score": 0.9
            },
            {
                "title": "Test Title 2",
                "url": "http://example.com/2",
                "content": "Test content 2",
                "score": 0.8
            }
        ]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_successful_no_results(component):
    """Test successful API response with no results."""
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_include_answer(component):
    """Test API response with an answer included."""
    component.include_answer = True
    response_data = {
        "answer": "This is the answer",
        "results": []
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_include_images(component):
    """Test API response with images included."""
    component.include_images = True
    response_data = {
        "results": [],
        "images": ["image1.jpg", "image2.jpg"]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_http_status_error(component):
    """Test handling of HTTP status errors."""
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=404, text="Not Found")
        mock_post.return_value.raise_for_status.side_effect = httpx.HTTPStatusError(
            "HTTP error", request=Mock(), response=mock_post.return_value
        )
        codeflash_output = component.fetch_content()

def test_request_error(component):
    """Test handling of request errors."""
    with patch('httpx.Client.post') as mock_post:
        mock_post.side_effect = httpx.RequestError("Request error")
        codeflash_output = component.fetch_content()


def test_empty_payload_fields(component):
    """Test with some empty payload fields."""
    component.query = ""
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_large_payload(component):
    """Test with a large payload."""
    component.query = "a" * 1000
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_special_characters_in_query(component):
    """Test with special characters in the query."""
    component.query = "!@#$%^&*()"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_non_string_data_types(component):
    """Test with non-string data types in the payload."""
    component.max_results = 10
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_large_number_of_results(component):
    """Test with a large number of results."""
    response_data = {
        "results": [{"title": f"Title {i}", "url": f"http://example.com/{i}", "content": f"Content {i}", "score": 0.9} for i in range(1000)]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_large_content_size(component):
    """Test with large content size."""
    large_content = "a" * 10000
    response_data = {
        "results": [{"title": "Large Content", "url": "http://example.com", "content": large_content, "score": 0.9}]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_logging_errors(component, caplog):
    """Test logging of errors."""
    with patch('httpx.Client.post') as mock_post:
        mock_post.side_effect = httpx.RequestError("Request error")
        component.fetch_content()

def test_status_update(component):
    """Test status update after fetching content."""
    response_data = {
        "results": [{"title": "Test Title", "url": "http://example.com", "content": "Test content", "score": 0.9}]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        component.fetch_content()

def test_specific_field_combinations(component):
    """Test specific field combinations in the response."""
    response_data = {
        "results": [
            {"title": "Title 1", "url": "http://example.com/1", "content": "Content 1"},
            {"title": "Title 2", "content": "Content 2"}
        ]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_different_time_ranges(component):
    """Test with different time ranges."""
    component.time_range = "last_month"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_valid_api_key(component):
    """Test with a valid API key."""
    component.api_key = "valid_api_key"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_invalid_api_key(component):
    """Test with an invalid API key."""
    component.api_key = "invalid_api_key"
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=401, text="Unauthorized")
        mock_post.return_value.raise_for_status.side_effect = httpx.HTTPStatusError(
            "HTTP error", request=Mock(), response=mock_post.return_value
        )
        codeflash_output = component.fetch_content()

def test_short_query(component):
    """Test with a short query."""
    component.query = "a"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_long_query(component):
    """Test with a long query."""
    component.query = "a" * 1000
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_query_with_spaces(component):
    """Test with a query containing spaces."""
    component.query = "test query with spaces"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_different_search_depths(component):
    """Test with different search depths."""
    component.search_depth = 5
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_different_topics(component):
    """Test with different topics."""
    component.topic = "new_topic"
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_different_max_results(component):
    """Test with different max results."""
    component.max_results = 10
    response_data = {"results": []}
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_include_images_and_answer_together(component):
    """Test with both include_images and include_answer set to True."""
    component.include_images = True
    component.include_answer = True
    response_data = {
        "answer": "This is the answer",
        "results": [],
        "images": ["image1.jpg", "image2.jpg"]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()

def test_mixed_data_types_in_response(component):
    """Test with mixed data types in the response."""
    response_data = {
        "results": [
            {"title": "Title", "url": "http://example.com", "content": "Content", "score": "0.9"}
        ]
    }
    with patch('httpx.Client.post') as mock_post:
        mock_post.return_value = Mock(status_code=200, json=lambda: response_data)
        codeflash_output = component.fetch_content()
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

Codeflash

…n PR #6028 (`PlaygroundPage`)

Here's the optimized version of the original program.

### Key Changes.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Feb 6, 2025
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 6, 2025
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 size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant