Microsoft Azure Question Answering Client Library for Python
Project description
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. 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)
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.
Examples
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:
from azure.ai.language.questionanswering import models as qna
params = qna.QueryKnowledgeBaseOptions(
question="How long should my Surface battery last?"
)
output = client.query_knowledge_base(
params,
project_name="FAQ",
)
for candidate in output.answers:
print("({}) {}".format(candidate.confidence_score, candidate.answer))
print("Source: {}".format(candidate.source))
You can set additional properties on QueryKnowledgeBaseOptions
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:
params = qna.models.QueryKnowledgeBaseOptions(
question="How long should charging take?"
context=qna.models.KnowledgeBaseAnswerRequestContext(
previous_qna_id=previous_answer.id
)
)
output = client.query_knowledge_base(
params,
project_name="FAQ"
)
for candidate in output.answers:
print("({}) {}".format(candidate.confidence_score, 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
from azure.ai.language.questionanswering import models as qna
client = QuestionAnsweringClient(endpoint, credential)
params = qna.QueryKnowledgeBaseOptions(
question="How long should my Surface battery last?"
)
output = await client.query_knowledge_base(
params,
project_name="FAQ"
)
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.query_knowledge_base(
params,
project_name="invalid-knowledge-base"
)
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file azure-ai-language-questionanswering-1.0.0b2.zip
.
File metadata
- Download URL: azure-ai-language-questionanswering-1.0.0b2.zip
- Upload date:
- Size: 74.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97ccd1ff1b0db6777ea9cfdcbfcba821fb4f1a350db9b41b0a20badcbd144f5d |
|
MD5 | 7caa138310377ac754f0a480d5a1b086 |
|
BLAKE2b-256 | 8192c8c0c79fcdec47ad3dd87a6d759f19bc24ce906acea9f85a100ae7c0910c |
File details
Details for the file azure_ai_language_questionanswering-1.0.0b2-py2.py3-none-any.whl
.
File metadata
- Download URL: azure_ai_language_questionanswering-1.0.0b2-py2.py3-none-any.whl
- Upload date:
- Size: 33.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 679ba246c253309fed70d4c448830dd4a9724838d3dafe0106b93d04cd07a367 |
|
MD5 | ea49398de2d9b9e1cc14d1b1ad3a4afd |
|
BLAKE2b-256 | edf5a600ffb490f2a2539254cec96da191c0e8f06541cf09756bab6edb3ed358 |