mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-09-01 20:56:59 +08:00
feat: 1) Present 'maxtokens' as properties tied to a single model. 2) Remove the original author's implementation of the send verification logic and replace it with a user input validator. Pre-verification 3) Provides the ability to pull the 'User Visible modellist' provided by 'provider' 4) Provider-related parameters are passed in the constructor of 'providerClient'. Not passed in the 'chat' method
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import { SettingItem } from "../../core/types";
|
||||
import { SettingItem } from "../../common";
|
||||
import Locale from "./locale";
|
||||
|
||||
export const GoogleMetas = {
|
||||
ExampleEndpoint: "https://generativelanguage.googleapis.com/",
|
||||
ChatPath: (modelName: string) => `v1beta/models/${modelName}:generateContent`,
|
||||
VisionChatPath: (modelName: string) =>
|
||||
`v1beta/models/${modelName}:generateContent`,
|
||||
};
|
||||
|
||||
export type SettingKeys = "googleUrl" | "googleApiKey" | "googleApiVersion";
|
||||
@@ -41,7 +39,20 @@ export const settingItems: SettingItem<SettingKeys>[] = [
|
||||
description: Locale.Endpoint.SubTitle + GoogleMetas.ExampleEndpoint,
|
||||
placeholder: GoogleMetas.ExampleEndpoint,
|
||||
type: "input",
|
||||
validators: ["required"],
|
||||
validators: [
|
||||
async (v: any) => {
|
||||
if (typeof v === "string") {
|
||||
try {
|
||||
new URL(v);
|
||||
} catch (e) {
|
||||
return Locale.Endpoint.Error.IllegalURL;
|
||||
}
|
||||
}
|
||||
if (typeof v === "string" && v.endsWith("/")) {
|
||||
return Locale.Endpoint.Error.EndWithBackslash;
|
||||
}
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "googleApiKey",
|
||||
@@ -50,7 +61,7 @@ export const settingItems: SettingItem<SettingKeys>[] = [
|
||||
placeholder: Locale.ApiKey.Placeholder,
|
||||
type: "input",
|
||||
inputType: "password",
|
||||
validators: ["required"],
|
||||
// validators: ["required"],
|
||||
},
|
||||
{
|
||||
name: "googleApiVersion",
|
||||
@@ -58,6 +69,6 @@ export const settingItems: SettingItem<SettingKeys>[] = [
|
||||
description: Locale.ApiVersion.SubTitle,
|
||||
placeholder: "2023-08-01-preview",
|
||||
type: "input",
|
||||
validators: ["required"],
|
||||
// validators: ["required"],
|
||||
},
|
||||
];
|
||||
|
@@ -1,13 +1,34 @@
|
||||
import { getMessageImages, getMessageTextContent } from "@/app/utils";
|
||||
import { SettingKeys, modelConfigs, settingItems, GoogleMetas } from "./config";
|
||||
import {
|
||||
ChatHandlers,
|
||||
InternalChatRequestPayload,
|
||||
IProviderTemplate,
|
||||
ModelInfo,
|
||||
StandChatReponseMessage,
|
||||
} from "../../core/types";
|
||||
getMessageTextContent,
|
||||
getMessageImages,
|
||||
} from "../../common";
|
||||
import { ensureProperEnding, makeBearer, validString } from "./utils";
|
||||
|
||||
export type GoogleProviderSettingKeys = SettingKeys;
|
||||
|
||||
interface ModelList {
|
||||
models: Array<{
|
||||
name: string;
|
||||
baseModelId: string;
|
||||
version: string;
|
||||
displayName: string;
|
||||
description: string;
|
||||
inputTokenLimit: number; // Integer
|
||||
outputTokenLimit: number; // Integer
|
||||
supportedGenerationMethods: [string];
|
||||
temperature: number;
|
||||
topP: number;
|
||||
topK: number; // Integer
|
||||
}>;
|
||||
nextPageToken: string;
|
||||
}
|
||||
|
||||
export default class GoogleProvider
|
||||
implements IProviderTemplate<SettingKeys, "google", typeof GoogleMetas>
|
||||
{
|
||||
@@ -18,7 +39,7 @@ export default class GoogleProvider
|
||||
displayName: "Google",
|
||||
settingItems,
|
||||
};
|
||||
models = modelConfigs.map((c) => ({ ...c, providerTemplateName: this.name }));
|
||||
defaultModels = modelConfigs;
|
||||
|
||||
readonly REQUEST_TIMEOUT_MS = 60000;
|
||||
|
||||
@@ -33,19 +54,8 @@ export default class GoogleProvider
|
||||
Accept: "application/json",
|
||||
};
|
||||
|
||||
const authHeader = "Authorization";
|
||||
|
||||
const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
||||
const validString = (x?: string): x is string => Boolean(x && x.length > 0);
|
||||
|
||||
// when using google api in app, not set auth header
|
||||
if (!isApp) {
|
||||
// use user's api key first
|
||||
if (validString(googleApiKey)) {
|
||||
headers[authHeader] = makeBearer(googleApiKey);
|
||||
} else {
|
||||
throw new Error("no apiKey when chat through google");
|
||||
}
|
||||
if (!isApp && validString(googleApiKey)) {
|
||||
headers["Authorization"] = makeBearer(googleApiKey);
|
||||
}
|
||||
|
||||
return headers;
|
||||
@@ -135,15 +145,9 @@ export default class GoogleProvider
|
||||
],
|
||||
};
|
||||
|
||||
let baseUrl = googleUrl;
|
||||
let googleChatPath = GoogleMetas.ChatPath(model);
|
||||
|
||||
let googleChatPath = isVisionModel
|
||||
? GoogleMetas.VisionChatPath(model)
|
||||
: GoogleMetas.ChatPath(model);
|
||||
|
||||
if (!baseUrl) {
|
||||
baseUrl = "/api/google/" + googleChatPath;
|
||||
}
|
||||
let baseUrl = googleUrl ?? "/api/google/" + googleChatPath;
|
||||
|
||||
if (isApp) {
|
||||
baseUrl += `?key=${googleApiKey}`;
|
||||
@@ -193,44 +197,13 @@ export default class GoogleProvider
|
||||
|
||||
streamChat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
onProgress: (message: string, chunk: string) => void,
|
||||
onFinish: (message: string) => void,
|
||||
onError: (err: Error) => void,
|
||||
handlers: ChatHandlers,
|
||||
) {
|
||||
const requestPayload = this.formatChatPayload(payload);
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
|
||||
const timer = this.getTimer();
|
||||
|
||||
let existingTexts: string[] = [];
|
||||
const finish = () => {
|
||||
finished = true;
|
||||
onFinish(existingTexts.join(""));
|
||||
};
|
||||
|
||||
// animate response to make it looks smooth
|
||||
const animateResponseText = () => {
|
||||
if (finished || timer.signal.aborted) {
|
||||
responseText += remainText;
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
if (remainText.length > 0) {
|
||||
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
||||
const fetchText = remainText.slice(0, fetchCount);
|
||||
responseText += fetchText;
|
||||
remainText = remainText.slice(fetchCount);
|
||||
onProgress(responseText, fetchText);
|
||||
}
|
||||
|
||||
requestAnimationFrame(animateResponseText);
|
||||
};
|
||||
|
||||
// start animaion
|
||||
animateResponseText();
|
||||
|
||||
fetch(requestPayload.url, {
|
||||
...requestPayload,
|
||||
@@ -250,18 +223,16 @@ export default class GoogleProvider
|
||||
try {
|
||||
let data = JSON.parse(ensureProperEnding(partialData));
|
||||
if (data && data[0].error) {
|
||||
onError(new Error(data[0].error.message));
|
||||
handlers.onError(new Error(data[0].error.message));
|
||||
} else {
|
||||
onError(new Error("Request failed"));
|
||||
handlers.onError(new Error("Request failed"));
|
||||
}
|
||||
} catch (_) {
|
||||
onError(new Error("Request failed"));
|
||||
handlers.onError(new Error("Request failed"));
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Stream complete");
|
||||
// options.onFinish(responseText + remainText);
|
||||
finished = true;
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@@ -285,7 +256,7 @@ export default class GoogleProvider
|
||||
if (textArray.length > existingTexts.length) {
|
||||
const deltaArray = textArray.slice(existingTexts.length);
|
||||
existingTexts = textArray;
|
||||
remainText += deltaArray.join("");
|
||||
handlers.onProgress(deltaArray.join(""));
|
||||
}
|
||||
} catch (error) {
|
||||
// console.log("[Response Animation] error: ", error,partialData);
|
||||
@@ -300,6 +271,7 @@ export default class GoogleProvider
|
||||
});
|
||||
return timer;
|
||||
}
|
||||
|
||||
async chat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
): Promise<StandChatReponseMessage> {
|
||||
@@ -328,11 +300,19 @@ export default class GoogleProvider
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
function ensureProperEnding(str: string) {
|
||||
if (str.startsWith("[") && !str.endsWith("]")) {
|
||||
return str + "]";
|
||||
async getAvailableModels(
|
||||
providerConfig: Record<SettingKeys, string>,
|
||||
): Promise<ModelInfo[]> {
|
||||
const { googleApiKey, googleUrl } = providerConfig;
|
||||
const res = await fetch(`${googleUrl}/v1beta/models?key=${googleApiKey}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${googleApiKey}`,
|
||||
},
|
||||
method: "GET",
|
||||
});
|
||||
const data: ModelList = await res.json();
|
||||
|
||||
return data.models;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { getLocaleText } from "../../core/locale";
|
||||
import { getLocaleText } from "../../common";
|
||||
|
||||
export default getLocaleText<
|
||||
{
|
||||
@@ -10,6 +10,10 @@ export default getLocaleText<
|
||||
Endpoint: {
|
||||
Title: string;
|
||||
SubTitle: string;
|
||||
Error: {
|
||||
EndWithBackslash: string;
|
||||
IllegalURL: string;
|
||||
};
|
||||
};
|
||||
ApiVersion: {
|
||||
Title: string;
|
||||
@@ -29,6 +33,10 @@ export default getLocaleText<
|
||||
Endpoint: {
|
||||
Title: "终端地址",
|
||||
SubTitle: "示例:",
|
||||
Error: {
|
||||
EndWithBackslash: "不能以「/」结尾",
|
||||
IllegalURL: "请输入一个完整可用的url",
|
||||
},
|
||||
},
|
||||
|
||||
ApiVersion: {
|
||||
@@ -46,6 +54,10 @@ export default getLocaleText<
|
||||
Endpoint: {
|
||||
Title: "Endpoint Address",
|
||||
SubTitle: "Example:",
|
||||
Error: {
|
||||
EndWithBackslash: "Cannot end with '/'",
|
||||
IllegalURL: "Please enter a complete available url",
|
||||
},
|
||||
},
|
||||
|
||||
ApiVersion: {
|
||||
@@ -64,6 +76,10 @@ export default getLocaleText<
|
||||
Endpoint: {
|
||||
Title: "Adresa koncového bodu",
|
||||
SubTitle: "Príklad:",
|
||||
Error: {
|
||||
EndWithBackslash: "Nemôže končiť znakom „/“",
|
||||
IllegalURL: "Zadajte úplnú dostupnú adresu URL",
|
||||
},
|
||||
},
|
||||
|
||||
ApiVersion: {
|
||||
@@ -81,6 +97,10 @@ export default getLocaleText<
|
||||
Endpoint: {
|
||||
Title: "終端地址",
|
||||
SubTitle: "範例:",
|
||||
Error: {
|
||||
EndWithBackslash: "不能以「/」結尾",
|
||||
IllegalURL: "請輸入一個完整可用的url",
|
||||
},
|
||||
},
|
||||
|
||||
ApiVersion: {
|
||||
|
10
app/client/providers/google/utils.ts
Normal file
10
app/client/providers/google/utils.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export const makeBearer = (s: string) => `Bearer ${s.trim()}`;
|
||||
export const validString = (x?: string): x is string =>
|
||||
Boolean(x && x.length > 0);
|
||||
|
||||
export function ensureProperEnding(str: string) {
|
||||
if (str.startsWith("[") && !str.endsWith("]")) {
|
||||
return str + "]";
|
||||
}
|
||||
return str;
|
||||
}
|
Reference in New Issue
Block a user