forked from XiaoMo/ChatGPT-Next-Web
fix: fix gemini pro streaming api duplicated issue (#3721)
* fix: streaming duplicated issue * chore: remove debug logs * chore: add types defination
This commit is contained in:
parent
eade013138
commit
a80502f7db
@ -9,6 +9,7 @@ import { prettyObject } from "@/app/utils/format";
|
|||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import Locale from "../../locales";
|
import Locale from "../../locales";
|
||||||
import { getServerSideConfig } from "@/app/config/server";
|
import { getServerSideConfig } from "@/app/config/server";
|
||||||
|
import de from "@/app/locales/de";
|
||||||
export class GeminiProApi implements LLMApi {
|
export class GeminiProApi implements LLMApi {
|
||||||
extractMessage(res: any) {
|
extractMessage(res: any) {
|
||||||
console.log("[Response] gemini-pro response: ", res);
|
console.log("[Response] gemini-pro response: ", res);
|
||||||
@ -87,9 +88,11 @@ export class GeminiProApi implements LLMApi {
|
|||||||
"streamGenerateContent",
|
"streamGenerateContent",
|
||||||
);
|
);
|
||||||
let finished = false;
|
let finished = false;
|
||||||
|
|
||||||
|
let existingTexts: string[] = [];
|
||||||
const finish = () => {
|
const finish = () => {
|
||||||
finished = true;
|
finished = true;
|
||||||
options.onFinish(responseText + remainText);
|
options.onFinish(existingTexts.join(""));
|
||||||
};
|
};
|
||||||
|
|
||||||
// animate response to make it looks smooth
|
// animate response to make it looks smooth
|
||||||
@ -134,11 +137,26 @@ export class GeminiProApi implements LLMApi {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let data = JSON.parse(ensureProperEnding(partialData));
|
let data = JSON.parse(ensureProperEnding(partialData));
|
||||||
console.log(data);
|
|
||||||
let fetchText = apiClient.extractMessage(data[data.length - 1]);
|
const textArray = data.reduce(
|
||||||
console.log("[Response Animation] fetchText: ", fetchText);
|
(acc: string[], item: { candidates: any[] }) => {
|
||||||
remainText += fetchText;
|
const texts = item.candidates.map((candidate) =>
|
||||||
|
candidate.content.parts
|
||||||
|
.map((part: { text: any }) => part.text)
|
||||||
|
.join(""),
|
||||||
|
);
|
||||||
|
return acc.concat(texts);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (textArray.length > existingTexts.length) {
|
||||||
|
const deltaArray = textArray.slice(existingTexts.length);
|
||||||
|
existingTexts = textArray;
|
||||||
|
remainText += deltaArray.join("");
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// console.log("[Response Animation] error: ", error,partialData);
|
||||||
// skip error message when parsing json
|
// skip error message when parsing json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user