handles google streaming json response format variation

This commit is contained in:
nai-degen 2024-02-19 00:12:09 -06:00
parent 68d829bceb
commit 59bda40bbc
1 changed files with 8 additions and 6 deletions

View File

@ -84,26 +84,28 @@ export class SSEStreamAdapter extends Transform {
}
/** Processes an incoming array element from the Google AI JSON stream. */
protected processGoogleObject(value: any): string | null {
protected processGoogleObject(data: any): string | null {
// Sometimes data has fields key and value, sometimes it's just the
// candidates array.
const candidates = data.value?.candidates ?? data.candidates ?? [{}];
try {
const candidates = value.candidates ?? [{}];
const hasParts = candidates[0].content?.parts?.length > 0;
if (hasParts) {
return `data: ${JSON.stringify(value)}`;
return `data: ${JSON.stringify(data)}`;
} else {
this.log.error({ event: value }, "Received bad Google AI event");
this.log.error({ event: data }, "Received bad Google AI event");
return `data: ${makeCompletionSSE({
format: "google-ai",
title: "Proxy stream error",
message:
"The proxy received malformed or unexpected data from Google AI while streaming.",
obj: value,
obj: data,
reqId: "proxy-sse-adapter-message",
model: "",
})}`;
}
} catch (error) {
error.lastEvent = value;
error.lastEvent = data;
this.emit("error", error);
}
return null;