Skip to main content

Microsoft Azure Question Answering Client Library for Python

Project description

Build Status

Azure Cognitive Language Services Question Answering client library for Python

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 is ending 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 2.7, or 3.6 or later is required to use this package.
  • An Azure subscription
  • An existing Question Answering resource

Note: the new unified Cognitive Language Services are not currently available for deployment.

Install the package

Install the Azure QuestionAnswering client library for Python with pip:

pip install azure-ai-language-questionanswering

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 QuestionAnsweringProjectsClient 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 Cognitive Services resource or Question Answering resource in the Azure Portal.

Alternatively, use the Azure CLI command shown below to get the API key from the Question Answering 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 QuestionAnsweringProjectsClient

With your endpoint and API key, you can instantiate a QuestionAnsweringProjectsClient:

from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.projects import QuestionAnsweringProjectsClient

endpoint = "https://{myaccount}.api.cognitive.microsoft.com"
credential = AzureKeyCredential("{api-key}")

client = QuestionAnsweringProjectsClient(endpoint, 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.

QuestionAnsweringProjectsClient

The QuestionAnsweringProjectsClient 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.

The following examples show common scenarios using the client created above.

Ask a question

The only input required to ask a question using a knowledge base is just the question itself:

output = client.get_answers(
    question="How long should my Surface battery last?",
    project_name="FAQ",
    deployment_name="test"
)
for candidate in output.answers:
    print("({}) {}".format(candidate.confidence, candidate.answer))
    print("Source: {}".format(candidate.source))

You can set additional keyword options to limit the number of answers, specify a minimum confidence score, and more.

Ask a follow-up question

If your knowledge base is configured for chit-chat, the answers from the knowledge base may include suggested prompts for follow-up questions to initiate a conversation. You can ask a follow-up question by providing the ID of your chosen answer as the context for the continued conversation:

from azure.ai.language.questionanswering import models

output = client.get_answers(
    question="How long should charging take?",
    answer_context=models.KnowledgeBaseAnswerContext(
        previous_qna_id=previous_answer.qna_id
    ),
    project_name="FAQ",
    deployment_name="live"
)
for candidate in output.answers:
    print("({}) {}".format(candidate.confidence, candidate.answer))
    print("Source: {}".format(candidate.source))

Asynchronous operations

The above examples can also be run asynchronously using the client in the aio namespace:

from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.aio import QuestionAnsweringClient

client = QuestionAnsweringClient(endpoint, credential)

output = await client.get_answers(
    question="How long should my Surface battery last?",
    project_name="FAQ",
    deployment_name="production"
)

QuestionAnsweringProjectsClient

Create a new project

import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering.projects import QuestionAnsweringProjectsClient

# get service secrets
endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]

# create client
client = QuestionAnsweringProjectsClient(endpoint, AzureKeyCredential(key))
with client:

    # create project
    project_name = "IssacNewton"
    project = client.create_project(
        project_name=project_name,
        options={
            "description": "biography of Sir Issac Newton",
            "language": "en",
            "multilingualResource": True,
            "settings": {
                "defaultAnswer": "no answer"
            }
        })

    print("view created project info:")
    print("\tname: {}".format(project["projectName"]))
    print("\tlanguage: {}".format(project["language"]))
    print("\tdescription: {}".format(project["description"]))

Add a knowledge source

update_sources_poller = client.begin_update_sources(
    project_name=project_name,
    sources=[
        {
            "op": "add",
            "value": {
                "displayName": "Issac Newton Bio",
                "sourceUri": "https://wikipedia.org/wiki/Isaac_Newton",
                "sourceKind": "url"
            }
        }
    ]
)
update_sources_poller.result()

# list sources
print("list project sources")
sources = client.list_sources(
    project_name=project_name
)
for source in sources:
    print("project: {}".format(source["displayName"]))
    print("\tsource: {}".format(source["source"]))
    print("\tsource Uri: {}".format(source["sourceUri"]))
    print("\tsource kind: {}".format(source["sourceKind"]))

Deploy your project

# deploy project
deployment_poller = client.begin_deploy_project(
    project_name=project_name,
    deployment_name="production"
)
deployment_poller.result()

# list all deployments
deployments = client.list_deployments(
    project_name=project_name
)

print("view project deployments")
for d in deployments:
    print(d)

Optional Configuration

Optional keyword arguments can be passed in at the client and per-operation level. The azure-core reference documentation describes available configurations for retries, logging, transport protocols, and more.

Troubleshooting

General

Azure QuestionAnswering clients raise exceptions defined in Azure Core. When you interact with the Cognitive Language Services Question Answering client library using the Python SDK, errors returned by the service correspond to the same HTTP status codes returned for REST API requests.

For example, if you submit a question to a non-existant knowledge base, a 400 error is returned indicating "Bad Request".

from azure.core.exceptions import HttpResponseError

try:
    client.get_answers(
        question="Why?",
        project_name="invalid-knowledge-base",
        deployment_name="test"
    )
except HttpResponseError as error:
    print("Query failed: {}".format(error.message))

Logging

This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.

Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the logging_enable argument.

See full SDK logging documentation with examples here.

Next steps

  • View our samples.
  • Read about the different features of the Question Answering service.
  • Try our service demos.

Contributing

See the CONTRIBUTING.md for details on building, testing, and contributing to this library.

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

azure-ai-language-questionanswering-1.1.0b1.zip (129.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file azure-ai-language-questionanswering-1.1.0b1.zip.

File metadata

  • Download URL: azure-ai-language-questionanswering-1.1.0b1.zip
  • Upload date:
  • Size: 129.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for azure-ai-language-questionanswering-1.1.0b1.zip
Algorithm Hash digest
SHA256 b4693bda1df76ada5247314f290d9229127755fa6a8a4f94ea9f588baf850c0c
MD5 8dd693873e6e3750d01afd02d4dbbb27
BLAKE2b-256 70cf6f266e3eb39495eb89b43f23583f7f0709fc5765205dd3d0acedfbd4f27e

See more details on using hashes here.

File details

Details for the file azure_ai_language_questionanswering-1.1.0b1-py2.py3-none-any.whl.

File metadata

  • Download URL: azure_ai_language_questionanswering-1.1.0b1-py2.py3-none-any.whl
  • Upload date:
  • Size: 64.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for azure_ai_language_questionanswering-1.1.0b1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ee809d235fd370df1a2ca369d060ecb803be0c28aca62d68b4137f8422fb402b
MD5 9bc39355b74e4a1f58903edda7fb713a
BLAKE2b-256 70c93d944cd42bff4ca5480a9fb1f5c4eeba40faf351976572ba03a25f493432

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page