Test chat prompts (Generative AI)
Stay organized with collections
Save and categorize content based on your preferences.
Test a text prompt using a publisher chat 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,["# Test chat prompts (Generative AI)\n\nTest a text prompt using a publisher chat 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.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 // Send a Predict request to a large language model to test a chat prompt\n public class PredictChatPromptSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String instance =\n \"{\\n\"\n + \" \\\"context\\\": \\\"My name is Ned. You are my personal assistant. My favorite movies\"\n + \" are Lord of the Rings and Hobbit.\\\",\\n\"\n + \" \\\"examples\\\": [ { \\n\"\n + \" \\\"input\\\": {\\\"content\\\": \\\"Who do you work for?\\\"},\\n\"\n + \" \\\"output\\\": {\\\"content\\\": \\\"I work for Ned.\\\"}\\n\"\n + \" },\\n\"\n + \" { \\n\"\n + \" \\\"input\\\": {\\\"content\\\": \\\"What do I like?\\\"},\\n\"\n + \" \\\"output\\\": {\\\"content\\\": \\\"Ned likes watching movies.\\\"}\\n\"\n + \" }],\\n\"\n + \" \\\"messages\\\": [\\n\"\n + \" { \\n\"\n + \" \\\"author\\\": \\\"user\\\",\\n\"\n + \" \\\"content\\\": \\\"Are my favorite movies based on a book series?\\\"\\n\"\n + \" }]\\n\"\n + \"}\";\n String parameters =\n \"{\\n\"\n + \" \\\"temperature\\\": 0.3,\\n\"\n + \" \\\"maxDecodeSteps\\\": 200,\\n\"\n + \" \\\"topP\\\": 0.8,\\n\"\n + \" \\\"topK\\\": 40\\n\"\n + \"}\";\n String project = \"YOUR_PROJECT_ID\";\n String publisher = \"google\";\n String model = \"chat-bison@001\";\n\n predictChatPrompt(instance, parameters, project, publisher, model);\n }\n\n static void predictChatPrompt(\n String instance, String parameters, String project, String publisher, String model)\n throws IOException {\n PredictionServiceSettings predictionServiceSettings =\n PredictionServiceSettings.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 (PredictionServiceClient predictionServiceClient =\n PredictionServiceClient.create(predictionServiceSettings)) {\n String location = \"us-central1\";\n final EndpointName endpointName =\n EndpointName.ofProjectLocationPublisherModelName(project, location, publisher, model);\n\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.html.Builder instanceValue = https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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/protobuf/latest/com.google.protobuf.Value.html.Builder parameterValueBuilder = https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.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/protobuf/latest/com.google.protobuf.Value.html parameterValue = parameterValueBuilder.build();\n\n PredictResponse 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)."]]