diff --git a/internal/gateway/audio_input_test.go b/internal/gateway/audio_input_test.go new file mode 100644 index 000000000..8e52a9310 --- /dev/null +++ b/internal/gateway/audio_input_test.go @@ -0,0 +1,36 @@ +package gateway + +import ( + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// A Chat input_audio part reaches a Gemini upstream as inlineData with its +// format's MIME type. It was dropped on the way in, so the model answered +// as if no audio had been sent (Parlevent_ on X, Gemini on Vertex AI). +func TestChatAudioReachesGemini(t *testing.T) { + fresh(t) + noVision(t) + gem, gemUp := videoUp(t) + saveVideoProvider(t, provider.Provider{ID: "gem", Name: "Gem", Gemini: gemUp.URL + "/v1beta", Key: "k", Models: []string{"v", "t", "u"}}, gemUp.URL+"/v1beta") + s := New() + const audio = "UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAIA+AAACABAAZGF0YQAAAAA=" + for _, c := range []struct{ format, mime string }{{"wav", "audio/wav"}, {"mp3", "audio/mp3"}} { + gem.reset() + rec := serveVideo(s, "/v1/chat/completions", `{"model":"gem/u","messages":[{"role":"user","content":[{"type":"text","text":"这段音频说了什么?"},{"type":"input_audio","input_audio":{"data":"`+audio+`","format":"`+c.format+`"}}]}]}`) + bodies, _ := gem.sent() + if rec.Code != 200 || len(bodies) != 1 || !strings.Contains(bodies[0], `"mimeType":"`+c.mime+`"`) || !strings.Contains(bodies[0], audio) { + t.Errorf("%s audio: %d %s; upstream %v", c.format, rec.Code, rec.Body.String(), bodies) + } + } +} + +func TestAudioType(t *testing.T) { + for in, want := range map[string]string{"": "audio/wav", "wav": "audio/wav", "MP3": "audio/mp3", "mpeg": "audio/mp3", "flac": "audio/flac", "audio/ogg": "audio/ogg"} { + if got := audioType(in); got != want { + t.Errorf("audioType(%q) = %q, want %q", in, got, want) + } + } +} diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go index b3e5bf8aa..5726372aa 100644 --- a/internal/gateway/chat.go +++ b/internal/gateway/chat.go @@ -247,6 +247,10 @@ func chatParts(raw json.RawMessage) []Part { VideoURL struct { URL string `json:"url"` } `json:"video_url"` + InputAudio struct { + Data string `json:"data"` + Format string `json:"format"` + } `json:"input_audio"` // the AI SDK's file part, flat (#4101: ZCode's), whose video // alone is read here Data string `json:"data"` @@ -278,11 +282,34 @@ func chatParts(raw json.RawMessage) []Part { if v, ok := videoPart(it.VideoURL.URL, "", ""); ok { out = append(out, v) } + case "input_audio": + // a recording, as OpenAI's Chat takes one ({data, format}): + // left out before, so Gemini answered for the words alone + // (Parlevent_ on X); a Gemini upstream is given it inline + if it.InputAudio.Data != "" { + out = append(out, Part{Kind: File, MediaType: audioType(it.InputAudio.Format), Data: it.InputAudio.Data}) + } } } return out } +// audioType is the media type of a Chat input_audio's format: wav and mp3 +// are OpenAI's, a type given whole is kept, and the others are taken as +// audio/, as Gemini names them (audio/aac, audio/flac, audio/ogg). +func audioType(format string) string { + f := strings.ToLower(strings.TrimSpace(format)) + switch { + case f == "": + return "audio/wav" + case strings.Contains(f, "/"): + return f + case f == "mpeg": + return "audio/mp3" + } + return "audio/" + f +} + // chatFile is a Chat Completions file part (a PDF, say) as a file of the // request's, which a Gemini upstream is given as inline data and the // others are told of (attachmentText), rather than left out for the model