##
Question Answering

Question Answering models can retrieve the answer to a question from a given text, which is useful for searching for an answer in a document.

> For more details about the `question-answering` task, check out its dedicated page! You will find examples and related materials.

###
Recommended models

- deepset/roberta-base-squad2: A robust baseline model for most question answering domains.
- distilbert/distilbert-base-cased-distilled-squad: Small yet robust model that can answer questions.
- google/tapas-base-finetuned-wtq: A special model that can answer questions from tables.

Explore all available models and find the one that suits you best here.

###
Using the API

Provider

Settings

    import os
    from huggingface_hub import InferenceClient

    client = InferenceClient(
        provider="hf-inference",
        api_key=os.environ["HF_TOKEN"],
    )

    answer = client.question_answering(
        question="What is my name?",
        context="My name is Clara and I live in Berkeley.",
        model="deepset/roberta-base-squad2",
    )

###
API specification

####
Request

Headers |  |
--- | --- | ---
authorization | string | Authentication header in the form `'Bearer: hf_****'` when `hf_****` is a personal user access token with “Inference Providers” permission. You can generate one from your settings page.

Payload |  |
--- | --- | ---
inputs* | object | One (context, question) pair to answer
context* | string | The context to be used for answering the question
question* | string | The question to be answered
parameters | object |
top_k | integer | The number of answers to return (will be chosen by order of likelihood). Note that we return less than topk answers if there are not enough options available within the context.
doc_stride | integer | If the context is too long to fit with the question for the model, it will be split in several chunks with some overlap. This argument controls the size of that overlap.
max_answer_len | integer | The maximum length of predicted answers (e.g., only answers with a shorter length are considered).
max_seq_len | integer | The maximum length of the total sentence (context + question) in tokens of each chunk passed to the model. The context will be split in several chunks (using docStride as overlap) if needed.
max_question_len | integer | The maximum length of the question after tokenization. It will be truncated if needed.
handle_impossible_answer | boolean | Whether to accept impossible as an answer.
align_to_words | boolean | Attempts to align the answer to real words. Improves quality on space separated languages. Might hurt on non-space-separated languages (like Japanese or Chinese)

####
Response

Body |  |
--- | --- | ---
(array) | object[] | Output is an array of objects.
answer | string | The answer to the question.
score | number | The probability associated to the answer.
start | integer | The character position in the input where the answer begins.
end | integer | The character position in the input where the answer ends.

Update on GitHub