Mulai 29 April 2025, model Gemini 1.5 Pro dan Gemini 1.5 Flash tidak tersedia di project yang belum pernah menggunakan model ini, termasuk project baru. Untuk mengetahui detailnya, lihat
Versi dan siklus proses model.
Menentukan nilai enum pembuatan terkontrol dalam skema JSON
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Tentukan daftar nilai enum respons dalam skema JSON. Model memilih nilai enum dari daftar nilai yang ditentukan dalam skema.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","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)."]]