英文:
how to specify similarity threshold in langchain faiss retriever?
问题
我想要传递一个相似性阈值给检索器。到目前为止,我只能找到如何传递一个k值,但这不是我想要的。我怎样才能传递一个阈值呢?
from langchain.document_loaders import PyPDFLoader
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
def get_conversation_chain(vectorstore):
llm = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo')
qa = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(search_kwargs={'k': 2}), return_source_documents=True, verbose=True)
return qa
loader = PyPDFLoader("sample.pdf")
# get pdf raw text
pages = loader.load_and_split()
faiss_index = FAISS.from_documents(list_of_documents, OpenAIEmbeddings())
# create conversation chain
chat_history = []
qa = get_conversation_chain(faiss_index)
query = "What is a sunflower?"
result = qa({"question": query, "chat_history": chat_history})
希望这个翻译对您有帮助。
英文:
I would like to pass to the retriever a similarity threshold. So far I could only figure out how to pass a k value but this was not what I wanted. How can I pass a threshold instead?
from langchain.document_loaders import PyPDFLoader
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
def get_conversation_chain(vectorstore):
llm = ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo')
qa = ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(search_kwargs={'k': 2}), return_source_documents=True, verbose=True)
return qa
loader = PyPDFLoader("sample.pdf")
# get pdf raw text
pages = loader.load_and_split()
faiss_index = FAISS.from_documents(list_of_documents, OpenAIEmbeddings())
# create conversation chain
chat_history = []
qa = get_conversation_chain(faiss_index)
query = "What is a sunflower?"
result = qa({"question": query, "chat_history": chat_history})
答案1
得分: 1
这是来自api文档的答案 search_kwargs={'score_threshold': 0.3}
。
英文:
This was the answer search_kwargs={'score_threshold': 0.3})
from the api docs.
答案2
得分: 1
你可以使用以下内容作为VectorStoreRetriever,就像你说的那样,但要加上search_type参数。
retriever = dbFAISS.as_retriever(search_type="similarity_score_threshold",
search_kwargs={"score_threshold": .5,
"k": top_k})
英文:
You can use the following as a VectorStoreRetriever as you say but with the search_type parameter.
retriever = dbFAISS.as_retriever(search_type="similarity_score_threshold",
search_kwargs={"score_threshold": .5,
"k": top_k})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论