mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-09 19:19:27 +08:00
feat: add multi-model support
This commit is contained in:
28
app/client/common/auth.ts
Normal file
28
app/client/common/auth.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { getClientConfig } from "@/app/config/client";
|
||||
import { ACCESS_CODE_PREFIX } from "@/app/constant";
|
||||
import { useAccessStore } from "@/app/store";
|
||||
|
||||
export function bearer(value: string) {
|
||||
return `Bearer ${value.trim()}`;
|
||||
}
|
||||
|
||||
export function getAuthHeaders(apiKey = "") {
|
||||
const accessStore = useAccessStore.getState();
|
||||
const isApp = !!getClientConfig()?.isApp;
|
||||
|
||||
let headers: Record<string, string> = {};
|
||||
|
||||
if (apiKey) {
|
||||
// use user's api key first
|
||||
headers.Authorization = bearer(apiKey);
|
||||
} else if (
|
||||
accessStore.enabledAccessControl() &&
|
||||
!isApp &&
|
||||
!!accessStore.accessCode
|
||||
) {
|
||||
// or use access code
|
||||
headers.Authorization = bearer(ACCESS_CODE_PREFIX + accessStore.accessCode);
|
||||
}
|
||||
|
||||
return headers;
|
||||
}
|
5
app/client/common/config.ts
Normal file
5
app/client/common/config.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const COMMON_PROVIDER_CONFIG = {
|
||||
customModels: "",
|
||||
models: [] as string[],
|
||||
autoFetchModels: false, // fetch available models from server or not
|
||||
};
|
37
app/client/common/controller.ts
Normal file
37
app/client/common/controller.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// To store message streaming controller
|
||||
export const ChatControllerPool = {
|
||||
controllers: {} as Record<string, AbortController>,
|
||||
|
||||
addController(
|
||||
sessionId: string,
|
||||
messageId: string,
|
||||
controller: AbortController,
|
||||
) {
|
||||
const key = this.key(sessionId, messageId);
|
||||
this.controllers[key] = controller;
|
||||
return key;
|
||||
},
|
||||
|
||||
stop(sessionId: string, messageId: string) {
|
||||
const key = this.key(sessionId, messageId);
|
||||
const controller = this.controllers[key];
|
||||
controller?.abort();
|
||||
},
|
||||
|
||||
stopAll() {
|
||||
Object.values(this.controllers).forEach((v) => v.abort());
|
||||
},
|
||||
|
||||
hasPending() {
|
||||
return Object.values(this.controllers).length > 0;
|
||||
},
|
||||
|
||||
remove(sessionId: string, messageId: string) {
|
||||
const key = this.key(sessionId, messageId);
|
||||
delete this.controllers[key];
|
||||
},
|
||||
|
||||
key(sessionId: string, messageIndex: string) {
|
||||
return `${sessionId},${messageIndex}`;
|
||||
},
|
||||
};
|
44
app/client/common/share.ts
Normal file
44
app/client/common/share.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { getClientConfig } from "@/app/config/client";
|
||||
import { ChatMessage } from "@/app/store";
|
||||
|
||||
export async function shareToShareGPT(
|
||||
messages: ChatMessage[],
|
||||
avatarUrl: string | null = null,
|
||||
) {
|
||||
const msgs = messages
|
||||
.map((m) => ({
|
||||
from: m.role === "user" ? "human" : "gpt",
|
||||
value: m.content,
|
||||
}))
|
||||
.concat([
|
||||
{
|
||||
from: "human",
|
||||
// 敬告二开开发者们,为了开源大模型的发展,请不要修改上述消息,此消息用于后续数据清洗使用
|
||||
// Please do not modify this message
|
||||
value:
|
||||
"Share from [ChatGPT Next Web]: https://github.com/Yidadaa/ChatGPT-Next-Web",
|
||||
},
|
||||
]);
|
||||
|
||||
console.log("[Share]", messages, msgs);
|
||||
const clientConfig = getClientConfig();
|
||||
const proxyUrl = "/sharegpt";
|
||||
const rawUrl = "https://sharegpt.com/api/conversations";
|
||||
const shareUrl = clientConfig?.isApp ? rawUrl : proxyUrl;
|
||||
const res = await fetch(shareUrl, {
|
||||
body: JSON.stringify({
|
||||
avatarUrl,
|
||||
items: msgs,
|
||||
}),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
const resJson = await res.json();
|
||||
console.log("[Share]", resJson);
|
||||
if (resJson.id) {
|
||||
return `https://shareg.pt/${resJson.id}`;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user