Test a test prompt to generate ideas using a publisher text model.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).


    import com.google.cloud.aiplatform.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html;
    import com.google.cloud.aiplatform.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictResponse.html;
    import com.google.cloud.aiplatform.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html;
    import com.google.cloud.aiplatform.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html;
    import com.google.protobuf.https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html;
    import com.google.protobuf.util.https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    public class PredictTextPromptSample {

      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
        // Details of designing text prompts for supported large language models:
        // https://cloud.google.com/vertex-ai/docs/generative-ai/text/text-overview
        String instance =
            "{ \"prompt\": " + "\"Give me ten interview questions for the role of program manager.\"}";
        String parameters =
            "{\n"
                + "  \"temperature\": 0.2,\n"
                + "  \"maxOutputTokens\": 256,\n"
                + "  \"topP\": 0.95,\n"
                + "  \"topK\": 40\n"
                + "}";
        String project = "YOUR_PROJECT_ID";
        String location = "us-central1";
        String publisher = "google";
        String model = "text-bison@001";

        predictTextPrompt(instance, parameters, project, location, publisher, model);
      }

      // Get a text prompt from a supported text model
      public static void predictTextPrompt(
          String instance,
          String parameters,
          String project,
          String location,
          String publisher,
          String model)
          throws IOException {
        String endpoint = String.format("%s-aiplatform.googleapis.com:443", location);
        https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html predictionServiceSettings =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceSettings.html.newBuilder().setEndpoint(endpoint).build();

        // Initialize client that will be used to send requests. This client only needs to be created
        // once, and can be reused for multiple requests.
        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html predictionServiceClient =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictionServiceClient.html.create(predictionServiceSettings)) {
          final https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html endpointName =
              https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.EndpointName.html.https://docs.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);

          // Initialize client that will be used to send requests. This client only needs to be created
          // once, and can be reused for multiple requests.
          https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.Builder instanceValue = https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder();
          https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html.parser().merge(instance, instanceValue);
          https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ListValue.html instances = new ArrayList<>();
          instances.add(instanceValue.build());

          // Use Value.Builder to convert instance to a dynamically typed value that can be
          // processed by the service.
          https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.Builder parameterValueBuilder = https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html.newBuilder();
          https://docs.cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.util.JsonFormat.html.parser().merge(parameters, parameterValueBuilder);
          https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.Value.html parameterValue = parameterValueBuilder.build();

          https://docs.cloud.google.com/java/docs/reference/google-cloud-aiplatform/latest/com.google.cloud.aiplatform.v1.PredictResponse.html predictResponse =
              predictionServiceClient.predict(endpointName, instances, parameterValue);
          System.out.println("Predict Response");
          System.out.println(predictResponse);
        }
      }
    }

### Ruby


Before trying this sample, follow the Ruby setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Ruby API
reference documentation](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-ai_platform/latest).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    require "google/cloud/ai_platform/v1"

    ##
    # Vertex AI Predict Text Prompt
    #
    # @param project_id [String] Your Google Cloud project (e.g. "my-project")
    # @param location_id [String] Your Processor Location (e.g. "us-central1")
    # @param publisher [String] The Model Publisher (e.g. "google")
    # @param model [String] The Model Identifier (e.g. "text-bison@001")
    #
    def predict_text_prompt project_id:, location_id:, publisher:, model:
      # Create the Vertex AI client.
      client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|
        config.endpoint = "#{location_id}-aiplatform.googleapis.com"
      end

      # Build the resource name from the project.
      endpoint = client.endpoint_path(
        project: project_id,
        location: location_id,
        publisher: publisher,
        model: model
      )

      prompt = "Give me ten interview questions for the role of program manager."

      # Initialize the request arguments
      instance = Google::Protobuf::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-billing-budgets-v1beta1/latest/Google-Protobuf-Value.html.new(
        struct_value: Google::Protobuf::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-billing-budgets-v1beta1/latest/Google-Protobuf-Struct.html.new(
          fields: {
            "prompt" => Google::Protobuf::Value.new(
              string_value: prompt
            )
          }
        )
      )

      instances = [instance]

      parameters = Google::Protobuf::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-billing-budgets-v1beta1/latest/Google-Protobuf-Value.html.new(
        struct_value: Google::Protobuf::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-billing-budgets-v1beta1/latest/Google-Protobuf-Struct.html.new(
          fields: {
            "temperature" => Google::Protobuf::Value.new(number_value: 0.2),
            "maxOutputTokens" => Google::Protobuf::Value.new(number_value: 256),
            "topP" => Google::Protobuf::Value.new(number_value: 0.95),
            "topK" => Google::Protobuf::Value.new(number_value: 40)
          }
        )
      )

      # Make the prediction request
      response = client.predict endpoint: endpoint, instances: instances, parameters: parameters

      # Handle the prediction response
      puts "Predict Response"
      puts response
    end

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=aiplatform).