api-inference 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

Python
JavaScript
cURL
import requests

API_URL = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
headers = {"Authorization": "Bearer hf_***"}

def query(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()
	
output = query({
	"inputs": {
	"question": "What is my name?",
	"context": "My name is Clara and I live in Berkeley."
},
})

To use the Python client, see huggingface_hub’s package reference.

API specification

Request

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 Additional inference parameters for Question Answering
        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)

Some options can be configured by passing headers to the Inference API. Here are the available headers:

Headers
authorization string Authentication header in the form 'Bearer: hf_****' when hf_**** is a personal user access token with Inference API permission. You can generate one from your settings page.
x-use-cache boolean, default to true There is a cache layer on the inference API to speed up requests we have already seen. Most models can use those results as they are deterministic (meaning the outputs will be the same anyway). However, if you use a nondeterministic model, you can set this parameter to prevent the caching mechanism from being used, resulting in a real new query. Read more about caching here.
x-wait-for-model boolean, default to false If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error, as it will limit hanging in your application to known places. Read more about model availability here.

For more information about Inference API headers, check out the parameters guide.

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