Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-Web into chatgpt-main
  • Loading branch information
Kosette committed Apr 26, 2024
2 parents ec466af + 506c17a commit e82db4c
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 282 deletions.
1 change: 1 addition & 0 deletions app/api/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DANGER_CONFIG = {
hideBalanceQuery: serverConfig.hideBalanceQuery,
disableFastLink: serverConfig.disableFastLink,
customModels: serverConfig.customModels,
defaultModel: serverConfig.defaultModel,
};

declare global {
Expand Down
6 changes: 5 additions & 1 deletion app/client/platforms/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ export class ClaudeApi implements LLMApi {
path(path: string): string {
const accessStore = useAccessStore.getState();

let baseUrl: string = accessStore.anthropicUrl;
let baseUrl: string = "";

if (accessStore.useCustomConfig) {
baseUrl = accessStore.anthropicUrl;
}

// if endpoint is empty, use default endpoint
if (baseUrl.trim().length === 0) {
Expand Down
21 changes: 11 additions & 10 deletions app/client/platforms/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ export class GeminiProApi implements LLMApi {
}
async chat(options: ChatOptions): Promise<void> {
// const apiClient = this;
const visionModel = isVisionModel(options.config.model);
let multimodal = false;
const messages = options.messages.map((v) => {
let parts: any[] = [{ text: getMessageTextContent(v) }];
if (visionModel) {
if (isVisionModel(options.config.model)) {
const images = getMessageImages(v);
if (images.length > 0) {
multimodal = true;
Expand Down Expand Up @@ -104,24 +103,25 @@ export class GeminiProApi implements LLMApi {
};

const accessStore = useAccessStore.getState();
let baseUrl = accessStore.googleUrl;

let baseUrl = "";

if (accessStore.useCustomConfig) {
baseUrl = accessStore.googleUrl;
}

const isApp = !!getClientConfig()?.isApp;

let shouldStream = !!options.config.stream;
const controller = new AbortController();
options.onController?.(controller);
try {
let googleChatPath = visionModel
? Google.VisionChatPath(modelConfig.model)
: Google.ChatPath(modelConfig.model);
let chatPath = this.path(googleChatPath);

// let baseUrl = accessStore.googleUrl;

if (!baseUrl) {
baseUrl = isApp
? DEFAULT_API_HOST + "/api/proxy/google/" + googleChatPath
: chatPath;
? DEFAULT_API_HOST + "/api/proxy/google/" + Google.ChatPath(modelConfig.model)
: this.path(Google.ChatPath(modelConfig.model));
}

if (isApp) {
Expand All @@ -139,6 +139,7 @@ export class GeminiProApi implements LLMApi {
() => controller.abort(),
REQUEST_TIMEOUT_MS,
);

if (shouldStream) {
let responseText = "";
let remainText = "";
Expand Down
28 changes: 16 additions & 12 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ export class ChatGPTApi implements LLMApi {
path(path: string): string {
const accessStore = useAccessStore.getState();

const isAzure = accessStore.provider === ServiceProvider.Azure;
let baseUrl = "";

if (isAzure && !accessStore.isValidAzure()) {
throw Error(
"incomplete azure config, please check it in your settings page",
);
}
if (accessStore.useCustomConfig) {
const isAzure = accessStore.provider === ServiceProvider.Azure;

let baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl;
if (isAzure && !accessStore.isValidAzure()) {
throw Error(
"incomplete azure config, please check it in your settings page",
);
}

if (isAzure) {
path = makeAzurePath(path, accessStore.azureApiVersion);
}

baseUrl = isAzure ? accessStore.azureUrl : accessStore.openaiUrl;
}

if (baseUrl.length === 0) {
const isApp = !!getClientConfig()?.isApp;
Expand All @@ -84,10 +92,6 @@ export class ChatGPTApi implements LLMApi {
baseUrl = "https://" + baseUrl;
}

if (isAzure) {
path = makeAzurePath(path, accessStore.azureApiVersion);
}

console.log("[Proxy Endpoint] ", baseUrl, path);

return [baseUrl, path].join("/");
Expand Down Expand Up @@ -125,7 +129,7 @@ export class ChatGPTApi implements LLMApi {
};

// add max_tokens to vision model
if (visionModel) {
if (visionModel && modelConfig.model.includes("preview")) {
requestPayload["max_tokens"] = Math.max(modelConfig.max_tokens, 4000);
}

Expand Down
66 changes: 61 additions & 5 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,20 @@ export function ChatActions(props: {
// switch model
const currentModel = chatStore.currentSession().mask.modelConfig.model;
const allModels = useAllModels();
const models = useMemo(
() => allModels.filter((m) => m.available),
[allModels],
);
const models = useMemo(() => {
const filteredModels = allModels.filter((m) => m.available);
const defaultModel = filteredModels.find((m) => m.isDefault);

if (defaultModel) {
const arr = [
defaultModel,
...filteredModels.filter((m) => m !== defaultModel),
];
return arr;
} else {
return filteredModels;
}
}, [allModels]);
const [showModelSelector, setShowModelSelector] = useState(false);
const [showUploadImage, setShowUploadImage] = useState(false);

Expand All @@ -467,7 +477,10 @@ export function ChatActions(props: {
// switch to first available model
const isUnavaliableModel = !models.some((m) => m.name === currentModel);
if (isUnavaliableModel && models.length > 0) {
const nextModel = models[0].name as ModelType;
// show next model to default model if exist
let nextModel: ModelType = (
models.find((model) => model.isDefault) || models[0]
).name;
chatStore.updateCurrentSession(
(session) => (session.mask.modelConfig.model = nextModel),
);
Expand Down Expand Up @@ -1103,6 +1116,49 @@ function _Chat() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handlePaste = useCallback(
async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
const currentModel = chatStore.currentSession().mask.modelConfig.model;
if (!isVisionModel(currentModel)) {
return;
}
const items = (event.clipboardData || window.clipboardData).items;
for (const item of items) {
if (item.kind === "file" && item.type.startsWith("image/")) {
event.preventDefault();
const file = item.getAsFile();
if (file) {
const images: string[] = [];
images.push(...attachImages);
images.push(
...(await new Promise<string[]>((res, rej) => {
setUploading(true);
const imagesData: string[] = [];
compressImage(file, 256 * 1024)
.then((dataUrl) => {
imagesData.push(dataUrl);
setUploading(false);
res(imagesData);
})
.catch((e) => {
setUploading(false);
rej(e);
});
})),
);
const imagesLength = images.length;

if (imagesLength > 3) {
images.splice(3, imagesLength - 3);
}
setAttachImages(images);
}
}
}
},
[attachImages, chatStore],
);

async function uploadImage() {
const images: string[] = [];
images.push(...attachImages);
Expand Down
4 changes: 4 additions & 0 deletions app/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare global {
ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not
DISABLE_FAST_LINK?: string; // disallow parse settings from url or not
CUSTOM_MODELS?: string; // to control custom models
DEFAULT_MODEL?: string; // to cnntrol default model in every new chat window

// azure only
AZURE_URL?: string; // https://{azure-url}/openai/deployments/{deploy-name}
Expand Down Expand Up @@ -59,12 +60,14 @@ export const getServerSideConfig = () => {

const disableGPT4 = !!process.env.DISABLE_GPT4;
let customModels = process.env.CUSTOM_MODELS ?? "";
let defaultModel = process.env.DEFAULT_MODEL ?? "";

if (disableGPT4) {
if (customModels) customModels += ",";
customModels += DEFAULT_MODELS.filter((m) => m.name.startsWith("gpt-4"))
.map((m) => "-" + m.name)
.join(",");
if (defaultModel.startsWith("gpt-4")) defaultModel = "";
}

const isAzure = !!process.env.AZURE_URL;
Expand Down Expand Up @@ -116,6 +119,7 @@ export const getServerSideConfig = () => {
hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY,
disableFastLink: !!process.env.DISABLE_FAST_LINK,
customModels,
defaultModel,
whiteWebDevEndpoints,
};
};
Loading

0 comments on commit e82db4c

Please sign in to comment.