自 2025 年 4 月 29 日起,Gemini 1.5 Pro 和 Gemini 1.5 Flash 模型將無法用於先前未使用這些模型的專案,包括新專案。詳情請參閱「
模型版本和生命週期」。
在 JSON 結構定義中指定受控產生類別值
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 JSON 結構定義中指定回應列舉值清單。模型會從結構定義中定義的清單值選取列舉值。
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 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,["# Specify controlled generation enum values in a JSON schema\n\nSpecify the list of response enum values in a JSON schema. The model selects an enum value from a list values that are defined in the schema.\n\nCode sample\n-----------\n\n### Go\n\n\nBefore trying this sample, follow the Go 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 Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/aiplatform/latest/apiv1).\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 import (\n \t\"context\"\n \t\"errors\"\n \t\"fmt\"\n \t\"io\"\n\n \t\"cloud.google.com/go/vertexai/genai\"\n )\n\n // controlledGenerationResponseSchemaEnum demonstrates how to constrain model responses\n // to a predefined set of enum values for genre classification.\n func controlledGenerationResponseSchemaEnum(w io.Writer, projectID, location, modelName string) error {\n \t// location = \"us-central1\"\n \t// modelName = \"gemini-2.0-flash-001\"\n \tctx := context.Background()\n \tclient, err := genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_NewClient(ctx, projectID, location)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create GenAI client: %w\", err)\n \t}\n \tdefer client.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Client_Close()\n\n \tmodel := client.GenerativeModel(modelName)\n\n \tmodel.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerationConfig.ResponseMIMEType = \"text/x.enum\"\n \tmodel.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerationConfig.ResponseSchema = &genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Schema{\n \t\tType: genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_TypeUnspecified_TypeString_TypeNumber_TypeInteger_TypeBoolean_TypeArray_TypeObject,\n \t\tEnum: []string{\"drama\", \"comedy\", \"documentary\"},\n \t}\n\n \tprompt := `\n The film aims to educate and inform viewers about real-life subjects, events, or people.\n It offers a factual record of a particular topic by combining interviews, historical footage,\n and narration. The primary purpose of a film is to present information and provide insights\n into various aspects of reality.\n `\n\n \tres, err := model.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_GenerativeModel_GenerateContent(ctx, genai.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Text(prompt))\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to generate content: %w\", err)\n \t}\n\n \tif len(res.Candidates) == 0 || len(res.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts) == 0 {\n \t\treturn errors.https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai/tokenizer.html#cloud_google_com_go_vertexai_genai_tokenizer_Tokenizer_New(\"got empty response from model\")\n \t}\n\n \tfmt.Fprintf(w, \"Candidate label: %q\", res.Candidates[0].https://cloud.google.com/vertex-ai/generative-ai/docs/reference/go/latest/genai.html#cloud_google_com_go_vertexai_genai_Content.Parts[0])\n \t// Example response:\n \t// Candidate label: \"documentary\"\n\n \treturn nil\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)."]]