Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Version 2 model in natural language API

Mt
Bronze 1
Bronze 1

Hi, could anyone share the python code on how to get  natural language API to use version 2 classify text  categories?

I can get it working well with the default (version 1) categories but can't figure out where to adapt the standard code (as here: https://cloud.google.com/natural-language/docs/samples/language-classify-text-tutorial-classify?hl=e...)  to  use model version 2.

Many thanks 

Solved Solved
0 1 1,232
1 ACCEPTED SOLUTION

From the Classifying Content guide, you can include classification_model_options within the request dictionary argument to the classify_text() function. In these options, you can define the model and version to use for content categories.

// ...
content_categories_version = (
        language_v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion.V2) // Assigning the v2 model type
    response = client.classify_text(request = {
        "document": document,
        "classification_model_options": {
            "v2_model": {
                "content_categories_version": content_categories_version
            }
        }
    })
// ...

You can also check ClassificationModelOptions reference for available options.

View solution in original post

1 REPLY 1

From the Classifying Content guide, you can include classification_model_options within the request dictionary argument to the classify_text() function. In these options, you can define the model and version to use for content categories.

// ...
content_categories_version = (
        language_v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion.V2) // Assigning the v2 model type
    response = client.classify_text(request = {
        "document": document,
        "classification_model_options": {
            "v2_model": {
                "content_categories_version": content_categories_version
            }
        }
    })
// ...

You can also check ClassificationModelOptions reference for available options.