Inference Providers documentation

Question Answering

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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

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

Using the API

from huggingface_hub import InferenceClient

client = InferenceClient(
    provider="hf-inference",
    api_key="hf_xxxxxxxxxxxxxxxxxxxxxxxx",
)

result = client.question_answering(
    inputs={
    "question": "What is my name?",
    "context": "My name is Clara and I live in Berkeley."
},
    model="distilbert/distilbert-base-cased-distilled-squad",
)

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