Skip to content

Commit f117ad5

Browse files
authored
fix(go/ai): enable use of media URIs in prompts (#3630)
1 parent 69e2823 commit f117ad5

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

go/ai/prompt.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,11 @@ func convertToPartPointers(parts []dotprompt.Part) ([]*Part, error) {
468468
result[i] = NewTextPart(p.Text)
469469
}
470470
case *dotprompt.MediaPart:
471-
ct, err := contentType(p.Media.URL)
471+
ct, data, err := contentType(p.Media.ContentType, p.Media.URL)
472472
if err != nil {
473473
return nil, err
474474
}
475-
result[i] = NewMediaPart(ct, p.Media.URL)
475+
result[i] = NewMediaPart(ct, string(data))
476476
}
477477
}
478478
return result, nil
@@ -644,24 +644,30 @@ func variantKey(variant string) string {
644644
}
645645

646646
// contentType determines the MIME content type of the given data URI
647-
func contentType(uri string) (string, error) {
647+
func contentType(ct, uri string) (string, []byte, error) {
648648
if uri == "" {
649-
return "", errors.New("found empty URI in part")
649+
return "", nil, errors.New("found empty URI in part")
650650
}
651651

652652
if strings.HasPrefix(uri, "gs://") || strings.HasPrefix(uri, "http") {
653-
return "", errors.New("data URI is the only media type supported")
653+
if ct == "" {
654+
return "", nil, errors.New("must supply contentType when using media from gs:// or http(s):// URLs")
655+
}
656+
return ct, []byte(uri), nil
654657
}
655658
if contents, isData := strings.CutPrefix(uri, "data:"); isData {
656659
prefix, _, found := strings.Cut(contents, ",")
657660
if !found {
658-
return "", errors.New("failed to parse data URI: missing comma")
661+
return "", nil, errors.New("failed to parse data URI: missing comma")
659662
}
660663

661664
if p, isBase64 := strings.CutSuffix(prefix, ";base64"); isBase64 {
662-
return p, nil
665+
if ct == "" {
666+
ct = p
667+
}
668+
return ct, []byte(uri), nil
663669
}
664670
}
665671

666-
return "", errors.New("uri content type not found")
672+
return "", nil, errors.New("uri content type not found")
667673
}

go/samples/prompts/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ func PromptWithMultiMessage(ctx context.Context, g *genkit.Genkit) {
206206
if prompt == nil {
207207
log.Fatal("empty prompt")
208208
}
209-
resp, err := prompt.Execute(ctx)
209+
resp, err := prompt.Execute(ctx,
210+
ai.WithModelName("googleai/gemini-2.5-pro"),
211+
ai.WithInput(map[string]any{"videoUrl": "https://www.youtube.com/watch?v=K-hY0E6cGfo video/mp4"}),
212+
)
210213
if err != nil {
211214
log.Fatal(err)
212215
}
@@ -252,6 +255,7 @@ func PromptWithMessageHistory(ctx context.Context, g *genkit.Genkit) {
252255
helloPrompt := genkit.DefinePrompt(
253256
g, "PromptWithMessageHistory",
254257
ai.WithSystem("You are a helpful AI assistant named Walt"),
258+
ai.WithModelName("googleai/gemini-2.5-flash-lite"),
255259
ai.WithMessages(
256260
ai.NewUserTextMessage("Hi, my name is Bob"),
257261
ai.NewModelTextMessage("Hi, my name is Walt, what can I help you with?"),
@@ -277,7 +281,7 @@ func PromptWithExecuteOverrides(ctx context.Context, g *genkit.Genkit) {
277281

278282
// Call the model and add additional messages from the user.
279283
resp, err := helloPrompt.Execute(ctx,
280-
ai.WithModel(googlegenai.GoogleAIModel(g, "gemini-2.5-pro")),
284+
ai.WithModel(googlegenai.GoogleAIModel(g, "gemini-2.5-flash-lite")),
281285
ai.WithMessages(ai.NewUserTextMessage("And I like turtles.")),
282286
)
283287
if err != nil {
@@ -324,8 +328,8 @@ func PromptWithMediaType(ctx context.Context, g *genkit.Genkit) {
324328
log.Fatal("empty prompt")
325329
}
326330
resp, err := prompt.Execute(ctx,
327-
ai.WithModelName("googleai/gemini-2.0-flash"),
328-
ai.WithInput(map[string]any{"imageUrl": "data:image/jpg;base64," + img}),
331+
ai.WithModelName("googleai/gemini-2.5-flash"),
332+
ai.WithInput(map[string]any{"imageUrl": "data:image/jpeg;base64," + img}),
329333
)
330334
if err != nil {
331335
log.Fatal(err)

go/samples/prompts/prompts/media.prompt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
model: googleai/gemini-2.5-flash-preview-04-17
2+
model: googleai/gemini-2.5-flash
33
config:
44
temperature: 0.1
55
input:
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
---
2-
model: googleai/gemini-2.0-flash
2+
model: googleai/gemini-2.5-flash
3+
input:
4+
schema:
5+
videoUrl: string
6+
output:
7+
summary: string
38
---
49
{{ role "system" }}
510

6-
You are a great pirate and an AI assistant.
11+
You are a great AI assistant that summarizes videos talking as a pirate
712

813
{{ role "user" }}
914

10-
Say hi
15+
Give me a summary of this video
16+
{{media url=videoUrl}}

0 commit comments

Comments
 (0)