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
Next Next commit
return metadata changes
  • Loading branch information
Athe-kunal committed Apr 4, 2024
commit 9632e5ef08e53b1b41aa844e36d4a16de1417432
29 changes: 17 additions & 12 deletions dsp/primitives/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def retrieve(query: str, k: int, **kwargs) -> list[str]:
# 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]
# passages = [psg.long_text for psg in passages]

if dsp.settings.reranker:
passages_cs_scores = dsp.settings.reranker(query, passages)
Expand Down Expand Up @@ -55,17 +55,22 @@ def retrieveEnsemble(queries: list[str], k: int, by_prob: bool = True,**kwargs)

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

passages = {}
all_queries_passages = []
for q in queries:
for psg in dsp.settings.rm(q, k=k * 3,**kwargs):
passages = {}
retrieved_passages = dsp.settings.rm(q, k=k * 3,**kwargs)
# for idx,psg in enumerate(retrieved_passages):
# retrieved_passages[idx]["tracking_idx"] = idx
for idx,psg in enumerate(retrieved_passages):
if by_prob:
passages[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[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
passages[(idx,psg.long_text)] = passages.get(psg.long_text, 0.0) + psg.score
retrieved_passages[idx]["tracking_idx"] = idx
# passages = [(score, text) for text, score in passages.items()]
Athe-kunal marked this conversation as resolved.
Show resolved Hide resolved
passages = sorted(passages.items(), key=lambda item: item[1])[:k]
# passages = sorted(passages, reverse=True)[: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]
all_queries_passages.append(passages)
return all_queries_passages
25 changes: 23 additions & 2 deletions dspy/retrieve/retrieve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
from typing import List, Optional, Union
from typing import List, Optional, Union, Dict, Any

import dsp
from dspy.predict.parameter import Parameter
Expand Down Expand Up @@ -37,6 +37,27 @@ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = No
# 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)
if isinstance(passages[0],List):
pred_returns = []
for query_passages in passages:
passages_dict = {key:[] for key in list(query_passages[0].keys()) if key!="tracking_idx"}
for psg in query_passages:
for key,value in psg.items():
if key == "tracking_idx": continue
passages_dict[key].append(value)
if "long_text" in passages_dict:
passages_dict["passages"] = passages_dict.pop("long_text")
pred_returns.append(Prediction(**passages_dict))
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)

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