mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-09-05 23:30:31 +08:00
feat: model provider refactor done
This commit is contained in:
170
app/client/providers/openai/config.ts
Normal file
170
app/client/providers/openai/config.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { SettingItem } from "../../core/types";
|
||||
import Locale from "./locale";
|
||||
|
||||
export const OPENAI_BASE_URL = "https://api.openai.com";
|
||||
|
||||
export const OpenaiMetas = {
|
||||
ChatPath: "v1/chat/completions",
|
||||
UsagePath: "dashboard/billing/usage",
|
||||
SubsPath: "dashboard/billing/subscription",
|
||||
ListModelPath: "v1/models",
|
||||
};
|
||||
|
||||
export type SettingKeys = "openaiUrl" | "openaiApiKey";
|
||||
|
||||
export const defaultModal = "gpt-3.5-turbo";
|
||||
|
||||
export const modelConfigs = [
|
||||
{
|
||||
name: "gpt-3.5-turbo",
|
||||
displayName: "gpt-3.5-turbo",
|
||||
isVision: false,
|
||||
isDefaultActive: true,
|
||||
isDefaultSelected: true,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-0301",
|
||||
displayName: "gpt-3.5-turbo-0301",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-0613",
|
||||
displayName: "gpt-3.5-turbo-0613",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-1106",
|
||||
displayName: "gpt-3.5-turbo-1106",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-0125",
|
||||
displayName: "gpt-3.5-turbo-0125",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-16k",
|
||||
displayName: "gpt-3.5-turbo-16k",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-3.5-turbo-16k-0613",
|
||||
displayName: "gpt-3.5-turbo-16k-0613",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4",
|
||||
displayName: "gpt-4",
|
||||
isVision: false,
|
||||
isDefaultActive: true,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-0314",
|
||||
displayName: "gpt-4-0314",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-0613",
|
||||
displayName: "gpt-4-0613",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-1106-preview",
|
||||
displayName: "gpt-4-1106-preview",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-0125-preview",
|
||||
displayName: "gpt-4-0125-preview",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-32k",
|
||||
displayName: "gpt-4-32k",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-32k-0314",
|
||||
displayName: "gpt-4-32k-0314",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-32k-0613",
|
||||
displayName: "gpt-4-32k-0613",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-turbo",
|
||||
displayName: "gpt-4-turbo",
|
||||
isVision: true,
|
||||
isDefaultActive: true,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-turbo-preview",
|
||||
displayName: "gpt-4-turbo-preview",
|
||||
isVision: false,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-vision-preview",
|
||||
displayName: "gpt-4-vision-preview",
|
||||
isVision: true,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
{
|
||||
name: "gpt-4-turbo-2024-04-09",
|
||||
displayName: "gpt-4-turbo-2024-04-09",
|
||||
isVision: true,
|
||||
isDefaultActive: false,
|
||||
isDefaultSelected: false,
|
||||
},
|
||||
];
|
||||
|
||||
export const settingItems: SettingItem<SettingKeys>[] = [
|
||||
{
|
||||
name: "openaiUrl",
|
||||
title: Locale.Endpoint.Title,
|
||||
description: Locale.Endpoint.SubTitle,
|
||||
defaultValue: OPENAI_BASE_URL,
|
||||
type: "input",
|
||||
},
|
||||
{
|
||||
name: "openaiApiKey",
|
||||
title: Locale.ApiKey.Title,
|
||||
description: Locale.ApiKey.SubTitle,
|
||||
placeholder: Locale.ApiKey.Placeholder,
|
||||
type: "input",
|
||||
inputType: "password",
|
||||
validators: ["required"],
|
||||
},
|
||||
];
|
312
app/client/providers/openai/index.ts
Normal file
312
app/client/providers/openai/index.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import { modelConfigs, settingItems, SettingKeys, OpenaiMetas } from "./config";
|
||||
import { getMessageTextContent } from "@/app/utils";
|
||||
import {
|
||||
InternalChatRequestPayload,
|
||||
IProviderTemplate,
|
||||
} from "../../core/types";
|
||||
import {
|
||||
EventStreamContentType,
|
||||
fetchEventSource,
|
||||
} from "@fortaine/fetch-event-source";
|
||||
import { prettyObject } from "@/app/utils/format";
|
||||
import Locale from "@/app/locales";
|
||||
|
||||
export type OpenAIProviderSettingKeys = SettingKeys;
|
||||
|
||||
export const ROLES = ["system", "user", "assistant"] as const;
|
||||
export type MessageRole = (typeof ROLES)[number];
|
||||
|
||||
export interface MultimodalContent {
|
||||
type: "text" | "image_url";
|
||||
text?: string;
|
||||
image_url?: {
|
||||
url: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface RequestMessage {
|
||||
role: MessageRole;
|
||||
content: string | MultimodalContent[];
|
||||
}
|
||||
|
||||
interface RequestPayload {
|
||||
messages: {
|
||||
role: "system" | "user" | "assistant";
|
||||
content: string | MultimodalContent[];
|
||||
}[];
|
||||
stream?: boolean;
|
||||
model: string;
|
||||
temperature: number;
|
||||
presence_penalty: number;
|
||||
frequency_penalty: number;
|
||||
top_p: number;
|
||||
max_tokens?: number;
|
||||
}
|
||||
|
||||
class OpenAIProvider
|
||||
implements IProviderTemplate<SettingKeys, "openai", typeof OpenaiMetas>
|
||||
{
|
||||
name = "openai" as const;
|
||||
metas = OpenaiMetas;
|
||||
|
||||
readonly REQUEST_TIMEOUT_MS = 60000;
|
||||
|
||||
models = modelConfigs.map((c) => ({ ...c, providerTemplateName: this.name }));
|
||||
|
||||
providerMeta = {
|
||||
displayName: "OpenAI",
|
||||
settingItems,
|
||||
};
|
||||
|
||||
private path(payload: InternalChatRequestPayload<SettingKeys>): string {
|
||||
const {
|
||||
providerConfig: { openaiUrl },
|
||||
} = payload;
|
||||
|
||||
const path = OpenaiMetas.ChatPath;
|
||||
|
||||
let baseUrl = openaiUrl;
|
||||
|
||||
if (!baseUrl) {
|
||||
baseUrl = "/api/openai";
|
||||
}
|
||||
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||
}
|
||||
if (!baseUrl.startsWith("http") && !baseUrl.startsWith("/api/openai")) {
|
||||
baseUrl = "https://" + baseUrl;
|
||||
}
|
||||
|
||||
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||
|
||||
return [baseUrl, path].join("/");
|
||||
}
|
||||
|
||||
private getHeaders(payload: InternalChatRequestPayload<SettingKeys>) {
|
||||
const { openaiApiKey } = payload.providerConfig;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
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 (validString(openaiApiKey)) {
|
||||
headers[authHeader] = makeBearer(openaiApiKey);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
||||
|
||||
private formatChatPayload(payload: InternalChatRequestPayload<SettingKeys>) {
|
||||
const { messages, isVisionModel, model, stream, modelConfig } = payload;
|
||||
const {
|
||||
temperature,
|
||||
presence_penalty,
|
||||
frequency_penalty,
|
||||
top_p,
|
||||
max_tokens,
|
||||
} = modelConfig;
|
||||
|
||||
const openAiMessages = messages.map((v) => ({
|
||||
role: v.role,
|
||||
content: isVisionModel ? v.content : getMessageTextContent(v),
|
||||
}));
|
||||
|
||||
const requestPayload: RequestPayload = {
|
||||
messages: openAiMessages,
|
||||
stream,
|
||||
model,
|
||||
temperature,
|
||||
presence_penalty,
|
||||
frequency_penalty,
|
||||
top_p,
|
||||
};
|
||||
|
||||
// add max_tokens to vision model
|
||||
if (isVisionModel) {
|
||||
requestPayload["max_tokens"] = Math.max(max_tokens, 4000);
|
||||
}
|
||||
|
||||
console.log("[Request] openai payload: ", requestPayload);
|
||||
|
||||
return {
|
||||
headers: this.getHeaders(payload),
|
||||
body: JSON.stringify(requestPayload),
|
||||
method: "POST",
|
||||
url: this.path(payload),
|
||||
};
|
||||
}
|
||||
|
||||
private readWholeMessageResponseBody(res: any) {
|
||||
return {
|
||||
message: res.choices?.at(0)?.message?.content ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
private getTimer = () => {
|
||||
const controller = new AbortController();
|
||||
|
||||
// make a fetch request
|
||||
const requestTimeoutId = setTimeout(
|
||||
() => controller.abort(),
|
||||
this.REQUEST_TIMEOUT_MS,
|
||||
);
|
||||
|
||||
return {
|
||||
...controller,
|
||||
clear: () => {
|
||||
clearTimeout(requestTimeoutId);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
async chat(payload: InternalChatRequestPayload<SettingKeys>) {
|
||||
const requestPayload = this.formatChatPayload(payload);
|
||||
|
||||
const timer = this.getTimer();
|
||||
|
||||
const res = await fetch(requestPayload.url, {
|
||||
headers: {
|
||||
...requestPayload.headers,
|
||||
},
|
||||
body: requestPayload.body,
|
||||
method: requestPayload.method,
|
||||
signal: timer.signal,
|
||||
});
|
||||
|
||||
timer.clear();
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.readWholeMessageResponseBody(resJson);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
streamChat(
|
||||
payload: InternalChatRequestPayload<SettingKeys>,
|
||||
onProgress: (message: string, chunk: string) => void,
|
||||
onFinish: (message: string) => void,
|
||||
onError: (err: Error) => void,
|
||||
) {
|
||||
const requestPayload = this.formatChatPayload(payload);
|
||||
|
||||
const timer = this.getTimer();
|
||||
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
const animateResponseText = () => {
|
||||
if (finished || timer.signal.aborted) {
|
||||
responseText += remainText;
|
||||
console.log("[Response Animation] finished");
|
||||
if (responseText?.length === 0) {
|
||||
onError(new Error("empty response from server"));
|
||||
}
|
||||
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();
|
||||
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
onFinish(responseText + remainText);
|
||||
}
|
||||
};
|
||||
|
||||
timer.signal.onabort = finish;
|
||||
|
||||
fetchEventSource(requestPayload.url, {
|
||||
...requestPayload,
|
||||
async onopen(res) {
|
||||
timer.clear();
|
||||
const contentType = res.headers.get("content-type");
|
||||
console.log("[OpenAI] request response content type: ", contentType);
|
||||
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
}
|
||||
|
||||
if (
|
||||
!res.ok ||
|
||||
!res.headers
|
||||
.get("content-type")
|
||||
?.startsWith(EventStreamContentType) ||
|
||||
res.status !== 200
|
||||
) {
|
||||
const responseTexts = [responseText];
|
||||
let extraInfo = await res.clone().text();
|
||||
try {
|
||||
const resJson = await res.clone().json();
|
||||
extraInfo = prettyObject(resJson);
|
||||
} catch {}
|
||||
|
||||
if (res.status === 401) {
|
||||
responseTexts.push(Locale.Error.Unauthorized);
|
||||
}
|
||||
|
||||
if (extraInfo) {
|
||||
responseTexts.push(extraInfo);
|
||||
}
|
||||
|
||||
responseText = responseTexts.join("\n\n");
|
||||
|
||||
return finish();
|
||||
}
|
||||
},
|
||||
onmessage(msg) {
|
||||
if (msg.data === "[DONE]" || finished) {
|
||||
return finish();
|
||||
}
|
||||
const text = msg.data;
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
const choices = json.choices as Array<{
|
||||
delta: { content: string };
|
||||
}>;
|
||||
const delta = choices[0]?.delta?.content;
|
||||
const textmoderation = json?.prompt_filter_results;
|
||||
|
||||
if (delta) {
|
||||
remainText += delta;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[Request] parse error", text, msg);
|
||||
}
|
||||
},
|
||||
onclose() {
|
||||
finish();
|
||||
},
|
||||
onerror(e) {
|
||||
onError(e);
|
||||
throw e;
|
||||
},
|
||||
openWhenHidden: true,
|
||||
});
|
||||
|
||||
return timer;
|
||||
}
|
||||
}
|
||||
|
||||
export default OpenAIProvider;
|
82
app/client/providers/openai/locale.ts
Normal file
82
app/client/providers/openai/locale.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { getLocaleText } from "../../core/locale";
|
||||
|
||||
export default getLocaleText<
|
||||
{
|
||||
ApiKey: {
|
||||
Title: string;
|
||||
SubTitle: string;
|
||||
Placeholder: string;
|
||||
};
|
||||
|
||||
Endpoint: {
|
||||
Title: string;
|
||||
SubTitle: string;
|
||||
};
|
||||
},
|
||||
"en"
|
||||
>(
|
||||
{
|
||||
cn: {
|
||||
ApiKey: {
|
||||
Title: "API Key",
|
||||
SubTitle: "使用自定义 OpenAI Key 绕过密码访问限制",
|
||||
Placeholder: "OpenAI API Key",
|
||||
},
|
||||
|
||||
Endpoint: {
|
||||
Title: "接口地址",
|
||||
SubTitle: "除默认地址外,必须包含 http(s)://",
|
||||
},
|
||||
},
|
||||
en: {
|
||||
ApiKey: {
|
||||
Title: "OpenAI API Key",
|
||||
SubTitle: "User custom OpenAI Api Key",
|
||||
Placeholder: "sk-xxx",
|
||||
},
|
||||
|
||||
Endpoint: {
|
||||
Title: "OpenAI Endpoint",
|
||||
SubTitle: "Must starts with http(s):// or use /api/openai as default",
|
||||
},
|
||||
},
|
||||
pt: {
|
||||
ApiKey: {
|
||||
Title: "Chave API OpenAI",
|
||||
SubTitle: "Usar Chave API OpenAI personalizada",
|
||||
Placeholder: "sk-xxx",
|
||||
},
|
||||
|
||||
Endpoint: {
|
||||
Title: "Endpoint OpenAI",
|
||||
SubTitle: "Deve começar com http(s):// ou usar /api/openai como padrão",
|
||||
},
|
||||
},
|
||||
sk: {
|
||||
ApiKey: {
|
||||
Title: "API kľúč OpenAI",
|
||||
SubTitle: "Použiť vlastný API kľúč OpenAI",
|
||||
Placeholder: "sk-xxx",
|
||||
},
|
||||
|
||||
Endpoint: {
|
||||
Title: "Koncový bod OpenAI",
|
||||
SubTitle:
|
||||
"Musí začínať http(s):// alebo použiť /api/openai ako predvolený",
|
||||
},
|
||||
},
|
||||
tw: {
|
||||
ApiKey: {
|
||||
Title: "API Key",
|
||||
SubTitle: "使用自定義 OpenAI Key 繞過密碼存取限制",
|
||||
Placeholder: "OpenAI API Key",
|
||||
},
|
||||
|
||||
Endpoint: {
|
||||
Title: "介面(Endpoint) 地址",
|
||||
SubTitle: "除預設地址外,必須包含 http(s)://",
|
||||
},
|
||||
},
|
||||
},
|
||||
"en",
|
||||
);
|
Reference in New Issue
Block a user