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

fix: Loading of embedddings #22260

Merged
merged 3 commits into from May 20, 2024
Merged

fix: Loading of embedddings #22260

merged 3 commits into from May 20, 2024

Conversation

benjackwhite
Copy link
Contributor

Problem

We have a code path to load embeddings and it runs at startup which is pretty nasty because it depends on a request to openai. If that isn't available (noticed when locally devving) then the whole process fails :o

Changes

  • Change it to a lazy initialiser

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Does this work well for both Cloud and self-hosted?

How did you test this code?

@benjackwhite benjackwhite requested a review from daibhin May 11, 2024 05:57
@posthog-bot
Copy link
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, post a comment or remove the stale label – otherwise this will be closed in another week.

Copy link

coderabbitai bot commented May 20, 2024

Walkthrough

The embeddings_runner.py file now includes a lazy loading mechanism for the encoding object. This enhancement replaces the encoding variable with a _encoding variable of type Optional[tiktoken.Encoding]. Additionally, a get_encoding() function has been introduced to retrieve and cache the encoding object only when needed, optimizing performance and resource usage.

Changes

File Path Change Summary
ee/session_recordings/ai/... Introduced lazy loading for encoding object. Replaced encoding with _encoding, added get_encoding() function, and updated code to use get_encoding().encode(string).

In code we trust, as bytes align,
Lazy loading, oh so fine.
Encoding waits, now just in time,
To optimize, to peak, to prime.
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits Files that changed from the base of the PR and between 1bded42 and bf38c12.
Files selected for processing (1)
  • ee/session_recordings/ai/embeddings_runner.py (3 hunks)
Additional comments not posted (3)
ee/session_recordings/ai/embeddings_runner.py (3)

6-6: Ensure the new import Optional is used appropriately throughout the file.


26-26: The initialization of _encoding as None is a good practice for lazy loading.


206-206: Correct usage of get_encoding() ensures that encoding is loaded lazily, aligning with the PR's objectives.

Comment on lines +29 to +36
def get_encoding() -> tiktoken.Encoding:
global _encoding
if not _encoding:
# NOTE: This does an API request so we want to ensure we load it lazily and not at startup
# tiktoken.encoding_for_model(model_name) specifies encoder
# model_name = "text-embedding-3-small" for this usecase
_encoding = tiktoken.get_encoding("cl100k_base")
return _encoding
Copy link

Choose a reason for hiding this comment

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

The implementation of get_encoding supports lazy loading effectively. Consider adding error handling for the API request to tiktoken.get_encoding.

def get_encoding() -> tiktoken.Encoding:
    global _encoding
    if not _encoding:
        try:
            _encoding = tiktoken.get_encoding("cl100k_base")
        except Exception as e:
            logger.error("Failed to load encoding", error=str(e))
            raise
    return _encoding

@daibhin daibhin merged commit 33a0757 into master May 20, 2024
76 checks passed
@daibhin daibhin deleted the fix/local-dev-embeddings branch May 20, 2024 15:26
timgl pushed a commit that referenced this pull request May 21, 2024
thmsobrmlr pushed a commit that referenced this pull request May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants