Skip to content

QA

Question Answering model. QA models needs input documents contents to run.

Parameters

  • on (Union[str, list])

    Fields to use to answer to the question.

  • model

    Hugging Face question answering model available here.

  • batch_size (int) – defaults to 32

Examples

>>> from pprint import pprint as print
>>> from cherche import retrieve, qa
>>> from transformers import pipeline

>>> documents = [
...    {"id": 0, "title": "Paris France"},
...    {"id": 1, "title": "Madrid Spain"},
...    {"id": 2, "title": "Montreal Canada"}
... ]

>>> retriever = retrieve.TfIdf(key="id", on=["title"], documents=documents)

>>> qa_model = qa.QA(
...     model = pipeline("question-answering", model = "deepset/roberta-base-squad2", tokenizer = "deepset/roberta-base-squad2"),
...     on = ["title"],
...  )

>>> pipeline = retriever + documents + qa_model

>>> pipeline
TfIdf retriever
    key      : id
    on       : title
    documents: 3
Mapping to documents
Question Answering
    on: title

>>> print(pipeline(q="what is the capital of france?"))
[{'answer': 'Paris',
  'end': 5,
  'id': 0,
  'question': 'what is the capital of france?',
  'score': 0.05615315958857536,
  'similarity': 0.5962847939999439,
  'start': 0,
  'title': 'Paris France'},
 {'answer': 'Montreal',
  'end': 8,
  'id': 2,
  'question': 'what is the capital of france?',
  'score': 0.01080897357314825,
  'similarity': 0.0635641726163728,
  'start': 0,
  'title': 'Montreal Canada'}]

>>> print(pipeline(["what is the capital of France?", "what is the capital of Canada?"]))
[[{'answer': 'Paris',
   'end': 5,
   'id': 0,
   'question': 'what is the capital of France?',
   'score': 0.1554129421710968,
   'similarity': 0.5962847939999439,
   'start': 0,
   'title': 'Paris France'},
  {'answer': 'Montreal',
   'end': 8,
   'id': 2,
   'question': 'what is the capital of France?',
   'score': 1.2884755960840266e-05,
   'similarity': 0.0635641726163728,
   'start': 0,
   'title': 'Montreal Canada'}],
 [{'answer': 'Montreal',
   'end': 8,
   'id': 2,
   'question': 'what is the capital of Canada?',
   'score': 0.05316793918609619,
   'similarity': 0.5125692857821978,
   'start': 0,
   'title': 'Montreal Canada'},
  {'answer': 'Paris France',
   'end': 12,
   'id': 0,
   'question': 'what is the capital of Canada?',
   'score': 4.7594025431862974e-07,
   'similarity': 0.035355339059327376,
   'start': 0,
   'title': 'Paris France'}]]

Methods

call

Question answering main method.

Parameters

  • q (Union[str, List[str]])
  • documents (Union[List[List[Dict[str, str]]], List[Dict[str, str]]])
  • batch_size (Optional[int]) – defaults to None
  • kwargs
get_question_context