Merge branch 'main' of https://github.com/ConnectAI-E/ChatGPT-Next-Web into feat-baidu
This commit is contained in:
commit
9f7d137b05
|
@ -162,19 +162,20 @@ export class ClientApi {
|
||||||
|
|
||||||
export function getHeaders() {
|
export function getHeaders() {
|
||||||
const accessStore = useAccessStore.getState();
|
const accessStore = useAccessStore.getState();
|
||||||
|
const chatStore = useChatStore.getState();
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
};
|
};
|
||||||
const modelConfig = useChatStore.getState().currentSession().mask.modelConfig;
|
|
||||||
|
const clientConfig = getClientConfig();
|
||||||
|
|
||||||
|
function getConfig() {
|
||||||
|
const modelConfig = chatStore.currentSession().mask.modelConfig;
|
||||||
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
|
const isGoogle = modelConfig.providerName == ServiceProvider.Google;
|
||||||
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
|
const isAzure = modelConfig.providerName === ServiceProvider.Azure;
|
||||||
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
|
const isAnthropic = modelConfig.providerName === ServiceProvider.Anthropic;
|
||||||
const authHeader = isAzure
|
const isEnabledAccessControl = accessStore.enabledAccessControl();
|
||||||
? "api-key"
|
|
||||||
: isAnthropic
|
|
||||||
? "x-api-key"
|
|
||||||
: "Authorization";
|
|
||||||
const apiKey = isGoogle
|
const apiKey = isGoogle
|
||||||
? accessStore.googleApiKey
|
? accessStore.googleApiKey
|
||||||
: isAzure
|
: isAzure
|
||||||
|
@ -182,26 +183,49 @@ export function getHeaders() {
|
||||||
: isAnthropic
|
: isAnthropic
|
||||||
? accessStore.anthropicApiKey
|
? accessStore.anthropicApiKey
|
||||||
: accessStore.openaiApiKey;
|
: accessStore.openaiApiKey;
|
||||||
const clientConfig = getClientConfig();
|
return { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl };
|
||||||
const makeBearer = (s: string) =>
|
}
|
||||||
`${isAzure || isAnthropic ? "" : "Bearer "}${s.trim()}`;
|
|
||||||
const validString = (x: string) => x && x.length > 0;
|
|
||||||
|
|
||||||
|
function getAuthHeader(): string {
|
||||||
|
return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization";
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBearerToken(apiKey: string, noBearer: boolean = false): string {
|
||||||
|
return validString(apiKey)
|
||||||
|
? `${noBearer ? "" : "Bearer "}${apiKey.trim()}`
|
||||||
|
: "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function validString(x: string): boolean {
|
||||||
|
return x?.length > 0;
|
||||||
|
}
|
||||||
|
const { isGoogle, isAzure, isAnthropic, apiKey, isEnabledAccessControl } =
|
||||||
|
getConfig();
|
||||||
// when using google api in app, not set auth header
|
// when using google api in app, not set auth header
|
||||||
if (!(isGoogle && clientConfig?.isApp)) {
|
if (isGoogle && clientConfig?.isApp) return headers;
|
||||||
// use user's api key first
|
|
||||||
if (validString(apiKey)) {
|
const authHeader = getAuthHeader();
|
||||||
headers[authHeader] = makeBearer(apiKey);
|
|
||||||
} else if (
|
const bearerToken = getBearerToken(apiKey, isAzure || isAnthropic);
|
||||||
accessStore.enabledAccessControl() &&
|
|
||||||
validString(accessStore.accessCode)
|
if (bearerToken) {
|
||||||
) {
|
headers[authHeader] = bearerToken;
|
||||||
// access_code must send with header named `Authorization`, will using in auth middleware.
|
} else if (isEnabledAccessControl && validString(accessStore.accessCode)) {
|
||||||
headers["Authorization"] = makeBearer(
|
headers["Authorization"] = getBearerToken(
|
||||||
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
ACCESS_CODE_PREFIX + accessStore.accessCode,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getClientApi(provider: ServiceProvider): ClientApi {
|
||||||
|
switch (provider) {
|
||||||
|
case ServiceProvider.Google:
|
||||||
|
return new ClientApi(ModelProvider.GeminiPro);
|
||||||
|
case ServiceProvider.Anthropic:
|
||||||
|
return new ClientApi(ModelProvider.Claude);
|
||||||
|
default:
|
||||||
|
return new ClientApi(ModelProvider.GPT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -36,13 +36,9 @@ import { toBlob, toPng } from "html-to-image";
|
||||||
import { DEFAULT_MASK_AVATAR } from "../store/mask";
|
import { DEFAULT_MASK_AVATAR } from "../store/mask";
|
||||||
|
|
||||||
import { prettyObject } from "../utils/format";
|
import { prettyObject } from "../utils/format";
|
||||||
import {
|
import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
|
||||||
EXPORT_MESSAGE_CLASS_NAME,
|
|
||||||
ModelProvider,
|
|
||||||
ServiceProvider,
|
|
||||||
} from "../constant";
|
|
||||||
import { getClientConfig } from "../config/client";
|
import { getClientConfig } from "../config/client";
|
||||||
import { ClientApi } from "../client/api";
|
import { type ClientApi, getClientApi } from "../client/api";
|
||||||
import { getMessageTextContent } from "../utils";
|
import { getMessageTextContent } from "../utils";
|
||||||
|
|
||||||
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
|
||||||
|
@ -316,16 +312,7 @@ export function PreviewActions(props: {
|
||||||
const onRenderMsgs = (msgs: ChatMessage[]) => {
|
const onRenderMsgs = (msgs: ChatMessage[]) => {
|
||||||
setShouldExport(false);
|
setShouldExport(false);
|
||||||
|
|
||||||
var api: ClientApi;
|
const api: ClientApi = getClientApi(config.modelConfig.providerName);
|
||||||
if (config.modelConfig.providerName == ServiceProvider.Google) {
|
|
||||||
api = new ClientApi(ModelProvider.GeminiPro);
|
|
||||||
} else if (config.modelConfig.providerName == ServiceProvider.Anthropic) {
|
|
||||||
api = new ClientApi(ModelProvider.Claude);
|
|
||||||
} else if (config.modelConfig.providerName == ServiceProvider.Baidu) {
|
|
||||||
api = new ClientApi(ModelProvider.Ernie);
|
|
||||||
} else {
|
|
||||||
api = new ClientApi(ModelProvider.GPT);
|
|
||||||
}
|
|
||||||
|
|
||||||
api
|
api
|
||||||
.share(msgs)
|
.share(msgs)
|
||||||
|
|
|
@ -12,7 +12,7 @@ import LoadingIcon from "../icons/three-dots.svg";
|
||||||
import { getCSSVar, useMobileScreen } from "../utils";
|
import { getCSSVar, useMobileScreen } from "../utils";
|
||||||
|
|
||||||
import dynamic from "next/dynamic";
|
import dynamic from "next/dynamic";
|
||||||
import { ServiceProvider, ModelProvider, Path, SlotID } from "../constant";
|
import { Path, SlotID } from "../constant";
|
||||||
import { ErrorBoundary } from "./error";
|
import { ErrorBoundary } from "./error";
|
||||||
|
|
||||||
import { getISOLang, getLang } from "../locales";
|
import { getISOLang, getLang } from "../locales";
|
||||||
|
@ -27,7 +27,7 @@ import { SideBar } from "./sidebar";
|
||||||
import { useAppConfig } from "../store/config";
|
import { useAppConfig } from "../store/config";
|
||||||
import { AuthPage } from "./auth";
|
import { AuthPage } from "./auth";
|
||||||
import { getClientConfig } from "../config/client";
|
import { getClientConfig } from "../config/client";
|
||||||
import { ClientApi } from "../client/api";
|
import { type ClientApi, getClientApi } from "../client/api";
|
||||||
import { useAccessStore } from "../store";
|
import { useAccessStore } from "../store";
|
||||||
|
|
||||||
export function Loading(props: { noLogo?: boolean }) {
|
export function Loading(props: { noLogo?: boolean }) {
|
||||||
|
@ -170,16 +170,8 @@ function Screen() {
|
||||||
export function useLoadData() {
|
export function useLoadData() {
|
||||||
const config = useAppConfig();
|
const config = useAppConfig();
|
||||||
|
|
||||||
var api: ClientApi;
|
const api: ClientApi = getClientApi(config.modelConfig.providerName);
|
||||||
if (config.modelConfig.providerName == ServiceProvider.Google) {
|
|
||||||
api = new ClientApi(ModelProvider.GeminiPro);
|
|
||||||
} else if (config.modelConfig.providerName == ServiceProvider.Anthropic) {
|
|
||||||
api = new ClientApi(ModelProvider.Claude);
|
|
||||||
} else if (config.modelConfig.providerName == ServiceProvider.Baidu) {
|
|
||||||
api = new ClientApi(ModelProvider.Ernie);
|
|
||||||
} else {
|
|
||||||
api = new ClientApi(ModelProvider.GPT);
|
|
||||||
}
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
const models = await api.llm.models();
|
const models = await api.llm.models();
|
||||||
|
|
|
@ -15,7 +15,12 @@ import {
|
||||||
SUMMARIZE_MODEL,
|
SUMMARIZE_MODEL,
|
||||||
GEMINI_SUMMARIZE_MODEL,
|
GEMINI_SUMMARIZE_MODEL,
|
||||||
} from "../constant";
|
} from "../constant";
|
||||||
import { ClientApi, RequestMessage, MultimodalContent } from "../client/api";
|
import { getClientApi } from "../client/api";
|
||||||
|
import type {
|
||||||
|
ClientApi,
|
||||||
|
RequestMessage,
|
||||||
|
MultimodalContent,
|
||||||
|
} from "../client/api";
|
||||||
import { ChatControllerPool } from "../client/controller";
|
import { ChatControllerPool } from "../client/controller";
|
||||||
import { prettyObject } from "../utils/format";
|
import { prettyObject } from "../utils/format";
|
||||||
import { estimateTokenLength } from "../utils/token";
|
import { estimateTokenLength } from "../utils/token";
|
||||||
|
@ -363,17 +368,7 @@ export const useChatStore = createPersistStore(
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
var api: ClientApi;
|
const api: ClientApi = getClientApi(modelConfig.providerName);
|
||||||
if (modelConfig.providerName == ServiceProvider.Google) {
|
|
||||||
api = new ClientApi(ModelProvider.GeminiPro);
|
|
||||||
} else if (modelConfig.providerName == ServiceProvider.Anthropic) {
|
|
||||||
api = new ClientApi(ModelProvider.Claude);
|
|
||||||
} else if (modelConfig.providerName == ServiceProvider.Baidu) {
|
|
||||||
api = new ClientApi(ModelProvider.Ernie);
|
|
||||||
} else {
|
|
||||||
api = new ClientApi(ModelProvider.GPT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// make request
|
// make request
|
||||||
api.llm.chat({
|
api.llm.chat({
|
||||||
messages: sendMessages,
|
messages: sendMessages,
|
||||||
|
@ -549,16 +544,7 @@ export const useChatStore = createPersistStore(
|
||||||
const session = get().currentSession();
|
const session = get().currentSession();
|
||||||
const modelConfig = session.mask.modelConfig;
|
const modelConfig = session.mask.modelConfig;
|
||||||
|
|
||||||
var api: ClientApi;
|
const api: ClientApi = getClientApi(modelConfig.providerName);
|
||||||
if (modelConfig.providerName == ServiceProvider.Google) {
|
|
||||||
api = new ClientApi(ModelProvider.GeminiPro);
|
|
||||||
} else if (modelConfig.providerName == ServiceProvider.Anthropic) {
|
|
||||||
api = new ClientApi(ModelProvider.Claude);
|
|
||||||
} else if (modelConfig.providerName == ServiceProvider.Baidu) {
|
|
||||||
api = new ClientApi(ModelProvider.Ernie);
|
|
||||||
} else {
|
|
||||||
api = new ClientApi(ModelProvider.GPT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove error messages if any
|
// remove error messages if any
|
||||||
const messages = session.messages;
|
const messages = session.messages;
|
||||||
|
|
Loading…
Reference in New Issue