1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
"""
FILE: sample_query_text.py
DESCRIPTION:
This sample demonstrates how to ask a question from supplied text data.
USAGE:
python sample_query_text.py
Set the environment variables with your own values before running the sample:
1) AZURE_QUESTIONANSWERING_ENDPOINT - the endpoint to your QuestionAnswering resource.
2) AZURE_QUESTIONANSWERING_KEY - your QuestionAnswering API key.
"""
def sample_query_text():
# [START query_text]
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.language.questionanswering import QuestionAnsweringClient
from azure.ai.language.questionanswering import models as qna
endpoint = os.environ["AZURE_QUESTIONANSWERING_ENDPOINT"]
key = os.environ["AZURE_QUESTIONANSWERING_KEY"]
client = QuestionAnsweringClient(endpoint, AzureKeyCredential(key))
with client:
question="How long it takes to charge surface?"
input = qna.AnswersFromTextOptions(
question=question,
text_documents=[
"Power and charging. It takes two to four hours to charge the Surface Pro 4 battery fully from an empty state. " +
"It can take longer if you're using your Surface for power-intensive activities like gaming or video streaming while you're charging it.",
"You can use the USB port on your Surface Pro 4 power supply to charge other devices, like a phone, while your Surface charges. " +
"The USB port on the power supply is only for charging, not for data transfer. If you want to use a USB device, plug it into the USB port on your Surface.",
]
)
output = client.get_answers_from_text(input)
if output.answers:
best_answer = [a for a in output.answers if a.confidence and a.confidence > 0.9][0]
print("Q: {}".format(question))
print("A: {}".format(best_answer.answer))
else:
print(f"No answers returned from question '{question}'")
# [END query_text]
if __name__ == '__main__':
sample_query_text()
|