import sys from haystack import Document, Pipeline from haystack.components.retrievers.in_memory import InMemoryBM25Retriever from haystack.document_stores.in_memory import InMemoryDocumentStore question = ( sys.argv[1] if len(sys.argv) > 1 else "How can a customer reset a forgotten password?" ) documents = [ Document( id="faq-password-reset", content=( "Customers reset forgotten passwords from the account login page. " "Send the password reset email and keep the reset link valid for 30 minutes." ), meta={ "title": "Reset a forgotten password", "category": "account", "answer": "Send the password reset email from the account login page.", }, ), Document( id="faq-invoice-copy", content=( "Customers download invoice copies from the billing portal after payment " "has been posted." ), meta={ "title": "Download invoice copy", "category": "billing", "answer": "Open the billing portal and download the paid invoice.", }, ), Document( id="faq-shipping-address", content=( "Customers can change a shipping address only before the order is dispatched." ), meta={ "title": "Change shipping address", "category": "shipping", "answer": "Edit the shipping address before dispatch.", }, ), ] document_store = InMemoryDocumentStore() written = document_store.write_documents(documents) pipeline = Pipeline() pipeline.add_component( "faq_retriever", InMemoryBM25Retriever(document_store=document_store, top_k=1, scale_score=True), ) result = pipeline.run({"faq_retriever": {"query": question}}) match = result["faq_retriever"]["documents"][0] print(f"documents indexed: {written}") print(f"question: {question}") print(f"top faq id: {match.id}") print(f"title: {match.meta['title']}") print(f"category: {match.meta['category']}") print(f"answer: {match.meta['answer']}") print(f"score: {match.score:.4f}")