Python is great!! With just few lines of code, we can implement a service that authenticates, calls an OCI service, and prints the result 🙂
import oci
signer = oci.auth.signers.InstancePrincipalsSecurityTokenSigner()
ai_language_client = oci.ai_language.AIServiceLanguageClient(config={'region': 'eu-frankfurt-1'}, signer=signer)
detect_language_sentiments_response = ai_language_client.detect_language_sentiments(
detect_language_sentiments_details=oci.ai_language.models.DetectLanguageSentimentsDetails(
text="I do not like PSG"), opc_request_id="ZSDZFYMMEQIJ2HXSFSD23>")
print(detect_language_sentiments_response.data)
Granted, this authentication method (Instance Principals) only works from within an OCI Compute instance. Locally we would use the .oci config file for authentication.
config = oci.config.from_file(file_location=’C:/Users/DANIMMAR/Documents/.oci/config’)
import oci
config = oci.config.from_file(file_location='C:/Users/DANIMMAR/Documents/.oci/config')
language_client = oci.ai_language.AIServiceLanguageClient(config)
detect_dominant_language_response = language_client.detect_dominant_language(
detect_dominant_language_details=oci.ai_language.models.DetectDominantLanguageDetails(
text="Muito bom dia. Estou a fazer um teste do serviço de inteligência artificial."), opc_request_id="ZSDZFYMMEQIJ2HXWVI7SSDFGSDFG")
print(detect_dominant_language_response.data)
The batch request supports also Sentence based analysis
Instead of analyzing aspect-based (words) sentiment, we can also have sentence-based.
batch_detect_language_sentiments_response = ai_language_client.batch_detect_language_sentiments(
batch_detect_language_sentiments_details=oci.ai_language.models.BatchDetectLanguageSentimentsDetails(
documents=[
oci.ai_language.models.SentimentsDocument(
key="01",
text="<your text>",
)]),
opc_request_id="CIXQHCIX0WNU7J6Y564564",
level=["SENTENCE"])
The result is easier to visualize in the console, but the output is obviously the same.