The error message you encountered, stating that BaseRunManager.__init__() is missing required keyword-only arguments (run_id, handlers, and inheritable_handlers), suggests that the callback manager you are using requires these parameters to be properly initialized.
In Langchain, when you utilize callback managers, you typically need to provide these arguments to manage the execution of callbacks effectively. Below is how you can configure the CallbackManagerForRetrieverRun correctly with all the required arguments using a custom ensemble retriever.
Updated Step-by-Step Guide
Step 1: Create Custom Retrievers
from langchain.retrievers import BaseRetriever
class SimpleRetriever1(BaseRetriever):
def _get_relevant_documents(self, query):
return [f"SimpleRetriever1: {query} found in Document A"]
class SimpleRetriever2(BaseRetriever):
def _get_relevant_documents(self, query):
return [f"SimpleRetriever2: {query} found in Document B"]
Step 2: Create an Ensemble Retriever
from langchain.retrievers import EnsembleRetriever
class CustomEnsembleRetriever(EnsembleRetriever):
def __init__(self, retrievers):
super().__init__(retrievers=retrievers)
def _get_relevant_documents(self, query):
results = []
for retriever in self.retrievers:
results.extend(retriever._get_relevant_documents(query))
return results
Step 3: Implement Callbacks
You will define a callback class to manage the start and end of the retrieval process:
from langchain.callbacks import BaseCallbackHandler
class PrintCallback(BaseCallbackHandler):
def on_retriever_start(self, run_id: str, **kwargs):
print(f"Retrieval started for Run ID: {run_id}")
def on_retriever_end(self, run_id: str, **kwargs):
print(f"Retrieval finished for Run ID: {run_id}")
Step 4: Configure the CallbackManager
Now, set up the CallbackManager properly with the required arguments:
from langchain.callbacks import CallbackManagerForRetrieverRun
# Initialize custom retrievers
retriever1 = SimpleRetriever1()
retriever2 = SimpleRetriever2()
# Create an instance of the ensemble retriever
ensemble_retriever = CustomEnsembleRetriever(retrievers=[retriever1, retriever2])
# Set up the callback manager
callback_manager = CallbackManagerForRetrieverRun(
run_id="my_run_id",
handlers=[PrintCallback()], # List of callback handlers
inheritable_handlers=[] # Inheritable handlers, if any
)
Step 5: Executing the Retrieval
Now you can run your retrieval using the ensemble retriever and callback manager:
def run_ensemble_retrieval(query):
# Pass the run_id and callback manager to manage and track the run
callback_manager.on_retriever_start(run_id="my_run_id") # Pass relevant ID here
results = ensemble_retriever._get_relevant_documents(query)
callback_manager.on_retriever_end(run_id="my_run_id") # Use the same ID
return results
# Example usage
query = "test query"
relevant_documents = run_ensemble_retrieval(query)
print("Relevant Documents:", relevant_documents)
Key Changes Made:
- Initialization of CallbackManager: The
CallbackManagerForRetrieverRun is initialized with run_id, handlers, and inheritable_handlers as required parameters.
- Custom Callbacks: Defined the
PrintCallback class that logs the start and end of the retrieval process, incorporating run_id.
- Function Signature Updates: Ensured that
on_retriever_start and on_retriever_end now take run_id as a parameter to keep track of the specific retrieval run.
This setup should work without throwing errors regarding missing arguments when initializing the callback manager. You can expand upon the callbacks and logic based on your specific requirements.