Skip to content

Commit a206bb9

Browse files
authored
Merge pull request google-gemini#53 from kinarr/go-update-x-enum-sample
Update x.enum in the controlled generation Go samples
2 parents f18def1 + 741f655 commit a206bb9

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

go/controlled_generation.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ func JsonNoSchema() (*genai.GenerateContentResponse, error) {
6666
log.Fatal(err)
6767
}
6868
prompt := "List a few popular cookie recipes in JSON format.\n\n" +
69-
"Use this JSON schema:\n\n" +
70-
"Recipe = {'recipe_name': str, 'ingredients': list[str]}\n" +
71-
"Return: list[Recipe]"
69+
"Use this JSON schema:\n\n" +
70+
"Recipe = {'recipe_name': str, 'ingredients': list[str]}\n" +
71+
"Return: list[Recipe]"
7272
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", genai.Text(prompt), nil)
7373
if err != nil {
7474
log.Fatal(err)
@@ -222,7 +222,7 @@ func JsonEnumRaw() (*genai.GenerateContentResponse, error) {
222222
}
223223

224224
parts := []*genai.Part{
225-
{Text: "Please summarize this transcript"},
225+
{Text: "What kind of instrument is this:"},
226226
{InlineData: &genai.Blob{Data: data, MIMEType: "image/jpeg"}},
227227
}
228228
contents := []*genai.Content{
@@ -252,12 +252,32 @@ func XEnum() (*genai.GenerateContentResponse, error) {
252252
log.Fatal(err)
253253
}
254254

255+
// Choice is a custom type representing a musical instrument category.
256+
type Choice string
257+
258+
const (
259+
Percussion Choice = "Percussion"
260+
String Choice = "String"
261+
Woodwind Choice = "Woodwind"
262+
Brass Choice = "Brass"
263+
Keyboard Choice = "Keyboard"
264+
)
265+
266+
// Define a schema restricting the response to the allowed Choice enum values.
267+
schema := &genai.Schema{
268+
Type: genai.TypeString,
269+
Enum: []string{
270+
string(Percussion),
271+
string(String),
272+
string(Woodwind),
273+
string(Brass),
274+
string(Keyboard),
275+
},
276+
}
277+
255278
config := &genai.GenerateContentConfig{
256279
ResponseMIMEType: "text/x.enum",
257-
ResponseSchema: &genai.Schema{
258-
Type: genai.TypeString,
259-
Enum: []string{"Percussion", "String", "Woodwind", "Brass", "Keyboard"},
260-
},
280+
ResponseSchema: schema,
261281
}
262282

263283
// Open the file.
@@ -274,7 +294,7 @@ func XEnum() (*genai.GenerateContentResponse, error) {
274294
}
275295

276296
parts := []*genai.Part{
277-
{Text: "Please summarize this transcript"},
297+
{Text: "What kind of instrument is this:"},
278298
{InlineData: &genai.Blob{Data: data, MIMEType: "image/jpeg"}},
279299
}
280300
contents := []*genai.Content{
@@ -326,7 +346,7 @@ func XEnumRaw() (*genai.GenerateContentResponse, error) {
326346
}
327347

328348
parts := []*genai.Part{
329-
{Text: "Please summarize this transcript"},
349+
{Text: "What kind of instrument is this:"},
330350
{InlineData: &genai.Blob{Data: data, MIMEType: "image/jpeg"}},
331351
}
332352
contents := []*genai.Content{

0 commit comments

Comments
 (0)