elastic / elastic/eland

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

Open
#807 0 comments 0 reactions 1 assignee Claimed by @aarondelgiudice View on GitHub
enhancement topic:NLP
Dominant language
Python
Stars
693
Forks
112
PR merge metrics
No merged PRs in 30d

Description

The `LTRModelConfig` class provides the `to_dict()` method as easy way to export the object as a JSON:
```python
with open(, "w") as f:
json.dump(ltr_config.to_dict(), f, indent=4)
```
The exported JSON can be checked into version control and referenced later.

But the class doesn't support a way to instantiate a new `LTRModelConfig` object from an exported JSON:
```python
with open(, "r") as f:
config_dict = json.load(f)

def load_ltr_config(config_dict: dict) -> LTRModelConfig:
feature_extractors = []

for q in config_dict.get("learning_to_rank", {}).get("feature_extractors", {}):
q = q.get("query_extractor", {})
feature_name = q.get("feature_name")
query = q.get("query")

if feature_name and query:
feature_extractors.append(
QueryFeatureExtractor(feature_name=feature_name, query=query)
)

return LTRModelConfig(feature_extractors=feature_extractors)
```

A new method `from_dict()` would allow users to more easily import a LTR model config from JSON:
```python
@classmethod
def from_dict(cls, d: Mapping[str, Any]) -> "LTRModelConfig":
if TYPE_LEARNING_TO_RANK not in d:
raise ValueError(
f"Invalid LTR model config, missing '{TYPE_LEARNING_TO_RANK}' key"
)

feature_extractors = []
for feature_extractor in d[TYPE_LEARNING_TO_RANK]["feature_extractors"]:
if "query_extractor" in feature_extractor:
fe = feature_extractor["query_extractor"]
feature_extractors.append(
QueryFeatureExtractor(
feature_name=fe["feature_name"],
query=fe["query"],
default_score=fe.get("default_score"),
)
)
else:
raise ValueError(
f"Unknown feature extractor type: {list(feature_extractor.keys())}"
)

return cls(feature_extractors=feature_extractors)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.