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

Cache embeddings for recent thoughts to improve performance #69

Closed
sourcery-ai bot opened this issue Dec 7, 2024 · 0 comments · Fixed by #72
Closed

Cache embeddings for recent thoughts to improve performance #69

sourcery-ai bot opened this issue Dec 7, 2024 · 0 comments · Fixed by #72
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 7, 2024

Description

In the current implementation, embeddings for recent thoughts are recomputed each time the _analyze_patterns method is called. This can lead to unnecessary performance overhead, especially when the same thoughts are analyzed multiple times.

Suggested Improvement

To enhance performance, it is suggested to cache the embeddings for recent thoughts. This can be achieved by storing the embeddings in a thought_history attribute, which will prevent the need to recompute them repeatedly.

Proposed Solution

Introduce a caching mechanism within the _analyze_patterns method:

    def _analyze_patterns(self) -> float:
        if not hasattr(self, '_thought_embeddings'):
            self._thought_embeddings = {}

This change will ensure that embeddings are computed only once and reused in subsequent calls, thus improving the efficiency of the method.

Action Items

  • Implement the caching mechanism as suggested.
  • Test the implementation to ensure that it correctly caches and retrieves embeddings.
  • Measure performance improvements to validate the effectiveness of the caching strategy.

I created this issue for @leonvanbokhorst from #68 (comment).

Tips and commands

Interacting with Sourcery

  • Generate a plan of action: Comment @sourcery-ai plan on this issue.
  • Generate a pull request for this issue: Comment @sourcery-ai develop to
    generate a PR that addresses this issue.

Getting Help

@leonvanbokhorst leonvanbokhorst self-assigned this Dec 7, 2024
@leonvanbokhorst leonvanbokhorst added the enhancement New feature or request label Dec 7, 2024
@leonvanbokhorst leonvanbokhorst added this to the Phase 1 milestone Dec 7, 2024
leonvanbokhorst added a commit that referenced this issue Dec 7, 2024
Fixes #69

commit message: Implement LRU caching for embeddings to improve performance and maintainability
The commit adds LRU caching for embeddings using Python's built-in @lru_cache decorator. This change:
Adds a new cached _get_embedding method to avoid redundant embedding calculations
Refactors _analyze_thought_coherence and _analyze_patterns to use the cached method
Removes manual embedding caching logic in favor of Python's built-in LRU implementation
Improves performance by caching up to 100 most recently used embeddings
Makes the code more maintainable and thread-safe
The changes reduce code complexity while maintaining the same functionality and improving performance through efficient caching.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant