Clasifica opiniones en texto (IA generativa)
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Clasifica la opinión que se transmite en texto en forma positiva o negativa.
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["# Classify sentiment in text (Generative AI)\n\nClassify the sentiment conveyed in text as positive or negative.\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.v1beta1.EndpointName;\n import com.google.cloud.aiplatform.v1beta1.PredictResponse;\n import com.google.cloud.aiplatform.v1beta1.PredictionServiceClient;\n import com.google.cloud.aiplatform.v1beta1.PredictionServiceSettings;\n import com.google.protobuf.https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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 sentiment analysis with a Large Language Model\n public class PredictTextSentimentSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // The details of designing text prompts for supported large language models:\n // https://cloud.google.com/vertex-ai/docs/generative-ai/text/text-overview\n String instance =\n \"{ \\\"content\\\": \\\"I had to compare two versions of Hamlet for my Shakespeare \\n\"\n + \"class and unfortunately I picked this version. Everything from the acting \\n\"\n + \"(the actors deliver most of their lines directly to the camera) to the camera \\n\"\n + \"shots (all medium or close up shots...no scenery shots and very little back \\n\"\n + \"ground in the shots) were absolutely terrible. I watched this over my spring \\n\"\n + \"break and it is very safe to say that I feel that I was gypped out of 114 \\n\"\n + \"minutes of my vacation. Not recommended by any stretch of the imagination.\\n\"\n + \"Classify the sentiment of the message: negative\\n\"\n + \"\\n\"\n + \"Something surprised me about this movie - it was actually original. It was \\n\"\n + \"not the same old recycled crap that comes out of Hollywood every month. I saw \\n\"\n + \"this movie on video because I did not even know about it before I saw it at my \\n\"\n + \"local video store. If you see this movie available - rent it - you will not \\n\"\n + \"regret it.\\n\"\n + \"Classify the sentiment of the message: positive\\n\"\n + \"\\n\"\n + \"My family has watched Arthur Bach stumble and stammer since the movie first \\n\"\n + \"came out. We have most lines memorized. I watched it two weeks ago and still \\n\"\n + \"get tickled at the simple humor and view-at-life that Dudley Moore portrays. \\n\"\n + \"Liza Minelli did a wonderful job as the side kick - though I'm not her \\n\"\n + \"biggest fan. This movie makes me just enjoy watching movies. My favorite scene \\n\"\n + \"is when Arthur is visiting his fiancée's house. His conversation with the \\n\"\n + \"butler and Susan's father is side-spitting. The line from the butler, \\n\"\n + \"\\\\\\\"Would you care to wait in the Library\\\\\\\" followed by Arthur's reply, \\n\"\n + \"\\\\\\\"Yes I would, the bathroom is out of the question\\\\\\\", is my NEWMAIL \\n\"\n + \"notification on my computer.\\n\"\n + \"Classify the sentiment of the message: positive\\n\"\n + \"\\n\"\n + \"This Charles outing is decent but this is a pretty low-key performance. Marlon \\n\"\n + \"Brando stands out. There's a subplot with Mira Sorvino and Donald Sutherland \\n\"\n + \"that forgets to develop and it hurts the film a little. I'm still trying to \\n\"\n + \"figure out why Charlie want to change his name.\\n\"\n + \"Classify the sentiment of the message: negative\\n\"\n + \"\\n\"\n + \"Tweet: The Pixel 7 Pro, is too big to fit in my jeans pocket, so I bought new \\n\"\n + \"jeans.\\n\"\n + \"Classify the sentiment of the message: \\\"}\";\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 location = \"us-central1\";\n String publisher = \"google\";\n String model = \"text-bison@001\";\n\n predictTextSentiment(instance, parameters, project, location, publisher, model);\n }\n\n static void predictTextSentiment(\n String instance,\n String parameters,\n String project,\n String location,\n String publisher,\n String model)\n throws IOException {\n String endpoint = String.format(\"%s-aiplatform.googleapis.com:443\", location);\n PredictionServiceSettings predictionServiceSettings =\n PredictionServiceSettings.newBuilder().setEndpoint(endpoint).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 (PredictionServiceClient predictionServiceClient =\n PredictionServiceClient.create(predictionServiceSettings)) {\n final EndpointName endpointName =\n EndpointName.ofProjectLocationPublisherModelName(project, location, publisher, model);\n\n // Use Value.Builder to convert instance to a dynamically typed value that can be\n // processed by the service.\n Vhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.htmlBuilder instanceValue = Vhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.htmlnewBuilder();\n Jhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.htmlparser().merge(instance, instanceValue);\n Lhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ListValue.htmlinstances = new ArrayList\u003c\u003e();\n instances.add(instanceValue.build());\n\n // Use Value.Builder to convert parameter to a dynamically typed value that can be\n // processed by the service.\n Vhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.htmlBuilder parameterValueBuilder = Vhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.htmlnewBuilder();\n Jhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.htmlparser().merge(parameters, parameterValueBuilder);\n Vhttps://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.htmlparameterValue = parameterValueBuilder.build();\n\n PredictResponse predictResponse =\n predictionServiceClient.predict(endpointName, instances, parameterValue);\n System.out.println(\"Predict Response\");\n System.out.println(predictResponse);\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)."]]