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 _format_message() by 81% in embedchain/store/assistants.py #1261

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

Conversation

misrasaurabh1
Copy link
Contributor

Description

📄 _format_message() in embedchain/store/assistants.py

📈 Performance went up by 81% (0.81x faster)

⏱️ Runtime went down from 47.00μs to 25.90μs

Explanation and details

(click to show)

The given code mostly consists of class and method definitions, so there's limited room for optimization because the computational complexity is relatively low. However, there are minor changes that can be applied for a bit of efficiency:

Using is not None comparisons in place of or checks in the constructor could prevent potential issues with zero values (0, '', False, etc.), which are False in a boolean context.

Used c.__class__ is MessageContentText instead of isinstance(c, MessageContentText). This checks for the exact type, not subclasses, which is faster if no subclassing is being used.

Type of change

Please delete options that are not relevant.

  • Refactor (does not change functionality, e.g. code style improvements, linting)

How Has This Been Tested?

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

✅ 10 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import pytest  # used for our unit tests
from typing import cast
from unittest.mock import MagicMock

# Assuming the existence of these classes based on the function's usage
class ThreadMessage:
    def __init__(self, content):
        self.content = content

class MessageContentText:
    def __init__(self, value):
        self.text = MagicMock(value=value)

class MessageContentOther:
    pass
from embedchain.store.assistants import OpenAIAssistant
# unit tests

# Test normal case with text content
def test_format_message_with_text_content():
    content = [MessageContentText("Hello,"), MessageContentText("World!")]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "Hello, World!"

# Test empty content
def test_format_message_empty_content():
    thread_message = ThreadMessage([])
    assert OpenAIAssistant._format_message(thread_message) == ""

# Test content with non-text types
def test_format_message_with_non_text_content():
    content = [MessageContentText("Text"), MessageContentOther()]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "Text"

# Test content with None values
def test_format_message_with_none_values():
    content = [None, MessageContentText("Text"), None]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "Text"

# Test content with special characters and whitespace
def test_format_message_with_special_characters():
    content = [MessageContentText("Hello\n"), MessageContentText("\tWorld!")]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "Hello\n \tWorld!"

# Test content with long texts
def test_format_message_with_long_text():
    long_text = "A" * 10000
    content = [MessageContentText(long_text)]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == long_text

# Test content with non-string text values
def test_format_message_with_non_string_text_values():
    content = [MessageContentText(123), MessageContentText(True)]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "123 True"

# Test invalid input types
def test_format_message_with_invalid_input():
    with pytest.raises(AttributeError):
        OpenAIAssistant._format_message("Not a ThreadMessage")

# Test no text content
def test_format_message_no_text_content():
    content = [MessageContentOther(), MessageContentOther()]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == ""

# Test thread message with nested content
# This test assumes that nested content is not supported and should be ignored
def test_format_message_with_nested_content():
    nested_content = MessageContentText("Nested")
    content = [MessageContentText("Hello"), nested_content]
    thread_message = ThreadMessage(content)
    assert OpenAIAssistant._format_message(thread_message) == "Hello"

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Feb 16, 2024
Copy link

codecov bot commented Feb 16, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (2985b66) 56.60% compared to head (dd130d0) 56.59%.
Report is 20 commits behind head on main.

Files Patch % Lines
embedchain/store/assistants.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1261      +/-   ##
==========================================
- Coverage   56.60%   56.59%   -0.02%     
==========================================
  Files         146      146              
  Lines        5923     5955      +32     
==========================================
+ Hits         3353     3370      +17     
- Misses       2570     2585      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XS This PR changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant