BaseRetriever kwargs #27227
Replies: 3 comments 2 replies
-
To pass Here's a refined approach:
Here's how you can modify your code: def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun, metadata_filters: str) -> List[Document]:
docs = []
load_dotenv()
admins = os.getenv('ADMINS_MSL', '')
admins_list = admins.split(',')
is_admin = self.username.split('@')[0] in admins_list
if not is_admin:
print("User is not an admin, no access to documents.")
return docs
# Convert JSON string to Python dictionary
try:
metadata_filters_dict = json.loads(metadata_filters)["metadata_filters"]
except (json.JSONDecodeError, KeyError) as e:
print(f"Error loading metadata filters: {e}")
return docs # Return empty docs on error
# Construct the filter query
filter_conditions = {}
for key, value in metadata_filters_dict.items():
if value: # Only process keys with non-empty values
filter_conditions[key] = value
try:
search_results = self.vectorstore.similarity_search(
query,
search_type="hybrid",
k=NUMBER_OF_CHUNKS_TO_RETURN_MSL,
filter=filter_conditions
)
except Exception as e:
print(f"Error during similarity search: {e}")
return docs
return search_results Key Points:
This approach should allow you to pass |
Beta Was this translation helpful? Give feedback.
-
@dosu This is not exactly the issue I'm facing. The issue is that the langchain BaseRetriever does not seem to accept the metadata_filters as an argument. The metadata_filters never arrive in the retriever class and cannot be used. The error I'm getting is as follows: UserScoringRetrieverMSLLCEL._get_relevant_documents() missing 1 required keyword-only argument: 'metadata_filters' |
Beta Was this translation helpful? Give feedback.
-
@dosu How do I implement this in my chain I have in place? The chain looks like this:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I made the above custom retriever based on the langchain BaseRetriever. This retriever is than used in the following chain:
retriever = UserScoringRetrieverMSLLCEL(vectorstore=vector_store, username=username, metadata_filters=metadata_filters)
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
The issue is that I cannot pass the metadata_filters to the retriever. How can I pass this metadata_filters as an argument to make sure I can filter in my similarity search?
Beta Was this translation helpful? Give feedback.
All reactions