-
Notifications
You must be signed in to change notification settings - Fork 35
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
a more clean style code #2
Open
sanowl
wants to merge
1
commit into
GAIR-NLP:main
Choose a base branch
from
sanowl:feature/optimize-hybrid-retriever
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,68 @@ | ||
from llama_index.core.retrievers import BaseRetriever | ||
from service.field_selector import field_selector | ||
from service.date_selector import date_selector | ||
from config import category_list, qdrant_month_list, elastic_seach_month_list | ||
from config import category_list, qdrant_month_list, elastic_search_month_list | ||
import concurrent.futures | ||
|
||
def retrieve_bm25(elastic_search_retriever, query, need_categories, need_months): | ||
"""Retrieve nodes using BM25 algorithm.""" | ||
return elastic_search_retriever.custom_retrieve(query, need_categories, need_months) | ||
|
||
def retrieve_vector(qdrant_retriever, query, need_categories, need_months): | ||
"""Retrieve nodes using vector search.""" | ||
return qdrant_retriever.custom_retrieve(query, need_categories, need_months) | ||
|
||
class HybridRetriever(BaseRetriever): | ||
def __init__(self, qdrant_retriever, | ||
elastic_search_retriever, | ||
node_postprocessors): | ||
def __init__(self, qdrant_retriever, elastic_search_retriever, node_postprocessors): | ||
super().__init__() | ||
self.qdrant_retriever = qdrant_retriever | ||
self.elastic_search_retriever = elastic_search_retriever | ||
self.node_postprocessors = node_postprocessors | ||
|
||
def _retrieve(self, query): | ||
need_categories = field_selector(query) | ||
if len(need_categories) == 0: | ||
need_categories = category_list | ||
qdrant_need_months, es_need_month = date_selector(query, range_type="all") | ||
if qdrant_need_months is None: | ||
qdrant_need_months = qdrant_month_list | ||
if es_need_month is None: | ||
es_need_month = elastic_seach_month_list | ||
"""Retrieve nodes using both BM25 and vector search concurrently.""" | ||
need_categories = self._get_categories(query) | ||
qdrant_need_months, es_need_month = self._get_date_ranges(query) | ||
|
||
with concurrent.futures.ThreadPoolExecutor() as executor: | ||
future_bm25 = executor.submit(retrieve_bm25, self.elastic_search_retriever, query, need_categories, es_need_month) | ||
future_vector = executor.submit(retrieve_vector, self.qdrant_retriever, query, need_categories, qdrant_need_months) | ||
|
||
bm25_nodes = future_bm25.result() | ||
vector_nodes = future_vector.result() | ||
|
||
nodes = bm25_nodes + vector_nodes | ||
for postprocessor in self.node_postprocessors: | ||
nodes = postprocessor.postprocess_nodes(nodes, query) | ||
return nodes | ||
|
||
return self._postprocess_nodes(nodes, query) | ||
|
||
def custom_retrieve_vector(self, query: str): | ||
need_categories = field_selector(query) | ||
if len(need_categories) == 0: | ||
need_categories = category_list | ||
qdrant_need_months = date_selector(query, range_type="qdrant") | ||
if qdrant_need_months is None: | ||
qdrant_need_months = qdrant_month_list | ||
"""Retrieve nodes using only vector search.""" | ||
need_categories = self._get_categories(query) | ||
qdrant_need_months = date_selector(query, range_type="qdrant") or qdrant_month_list | ||
nodes = retrieve_vector(self.qdrant_retriever, query, need_categories, qdrant_need_months) | ||
for postprocessor in self.node_postprocessors: | ||
nodes = postprocessor.postprocess_nodes(nodes, query) | ||
return nodes | ||
|
||
return self._postprocess_nodes(nodes, query) | ||
|
||
def custom_retrieve_bm25(self, query: str): | ||
need_categories = field_selector(query) | ||
if len(need_categories) == 0: | ||
need_categories = category_list | ||
es_need_month = date_selector(query, range_type="elastic search") | ||
if es_need_month is None: | ||
es_need_month = elastic_seach_month_list | ||
"""Retrieve nodes using only BM25 search.""" | ||
need_categories = self._get_categories(query) | ||
es_need_month = date_selector(query, range_type="elastic search") or elastic_search_month_list | ||
nodes = retrieve_bm25(self.elastic_search_retriever, query, need_categories, es_need_month) | ||
return self._postprocess_nodes(nodes, query) | ||
|
||
def _get_categories(self, query): | ||
"""Get categories for the query.""" | ||
need_categories = field_selector(query) | ||
return need_categories if need_categories else category_list | ||
|
||
def _get_date_ranges(self, query): | ||
"""Get date ranges for Qdrant and Elasticsearch.""" | ||
qdrant_need_months, es_need_month = date_selector(query, range_type="all") | ||
return ( | ||
qdrant_need_months or qdrant_month_list, | ||
es_need_month or elastic_search_month_list | ||
) | ||
|
||
def _postprocess_nodes(self, nodes, query): | ||
"""Apply postprocessors to the retrieved nodes.""" | ||
for postprocessor in self.node_postprocessors: | ||
nodes = postprocessor.postprocess_nodes(nodes, query) | ||
return nodes | ||
return nodes |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two variables with the same name. If the paper ID is already in the dictionary, the numbering in cite_str will be incorrect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest of the code is fine. Thank you for your suggested improvement!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey sure I will fix it dw