<jupyter_start><jupyter_text>ReaderWe've already explored the *reader* model in the first few sections of this chapter, where we loaded a pretrained model using the `transformers` library.Now, we'll do the same thing but via Haystack - which uses the same `transformers` library in the background (so we can use the same pretrained model names).To initialize our reader via Haystack all we need is:<jupyter_code>from haystack.reader.farm import FARMReader

reader = FARMReader(model_name_or_path='deepset/bert-base-cased-squad2', use_gpu=True)<jupyter_output>03/15/2022 08:19:10 - INFO - faiss -   Loading faiss.
03/15/2022 08:19:13 - INFO - farm.utils -   Using device: CPU 
03/15/2022 08:19:13 - INFO - farm.utils -   Number of GPUs: 0
03/15/2022 08:19:13 - INFO - farm.utils -   Distributed Training: False
03/15/2022 08:19:13 - INFO - farm.utils -   Automatic Mixed Precision: None
03/15/2022 08:19:29 - WARNING - farm.utils -   ML Logging is turned off. No parameters, metrics or artifacts will be logged to MLFlow.
03/15/2022 08:19:29 - INFO - farm.utils -   Using device: CPU 
03/15/2022 08:19:29 - INFO - farm.utils -   Number of GPUs: 0
03/15/2022 08:19:29 - INFO - farm.utils -   Distributed Training: False
03/15/2022 08:19:29 - INFO - farm.utils -   Automatic Mixed Precision: None
03/15/2022 08:19:30 - INFO - farm.infer -   Got ya 7 parallel workers to do inference ...
03/15/2022 08:19:30 - INFO - farm.infer -    0    0    0    0    0    0    0 
03/15/2022 08:19:30 - INFO - farm.infer -   /w\  /w\  /w\  /w\  /w\  /w\  /w\
03/15/2022 08:19:30[...]<jupyter_text>Now we have our reader model initialized, let's load up our FAISS index and DPR just like before.<jupyter_code>from haystack.document_store.faiss import FAISSDocumentStore
from haystack.retriever.dense import DensePassageRetriever

path = '../../models/faiss'

# load FAISS from file (the squad validation set index)
document_store = FAISSDocumentStore.load(f'{path}/squad_dev.faiss', f'sqlite:///{path}/squad_dev.db')

# initialize DPR model
retriever = DensePassageRetriever(
    document_store=document_store,
    query_embedding_model='facebook/dpr-question_encoder-single-nq-base',
    passage_embedding_model='facebook/dpr-ctx_encoder-single-nq-base',
    use_gpu=True,
    embed_title=True
)<jupyter_output><empty_output><jupyter_text>Haystack comes with a very convenient `ExtractiveQAPipeline` class which allows us to pass our `reader` and `retriever` to build an easy-to-use *extractive* Q&A pipeline:<jupyter_code>from haystack.pipeline import ExtractiveQAPipeline

pipeline = ExtractiveQAPipeline(reader=reader, retriever=retriever)<jupyter_output><empty_output><jupyter_text>We can ask questions using the `run` method, alongside the `query` parameter:<jupyter_code>pipeline.run(query='What does theoretical computer science cover?')<jupyter_output>Creating Embeddings: 100%|██████████| 1/1 [00:00<00:00,  2.23 Batches/s]
Inferencing Samples:   0%|          | 0/1 [00:00<?, ? Batches/s]/usr/local/Caskroom/miniconda/base/envs/ml/lib/python3.8/site-packages/farm/modeling/prediction_head.py:1112: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
  start_indices = flat_sorted_indices // max_seq_len
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.65 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.75 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.76 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.78 Batches/s]
Inferencing Samples: 100%|██████[...]<jupyter_text>Here we're returning a lot of different answers, which is not really necessary. We can limit the number of answers we return using two parameters, `top_k_retriever` and `top_k_reader`. Both of these limit the number of items being returned from the `retriever` and `reader` models respectively.<jupyter_code>pipeline.run(query='What does theoretical computer science cover?',
             top_k_retriever=3)<jupyter_output>Creating Embeddings: 100%|██████████| 1/1 [00:00<00:00,  5.35 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.70 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.72 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.72 Batches/s]<jupyter_text>And the `reader`:<jupyter_code>pipeline.run(query='What does theoretical computer science cover?',
             top_k_reader=3)<jupyter_output>Creating Embeddings: 100%|██████████| 1/1 [00:00<00:00,  5.17 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.58 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.66 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.72 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.81 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.74 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.74 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.75 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.76 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.78 Batches/s]
Inferencing Samples: 100%|██████████| 1/1 [00:00<00:00,  1.77 Batches/s]
{'fraction_non_alphanumeric': 0.16031086210012652, 'fraction_numerical': 0.09868064341225374, 'mean_word_length': 4.722854188210961, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '418975', 'license': 'MIT', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2033, 'n_words': 538, 'path': 'course/question_answering/13_reader.ipynb', 'repo_name': 'augustodn/NPL_with_Transformers', 'size': '5533'}