Replies: 1 comment
-
🤖 Yes, LangChain does provide a way to perform a semantic search on a list of queries in Elasticsearch without having to use a for loop for individual queries. The Here is a brief overview of how it works:
Here is the relevant code: class MultiQueryRetriever(BaseRetriever):
retriever: BaseRetriever
llm_chain: LLMChain
verbose: bool = True
parser_key: str = "lines"
def generate_queries(
self, question: str, run_manager: CallbackManagerForRetrieverRun
) -> List[str]:
response = self.llm_chain(
{"question": question}, callbacks=run_manager.get_child()
)
lines = getattr(response["text"], self.parser_key, [])
if self.verbose:
logger.info(f"Generated queries: {lines}")
return lines
def retrieve_documents(
self, queries: List[str], run_manager: CallbackManagerForRetrieverRun
) -> List[Document]:
documents = []
for query in queries:
docs = self.retriever.get_relevant_documents(
query, callbacks=run_manager.get_child()
)
documents.extend(docs)
return documents
def unique_union(self, documents: List[Document]) -> List[Document]:
return _unique_documents(documents)
def _get_relevant_documents(
self,
query: str,
*,
run_manager: CallbackManagerForRetrieverRun,
) -> List[Document]:
queries = self.generate_queries(query, run_manager)
documents = self.retrieve_documents(queries, run_manager)
return self.unique_union(documents) You can find more details in the MultiQueryRetriever class implementation. Please note that while 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.
-
I Have a list of queries and want to perform a semantic search on all these queries. Normally I would have to write a for loop to perform a semantic search on all these queries individually. Are there any retrievers in langchain that will take a list of queries to perform a semantic search on elasticsearch?
Beta Was this translation helpful? Give feedback.
All reactions