Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question Answering is a cloud-based API service that lets you create a conversational question-and-answer layer over your existing data. Use it to build a knowledge base by extracting questions and answers from your semi-structured content, including FAQ, manuals, and documents. Answer users’ questions with the best answers from the QnAs in your knowledge base—automatically. Your knowledge base gets smarter, too, as it continually learns from users' behavior.
Source code | Package (PyPI) | API reference documentation | Product documentation | Samples
Disclaimer
Azure SDK Python packages support for Python 2.7 ended 01 January 2022. For more information and questions, please refer to https://github.com/Azure/azure-sdk-for-python/issues/20691
Getting started
Prerequisites
- Python 3.7 or later is required to use this package.
- An Azure subscription
- A Language Service resource
Install the package
Install the Azure Question Answering client library for Python with pip:
pip install azure-ai-language-questionanswering
Note: this version of the client library defaults to the service API version
2021-10-01
.
Authenticate the client
In order to interact with the Question Answering service, you'll need to create an instance of the QuestionAnsweringClient class or an instance of the AuthoringClient for managing projects within your resource. You will need an endpoint, and an API key to instantiate a client object. For more information regarding authenticating with Cognitive Services, see Authenticate requests to Azure Cognitive Services.
Get an API key
You can get the endpoint and an API key from the Language resource in the Azure Portal.
Alternatively, use the Azure CLI command shown below to get the API key from the Language resource.
az cognitiveservices account keys list --resource-group <resource-group-name> --name <resource-name>
Create QuestionAnsweringClient
Once you've determined your endpoint and API key you can instantiate a QuestionAnsweringClient:
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering import QuestionAnsweringClient
endpoint = "https://{myaccount}.api.cognitive.microsoft.com"
credential = AzureKeyCredential("{api-key}")
client = QuestionAnsweringClient(endpoint, credential)
Create AuthoringClient
With your endpoint and API key, you can instantiate a AuthoringClient:
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.authoring import AuthoringClient
endpoint = "https://{myaccount}.api.cognitive.microsoft.com"
credential = AzureKeyCredential("{api-key}")
client = AuthoringClient(endpoint, credential)
Create a client with an Azure Active Directory Credential
To use an Azure Active Directory (AAD) token credential, provide an instance of the desired credential type obtained from the azure-identity library. Note that regional endpoints do not support AAD authentication. Create a custom subdomain name for your resource in order to use this type of authentication.
Authentication with AAD requires some initial setup:
- Install azure-identity
- Register a new AAD application
- Grant access to the Language service by assigning the "Cognitive Services Language Reader" role to your service principal.
After setup, you can choose which type of credential from azure.identity to use. As an example, DefaultAzureCredential can be used to authenticate the client:
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
AZURE_CLIENT_ID
, AZURE_TENANT_ID
, AZURE_CLIENT_SECRET
Use the returned token credential to authenticate the client:
from azure.ai.language.questionanswering import QuestionAnsweringClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
client = QuestionAnsweringClient(endpoint="https://<my-custom-subdomain>.cognitiveservices.azure.com/", credential=credential)
Key concepts
QuestionAnsweringClient
The QuestionAnsweringClient is the primary interface for asking questions using a knowledge base with your own information, or text input using pre-trained models.
For asynchronous operations, an async QuestionAnsweringClient
is in the azure.ai.language.questionanswering.aio
namespace.
AuthoringClient
The AuthoringClient provides an interface for managing Question Answering projects. Examples of the available operations include creating and deploying projects, updating your knowledge sources, and updating question and answer pairs. It provides both synchronous and asynchronous APIs.
Examples
QuestionAnsweringClient
The azure-ai-language-questionanswering
client library provides both synchronous and asynchronous APIs.