OCI Language AI: NLP, Sentiment Analysis, and Text Classification

OCI Language provides pre-trained NLP capabilities as API endpoints: sentiment analysis, named entity recognition, key phrase extraction, text classification, and language detection. You call the API with text, and get structured results back in milliseconds. For custom domains where pre-trained models do not perform well enough, you can train custom text classification and named entity recognition models on your own labeled data within the same service. This post covers API integration patterns and Terraform setup.

Step 1: IAM Policy and Endpoint

resource "oci_identity_policy" "language_policy" {
  compartment_id = var.compartment_id
  name           = "oci-language-policy"
  statements = [
    "Allow group AIEngineers to manage ai-service-language-family in compartment id COMPARTMENT_OCID",
    "Allow dynamic-group ApplicationInstances to use ai-service-language in compartment id COMPARTMENT_OCID"
  ]
}

Step 2: Custom Text Classification Model

resource "oci_objectstorage_bucket" "language_training_data" {
  compartment_id = var.compartment_id
  namespace      = var.tenancy_namespace
  name           = "language-training-data"
  access_type    = "NoPublicAccess"
}

resource "oci_ai_language_project" "support_classification" {
  compartment_id = var.compartment_id
  display_name   = "support-ticket-classification"
  description    = "Classify support tickets by category and priority"
  defined_tags   = { "Operations.ManagedBy" = "terraform" }
}

resource "oci_ai_language_model" "ticket_classifier" {
  compartment_id = var.compartment_id
  project_id     = oci_ai_language_project.support_classification.id
  display_name   = "support-ticket-classifier-v1"
  description    = "Classifies support tickets into 12 categories"

  model_details {
    model_type = "TEXT_CLASSIFICATION"
    language_code = "en"
    classification_mode {
      classification_mode = "SINGLE_LABEL"
    }
  }

  training_dataset {
    dataset_type = "OBJECT_STORAGE"
    location_details {
      location_type = "OBJECT_STORAGE_PREFIX"
      namespace     = var.tenancy_namespace
      bucket        = oci_objectstorage_bucket.language_training_data.name
      prefix        = "training/support-tickets/"
    }
  }

  defined_tags = { "Operations.ManagedBy" = "terraform" }
}

output "model_id" { value = oci_ai_language_model.ticket_classifier.id }

Pre-Trained API Patterns

The OCI Language pre-trained APIs require no setup beyond IAM policy. Sentiment analysis returns a document-level sentiment (positive, negative, neutral, mixed) plus sentence-level sentiment with confidence scores for each. Named entity recognition identifies people, organizations, locations, dates, quantities, and product names from unstructured text. Key phrase extraction pulls the most significant noun phrases that summarize the document content.

For batch processing, use the asynchronous Batch APIs rather than calling the synchronous endpoints for each document. Batch jobs process up to 10,000 documents from Object Storage in a single API call, write results back to Object Storage, and complete in minutes. Calling the synchronous API per document for large batches hits rate limits and runs far slower than a single batch job for the same volume of text.

Step 3: Batch Sentiment Analysis Job

resource "oci_ai_language_job" "batch_sentiment_job" {
  compartment_id = var.compartment_id
  display_name   = "product-review-sentiment-batch"
  description    = "Batch sentiment analysis of product reviews"

  input_location {
    location_type = "OBJECT_STORAGE_PREFIX"
    namespace     = var.tenancy_namespace
    bucket        = var.reviews_bucket_name
    prefix        = "reviews/pending/"
  }

  output_location {
    namespace = var.tenancy_namespace
    bucket    = var.reviews_bucket_name
    prefix    = "reviews/sentiment-results/"
  }

  model_metadata_details {
    model_type    = "PRE_TRAINED_SENTIMENT_ANALYSIS"
    language_code = "ar"  # Arabic language model
  }

  defined_tags = { "Operations.ManagedBy" = "terraform" }
}

output "batch_job_id" { value = oci_ai_language_job.batch_sentiment_job.id }

Operational Notes

OCI Language supports Arabic natively with dedicated Arabic pre-trained models. For organizations in the Gulf region processing Arabic customer feedback, Arabic social media content, and Arabic documents, the Arabic sentiment and NER models are purpose-built for Modern Standard Arabic and Gulf dialect text. The language detection API automatically identifies the language before routing to the appropriate pre-trained model, which is useful for multilingual document pipelines.

Custom model training requires labeled training data in JSONL format. Each line contains the text and the label. The minimum training set is 50 examples per class with at least 2 classes. Quality matters more than volume: 200 well-labeled examples per class outperforms 2,000 poorly labeled ones. Evaluate the model using the evaluation metrics in the project, and retrain with more examples for classes where recall is below 80 percent.

Regards,
Osama

#OCI #OracleCloud #LanguageAI #NLP #Sentiment #TextClassification #Terraform #IaC #TechBlog #Oracle #AI #MachineLearning #OracleCloudInfrastructure #NER #KeyPhrase #BatchProcessing #CustomModel #Arabic #GenAI #AIService

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.