Merge remote-tracking branch 'upstream/main'

This commit is contained in:
Hk-Gosuto
2024-01-02 09:32:09 +08:00
13 changed files with 55 additions and 14 deletions

View File

@@ -13,7 +13,15 @@ import {
LLMUsage,
} from "../api";
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
import {
EventStreamContentType,
fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getClientConfig } from "@/app/config/client";
import Locale from "../../locales";
import { getServerSideConfig } from "@/app/config/server";
import de from "@/app/locales/de";
export class GeminiProApi implements LLMApi {
toolAgentChat(options: AgentChatOptions): Promise<void> {
throw new Error("Method not implemented.");
@@ -66,6 +74,24 @@ export class GeminiProApi implements LLMApi {
topP: modelConfig.top_p,
// "topK": modelConfig.top_k,
},
safetySettings: [
{
category: "HARM_CATEGORY_HARASSMENT",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_HATE_SPEECH",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_SEXUALLY_EXPLICIT",
threshold: "BLOCK_ONLY_HIGH",
},
{
category: "HARM_CATEGORY_DANGEROUS_CONTENT",
threshold: "BLOCK_ONLY_HIGH",
},
],
};
console.log("[Request] google payload: ", requestPayload);
@@ -95,9 +121,11 @@ export class GeminiProApi implements LLMApi {
"streamGenerateContent",
);
let finished = false;
let existingTexts: string[] = [];
const finish = () => {
finished = true;
options.onFinish(responseText + remainText);
options.onFinish(existingTexts.join(""));
};
// animate response to make it looks smooth
@@ -142,11 +170,26 @@ export class GeminiProApi implements LLMApi {
try {
let data = JSON.parse(ensureProperEnding(partialData));
console.log(data);
let fetchText = apiClient.extractMessage(data[data.length - 1]);
console.log("[Response Animation] fetchText: ", fetchText);
remainText += fetchText;
const textArray = data.reduce(
(acc: string[], item: { candidates: any[] }) => {
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) {
// console.log("[Response Animation] error: ", error,partialData);
// skip error message when parsing json
}

View File

@@ -93,10 +93,8 @@ export const Azure = {
};
export const Google = {
ExampleEndpoint: "https://generativelanguage.googleapis.com",
ChatPath: "v1/models/gemini-pro:generateContent",
// /api/openai/v1/chat/completions
ExampleEndpoint: "https://generativelanguage.googleapis.com/",
ChatPath: "v1beta/models/gemini-pro:generateContent",
};
export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang

View File

@@ -323,7 +323,7 @@ const cn = {
Endpoint: {
Title: "接口地址",
SubTitle: "样例:",
SubTitle: "不包含请求路径,样例:",
},
ApiVerion: {