Skip to content

Add composite embedders and pooling for hf models #1104

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

Merged

Conversation

nnethercott
Copy link
Contributor

@nnethercott nnethercott commented May 2, 2025

Pull Request

Related issue

Fixes #1099

What does this PR do?

  • Adds a new embedder of type CompositeEmbedder, adds pooling: PoolingOpt to HuggingFaceEmbedders
  • Adds a new pytest fixture enabling the experimental feature "compositeEmbedders" and a basic test to make the client can configure a composite embedder through a POST to /indexes/{index_uid}/settings/embedders

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

Summary by CodeRabbit

  • New Features

    • Added support for composite embedders, enabling the use of different embedders for indexing and searching.
    • Introduced new pooling options for HuggingFace embedders to customize how token embeddings are aggregated.
  • Tests

    • Added tests to validate the configuration and behavior of composite embedders.
    • Introduced a fixture to toggle composite embedders feature during testing.

@nnethercott nnethercott force-pushed the add-composite-embeds-add-pool-hf branch from 9416155 to f75e2dc Compare May 2, 2025 15:01
@nnethercott nnethercott changed the title Add composite embedders add pooling for hf models Add composite embedders and pooling for hf models May 2, 2025
Copy link
Contributor

coderabbitai bot commented May 20, 2025

Walkthrough

The changes add support for composite embedders and pooling strategies for HuggingFace embedders. A new CompositeEmbedder class is introduced, along with a PoolingType enum for HuggingFace embedders. The codebase is updated to handle these new embedder types, and tests are added to verify the functionality.

Changes

File(s) Change Summary
meilisearch/models/embedders.py Added PoolingType enum, pooling attribute to HuggingFaceEmbedder, new CompositeEmbedder class, and updated EmbedderType union.
meilisearch/index.py Extended logic to support CompositeEmbedder in embedder handling functions.
tests/conftest.py Added fixture to enable/disable the compositeEmbedders experimental feature for testing.
tests/settings/test_settings_embedders.py Added test for composite embedder format, verifying structure and attribute presence.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Case
    participant API as Meilisearch API
    participant Index as Index Logic
    participant Embedder as CompositeEmbedder

    Test->>API: Update embedders with composite config
    API->>Index: update_embedders()
    Index->>Embedder: Instantiate CompositeEmbedder
    Embedder->>Index: Return composite embedder instance
    Index->>API: Update complete
    Test->>API: Get embedder settings
    API->>Index: get_settings()
    Index->>Embedder: Retrieve composite embedder
    Embedder->>Index: Return composite embedder structure
    Index->>API: Return embedder settings
    API->>Test: Respond with composite embedder info
Loading

Assessment against linked issues

Objective Addressed Explanation
Add new pooling parameter for huggingFace embedders (#1099)
Add new composite source for embedders (#1099)

Possibly related PRs

Suggested labels

enhancement

Poem

In burrows deep, a change takes flight,
Composite embedders join the night.
HuggingFace pools with style anew,
Enum and tests—oh what a view!
With fixtures set and logic clear,
This bunny hops with coder cheer.
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 89a9083 and fd0d285.

📒 Files selected for processing (1)
  • meilisearch/models/embedders.py (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • meilisearch/models/embedders.py
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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
Contributor

@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

🧹 Nitpick comments (1)
tests/settings/test_settings_embedders.py (1)

194-223: Well-structured test for composite embedders

The test correctly uses the enable_composite_embedders fixture and properly validates the structure and types of the composite embedder components.

Consider these improvements:

  1. Remove the print(embedders) statement on line 214 as it's not needed for the test.
  2. Update the assertion on line 214 from embedders.embedders["composite"] to embedders.embedders["default"] to match the key used when creating the embedder on line 203.
  3. Add a test to verify the pooling attribute of HuggingFaceEmbedder which was also added in this PR.
- print(embedders)
- assert embedders.embedders["composite"].source == "composite"
+ assert embedders.embedders["default"].source == "composite"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 2b0bd13 and 9903f95.

📒 Files selected for processing (4)
  • meilisearch/index.py (4 hunks)
  • meilisearch/models/embedders.py (5 hunks)
  • tests/conftest.py (1 hunks)
  • tests/settings/test_settings_embedders.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/conftest.py (1)
meilisearch/_httprequests.py (1)
  • patch (99-107)
🔇 Additional comments (9)
tests/conftest.py (1)

277-294: Implementation looks good!

The implementation of the enable_composite_embedders fixture follows the established pattern of other experimental feature toggles in this file. It properly enables the feature before yielding control to the test and disables it afterward, using appropriate HTTP PATCH requests with authentication and timeout settings.

meilisearch/index.py (4)

35-35: Good addition to the imports

The CompositeEmbedder import has been properly added to the existing imports from the meilisearch.models.embedders module.


981-982: Properly implemented condition handling for composite embedders

The addition of the composite embedder handling in the get_settings method follows the existing pattern for other embedder types and is implemented correctly.


940-941: Consistent implementation for get_embedders method

The implementation for handling composite embedders in the get_embedders method is consistent with the implementation in get_settings and other embedder types.


1985-1986: Consistent implementation for update_embedders method

The implementation for handling composite embedders in the update_embedders method matches the pattern established in the other methods and for other embedder types.

tests/settings/test_settings_embedders.py (1)

3-9: Added necessary imports for the new test

The imports have been properly updated to include pytest and the CompositeEmbedder class from the embedders module.

meilisearch/models/embedders.py (3)

24-40: Well-defined enumeration for pooling strategies

The PoolingType enumeration provides a clear set of options for HuggingFace embedder pooling strategies with good documentation.


101-114: Proper integration of pooling parameter into HuggingFaceEmbedder

The pooling parameter has been correctly added to the HuggingFaceEmbedder class with appropriate typing and documentation.


253-253: EmbedderType properly updated

The EmbedderType union alias has been correctly updated to include the new CompositeEmbedder type.

nnethercott and others added 3 commits May 20, 2025 14:44
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Member

@brunoocasali brunoocasali left a comment

Choose a reason for hiding this comment

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

hey there! the code looks nice, but the tests are failing, can you check the CI?

Thanks a lot for contributing to Meilisearch! <3

@nnethercott
Copy link
Contributor Author

hey there! the code looks nice, but the tests are failing, can you check the CI?

Thanks a lot for contributing to Meilisearch! <3

haha just seeing now that a diff suggested by coderabbitai I accepted messed up the class, ultimately my bad but also weird

@nnethercott nnethercott requested a review from brunoocasali May 26, 2025 18:20
Copy link
Contributor

@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

🧹 Nitpick comments (1)
tests/settings/test_settings_embedders.py (1)

225-234: Replace getattr calls with direct attribute access.

The static analysis correctly identifies that getattr with constant attribute names is unnecessary and less readable than direct attribute access.

-    # ensure search_embedder has no document_template
-    assert getattr(embedders.embedders["composite"].search_embedder, "document_template") is None
-    assert (
-        getattr(
-            embedders.embedders["composite"].search_embedder,
-            "document_template_max_bytes",
-        )
-        is None
-    )
-    assert getattr(embedders.embedders["composite"].indexing_embedder, "document_template")
+    # ensure search_embedder has no document_template
+    assert embedders.embedders["composite"].search_embedder.document_template is None
+    assert embedders.embedders["composite"].search_embedder.document_template_max_bytes is None
+    assert embedders.embedders["composite"].indexing_embedder.document_template
🧰 Tools
🪛 Ruff (0.11.9)

226-226: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


228-231: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


234-234: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c8dbe5 and 89a9083.

📒 Files selected for processing (2)
  • meilisearch/models/embedders.py (5 hunks)
  • tests/settings/test_settings_embedders.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • meilisearch/models/embedders.py
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/settings/test_settings_embedders.py (3)
meilisearch/models/embedders.py (5)
  • CompositeEmbedder (216-252)
  • HuggingFaceEmbedder (78-114)
  • OpenAiEmbedder (42-75)
  • PoolingType (24-39)
  • UserProvidedEmbedder (195-213)
tests/conftest.py (1)
  • empty_index (109-117)
meilisearch/index.py (4)
  • update_embedders (1950-1997)
  • update (106-128)
  • wait_for_task (232-259)
  • get_embedders (1908-1948)
🪛 Ruff (0.11.9)
tests/settings/test_settings_embedders.py

226-226: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


228-231: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)


234-234: Do not call getattr with a constant attribute value. It is not any safer than normal property access.

Replace getattr with attribute access

(B009)

🔇 Additional comments (3)
tests/settings/test_settings_embedders.py (3)

3-11: LGTM! Imports are correctly added for new functionality.

The new imports support the composite embedder feature and pooling functionality being tested.


197-197: LGTM! Proper use of pytest fixture for experimental feature.

The enable_composite_embedders fixture correctly enables the experimental composite embedders feature for this test.


198-224: LGTM! Comprehensive test structure for composite embedder.

The test correctly:

  • Creates a composite embedder configuration
  • Updates the index and waits for completion
  • Verifies the embedder types and structure
  • Tests serialization roundtrip

@brunoocasali brunoocasali added the enhancement New feature or request label Jun 6, 2025
Copy link
Member

@brunoocasali brunoocasali left a comment

Choose a reason for hiding this comment

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

LGTM!

Thanks for contributing to Meilisearch <3!

@brunoocasali
Copy link
Member

bors merge

Copy link
Contributor

meili-bors bot commented Jun 6, 2025

@meili-bors meili-bors bot merged commit b3cc7c7 into meilisearch:main Jun 6, 2025
12 checks passed
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 this pull request may close these issues.

[v1.14] Add composite embedders and pooling for huggingface embedders (experimental)
2 participants