測試文字提示 (生成式 AI)
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用發布商文字模型,透過測試提示生成構想。
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["Test a test prompt to generate ideas using a publisher text model.\n\nCode sample \n\nJava\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/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 public class PredictTextPromptSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // 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 \"{ \\\"prompt\\\": \" + \"\\\"Give me ten interview questions for the role of program manager.\\\"}\";\n String parameters =\n \"{\\n\"\n + \" \\\"temperature\\\": 0.2,\\n\"\n + \" \\\"maxOutputTokens\\\": 256,\\n\"\n + \" \\\"topP\\\": 0.95,\\n\"\n + \" \\\"topK\\\": 40\\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 predictTextPrompt(instance, parameters, project, location, publisher, model);\n }\n\n // Get a text prompt from a supported text model\n public static void predictTextPrompt(\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().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 (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 // 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 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 // 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/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 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\nRuby\n\n\nBefore trying this sample, follow the Ruby 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 Ruby API\nreference documentation](/ruby/docs/reference/google-cloud-ai_platform/latest).\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 require \"google/cloud/ai_platform/v1\"\n\n ##\n # Vertex AI Predict Text Prompt\n #\n # @param project_id [String] Your Google Cloud project (e.g. \"my-project\")\n # @param location_id [String] Your Processor Location (e.g. \"us-central1\")\n # @param publisher [String] The Model Publisher (e.g. \"google\")\n # @param model [String] The Model Identifier (e.g. \"text-bison@001\")\n #\n def predict_text_prompt project_id:, location_id:, publisher:, model:\n # Create the Vertex AI client.\n client = ::Google::Cloud::AIPlatform::V1::PredictionService::Client.new do |config|\n config.endpoint = \"#{location_id}-aiplatform.googleapis.com\"\n end\n\n # Build the resource name from the project.\n endpoint = client.endpoint_path(\n project: project_id,\n location: location_id,\n publisher: publisher,\n model: model\n )\n\n prompt = \"Give me ten interview questions for the role of program manager.\"\n\n # Initialize the request arguments\n instance = Google::Protobuf::https://cloud.google.com/ruby/docs/reference/google-cloud-logging-v2/latest/Google-Protobuf-Value.html.new(\n struct_value: Google::Protobuf::https://cloud.google.com/ruby/docs/reference/google-cloud-logging-v2/latest/Google-Protobuf-Struct.html.new(\n fields: {\n \"prompt\" =\u003e Google::Protobuf::Value.new(\n string_value: prompt\n )\n }\n )\n )\n\n instances = [instance]\n\n parameters = Google::Protobuf::https://cloud.google.com/ruby/docs/reference/google-cloud-logging-v2/latest/Google-Protobuf-Value.html.new(\n struct_value: Google::Protobuf::https://cloud.google.com/ruby/docs/reference/google-cloud-logging-v2/latest/Google-Protobuf-Struct.html.new(\n fields: {\n \"temperature\" =\u003e Google::Protobuf::Value.new(number_value: 0.2),\n \"maxOutputTokens\" =\u003e Google::Protobuf::Value.new(number_value: 256),\n \"topP\" =\u003e Google::Protobuf::Value.new(number_value: 0.95),\n \"topK\" =\u003e Google::Protobuf::Value.new(number_value: 40)\n }\n )\n )\n\n # Make the prediction request\n response = client.predict endpoint: endpoint, instances: instances, parameters: parameters\n\n # Handle the prediction response\n puts \"Predict Response\"\n puts response\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=aiplatform)."]]