测试聊天提示(生成式 AI)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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)."]]