Skip to content

Conversation

@aarondelgiudice
Copy link

@aarondelgiudice aarondelgiudice commented Sep 24, 2025

from_dict() method adds support for instantiating a LTRModelConfig object from a dictionary. Allows users to easily export/import LTRModelConfig objects as json objects.

Examples
--------
>>> from eland.ml.ltr import LTRModelConfig, QueryFeatureExtractor

>>> ltr_model_config = LTRModelConfig(
...     feature_extractors=[
...        QueryFeatureExtractor(
...            feature_name='title_bm25',
...            query={ "match": { "title": "{{query}}" } }
...        ),
...        QueryFeatureExtractor(
...            feature_name='descritption_bm25',
...            query={ "match": { "description": "{{query}}" } }
...        )
...     ]
... )
... # Optional, export 'ltr_model_config.to_dict()' to JSON for version control
...
... config_dict = < load config_dict from JSON>
... 
... # instantiate a new LTRModelConfig object
... ltr_model_config2 = LTRModelConfig.from_dict(config_dict)

Closes issues 807

@prodsecmachine
Copy link

prodsecmachine commented Sep 24, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a from_dict() class method to the LTRModelConfig class to enable instantiation from dictionary representations, supporting JSON import/export functionality for LTR model configurations.

  • Added from_dict() class method with comprehensive docstring and examples
  • Implemented parsing logic for query extractors from dictionary format
  • Added error handling for invalid configurations and unknown extractor types

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

... },
... {
... "query_extractor": {
... "feature_name": "description_bm25",
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

There's a spelling error in the example - 'description_bm25' should match the variable name 'descritption_bm25' used in the PR description, or the PR description should be corrected to use 'description_bm25'.

Copilot uses AI. Check for mistakes.
Comment on lines +198 to +200
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

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

Missing validation for the 'feature_extractors' key. If the dictionary doesn't contain this key, a KeyError will be raised. Add a check to ensure 'feature_extractors' exists in the nested dictionary.

Suggested change
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
if "feature_extractors" not in d[TYPE_LEARNING_TO_RANK]:
raise ValueError(
f"Invalid LTR model config, missing 'feature_extractors' key in '{TYPE_LEARNING_TO_RANK}'"
)
feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't instantiate a LTRModelConfig from JSON (e.g. via exported LTRModelConfig.to_dict())

3 participants