Summarize text content using Generative AI (Generative AI)
Stay organized with collections
Save and categorize content based on your preferences.
Summarize text content using a publisher text model.
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,["# Summarize text content using Generative AI (Generative AI)\n\nSummarize text content using a publisher text model.\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 Summarization with a Large Language Model\n public class PredictTextSummarizationSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // Designing prompts for text summerization with supported large language models:\n // https://cloud.google.com/vertex-ai/docs/generative-ai/text/summarization-prompts\n String instance =\n \"{ \\\"content\\\": \\\"Background: There is evidence that there have been significant changes \\n\"\n + \"in Amazon rainforest vegetation over the last 21,000 years through the Last \\n\"\n + \"Glacial Maximum (LGM) and subsequent deglaciation. Analyses of sediment \\n\"\n + \"deposits from Amazon basin paleo lakes and from the Amazon Fan indicate that \\n\"\n + \"rainfall in the basin during the LGM was lower than for the present, and this \\n\"\n + \"was almost certainly associated with reduced moist tropical vegetation cover \\n\"\n + \"in the basin. There is debate, however, over how extensive this reduction \\n\"\n + \"was. Some scientists argue that the rainforest was reduced to small, isolated \\n\"\n + \"refugia separated by open forest and grassland; other scientists argue that \\n\"\n + \"the rainforest remained largely intact but extended less far to the north, \\n\"\n + \"south, and east than is seen today. This debate has proved difficult to \\n\"\n + \"resolve because the practical limitations of working in the rainforest mean \\n\"\n + \"that data sampling is biased away from the center of the Amazon basin, and \\n\"\n + \"both explanations are reasonably well supported by the available data.\\n\"\n + \"\\n\"\n + \"Q: What does LGM stands for?\\n\"\n + \"A: Last Glacial Maximum.\\n\"\n + \"\\n\"\n + \"Q: What did the analysis from the sediment deposits indicate?\\n\"\n + \"A: Rainfall in the basin during the LGM was lower than for the present.\\n\"\n + \"\\n\"\n + \"Q: What are some of scientists arguments?\\n\"\n + \"A: The rainforest was reduced to small, isolated refugia separated by open forest\"\n + \" and grassland.\\n\"\n + \"\\n\"\n + \"Q: There have been major changes in Amazon rainforest vegetation over the last how\"\n + \" many years?\\n\"\n + \"A: 21,000.\\n\"\n + \"\\n\"\n + \"Q: What caused changes in the Amazon rainforest vegetation?\\n\"\n + \"A: The Last Glacial Maximum (LGM) and subsequent deglaciation\\n\"\n + \"\\n\"\n + \"Q: What has been analyzed to compare Amazon rainfall in the past and present?\\n\"\n + \"A: Sediment deposits.\\n\"\n + \"\\n\"\n + \"Q: What has the lower rainfall in the Amazon during the LGM been attributed to?\\n\"\n + \"A:\\\"}\";\n String parameters =\n \"{\\n\"\n + \" \\\"temperature\\\": 0,\\n\"\n + \" \\\"maxOutputTokens\\\": 32,\\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 predictTextSummarization(instance, parameters, project, location, publisher, model);\n }\n\n // Get summarization from a supported text model\n public static void predictTextSummarization(\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 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(endpoint)\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 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 // Use Value.Builder to convert instance to a dynamically typed value that can be\n // processed by the service.\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 // Use Value.Builder to convert parameter to a dynamically typed value that can be\n // processed by the service.\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 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)."]]