Replies: 2 comments
-
To access the Here's an example of how to do this:
Here's how you can modify your code: from typing import Any, Dict, List
from langchain_core.documents import Document
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
class CustomContextualCompressionRetriever(ContextualCompressionRetriever):
def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun, **kwargs: Any
) -> List[Document]:
"""Get docs, adding score information."""
docs_and_similarities = self.base_retriever.similarity_search_with_relevance_scores(query, **kwargs)
docs = [doc for doc, _ in docs_and_similarities]
for doc, similarity in docs_and_similarities:
doc.metadata["query_similarity_score"] = similarity
return docs
# Example usage
retriever = load_db.as_retriever(
search_type=db_search_type,
search_kwargs={'k': db_doc_k, 'fetch_k': db_doc_fetch_k}
)
embeddings_filter = EmbeddingsFilter(embeddings=embeddings, similarity_threshold=db_similarity_threshold)
compression_retriever = CustomContextualCompressionRetriever(
base_compressor=embeddings_filter, base_retriever=retriever
)
extraction_context = compression_retriever.invoke(extraction_question)
src_doc_score = [doc.metadata['query_similarity_score'] for doc in extraction_context]
print(src_doc_score) This code ensures that the similarity scores are included in the metadata of the documents returned by the |
Beta Was this translation helpful? Give feedback.
-
@dosu I am getting the error "VectorStoreRetriever object has no attribute similarity_search_with_relevance_scores." I am using the Milvus vector store retriever. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
above of code,
src_doc_score = [doc['query_similarity_score'] for doc in extraction_context]
this line code error represented.
how to get to a query_similarity_score of ContextualCompressionRetriever??
System Info
.
Beta Was this translation helpful? Give feedback.
All reactions