測試即時通訊提示 (生成式 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 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)."]]