Classify text with a large language model (Generative AI)
Stay organized with collections
Save and categorize content based on your preferences.
Perform classification tasks that assign a class or category to text. You can specify a list of categories to choose from or let the model choose from its own categories.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Classify text with a large language model (Generative AI)\n\nPerform classification tasks that assign a class or category to text. You can specify a list of categories to choose from or let the model choose from its own categories.\n\nCode sample\n-----------\n\n### Java\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Java API\nreference documentation](/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictResponse.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html;\n import com.google.cloud.aiplatform.v1.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html;\n import com.google.protobuf.util.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html;\n import java.io.IOException;\n import java.util.ArrayList;\n import java.util.List;\n\n // Text Classification with a Large Language Model\n public class PredictTextClassificationSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String instance =\n \"{ \\\"content\\\": \\\"What is the topic for a given news headline?\\n\"\n + \"- business\\n\"\n + \"- entertainment\\n\"\n + \"- health\\n\"\n + \"- sports\\n\"\n + \"- technology\\n\"\n + \"\\n\"\n + \"Text: Pixel 7 Pro Expert Hands On Review, the Most Helpful Google Phones.\\n\"\n + \"The answer is: technology\\n\"\n + \"\\n\"\n + \"Text: Quit smoking?\\n\"\n + \"The answer is: health\\n\"\n + \"\\n\"\n + \"Text: Roger Federer reveals why he touched Rafael Nadals hand while they were\"\n + \" crying\\n\"\n + \"The answer is: sports\\n\"\n + \"\\n\"\n + \"Text: Business relief from Arizona minimum-wage hike looking more remote\\n\"\n + \"The answer is: business\\n\"\n + \"\\n\"\n + \"Text: #TomCruise has arrived in Bari, Italy for #MissionImpossible.\\n\"\n + \"The answer is: entertainment\\n\"\n + \"\\n\"\n + \"Text: CNBC Reports Rising Digital Profit as Print Advertising Falls\\n\"\n + \"The answer is:\\\"}\";\n String parameters =\n \"{\\n\"\n + \" \\\"temperature\\\": 0,\\n\"\n + \" \\\"maxDecodeSteps\\\": 5,\\n\"\n + \" \\\"topP\\\": 0,\\n\"\n + \" \\\"topK\\\": 1\\n\"\n + \"}\";\n String project = \"YOUR_PROJECT_ID\";\n String publisher = \"google\";\n String model = \"text-bison@001\";\n\n predictTextClassification(instance, parameters, project, publisher, model);\n }\n\n static void predictTextClassification(\n String instance, String parameters, String project, String publisher, String model)\n throws IOException {\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html predictionServiceSettings =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html.newBuilder()\n .setEndpoint(\"us-central1-aiplatform.googleapis.com:443\")\n .build();\n\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n try (https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html predictionServiceClient =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html.create(predictionServiceSettings)) {\n String location = \"us-central1\";\n final https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html endpointName =\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html.https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html#com_google_cloud_aiplatform_v1_EndpointName_ofProjectLocationPublisherModelName_java_lang_String_java_lang_String_java_lang_String_java_lang_String_(project, location, publisher, model);\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.Builder instanceValue = https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder();\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html.parser().merge(instance, instanceValue);\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ListValue.html instances = new ArrayList\u003c\u003e();\n instances.add(instanceValue.build());\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.Builder parameterValueBuilder = https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder();\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html.parser().merge(parameters, parameterValueBuilder);\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html parameterValue = parameterValueBuilder.build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictResponse.html predictResponse =\n predictionServiceClient.predict(endpointName, instances, parameterValue);\n System.out.println(\"Predict Response\");\n }\n }\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=aiplatform)."]]