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 get_return_intermediate_steps() by 7% in libs/langchain/langchain/chains/combine_documents/refine.py #31

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

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Feb 16, 2024

📄 get_return_intermediate_steps() in libs/langchain/langchain/chains/combine_documents/refine.py

📈 Performance went up by 7% (0.07x faster)

⏱️ Runtime went down from 13.00μs to 12.10μs

Explanation and details

(click to show)

The current function code provided is already efficient and cannot really be optimized further. However, you should consider if the "return_refine_steps" key lookup happens very often, and iff it does not, you may use try/catch method to avoid unnecessary key lookups. This method is faster when the key is usually not present.

Here's the function rewritten with the try/catch method.

In this case, the function will directly try to pop the "return_refine_steps" key from the dictionary. If the key is present, it removes the key from the dictionary and returns its value. If it's not, instead of returning a KeyError, it will just pass and return the original dictionary. The explicit deletion of the key from the dictionary is not needed as pop method takes care of this.

Correctness verification

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

✅ 0 Passed − ⚙️ Existing Unit Tests

✅ 0 Passed − 🎨 Inspired Regression Tests

✅ 18 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import pytest  # used for our unit tests
from pydantic import root_validator, BaseModel
from typing import Dict

# function to test

class BaseCombineDocumentsChain(BaseModel):
    pass
from langchain.chains.combine_documents.refine import RefineDocumentsChain

# unit tests

# Test standard functionality with the key present
def test_key_present():
    values = {"return_refine_steps": True}
    expected = {"return_intermediate_steps": True}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test absence of the key
def test_key_absent():
    values = {"some_other_key": "some_value"}
    expected = {"some_other_key": "some_value"}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test mixed presence of keys
def test_mixed_keys():
    values = {"return_refine_steps": True, "another_key": 123}
    expected = {"return_intermediate_steps": True, "another_key": 123}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test edge cases with unusual values
@pytest.mark.parametrize("value", [None, "", [], {}, set()])
def test_unusual_values(value):
    values = {"return_refine_steps": value}
    expected = {"return_intermediate_steps": value}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test with different data types
@pytest.mark.parametrize("value", ["yes", 1, 1.0, False])
def test_different_data_types(value):
    values = {"return_refine_steps": value}
    expected = {"return_intermediate_steps": value}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test nested data structures
def test_nested_data_structures():
    nested_dict = {"nested_key": "nested_value"}
    values = {"return_refine_steps": nested_dict}
    expected = {"return_intermediate_steps": nested_dict}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test invalid inputs
@pytest.mark.parametrize("invalid_value", [None, "not a dict", 123, [1, 2, 3]])
def test_invalid_inputs(invalid_value):
    with pytest.raises(TypeError):
        RefineDocumentsChain.get_return_intermediate_steps(invalid_value)

# Test dictionary with the new key already present
def test_new_key_already_present():
    values = {"return_refine_steps": True, "return_intermediate_steps": False}
    expected = {"return_intermediate_steps": True}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

# Test dictionary with the key as an empty string or other falsy value
@pytest.mark.parametrize("falsy_value", ["", 0, False])
def test_falsy_values(falsy_value):
    values = {"return_refine_steps": falsy_value}
    expected = {"return_intermediate_steps": falsy_value}
    assert RefineDocumentsChain.get_return_intermediate_steps(values) == expected

@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by CodeFlash AI label Feb 16, 2024
@codeflash-ai codeflash-ai bot requested a review from aphexcx February 16, 2024 16:06
Copy link

@aphexcx aphexcx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro optimization

Copy link

@aphexcx aphexcx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noisey, 1 microsceond improvement

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.

1 participant