Skip to content

Commit 26d9c5d

Browse files
authored
Merge pull request #85 from kinarr/update-go-examples-to-role-constants
Update Go examples to use role constants (`RoleUser`, `RoleModel`)
2 parents 16520ae + 1a74986 commit 26d9c5d

11 files changed

+70
-70
lines changed

go/cache.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func CacheCreate() (*genai.GenerateContentResponse, error) {
3737
genai.NewPartFromURI(document.URI, document.MIMEType),
3838
}
3939
contents := []*genai.Content{
40-
genai.NewContentFromParts(parts, "user"),
40+
genai.NewContentFromParts(parts, genai.RoleUser),
4141
}
4242
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
4343
Contents: contents,
4444
SystemInstruction: genai.NewContentFromText(
45-
"You are an expert analyzing transcripts.", "user",
45+
"You are an expert analyzing transcripts.", genai.RoleUser,
4646
),
4747
})
4848
if err != nil {
@@ -97,12 +97,12 @@ func CacheCreateFromName() (*genai.GenerateContentResponse, error) {
9797
genai.NewPartFromURI(document.URI, document.MIMEType),
9898
}
9999
contents := []*genai.Content{
100-
genai.NewContentFromParts(parts, "user"),
100+
genai.NewContentFromParts(parts, genai.RoleUser),
101101
}
102102
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
103103
Contents: contents,
104104
SystemInstruction: genai.NewContentFromText(
105-
"You are an expert analyzing transcripts.", "user",
105+
"You are an expert analyzing transcripts.", genai.RoleUser,
106106
),
107107
})
108108
if err != nil {
@@ -151,7 +151,7 @@ func CacheCreateFromChat() (*genai.GenerateContentResponse, error) {
151151

152152
// Create initial chat with a system instruction.
153153
chat, err := client.Chats.Create(ctx, modelName, &genai.GenerateContentConfig{
154-
SystemInstruction: genai.NewContentFromText(systemInstruction, "user"),
154+
SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
155155
}, nil)
156156
if err != nil {
157157
log.Fatal(err)
@@ -199,7 +199,7 @@ func CacheCreateFromChat() (*genai.GenerateContentResponse, error) {
199199
// To cache the conversation so far, pass the chat history as the list of contents.
200200
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
201201
Contents: chat.History(false),
202-
SystemInstruction: genai.NewContentFromText(systemInstruction, "user"),
202+
SystemInstruction: genai.NewContentFromText(systemInstruction, genai.RoleUser),
203203
})
204204
if err != nil {
205205
log.Fatal(err)
@@ -258,13 +258,13 @@ func CacheDelete() error {
258258
genai.NewPartFromURI(document.URI, document.MIMEType),
259259
}
260260
contents := []*genai.Content{
261-
genai.NewContentFromParts(parts, "user"),
261+
genai.NewContentFromParts(parts, genai.RoleUser),
262262
}
263263

264264
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
265265
Contents: contents,
266266
SystemInstruction: genai.NewContentFromText(
267-
"You are an expert analyzing transcripts.", "user",
267+
"You are an expert analyzing transcripts.", genai.RoleUser,
268268
),
269269
})
270270
if err != nil {
@@ -306,13 +306,13 @@ func CacheGet() error {
306306
genai.NewPartFromURI(document.URI, document.MIMEType),
307307
}
308308
contents := []*genai.Content{
309-
genai.NewContentFromParts(parts, "user"),
309+
genai.NewContentFromParts(parts, genai.RoleUser),
310310
}
311311

312312
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
313313
Contents: contents,
314314
SystemInstruction: genai.NewContentFromText(
315-
"You are an expert analyzing transcripts.", "user",
315+
"You are an expert analyzing transcripts.", genai.RoleUser,
316316
),
317317
})
318318
if err != nil {
@@ -358,12 +358,12 @@ func CacheList() error {
358358
genai.NewPartFromURI(document.URI, document.MIMEType),
359359
}
360360
contents := []*genai.Content{
361-
genai.NewContentFromParts(parts, "user"),
361+
genai.NewContentFromParts(parts, genai.RoleUser),
362362
}
363363
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
364364
Contents: contents,
365365
SystemInstruction: genai.NewContentFromText(
366-
"You are an expert analyzing transcripts.", "user",
366+
"You are an expert analyzing transcripts.", genai.RoleUser,
367367
),
368368
})
369369
if err != nil {
@@ -425,12 +425,12 @@ func CacheUpdate() error {
425425
genai.NewPartFromURI(document.URI, document.MIMEType),
426426
}
427427
contents := []*genai.Content{
428-
genai.NewContentFromParts(parts, "user"),
428+
genai.NewContentFromParts(parts, genai.RoleUser),
429429
}
430430
cache, err := client.Caches.Create(ctx, modelName, &genai.CreateCachedContentConfig{
431431
Contents: contents,
432432
SystemInstruction: genai.NewContentFromText(
433-
"You are an expert analyzing transcripts.", "user",
433+
"You are an expert analyzing transcripts.", genai.RoleUser,
434434
),
435435
})
436436
if err != nil {

go/chat.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func Chat() error {
2424

2525
// Pass initial history using the History field.
2626
history := []*genai.Content{
27-
genai.NewContentFromText("Hello", "user"),
28-
genai.NewContentFromText("Great to meet you. What would you like to know?", "model"),
27+
genai.NewContentFromText("Hello", genai.RoleUser),
28+
genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
2929
}
3030

3131
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
@@ -61,8 +61,8 @@ func ChatStreaming() error {
6161
}
6262

6363
history := []*genai.Content{
64-
genai.NewContentFromText("Hello", "user"),
65-
genai.NewContentFromText("Great to meet you. What would you like to know?", "model"),
64+
genai.NewContentFromText("Hello", genai.RoleUser),
65+
genai.NewContentFromText("Great to meet you. What would you like to know?", genai.RoleModel),
6666
}
6767
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
6868
if err != nil {

go/controlled_generation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func JsonEnum() (*genai.GenerateContentResponse, error) {
131131
genai.NewPartFromURI(file.URI, file.MIMEType),
132132
}
133133
contents := []*genai.Content{
134-
genai.NewContentFromParts(parts, "user"),
134+
genai.NewContentFromParts(parts, genai.RoleUser),
135135
}
136136

137137
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash",
@@ -220,7 +220,7 @@ func JsonEnumRaw() (*genai.GenerateContentResponse, error) {
220220
genai.NewPartFromURI(file.URI, file.MIMEType),
221221
}
222222
contents := []*genai.Content{
223-
genai.NewContentFromParts(parts, "user"),
223+
genai.NewContentFromParts(parts, genai.RoleUser),
224224
}
225225

226226
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash",
@@ -290,7 +290,7 @@ func XEnum() (*genai.GenerateContentResponse, error) {
290290
genai.NewPartFromURI(file.URI, file.MIMEType),
291291
}
292292
contents := []*genai.Content{
293-
genai.NewContentFromParts(parts, "user"),
293+
genai.NewContentFromParts(parts, genai.RoleUser),
294294
}
295295
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash",
296296
contents,
@@ -339,7 +339,7 @@ func XEnumRaw() (*genai.GenerateContentResponse, error) {
339339
genai.NewPartFromURI(file.URI, file.MIMEType),
340340
}
341341
contents := []*genai.Content{
342-
genai.NewContentFromParts(parts, "user"),
342+
genai.NewContentFromParts(parts, genai.RoleUser),
343343
}
344344
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash",
345345
contents,

go/count_tokens.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TokensTextOnly() error {
4747

4848
// Convert prompt to a slice of *genai.Content using the helper.
4949
contents := []*genai.Content{
50-
genai.NewContentFromText(prompt, "user"),
50+
genai.NewContentFromText(prompt, genai.RoleUser),
5151
}
5252
countResp, err := client.Models.CountTokens(ctx, "gemini-2.0-flash", contents, nil)
5353
if err != nil {
@@ -81,8 +81,8 @@ func TokensChat() error {
8181

8282
// Initialize chat with some history.
8383
history := []*genai.Content{
84-
{Role: "user", Parts: []*genai.Part{{Text: "Hi my name is Bob"}}},
85-
{Role: "model", Parts: []*genai.Part{{Text: "Hi Bob!"}}},
84+
{Role: genai.RoleUser, Parts: []*genai.Part{{Text: "Hi my name is Bob"}}},
85+
{Role: genai.RoleModel, Parts: []*genai.Part{{Text: "Hi Bob!"}}},
8686
}
8787
chat, err := client.Chats.Create(ctx, "gemini-2.0-flash", nil, history)
8888
if err != nil {
@@ -104,7 +104,7 @@ func TokensChat() error {
104104
fmt.Printf("%#v\n", resp.UsageMetadata)
105105

106106
// Append an extra user message and recount.
107-
extra := genai.NewContentFromText("What is the meaning of life?", "user")
107+
extra := genai.NewContentFromText("What is the meaning of life?", genai.RoleUser)
108108
hist := chat.History(false)
109109
hist = append(hist, extra)
110110

@@ -144,7 +144,7 @@ func TokensMultimodalImageFileApi() error {
144144
genai.NewPartFromURI(file.URI, file.MIMEType),
145145
}
146146
contents := []*genai.Content{
147-
genai.NewContentFromParts(parts, "user"),
147+
genai.NewContentFromParts(parts, genai.RoleUser),
148148
}
149149

150150
tokenResp, err := client.Models.CountTokens(ctx, "gemini-2.0-flash", contents, nil)
@@ -205,7 +205,7 @@ func TokensMultimodalVideoAudioFileApi() error {
205205
genai.NewPartFromURI(file.URI, file.MIMEType),
206206
}
207207
contents := []*genai.Content{
208-
genai.NewContentFromParts(parts, "user"),
208+
genai.NewContentFromParts(parts, genai.RoleUser),
209209
}
210210

211211
tokenResp, err := client.Models.CountTokens(ctx, "gemini-2.0-flash", contents, nil)
@@ -252,7 +252,7 @@ func TokensMultimodalPdfFileApi() error {
252252
genai.NewPartFromURI(file.URI, file.MIMEType),
253253
}
254254
contents := []*genai.Content{
255-
genai.NewContentFromParts(parts, "user"),
255+
genai.NewContentFromParts(parts, genai.RoleUser),
256256
}
257257

258258
tokenResp, err := client.Models.CountTokens(ctx, "gemini-2.0-flash", contents, nil)
@@ -299,7 +299,7 @@ func TokensCachedContent() error {
299299
genai.NewPartFromURI(file.URI, file.MIMEType),
300300
}
301301
contents := []*genai.Content{
302-
genai.NewContentFromParts(parts, "user"),
302+
genai.NewContentFromParts(parts, genai.RoleUser),
303303
}
304304

305305
// Create cached content using a simple slice with text and a file.
@@ -312,14 +312,14 @@ func TokensCachedContent() error {
312312

313313
prompt := "Please give a short summary of this file."
314314
countResp, err := client.Models.CountTokens(ctx, "gemini-2.0-flash", []*genai.Content{
315-
genai.NewContentFromText(prompt, "user"),
315+
genai.NewContentFromText(prompt, genai.RoleUser),
316316
}, nil)
317317
if err != nil {
318318
log.Fatal(err)
319319
}
320320
fmt.Printf("%d", countResp.TotalTokens)
321321
response, err := client.Models.GenerateContent(ctx, "gemini-1.5-flash-001", []*genai.Content{
322-
genai.NewContentFromText(prompt, "user"),
322+
genai.NewContentFromText(prompt, genai.RoleUser),
323323
}, &genai.GenerateContentConfig{
324324
CachedContent: cache.Name,
325325
})

go/embed.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func EmbedContent() error {
2424
text := "Hello World!"
2525
outputDim := int32(10)
2626
contents := []*genai.Content{
27-
genai.NewContentFromText(text, "user"),
27+
genai.NewContentFromText(text, genai.RoleUser),
2828
}
2929
result, err := client.Models.EmbedContent(ctx, "text-embedding-004",
3030
contents, &genai.EmbedContentConfig{
@@ -55,9 +55,9 @@ func BatchEmbedContents() error {
5555
}
5656

5757
contents := []*genai.Content{
58-
genai.NewContentFromText("What is the meaning of life?", "user"),
59-
genai.NewContentFromText("How much wood would a woodchuck chuck?", "user"),
60-
genai.NewContentFromText("How does the brain work?", "user"),
58+
genai.NewContentFromText("What is the meaning of life?", genai.RoleUser),
59+
genai.NewContentFromText("How much wood would a woodchuck chuck?", genai.RoleUser),
60+
genai.NewContentFromText("How does the brain work?", genai.RoleUser),
6161
}
6262

6363
outputDim := int32(10)

go/files.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func FilesCreateText() (*genai.GenerateContentResponse, error) {
4141
}
4242

4343
contents := []*genai.Content{
44-
genai.NewContentFromParts(parts, "user"),
44+
genai.NewContentFromParts(parts, genai.RoleUser),
4545
}
4646

4747
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -83,7 +83,7 @@ func FilesCreateImage() (*genai.GenerateContentResponse, error) {
8383
}
8484

8585
contents := []*genai.Content{
86-
genai.NewContentFromParts(parts, "user"),
86+
genai.NewContentFromParts(parts, genai.RoleUser),
8787
}
8888

8989
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -124,7 +124,7 @@ func FilesCreateAudio() (*genai.GenerateContentResponse, error) {
124124
}
125125

126126
contents := []*genai.Content{
127-
genai.NewContentFromParts(parts, "user"),
127+
genai.NewContentFromParts(parts, genai.RoleUser),
128128
}
129129

130130
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -177,7 +177,7 @@ func FilesCreateVideo() (*genai.GenerateContentResponse, error) {
177177
}
178178

179179
contents := []*genai.Content{
180-
genai.NewContentFromParts(parts, "user"),
180+
genai.NewContentFromParts(parts, genai.RoleUser),
181181
}
182182

183183
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -217,7 +217,7 @@ func FilesCreatePdf() (*genai.GenerateContentResponse, error) {
217217
}
218218

219219
contents := []*genai.Content{
220-
genai.NewContentFromParts(parts, "user"),
220+
genai.NewContentFromParts(parts, genai.RoleUser),
221221
}
222222

223223
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -258,7 +258,7 @@ func FilesCreateFromIO() (*genai.GenerateContentResponse, error) {
258258
}
259259

260260
contents := []*genai.Content{
261-
genai.NewContentFromParts(parts, "user"),
261+
genai.NewContentFromParts(parts, genai.RoleUser),
262262
}
263263

264264
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
@@ -356,7 +356,7 @@ func FilesDelete() error {
356356
}
357357

358358
contents := []*genai.Content{
359-
genai.NewContentFromParts(parts, "user"),
359+
genai.NewContentFromParts(parts, genai.RoleUser),
360360
}
361361

362362
_, err = client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)

go/function_calling.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func FunctionCalling() error {
9191
// Create the content prompt.
9292
contents := []*genai.Content{
9393
genai.NewContentFromText(
94-
"I have 57 cats, each owns 44 mittens, how many mittens is that in total?", "user",
94+
"I have 57 cats, each owns 44 mittens, how many mittens is that in total?", genai.RoleUser,
9595
),
9696
}
9797

@@ -149,7 +149,7 @@ func FunctionCalling() error {
149149

150150
// Prepare the final result message as content.
151151
resultContents := []*genai.Content{
152-
genai.NewContentFromText("The final result is " + fmt.Sprintf("%v", result), "user"),
152+
genai.NewContentFromText("The final result is " + fmt.Sprintf("%v", result), genai.RoleUser),
153153
}
154154

155155
// Use GenerateContent to send the final result.

go/safety_settings.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SafetySettings() error {
3333
},
3434
}
3535
contents := []*genai.Content{
36-
genai.NewContentFromText(unsafePrompt, "user"),
36+
genai.NewContentFromText(unsafePrompt, genai.RoleUser),
3737
}
3838
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
3939
if err != nil {
@@ -82,7 +82,7 @@ func SafetySettingsMulti() error {
8282
},
8383
}
8484
contents := []*genai.Content{
85-
genai.NewContentFromText(unsafePrompt, "user"),
85+
genai.NewContentFromText(unsafePrompt, genai.RoleUser),
8686
}
8787
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)
8888
if err != nil {

go/system_instruction.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ func SystemInstruction() error {
2121

2222
// Construct the user message contents.
2323
contents := []*genai.Content{
24-
genai.NewContentFromText("Good morning! How are you?", "user"),
24+
genai.NewContentFromText("Good morning! How are you?", genai.RoleUser),
2525
}
2626

2727
// Set the system instruction as a *genai.Content.
2828
config := &genai.GenerateContentConfig{
29-
SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", "user"),
29+
SystemInstruction: genai.NewContentFromText("You are a cat. Your name is Neko.", genai.RoleUser),
3030
}
3131

3232
response, err := client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, config)

0 commit comments

Comments
 (0)