自 2025 年 4 月 29 日起,Gemini 1.5 Pro 和 Gemini 1.5 Flash 模型將無法用於先前未使用這些模型的專案,包括新專案。詳情請參閱「
模型版本和生命週期」。
程式碼完成功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Code Completions API,根據註解或部分撰寫的程式碼,以低延遲完成程式碼。
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 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,["# Code completion\n\nUse the Code Completions API to complete code based on comments or partially written code with low latency.\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.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.InvalidProtocolBufferException.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 PredictCodeCompletionCommentSample {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace this variable before running the sample.\n String project = \"YOUR_PROJECT_ID\";\n\n // Learn how to create prompts to work with a code model to create code completion suggestions:\n // https://cloud.google.com/vertex-ai/docs/generative-ai/code/code-completion-prompts\n String instance =\n \"{ \\\"prefix\\\": \\\"\"\n + \"def reverse_string(s):\\n\"\n + \" return s[::-1]\\n\"\n + \"#This function\"\n + \"\\\"}\";\n String parameters = \"{\\n\" + \" \\\"temperature\\\": 0.2,\\n\" + \" \\\"maxOutputTokens\\\": 64,\\n\" + \"}\";\n String location = \"us-central1\";\n String publisher = \"google\";\n String model = \"code-gecko@001\";\n\n predictComment(instance, parameters, project, location, publisher, model);\n }\n\n // Use Codey for Code Completion to complete a code comment\n public static void predictComment(\n String instance,\n String parameters,\n String project,\n String location,\n String publisher,\n String model)\n throws IOException {\n final 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 https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.html instanceValue = stringToValue(instance);\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.ListValue.html instances = new ArrayList\u003c\u003e();\n instances.add(instanceValue);\n\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.html parameterValue = stringToValue(parameters);\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 // Convert a Json string to a protobuf.Value\n static https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.html stringToValue(String value) throws https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.InvalidProtocolBufferException.html {\n https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.Value.html.Builder builder = 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(value, builder);\n return https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.TextFormatParseInfoTree.html#com_google_protobuf_TextFormatParseInfoTree_builder__.build();\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=generativeaionvertexai)."]]