Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go/plugins/compat_oai/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (g *ModelGenerator) WithMessages(messages []*ai.Message) *ModelGenerator {
case ai.RoleSystem:
oaiMessages = append(oaiMessages, openai.SystemMessage(content))
case ai.RoleModel:

am := openai.ChatCompletionAssistantMessageParam{}
am.Content.OfString = param.NewOpt(content)
toolCalls, err := convertToolCalls(msg.Content)
Expand Down Expand Up @@ -113,10 +112,11 @@ func (g *ModelGenerator) WithMessages(messages []*ai.Message) *ModelGenerator {
oaiMessages = append(oaiMessages, tm)
}
case ai.RoleUser:
oaiMessages = append(oaiMessages, openai.UserMessage(content))

parts := []openai.ChatCompletionContentPartUnionParam{}
for _, p := range msg.Content {
if p.IsText() {
parts = append(parts, openai.TextContentPart(p.Text))
}
if p.IsMedia() {
part := openai.ImageContentPart(
openai.ChatCompletionContentPartImageImageURLParam{
Expand Down
63 changes: 63 additions & 0 deletions go/plugins/compat_oai/openai/openai_live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ package openai_test

import (
"context"
"encoding/base64"
"io"
"math"
"net/http"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -279,4 +282,64 @@ func TestPlugin(t *testing.T) {
t.Fatalf("expecting 2 user messages, got: %d", userMsgCount)
}
})
t.Run("image", func(t *testing.T) {
image, err := fetchImgAsBase64()
if err != nil {
t.Fatalf("failed to fetch image: %v", err)
}
resp, err := genkit.Generate(ctx, g,
ai.WithModelName("openai/gpt-4.1-nano"),
ai.WithMessages(
ai.NewUserMessage(
ai.NewMediaPart("image/jpeg", "data:image/jpeg;base64,"+image),
ai.NewTextPart("What's in the image?."),
),
),
)
if err != nil {
t.Fatalf("failed to generate: %v", err)
}
if !strings.Contains(resp.Text(), "cat") {
t.Fatalf("image detection failed, want: cat, got: %s", resp.Text())
}
mediaMessages := 0
textMessages := 0
for _, m := range resp.Request.Messages {
for _, p := range m.Content {
if p.IsText() {
textMessages += 1
}
if p.IsMedia() {
mediaMessages += 1
}
}
}
if mediaMessages > 1 {
t.Fatalf("unwanted media message, want: 1, got: %d", mediaMessages)
}
if textMessages > 1 {
t.Fatalf("unwanted text message, want: 1, got %d", textMessages)
}
})
}

func fetchImgAsBase64() (string, error) {
// CC0 license image
imgURL := "https://pd.w.org/2025/07/896686fbbcd9990c9.84605288-2048x1365.jpg"
resp, err := http.Get(imgURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", err
}

imageBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

base64string := base64.StdEncoding.EncodeToString(imageBytes)
return base64string, nil
}
Loading