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

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
authorizationstringAuthentication 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*objectOne (context, question) pair to answer
        context*stringThe context to be used for answering the question
        question*stringThe question to be answered
parametersobject
        top_kintegerThe 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_strideintegerIf 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_lenintegerThe maximum length of predicted answers (e.g., only answers with a shorter length are considered).
        max_seq_lenintegerThe 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_lenintegerThe maximum length of the question after tokenization. It will be truncated if needed.
        handle_impossible_answerbooleanWhether to accept impossible as an answer.
        align_to_wordsbooleanAttempts 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.
        answerstringThe answer to the question.
        scorenumberThe probability associated to the answer.
        startintegerThe character position in the input where the answer begins.
        endintegerThe character position in the input where the answer ends.
Update on GitHub