Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[text analytics] Add how to get json response to sample #11102

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/textanalytics/azure-ai-textanalytics/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ what you can do with the Azure Text Analytics client library.

|**Advanced Sample File Name**|**Description**|
|----------------|-------------|
|[sample_get_detailed_diagnostics_information.py][get_detailed_diagnostics_information] and [sample_get_detailed_diagnostics_information_async.py][get_detailed_diagnostics_information_async]|Get the request batch statistics, model version, and raw response through a callback|
|[sample_get_detailed_diagnostics_information.py][get_detailed_diagnostics_information] and [sample_get_detailed_diagnostics_information_async.py][get_detailed_diagnostics_information_async]|Get the request batch statistics, model version, and raw response in JSON format through a callback|

[azure_identity]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/identity/azure-identity
[sample_authentication]: https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

DESCRIPTION:
This sample demonstrates how to retrieve batch statistics, the
model version used, and the raw response returned from the service.
model version used, and the raw response in JSON format returned from the service.

USAGE:
python sample_get_detailed_diagnostics_information_async.py
Expand All @@ -24,6 +24,7 @@
import os
import asyncio
import logging
import json

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,7 +36,9 @@ class GetDetailedDiagnosticsInformationSampleAsync(object):
async def get_detailed_diagnostics_information_async(self):
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics.aio import TextAnalyticsClient
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

# This client will log detailed information about its HTTP sessions, at DEBUG level
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key), logging_enable=True)

documents = [
"I had the best day of my life.",
Expand All @@ -45,12 +48,12 @@ async def get_detailed_diagnostics_information_async(self):
]

def callback(resp):
_LOGGER.info("document_count: {}".format(resp.statistics["document_count"]))
_LOGGER.info("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
_LOGGER.info("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
_LOGGER.info("transaction_count: {}".format(resp.statistics["transaction_count"]))
_LOGGER.info("model_version: {}".format(resp.model_version))
_LOGGER.info("raw_response: {}".format(resp.raw_response))
_LOGGER.debug("document_count: {}".format(resp.statistics["document_count"]))
_LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
_LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
_LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"]))
_LOGGER.debug("model_version: {}".format(resp.model_version))
_LOGGER.debug("raw_response in json format: {}".format(json.dumps(resp.raw_response)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we retrieve the response out of the callback function so a user can use it below? I think it makes sense to log the other pieces, but if someone goes through the trouble of passing the response hook to get the JSON response, I imagine it's because they want to use it, not just print it.


async with text_analytics_client:
result = await text_analytics_client.analyze_sentiment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

DESCRIPTION:
This sample demonstrates how to retrieve batch statistics, the
model version used, and the raw response returned from the service.
model version used, and the raw response in JSON format returned from the service.

USAGE:
python sample_get_detailed_diagnostics_information.py
Expand All @@ -23,6 +23,7 @@

import os
import logging
import json

_LOGGER = logging.getLogger(__name__)

Expand All @@ -33,7 +34,9 @@ class GetDetailedDiagnosticsInformationSample(object):
def get_detailed_diagnostics_information(self):
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key))

# This client will log detailed information about its HTTP sessions, at DEBUG level
text_analytics_client = TextAnalyticsClient(endpoint=self.endpoint, credential=AzureKeyCredential(self.key), logging_enable=True)

documents = [
"I had the best day of my life.",
Expand All @@ -43,12 +46,12 @@ def get_detailed_diagnostics_information(self):
]

def callback(resp):
_LOGGER.info("document_count: {}".format(resp.statistics["document_count"]))
_LOGGER.info("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
_LOGGER.info("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
_LOGGER.info("transaction_count: {}".format(resp.statistics["transaction_count"]))
_LOGGER.info("model_version: {}".format(resp.model_version))
_LOGGER.info("raw_response: {}".format(resp.raw_response))
_LOGGER.debug("document_count: {}".format(resp.statistics["document_count"]))
_LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"]))
_LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"]))
_LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"]))
_LOGGER.debug("model_version: {}".format(resp.model_version))
_LOGGER.debug("raw_response in json format: {}".format(json.dumps(resp.raw_response)))

result = text_analytics_client.analyze_sentiment(
documents,
Expand Down