Replies: 1 comment
-
🤖 Hi, Based on your requirement, you want to dynamically apply a filter value that is determined by the agent within the chain. To achieve this, you can modify the Firstly, you need to add a method in the class PineconeTranslator(Visitor):
# ... existing code ...
def apply_filter(self, filter_value):
self.filter_value = filter_value
def visit_structured_query(self, structured_query: StructuredQuery) -> Tuple[str, dict]:
if structured_query.filter is None:
kwargs = {}
else:
# Use the filter value set by the agent if it exists
if hasattr(self, 'filter_value'):
kwargs = {"filter": self.filter_value}
else:
kwargs = {"filter": structured_query.filter.accept(self)}
return structured_query.query, kwargs In this modification, the Next, you need to modify the class PineconeHybridSearchRetriever(BaseRetriever):
"""`Pinecone Hybrid Search` retriever."""
# ... existing attributes ...
filter_value: Optional[StructuredQuery] = None
"""Filter value for the Pinecone retriever."""
# ... existing methods ...
def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
) -> List[Document]:
# ... existing code ...
# translate the filter value to a valid Pinecone filter
translator = PineconeTranslator()
query, kwargs = translator.visit_structured_query(self.filter_value)
# query pinecone with the query parameters
result = self.index.query(
vector=dense_vec,
sparse_vector=sparse_vec,
top_k=self.top_k,
include_metadata=True,
**kwargs, # use the translated filter value here
)
# ... existing code ... In this modification, a new attribute I hope this helps! Let me know if you have any other questions. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all
I have created a chatbot which uses multiple tools to get an answer for a question. One of the tools queries a Pinecone index to get an answer. The structure of the chain is as follows:
Now, in testing and development, I know I can introduce a metadata filter as follows:
But how do I do it so that the filter value is always a variable, and is determined by the agent within the chain?
Following is the general structure of the code:
For example, if I type "What is the latest news of the market?", the agent should apply a filter in the retriever as below, but how?
Beta Was this translation helpful? Give feedback.
All reactions