mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-09-08 01:06:58 +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:
5
app/client/common/index.ts
Normal file
5
app/client/common/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from "./types";
|
||||
|
||||
export * from "./locale";
|
||||
|
||||
export * from "./utils";
|
19
app/client/common/locale.ts
Normal file
19
app/client/common/locale.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Lang, getLang } from "@/app/locales";
|
||||
|
||||
interface PlainConfig {
|
||||
[k: string]: PlainConfig | string;
|
||||
}
|
||||
|
||||
export type LocaleMap<
|
||||
TextPlainConfig extends PlainConfig,
|
||||
Default extends Lang,
|
||||
> = Partial<Record<Lang, TextPlainConfig>> & {
|
||||
[name in Default]: TextPlainConfig;
|
||||
};
|
||||
|
||||
export function getLocaleText<
|
||||
TextPlainConfig extends PlainConfig,
|
||||
DefaultLang extends Lang,
|
||||
>(textMap: LocaleMap<TextPlainConfig, DefaultLang>, defaultLang: DefaultLang) {
|
||||
return textMap[getLang()] || textMap[defaultLang];
|
||||
}
|
186
app/client/common/types.ts
Normal file
186
app/client/common/types.ts
Normal file
@@ -0,0 +1,186 @@
|
||||
import { RequestMessage } from "../api";
|
||||
|
||||
export { type RequestMessage };
|
||||
|
||||
// ===================================== LLM Types start ======================================
|
||||
|
||||
export interface ModelConfig {
|
||||
temperature: number;
|
||||
top_p: number;
|
||||
presence_penalty: number;
|
||||
frequency_penalty: number;
|
||||
max_tokens: number;
|
||||
}
|
||||
|
||||
export interface ModelSettings extends Omit<ModelConfig, "max_tokens"> {
|
||||
global_max_tokens: number;
|
||||
}
|
||||
|
||||
export type ModelTemplate = {
|
||||
name: string; // id of model in a provider
|
||||
displayName: string;
|
||||
isVisionModel?: boolean;
|
||||
isDefaultActive: boolean; // model is initialized to be active
|
||||
isDefaultSelected?: boolean; // model is initialized to be as default used model
|
||||
max_tokens?: number;
|
||||
};
|
||||
|
||||
export interface Model extends Omit<ModelTemplate, "isDefaultActive"> {
|
||||
providerTemplateName: string;
|
||||
isActive: boolean;
|
||||
providerName: string;
|
||||
available: boolean;
|
||||
customized: boolean; // Only customized model is allowed to be modified
|
||||
}
|
||||
|
||||
export interface ModelInfo extends Pick<ModelTemplate, "name"> {
|
||||
[k: string]: any;
|
||||
}
|
||||
|
||||
// ===================================== LLM Types end ======================================
|
||||
|
||||
// ===================================== Chat Request Types start ======================================
|
||||
|
||||
export interface ChatRequestPayload {
|
||||
messages: RequestMessage[];
|
||||
context: {
|
||||
isApp: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface StandChatRequestPayload extends ChatRequestPayload {
|
||||
modelConfig: ModelConfig;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export interface InternalChatRequestPayload<SettingKeys extends string = "">
|
||||
extends StandChatRequestPayload {
|
||||
providerConfig: Partial<Record<SettingKeys, string>>;
|
||||
isVisionModel: Model["isVisionModel"];
|
||||
stream: boolean;
|
||||
}
|
||||
|
||||
export interface ProviderRequestPayload {
|
||||
headers: Record<string, string>;
|
||||
body: string;
|
||||
url: string;
|
||||
method: string;
|
||||
}
|
||||
|
||||
export interface InternalChatHandlers {
|
||||
onProgress: (message: string, chunk: string) => void;
|
||||
onFinish: (message: string) => void;
|
||||
onError: (err: Error) => void;
|
||||
}
|
||||
|
||||
export interface ChatHandlers extends InternalChatHandlers {
|
||||
onProgress: (chunk: string) => void;
|
||||
onFinish: () => void;
|
||||
onFlash: (message: string) => void;
|
||||
}
|
||||
|
||||
// ===================================== Chat Request Types end ======================================
|
||||
|
||||
// ===================================== Chat Response Types start ======================================
|
||||
|
||||
export interface StandChatReponseMessage {
|
||||
message: string;
|
||||
}
|
||||
|
||||
// ===================================== Chat Request Types end ======================================
|
||||
|
||||
// ===================================== Provider Settings Types start ======================================
|
||||
|
||||
type NumberRange = [number, number];
|
||||
|
||||
export type Validator =
|
||||
| "required"
|
||||
| "number"
|
||||
| "string"
|
||||
| NumberRange
|
||||
| NumberRange[]
|
||||
| ((v: any) => Promise<string | void>);
|
||||
|
||||
export type CommonSettingItem<SettingKeys extends string> = {
|
||||
name: SettingKeys;
|
||||
title?: string;
|
||||
description?: string;
|
||||
validators?: Validator[];
|
||||
};
|
||||
|
||||
export type InputSettingItem = {
|
||||
type: "input";
|
||||
placeholder?: string;
|
||||
} & (
|
||||
| {
|
||||
inputType?: "password" | "normal";
|
||||
defaultValue?: string;
|
||||
}
|
||||
| {
|
||||
inputType?: "number";
|
||||
defaultValue?: number;
|
||||
}
|
||||
);
|
||||
|
||||
export type SelectSettingItem = {
|
||||
type: "select";
|
||||
options: {
|
||||
name: string;
|
||||
value: "number" | "string" | "boolean";
|
||||
}[];
|
||||
placeholder?: string;
|
||||
};
|
||||
|
||||
export type RangeSettingItem = {
|
||||
type: "range";
|
||||
range: NumberRange;
|
||||
};
|
||||
|
||||
export type SwitchSettingItem = {
|
||||
type: "switch";
|
||||
};
|
||||
|
||||
export type SettingItem<SettingKeys extends string = ""> =
|
||||
CommonSettingItem<SettingKeys> &
|
||||
(
|
||||
| InputSettingItem
|
||||
| SelectSettingItem
|
||||
| RangeSettingItem
|
||||
| SwitchSettingItem
|
||||
);
|
||||
|
||||
// ===================================== Provider Settings Types end ======================================
|
||||
|
||||
// ===================================== Provider Template Types start ======================================
|
||||
export interface IProviderTemplate<
|
||||
SettingKeys extends string,
|
||||
NAME extends string,
|
||||
Meta extends Record<string, any>,
|
||||
> {
|
||||
readonly name: NAME;
|
||||
|
||||
readonly metas: Meta;
|
||||
|
||||
readonly providerMeta: {
|
||||
displayName: string;
|
||||
settingItems: SettingItem<SettingKeys>[];
|
||||
};
|
||||
readonly defaultModels: ModelTemplate[];
|
||||
|
||||
streamChat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
handlers: ChatHandlers,
|
||||
): AbortController;
|
||||
|
||||
chat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
): Promise<StandChatReponseMessage>;
|
||||
|
||||
getAvailableModels?(
|
||||
providerConfig: InternalChatRequestPayload<SettingKeys>["providerConfig"],
|
||||
): Promise<ModelInfo[]>;
|
||||
}
|
||||
|
||||
export interface Serializable<Snapshot> {
|
||||
serialize(): Snapshot;
|
||||
}
|
26
app/client/common/utils.ts
Normal file
26
app/client/common/utils.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { RequestMessage } from "./types";
|
||||
|
||||
export function getMessageTextContent(message: RequestMessage) {
|
||||
if (typeof message.content === "string") {
|
||||
return message.content;
|
||||
}
|
||||
for (const c of message.content) {
|
||||
if (c.type === "text") {
|
||||
return c.text ?? "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function getMessageImages(message: RequestMessage): string[] {
|
||||
if (typeof message.content === "string") {
|
||||
return [];
|
||||
}
|
||||
const urls: string[] = [];
|
||||
for (const c of message.content) {
|
||||
if (c.type === "image_url") {
|
||||
urls.push(c.image_url?.url ?? "");
|
||||
}
|
||||
}
|
||||
return urls;
|
||||
}
|
Reference in New Issue
Block a user