# Utilizing existing FAQs for Question Answering [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/deepset-ai/haystack/blob/master/tutorials/Tutorial4_FAQ_style_QA.ipynb) While *extractive Question Answering* works on pure texts and is therefore more generalizable, there's also a common alternative that utilizes existing FAQ data. **Pros**: - Very fast at inference time - Utilize existing FAQ data - Quite good control over answers **Cons**: - Generalizability: We can only answer questions that are similar to existing ones in FAQ In some use cases, a combination of extractive QA and FAQ-style can also be an interesting option. ### Prepare environment #### Colab: Enable the GPU runtime Make sure you enable the GPU runtime to experience decent speed in this tutorial. **Runtime -> Change Runtime type -> Hardware accelerator -> GPU** ```python # Make sure you have a GPU running !nvidia-smi ``` ```python # Install the latest release of Haystack in your own environment #! pip install farm-haystack # Install the latest master of Haystack !pip install grpcio-tools==1.34.1 !pip install git+https://github.com/deepset-ai/haystack.git # If you run this notebook on Google Colab, you might need to # restart the runtime after installing haystack. ``` ```python from haystack.document_stores import ElasticsearchDocumentStore from haystack.nodes import EmbeddingRetriever import pandas as pd import requests ``` ### Start an Elasticsearch server You can start Elasticsearch on your local machine instance using Docker. If Docker is not readily available in your environment (eg., in Colab notebooks), then you can manually download and execute Elasticsearch from source. ```python # Recommended: Start Elasticsearch using Docker via the Haystack utility function from haystack.utils import launch_es launch_es() ``` ```python # In Colab / No Docker environments: Start Elasticsearch from source ! wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz -q ! tar -xzf elasticsearch-7.9.2-linux-x86_64.tar.gz ! chown -R daemon:daemon elasticsearch-7.9.2 import os from subprocess import Popen, PIPE, STDOUT es_server = Popen(['elasticsearch-7.9.2/bin/elasticsearch'], stdout=PIPE, stderr=STDOUT, preexec_fn=lambda: os.setuid(1) # as daemon ) # wait until ES has started ! sleep 30 ``` ### Init the DocumentStore In contrast to Tutorial 1 (extractive QA), we: * specify the name of our `text_field` in Elasticsearch that we want to return as an answer * specify the name of our `embedding_field` in Elasticsearch where we'll store the embedding of our question and that is used later for calculating our similarity to the incoming user question * set `excluded_meta_data=["question_emb"]` so that we don't return the huge embedding vectors in our search results ```python from haystack.document_stores import ElasticsearchDocumentStore document_store = ElasticsearchDocumentStore(host="localhost", username="", password="", index="document", embedding_field="question_emb", embedding_dim=384, excluded_meta_data=["question_emb"]) ``` ### Create a Retriever using embeddings Instead of retrieving via Elasticsearch's plain BM25, we want to use vector similarity of the questions (user question vs. FAQ ones). We can use the `EmbeddingRetriever` for this purpose and specify a model that we use for the embeddings. ```python retriever = EmbeddingRetriever(document_store=document_store, embedding_model="sentence-transformers/all-MiniLM-L6-v2", use_gpu=True) ``` ### Prepare & Index FAQ data We create a pandas dataframe containing some FAQ data (i.e curated pairs of question + answer) and index those in elasticsearch. Here: We download some question-answer pairs related to COVID-19 ```python # Download temp = requests.get("https://raw.githubusercontent.com/deepset-ai/COVID-QA/master/data/faqs/faq_covidbert.csv") open('small_faq_covid.csv', 'wb').write(temp.content) # Get dataframe with columns "question", "answer" and some custom metadata df = pd.read_csv("small_faq_covid.csv") # Minimal cleaning df.fillna(value="", inplace=True) df["question"] = df["question"].apply(lambda x: x.strip()) print(df.head()) # Get embeddings for our questions from the FAQs questions = list(df["question"].values) df["question_emb"] = retriever.embed_queries(texts=questions) df = df.rename(columns={"question": "content"}) # Convert Dataframe to list of dicts and index them in our DocumentStore docs_to_index = df.to_dict(orient="records") document_store.write_documents(docs_to_index) ``` ### Ask questions Initialize a Pipeline (this time without a reader) and ask questions ```python from haystack.pipelines import FAQPipeline pipe = FAQPipeline(retriever=retriever) ``` ```python from haystack.utils import print_answers prediction = pipe.run(query="How is the virus spreading?", params={"Retriever": {"top_k": 10}}) print_answers(prediction, details="medium") ``` ## About us This [Haystack](https://github.com/deepset-ai/haystack/) notebook was made with love by [deepset](https://deepset.ai/) in Berlin, Germany We bring NLP to the industry via open source! Our focus: Industry specific language models & large scale QA systems. Some of our other work: - [German BERT](https://deepset.ai/german-bert) - [GermanQuAD and GermanDPR](https://deepset.ai/germanquad) - [FARM](https://github.com/deepset-ai/FARM) Get in touch: [Twitter](https://twitter.com/deepset_ai) | [LinkedIn](https://www.linkedin.com/company/deepset-ai/) | [Slack](https://haystack.deepset.ai/community/join) | [GitHub Discussions](https://github.com/deepset-ai/haystack/discussions) | [Website](https://deepset.ai) By the way: [we're hiring!](https://www.deepset.ai/jobs)