vectorstore.as_retriever(...) how to query with embedding instead of str ? #27715
Replies: 2 comments
-
You can use the similarity_search_by_vector method of the underlying vector store. from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
faiss_vector_store = FAISS.from_texts(["apple", "banana"], embeddings)
vector = embeddings.embed_query("juice")
faiss_vector_store.similarity_search_by_vector(vector)
If you need the runnable interface (like with the retriever), you can make a short wrapper: from langchain_core.documents import Document
from langchain_core.runnables import chain
@chain
def search_by_vector(vector: list[float]) -> list[Document]:
return faiss_vector_store.similarity_search_by_vector(vector)
search_by_vector.invoke(vector) |
Beta Was this translation helpful? Give feedback.
-
thank you for your response, I am using an ensemble retriever : BM25bm25_retriever = BM25Retriever.from_texts( FAISStext_embedding_pairs = zip(contexts, context_embeddings) Ensembleensemble_retriever = EnsembleRetriever( @chain x = search_by_vector.invoke(query_embeddings[0]) errorAttributeError: 'EnsembleRetriever' object has no attribute 'similarity_search_by_vector' Is there a way for me to specify which retriever should use the embeddings and which should use the string(BM25) |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
I want to query using a vector instead of str
System Info
python
Beta Was this translation helpful? Give feedback.
All reactions