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 local modifications
  • Loading branch information
Athe-kunal committed Apr 8, 2024
commit 1639bd214b234ff15e0b2bc31c6a24108dec04d8
77 changes: 56 additions & 21 deletions dsp/modules/colbertv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
from typing import Any, Optional, Union, List

import requests
import colbert
from colbert import Indexer, Searcher
from colbert.infra import Run, RunConfig, ColBERTConfig
from colbert.data import Queries, Collection
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory
from dsp.utils import dotdict
import os
Expand Down Expand Up @@ -81,33 +77,72 @@ def colbertv2_post_request_v2_wrapped(*args, **kwargs):
os.environ['COLBERT_LOAD_TORCH_EXTENSION_VERBOSE'] = "True"
Athe-kunal marked this conversation as resolved.
Show resolved Hide resolved

class ColBERTv2Local:
def __init__(self,checkpoint:str='colbert-ir/colbertv2.0'):
def __init__(self,checkpoint:str='colbert-ir/colbertv2.0',passages:List[str]=[],index_name_or_path:str = "Colbert-RM",experiment_name:str="Colbert-Experiment",load_only:bool=False,nranks:int=1,nbits:int=2,DOC_MAXLEN:int=300,INDEX_BSIZE:int=256,KMEANS_ITER:int=8):


self.checkpoint = checkpoint
Athe-kunal marked this conversation as resolved.
Show resolved Hide resolved
self.index_name_or_path = index_name_or_path
self.experiment_name = experiment_name
self.nranks = nranks
self.nbits = nbits
self.DOC_MAXLEN = DOC_MAXLEN
self.INDEX_BSIZE = INDEX_BSIZE
self.KMEANS_ITER = KMEANS_ITER
self.passages = passages

if not load_only:
print(f"Building the index for experiment {self.experiment_name} with index name {self.index_name_or_path}")
self.build_index()

print(f"Loading the index for experiment {self.experiment_name} with index name {self.index_name_or_path}")
self.searcher = self.get_index()

def build_index(self):

def build_index(self,passages:List[str],nranks:int=1,index_name_or_path:str = "Colbert-RM-",nbits:int=2,DOC_MAXLEN:int=300,INDEX_BSIZE:int=256,KMEANS_ITER:int=8,experiment_name:str="Colbert-Experiment"):
try:
import colbert
except ImportError:
print("Colbert not found. Please check your installation or install the module using pip install colbert-ai[faiss-gpu,torch].")

with Run().context(RunConfig(nranks=nranks, experiment=experiment_name)):
config = ColBERTConfig(doc_maxlen=DOC_MAXLEN, nbits=nbits, kmeans_niters=KMEANS_ITER,index_bsize=INDEX_BSIZE)
from colbert import Indexer
from colbert.infra import Run, RunConfig, ColBERTConfig
with Run().context(RunConfig(nranks=self.nranks, experiment=self.experiment_name)):
config = ColBERTConfig(doc_maxlen=self.DOC_MAXLEN, nbits=self.nbits, kmeans_niters=self.KMEANS_ITER,index_bsize=self.INDEX_BSIZE)


indexer = Indexer(checkpoint=self.checkpoint, config=config)
indexer.index(name=index_name_or_path, collection=passages, overwrite=True)
indexer.index(name=self.index_name_or_path, collection=self.passages, overwrite=True)

def get_index(self,index_name_or_path:str = "Colbert-RM-",experiment_name:str="Colbert-Experiment",passages:List[str] = []):
with Run().context(RunConfig(experiment=experiment_name)):
searcher = Searcher(index=index_name_or_path, collection=passages)
self.searcher = searcher
def get_index(self):
try:
import colbert
except ImportError:
print("Colbert not found. Please check your installation or install the module using pip install colbert-ai[faiss-gpu,torch].")

from colbert import Searcher
from colbert.infra import Run, RunConfig

with Run().context(RunConfig(experiment=self.experiment_name)):
searcher = Searcher(index=self.index_name_or_path, collection=self.passages)
return searcher

def get_docs(self,searcher:Searcher,query:str,k:int=7):
def __call__(self,query:str,k:int=7,**kwargs):
try:
import colbert
except ImportError:
print("Colbert not found. Please check your installation or install the module using pip install colbert-ai[faiss-gpu,torch].")
import torch

results = searcher.search(
query,
#Number of passages to receive
k=k)
#Passing the filter function of relevant
# filter_fn=lambda pids: torch.tensor(
# [pid for pid in pids if pid in relevant_ids],dtype=torch.int32).to(device))
if kwargs.get("filtered_pids"):
filtered_pids = kwargs.get("filtered_pids")
assert type(filtered_pids) == List[int], "The filtered pids should be a list of integers"
device = "cuda" if torch.cuda.is_available() else "cpu"
results = self.searcher.search(
query,
#Number of passages to receive
k=k,
#Passing the filter function of relevant
filter_fn=lambda pids: torch.tensor(
[pid for pid in pids if pid in filtered_pids],dtype=torch.int32).to(device))

return results
10 changes: 5 additions & 5 deletions rm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@
"The early bird catches the worm."
]

col = dspy.ColBERTv2Local()
col.build_index(passages=passages)
searcher = col.get_index(passages=passages[:10])
res = searcher.get_docs(searcher,query="Software",k=5)
print(res)
col = dspy.ColBERTv2Local(passages=passages)

# searcher = col.get_index(passages=passages[:10])
# res = searcher.get_docs(searcher,query="Software",k=5)
# print(res)