gateway: Chat input_audio reaches the upstream as audio
A Chat Completions input_audio part was dropped on the way in, so a
Gemini model (Vertex AI, Gemini API, Code Assist) answered as if no
audio had been sent (Parlevent_ on X). It is now a file part with its
format's MIME type (wav -> audio/wav, mp3/mpeg -> audio/mp3, a full type
kept), which a Gemini upstream gets as inlineData, as a video or PDF
already is.
Run (mica, mica-run):
- TestChatAudioReachesGemini, TestAudioType: green; red on e9fcc6229
(upstream body had the text part alone)
- go test ./... and go vet (linux, windows) with the commit before
it: see that commit's record (same runs)
Not tried: a real Vertex AI request with audio. To a Chat, Responses
or Messages upstream the audio is still the "[attachment audio/wav]"
note, not carried as input_audio.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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/<format>, 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
|
||||
|
||||
Reference in New Issue
Block a user