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

Colbert local mode support both as retriever and reranker. #797

Merged
merged 32 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9632e5e
return metadata changes
Athe-kunal Apr 4, 2024
e415f39
Merge branch 'main' of https://github.com/Athe-kunal/dspy
Athe-kunal Apr 4, 2024
a4b3844
add metadata changes
Athe-kunal Apr 4, 2024
321a768
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 5, 2024
6cd1d56
add support for returning metadata and reranking
Athe-kunal Apr 6, 2024
eeafacb
colbert integration
Athe-kunal Apr 8, 2024
1639bd2
colbert local modifications
Athe-kunal Apr 8, 2024
ec062b6
kwargs filtered ids
Athe-kunal Apr 8, 2024
987d923
colbert return
Athe-kunal Apr 8, 2024
9ff5b28
colbert retriever and reranker
Athe-kunal Apr 9, 2024
825a272
colbert retriever error fixes
Athe-kunal Apr 9, 2024
c25e9c4
colbert config changes in __init__
Athe-kunal Apr 10, 2024
ab5b12e
colbert notebook
Athe-kunal Apr 10, 2024
63dd534
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 10, 2024
f6a9293
import errors for colbert
Athe-kunal Apr 10, 2024
197a2c2
improt dspy fixes and linting fixes
Athe-kunal Apr 10, 2024
4698b00
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 13, 2024
81d142f
PR fixes for colbert
Athe-kunal Apr 13, 2024
b73753c
making the linting gods happy
Athe-kunal Apr 13, 2024
0ec1ded
remove unnecessary outputs
Athe-kunal Apr 14, 2024
567d5c4
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 17, 2024
685df2a
colbertv2 docs
Athe-kunal Apr 17, 2024
fa2bc20
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 19, 2024
509b36c
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 20, 2024
34328fd
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 22, 2024
146ec7b
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 26, 2024
f0437e3
Merge branch 'stanfordnlp:main' into main
Athe-kunal Apr 29, 2024
9cb522b
Colbert PR fixes
Athe-kunal Apr 29, 2024
ec4b9b3
linting fixes
Athe-kunal Apr 29, 2024
326ce01
more linting fixes
Athe-kunal Apr 29, 2024
b5913fc
fixing previous cache breaks with separate funcs
Athe-kunal Jun 8, 2024
c60fadc
Merge branch 'main' into main
arnavsinghvi11 Jun 15, 2024
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
Prev Previous commit
Next Next commit
Colbert PR fixes
  • Loading branch information
Athe-kunal committed Apr 29, 2024
commit 9cb522bb6bd05249fa27c7ff258df6bdba5cfc32
2 changes: 1 addition & 1 deletion dsp/modules/colbertv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def forward(self,query:str,k:int=7,**kwargs):
searcher_results = self.searcher.search(query, k=k)
results = []
for pid,rank,score in zip(*searcher_results):
results.append(dotdict({'long_text':self.searcher.collection[pid],'pid':pid}))
results.append(dotdict({'long_text':self.searcher.collection[pid],'score':score,'pid':pid}))
return results

class ColBERTv2RerankerLocal:
Expand Down
107 changes: 92 additions & 15 deletions dsp/primitives/search.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import warnings
import logging
from collections.abc import Iterable

import numpy as np

import dsp

logger = logging.getLogger(__name__)

# def retrieve(query: str, k: int, **kwargs) -> list[str]:
# """Retrieves passages from the RM for the query and returns the top k passages."""
# if not dsp.settings.rm:
# raise AssertionError("No RM is loaded.")
# passages = dsp.settings.rm(query, k=k, **kwargs)
# if not isinstance(passages, Iterable):
# # it's not an iterable yet; make it one.
# # TODO: we should unify the type signatures of dspy.Retriever
# passages = [passages]
# passages = [psg.long_text for psg in passages]

# if dsp.settings.reranker:
# passages_cs_scores = dsp.settings.reranker(query, passages)
# passages_cs_scores_sorted = np.argsort(passages_cs_scores)[::-1]
# passages = [passages[idx] for idx in passages_cs_scores_sorted]


# return passages
def retrieve(query: str, k: int, **kwargs) -> list[str]:
"""Retrieves passages from the RM for the query and returns the top k passages."""

if not dsp.settings.rm:
raise AssertionError("No RM is loaded.")
if not dsp.settings.reranker:
warnings.warn("If you want to use the Reranker, please use dspy.RetrieveThenRerank",DeprecationWarning)
passages = dsp.settings.rm(query, k=k, **kwargs)
if not isinstance(passages, Iterable):
# it's not an iterable yet; make it one.
Expand All @@ -21,50 +39,109 @@ def retrieve(query: str, k: int, **kwargs) -> list[str]:
return passages


def retrieveRerankEnsemble(queries: list[str], k: int,**kwargs) -> list[str]:
# def retrieveRerankEnsemble(queries: list[str], k: int,**kwargs) -> list[str]:
# if not (dsp.settings.rm and dsp.settings.reranker):
# raise AssertionError("Both RM and Reranker are needed to retrieve & re-rank.")
# queries = [q for q in queries if q]
# passages = {}
# for query in queries:
# retrieved_passages = dsp.settings.rm(query, k=k*3,**kwargs)
# passages_cs_scores = dsp.settings.reranker(query, [psg.long_text for psg in retrieved_passages])
# for idx in np.argsort(passages_cs_scores)[::-1]:
# psg = retrieved_passages[idx]
# passages[psg.long_text] = passages.get(psg.long_text, []) + [
# passages_cs_scores[idx],
# ]


# passages = [(np.average(score), text) for text, score in passages.items()]
# return [text for _, text in sorted(passages, reverse=True)[:k]]
def retrieveRerankEnsemble(queries: list[str], k: int, **kwargs) -> list[str]:
if not (dsp.settings.rm and dsp.settings.reranker):
raise AssertionError("Both RM and Reranker are needed to retrieve & re-rank.")
queries = [q for q in queries if q]
all_queries_passages = []
for query in queries:
passages = []
retrieved_passages = dsp.settings.rm(query, k=k*3,**kwargs)
passages_cs_scores = dsp.settings.reranker(query,passages=[psg["long_text"] for psg in retrieved_passages])
retrieved_passages = dsp.settings.rm(query, k=k * 3, **kwargs)
passages_cs_scores = dsp.settings.reranker(
query, passages=[psg["long_text"] for psg in retrieved_passages]
)
for idx in np.argsort(passages_cs_scores)[::-1][:k]:
curr_passage = retrieved_passages[idx]
curr_passage['rerank_score'] = passages_cs_scores[idx]
curr_passage["rerank_score"] = passages_cs_scores[idx]
passages.append(curr_passage)
all_queries_passages.append(passages)
if len(queries) == 1:
return all_queries_passages[0]
else:
return all_queries_passages

def retrieveEnsemble(queries: list[str], k: int, by_prob: bool = True,**kwargs) -> list[str]:

# def retrieveEnsemble(queries: list[str], k: int, by_prob: bool = True,**kwargs) -> list[str]:
# """Retrieves passages from the RM for each query in queries and returns the top k passages
# based on the probability or score.
# """
# if not dsp.settings.rm:
# raise AssertionError("No RM is loaded.")
# if dsp.settings.reranker:
# return retrieveRerankEnsemble(queries, k)

# queries = [q for q in queries if q]

# if len(queries) == 1:
# return retrieve(queries[0], k, **kwargs)

# passages = {}
# for q in queries:
# for psg in dsp.settings.rm(q, k=k * 3,**kwargs):
# if by_prob:
# passages[psg.long_text] = passages.get(psg.long_text, 0.0) + psg.prob
# else:
# passages[psg.long_text] = passages.get(psg.long_text, 0.0) + psg.score

# passages = [(score, text) for text, score in passages.items()]
# passages = sorted(passages, reverse=True)[:k]
# passages = [text for _, text in passages]


# return passages
def retrieveEnsemble(
queries: list[str], k: int, by_prob: bool = True, **kwargs
) -> list[str]:
"""Retrieves passages from the RM for each query in queries and returns the top k passages
based on the probability or score.
"""

if not dsp.settings.rm:
raise AssertionError("No RM is loaded.")
if not dsp.settings.reranker:
warnings.warn("If you want to use the Reranker, please use dspy.RetrieveThenRerank. The reranking is ignored here.",DeprecationWarning)
Copy link
Collaborator

Choose a reason for hiding this comment

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

logging here too

Copy link
Collaborator

Choose a reason for hiding this comment

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

as with above -

"DeprecationWarning: 'display' has been deprecated. To see all information for debugging, use 'dspy.set_log_level('debug')'. In the future this will raise an error.",

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as with above-
The dspy logger object is not available in the dsp folder, hence I followed logging as done here for anthropic LM. Is there a better way to log this?


logger.warn(
"DeprecationWarning: 'dspy.Retrieve' for reranking has been deprecated, please use dspy.RetrieveThenRerank. The reranking is ignored here. In the future this will raise an error."
)

queries = [q for q in queries if q]

if len(queries) == 1:
return retrieve(queries[0], k)
all_queries_passages = []
for q in queries:
passages = {}
retrieved_passages = dsp.settings.rm(q, k=k * 3,**kwargs)
for idx,psg in enumerate(retrieved_passages):
retrieved_passages = dsp.settings.rm(q, k=k * 3, **kwargs)
for idx, psg in enumerate(retrieved_passages):
if by_prob:
passages[(idx,psg.long_text)] = passages.get(psg.long_text, 0.0) + psg.prob
passages[(idx, psg.long_text)] = (
passages.get(psg.long_text, 0.0) + psg.prob
)
else:
passages[(idx,psg.long_text)] = passages.get(psg.long_text, 0.0) + psg.score
passages[(idx, psg.long_text)] = (
passages.get(psg.long_text, 0.0) + psg.score
)
retrieved_passages[idx]["tracking_idx"] = idx
passages = sorted(passages.items(), key=lambda item: item[1])[:k]
req_indices = [psg[0][0] for psg in passages]
passages = [rp for rp in retrieved_passages if rp.get("tracking_idx") in req_indices]
passages = [
rp for rp in retrieved_passages if rp.get("tracking_idx") in req_indices
]
all_queries_passages.append(passages)
return all_queries_passages
33 changes: 18 additions & 15 deletions dspy/retrieve/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
from dspy.predict.parameter import Parameter
from dspy.primitives.prediction import Prediction

def single_query_passage(passages):
passages_dict = {key:[] for key in list(passages[0].keys())}
for docs in passages:
for key,value in docs.items():
passages_dict[key].append(value)
if "long_text" in passages_dict:
passages_dict["passages"] = passages_dict.pop("long_text")
return Prediction(**passages_dict)

class Retrieve(Parameter):
name = "Search"
Expand All @@ -30,6 +38,14 @@ def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)

def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = None,**kwargs) -> Union[Prediction,List[Prediction]]:
# queries = [query_or_queries] if isinstance(query_or_queries, str) else query_or_queries
# queries = [query.strip().split('\n')[0].strip() for query in queries]

# # print(queries)
# # TODO: Consider removing any quote-like markers that surround the query too.
# k = k if k is not None else self.k
# passages = dsp.retrieveEnsemble(queries, k=k,**kwargs)
# return Prediction(passages=passages)
queries = [query_or_queries] if isinstance(query_or_queries, str) else query_or_queries
queries = [query.strip().split('\n')[0].strip() for query in queries]

Expand All @@ -51,14 +67,7 @@ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = No
return pred_returns
elif isinstance(passages[0], Dict):
#passages dict will contain {"long_text":long_text_list,"metadatas";metadatas_list...}
passages_dict = {key:[] for key in list(passages[0].keys())}

for psg in passages:
for key,value in psg.items():
passages_dict[key].append(value)
if "long_text" in passages_dict:
passages_dict["passages"] = passages_dict.pop("long_text")
return Prediction(**passages_dict)
return single_query_passage(passages=passages)

# TODO: Consider doing Prediction.from_completions with the individual sets of passages (per query) too.

Expand Down Expand Up @@ -106,11 +115,5 @@ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = No
pred_returns.append(Prediction(**passages_dict))
return pred_returns
elif isinstance(passages[0], Dict):
passages_dict = {key:[] for key in list(passages[0].keys())}
for docs in passages:
for key,value in docs.items():
passages_dict[key].append(value)
if "long_text" in passages_dict:
passages_dict["passages"] = passages_dict.pop("long_text")
return Prediction(**passages_dict)
return single_query_passage(passages=passages)

Loading
Loading