Skip to content

Commit d165188

Browse files
author
Zhe Yu
committed
dynamically dispatch reranker class
1 parent 04b5c7c commit d165188

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/vectorcode/subcommands/query/reranker/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import sys
23

34
from vectorcode.cli_utils import Config
45

@@ -12,18 +13,21 @@
1213

1314

1415
def get_reranker(configs: Config) -> RerankerBase:
15-
if configs.reranker == "NaiveReranker" or not configs.reranker:
16+
if configs.reranker and hasattr(sys.modules[__name__], configs.reranker):
17+
# dynamic dispatch
18+
return getattr(sys.modules[__name__], configs.reranker)(configs)
19+
20+
# TODO: replace the following with an Exception before the release of 0.6.0.
21+
logger.warning(
22+
f""""reranker" option should be set to one of the following: {list(i for i in __all__ if i != "RerankerBase")}.
23+
To choose a CrossEncoderReranker model, you can set the "model_name_or_path" key in the "reranker_params" option to the name/path of the model.
24+
To use NaiveReranker, set the "reranker" option to "NaiveReranker".
25+
The old configuration syntax will be DEPRECATED in v0.6.0
26+
"""
27+
)
28+
if not configs.reranker:
1629
return NaiveReranker(configs)
17-
elif configs.reranker == "CrossEncoderReranker":
18-
return CrossEncoderReranker(configs)
1930
else:
20-
logger.warning(
21-
f"""
22-
"reranker" option should be set to one of the following: {list(i for i in __all__ if i != "RerankerBase")}.
23-
To choose a custom reranker model, you can set the "model_name_or_path" key in the "reranker_params" option to the name/path of the model.
24-
The old configuration syntax will be deprecated in v0.6.0
25-
"""
26-
)
2731
configs.reranker_params.update({"model_name_or_path": configs.reranker})
2832
configs.reranker = "CrossEncoderReranker"
2933
return CrossEncoderReranker(

0 commit comments

Comments
 (0)