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

Draft [In Progress]: draft to allow for multiple retrieval backends #23

Merged
merged 13 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions chatnoir_pyterrier/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from typing import Set, Optional, Iterable, Union, Any, Dict

from chatnoir_api import Index, Result, Slop, ExplainedResult
from chatnoir_api.model import SearchMethod
from chatnoir_api.v1 import (
search, search_phrases
)
from chatnoir_api.defaults import (
DEFAULT_INDEX, DEFAULT_SLOP, DEFAULT_RETRIES, DEFAULT_BACKOFF_SECONDS, DEFAULT_API_KEY
DEFAULT_INDEX, DEFAULT_SLOP, DEFAULT_RETRIES, DEFAULT_BACKOFF_SECONDS, DEFAULT_API_KEY, DEFAULT_SEARCH_METHOD
)
from pandas import DataFrame
from pandas.core.groupby import DataFrameGroupBy
Expand All @@ -35,13 +36,14 @@ class ChatNoirRetrieve(Transformer):
backoff_seconds: float = DEFAULT_BACKOFF_SECONDS
verbose: bool = False
api_key: str = DEFAULT_API_KEY
search_method: SearchMethod = DEFAULT_SEARCH_METHOD

def _merge_result(
self,
row: Dict[str, Any],
result: Union[
Result, ExplainedResult,
]
self,
row: Dict[str, Any],
result: Union[
Result, ExplainedResult,
]
) -> Dict[str, Any]:
row = {
**row,
Expand Down Expand Up @@ -135,6 +137,7 @@ def _transform_query(self, topic: DataFrame) -> DataFrame:
retries=self.retries,
backoff_seconds=self.backoff_seconds,
api_key=self.api_key,
search_method=self.search_method
).results
else:
results = search(
Expand All @@ -147,6 +150,7 @@ def _transform_query(self, topic: DataFrame) -> DataFrame:
retries=self.retries,
backoff_seconds=self.backoff_seconds,
api_key=self.api_key,
search_method=self.search_method
).results
else:
if explain:
Expand All @@ -161,6 +165,7 @@ def _transform_query(self, topic: DataFrame) -> DataFrame:
retries=self.retries,
backoff_seconds=self.backoff_seconds,
api_key=self.api_key,
search_method=self.search_method
).results
else:
results = search_phrases(
Expand All @@ -174,6 +179,7 @@ def _transform_query(self, topic: DataFrame) -> DataFrame:
retries=self.retries,
backoff_seconds=self.backoff_seconds,
api_key=self.api_key,
search_method=self.search_method
).results

if self.filter_unknown:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
"Topic :: Scientific/Engineering",
]
dependencies = [
"chatnoir-api~=3.1.3",
"chatnoir-api~=3.2.0",
"importlib-metadata~=8.5",
"pandas~=2.0",
"python-terrier>=0.11,<0.13",
Expand Down
10 changes: 6 additions & 4 deletions tests/test_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@


def test_retrieve_hash(api_key: str):
retrieve = ChatNoirRetrieve(api_key)
retrieve = ChatNoirRetrieve(
api_key=api_key,
)
retrieve_hash = hash(retrieve)
assert isinstance(retrieve_hash, int)

Expand All @@ -26,9 +28,9 @@ def test_retrieve_query(api_key: str, query: str, index: Index):


def test_retrieve_feature(
api_key: str,
query: str,
feature: Feature,
api_key: str,
query: str,
feature: Feature,
):
retrieve = ChatNoirRetrieve(
api_key=api_key,
Expand Down
Loading