mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-09-27 05:36:39 +08:00
fix: #349
This commit is contained in:
parent
a80ac42c92
commit
655d51401e
@ -8,7 +8,7 @@ import {
|
|||||||
import { prettyObject } from "@/app/utils/format";
|
import { prettyObject } from "@/app/utils/format";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { auth } from "@/app/api/auth";
|
import { auth } from "@/app/api/auth";
|
||||||
import { isModelAvailableInServer } from "@/app/utils/model";
|
import { isModelNotavailableInServer } from "@/app/utils/model";
|
||||||
|
|
||||||
const serverConfig = getServerSideConfig();
|
const serverConfig = getServerSideConfig();
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ async function request(req: NextRequest) {
|
|||||||
|
|
||||||
// not undefined and is false
|
// not undefined and is false
|
||||||
if (
|
if (
|
||||||
isModelAvailableInServer(
|
isModelNotavailableInServer(
|
||||||
serverConfig.customModels,
|
serverConfig.customModels,
|
||||||
jsonBody?.model as string,
|
jsonBody?.model as string,
|
||||||
ServiceProvider.ByteDance as string,
|
ServiceProvider.ByteDance as string,
|
||||||
|
@ -28,7 +28,11 @@ import {
|
|||||||
isClaudeThinkingModel,
|
isClaudeThinkingModel,
|
||||||
isVisionModel,
|
isVisionModel,
|
||||||
} from "@/app/utils";
|
} from "@/app/utils";
|
||||||
import { preProcessImageContent, streamWithThink } from "@/app/utils/chat";
|
import {
|
||||||
|
preProcessImageAndWebReferenceContent,
|
||||||
|
preProcessImageContent,
|
||||||
|
streamWithThink,
|
||||||
|
} from "@/app/utils/chat";
|
||||||
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
import { RequestPayload } from "./openai";
|
import { RequestPayload } from "./openai";
|
||||||
import { fetch } from "@/app/utils/stream";
|
import { fetch } from "@/app/utils/stream";
|
||||||
@ -290,7 +294,7 @@ export class ClaudeApi implements LLMApi {
|
|||||||
// try get base64image from local cache image_url
|
// try get base64image from local cache image_url
|
||||||
const messages: ChatOptions["messages"] = [];
|
const messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = await preProcessImageContent(v.content);
|
const content = await preProcessImageAndWebReferenceContent(v);
|
||||||
messages.push({ role: v.role, content });
|
messages.push({ role: v.role, content });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
import { ApiPath, ByteDance, BYTEDANCE_BASE_URL } from "@/app/constant";
|
||||||
import {
|
import {
|
||||||
ApiPath,
|
useAccessStore,
|
||||||
ByteDance,
|
useAppConfig,
|
||||||
BYTEDANCE_BASE_URL,
|
useChatStore,
|
||||||
REQUEST_TIMEOUT_MS,
|
ChatMessageTool,
|
||||||
} from "@/app/constant";
|
usePluginStore,
|
||||||
import { useAccessStore, useAppConfig, useChatStore } from "@/app/store";
|
} from "@/app/store";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AgentChatOptions,
|
AgentChatOptions,
|
||||||
@ -18,16 +19,17 @@ import {
|
|||||||
SpeechOptions,
|
SpeechOptions,
|
||||||
TranscriptionOptions,
|
TranscriptionOptions,
|
||||||
} from "../api";
|
} from "../api";
|
||||||
import Locale from "../../locales";
|
|
||||||
import {
|
import {
|
||||||
EventStreamContentType,
|
preProcessImageAndWebReferenceContent,
|
||||||
fetchEventSource,
|
streamWithThink,
|
||||||
} from "@fortaine/fetch-event-source";
|
} from "@/app/utils/chat";
|
||||||
import { prettyObject } from "@/app/utils/format";
|
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
|
import { preProcessImageContent } from "@/app/utils/chat";
|
||||||
import {
|
import {
|
||||||
getMessageTextContent,
|
|
||||||
getWebReferenceMessageTextContent,
|
getWebReferenceMessageTextContent,
|
||||||
|
getTimeoutMSByModel,
|
||||||
|
getMessageTextContent,
|
||||||
} from "@/app/utils";
|
} from "@/app/utils";
|
||||||
import { fetch } from "@/app/utils/stream";
|
import { fetch } from "@/app/utils/stream";
|
||||||
|
|
||||||
@ -40,7 +42,7 @@ export interface OpenAIListModelResponse {
|
|||||||
}>;
|
}>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RequestPayload {
|
interface RequestPayloadForByteDance {
|
||||||
messages: {
|
messages: {
|
||||||
role: "system" | "user" | "assistant";
|
role: "system" | "user" | "assistant";
|
||||||
content: string | MultimodalContent[];
|
content: string | MultimodalContent[];
|
||||||
@ -99,10 +101,14 @@ export class DoubaoApi implements LLMApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async chat(options: ChatOptions) {
|
async chat(options: ChatOptions) {
|
||||||
const messages = options.messages.map((v) => ({
|
const messages: ChatOptions["messages"] = [];
|
||||||
role: v.role,
|
for (const v of options.messages) {
|
||||||
content: getWebReferenceMessageTextContent(v),
|
const content =
|
||||||
}));
|
v.role === "assistant"
|
||||||
|
? getMessageTextContent(v)
|
||||||
|
: await preProcessImageAndWebReferenceContent(v);
|
||||||
|
messages.push({ role: v.role, content });
|
||||||
|
}
|
||||||
|
|
||||||
const modelConfig = {
|
const modelConfig = {
|
||||||
...useAppConfig.getState().modelConfig,
|
...useAppConfig.getState().modelConfig,
|
||||||
@ -113,7 +119,7 @@ export class DoubaoApi implements LLMApi {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const shouldStream = !!options.config.stream;
|
const shouldStream = !!options.config.stream;
|
||||||
const requestPayload: RequestPayload = {
|
const requestPayload: RequestPayloadForByteDance = {
|
||||||
messages,
|
messages,
|
||||||
stream: shouldStream,
|
stream: shouldStream,
|
||||||
model: modelConfig.model,
|
model: modelConfig.model,
|
||||||
@ -138,119 +144,103 @@ export class DoubaoApi implements LLMApi {
|
|||||||
// make a fetch request
|
// make a fetch request
|
||||||
const requestTimeoutId = setTimeout(
|
const requestTimeoutId = setTimeout(
|
||||||
() => controller.abort(),
|
() => controller.abort(),
|
||||||
REQUEST_TIMEOUT_MS,
|
getTimeoutMSByModel(options.config.model),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (shouldStream) {
|
if (shouldStream) {
|
||||||
let responseText = "";
|
const tools = [] as any;
|
||||||
let remainText = "";
|
const funcs = {};
|
||||||
let finished = false;
|
// const [tools, funcs] = usePluginStore
|
||||||
let responseRes: Response;
|
// .getState()
|
||||||
|
// .getAsTools(
|
||||||
|
// useChatStore.getState().currentSession().mask?.plugin || [],
|
||||||
|
// );
|
||||||
|
return streamWithThink(
|
||||||
|
chatPath,
|
||||||
|
requestPayload,
|
||||||
|
getHeaders(),
|
||||||
|
tools as any,
|
||||||
|
funcs,
|
||||||
|
controller,
|
||||||
|
// parseSSE
|
||||||
|
(text: string, runTools: ChatMessageTool[]) => {
|
||||||
|
// console.log("parseSSE", text, runTools);
|
||||||
|
const json = JSON.parse(text);
|
||||||
|
const choices = json.choices as Array<{
|
||||||
|
delta: {
|
||||||
|
content: string | null;
|
||||||
|
tool_calls: ChatMessageTool[];
|
||||||
|
reasoning_content: string | null;
|
||||||
|
};
|
||||||
|
}>;
|
||||||
|
|
||||||
// animate response to make it looks smooth
|
if (!choices?.length) return { isThinking: false, content: "" };
|
||||||
function animateResponseText() {
|
|
||||||
if (finished || controller.signal.aborted) {
|
const tool_calls = choices[0]?.delta?.tool_calls;
|
||||||
responseText += remainText;
|
if (tool_calls?.length > 0) {
|
||||||
console.log("[Response Animation] finished");
|
const index = tool_calls[0]?.index;
|
||||||
if (responseText?.length === 0) {
|
const id = tool_calls[0]?.id;
|
||||||
options.onError?.(new Error("empty response from server"));
|
const args = tool_calls[0]?.function?.arguments;
|
||||||
}
|
if (id) {
|
||||||
return;
|
runTools.push({
|
||||||
}
|
id,
|
||||||
|
type: tool_calls[0]?.type,
|
||||||
if (remainText.length > 0) {
|
function: {
|
||||||
const fetchCount = Math.max(1, Math.round(remainText.length / 60));
|
name: tool_calls[0]?.function?.name as string,
|
||||||
const fetchText = remainText.slice(0, fetchCount);
|
arguments: args,
|
||||||
responseText += fetchText;
|
},
|
||||||
remainText = remainText.slice(fetchCount);
|
});
|
||||||
options.onUpdate?.(responseText, fetchText);
|
} else {
|
||||||
}
|
// @ts-ignore
|
||||||
|
runTools[index]["function"]["arguments"] += args;
|
||||||
requestAnimationFrame(animateResponseText);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// start animaion
|
|
||||||
animateResponseText();
|
|
||||||
|
|
||||||
const finish = () => {
|
|
||||||
if (!finished) {
|
|
||||||
finished = true;
|
|
||||||
options.onFinish(responseText + remainText, responseRes);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
controller.signal.onabort = finish;
|
|
||||||
|
|
||||||
fetchEventSource(chatPath, {
|
|
||||||
fetch: fetch as any,
|
|
||||||
...chatPayload,
|
|
||||||
async onopen(res) {
|
|
||||||
clearTimeout(requestTimeoutId);
|
|
||||||
const contentType = res.headers.get("content-type");
|
|
||||||
console.log(
|
|
||||||
"[ByteDance] request response content type: ",
|
|
||||||
contentType,
|
|
||||||
);
|
|
||||||
responseRes = res;
|
|
||||||
if (contentType?.startsWith("text/plain")) {
|
|
||||||
responseText = await res.clone().text();
|
|
||||||
return finish();
|
|
||||||
}
|
}
|
||||||
|
const reasoning = choices[0]?.delta?.reasoning_content;
|
||||||
|
const content = choices[0]?.delta?.content;
|
||||||
|
|
||||||
|
// Skip if both content and reasoning_content are empty or null
|
||||||
if (
|
if (
|
||||||
!res.ok ||
|
(!reasoning || reasoning.length === 0) &&
|
||||||
!res.headers
|
(!content || content.length === 0)
|
||||||
.get("content-type")
|
|
||||||
?.startsWith(EventStreamContentType) ||
|
|
||||||
res.status !== 200
|
|
||||||
) {
|
) {
|
||||||
const responseTexts = [responseText];
|
return {
|
||||||
let extraInfo = await res.clone().text();
|
isThinking: false,
|
||||||
try {
|
content: "",
|
||||||
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 (reasoning && reasoning.length > 0) {
|
||||||
if (msg.data === "[DONE]" || finished) {
|
return {
|
||||||
return finish();
|
isThinking: true,
|
||||||
}
|
content: reasoning,
|
||||||
const text = msg.data;
|
};
|
||||||
try {
|
} else if (content && content.length > 0) {
|
||||||
const json = JSON.parse(text);
|
return {
|
||||||
const choices = json.choices as Array<{
|
isThinking: false,
|
||||||
delta: { content: string };
|
content: content,
|
||||||
}>;
|
};
|
||||||
const delta = choices[0]?.delta?.content;
|
|
||||||
if (delta) {
|
|
||||||
remainText += delta;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.error("[Request] parse error", text, msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
isThinking: false,
|
||||||
|
content: "",
|
||||||
|
};
|
||||||
},
|
},
|
||||||
onclose() {
|
// processToolMessage, include tool_calls message and tool call results
|
||||||
finish();
|
(
|
||||||
|
requestPayload: RequestPayloadForByteDance,
|
||||||
|
toolCallMessage: any,
|
||||||
|
toolCallResult: any[],
|
||||||
|
) => {
|
||||||
|
requestPayload?.messages?.splice(
|
||||||
|
requestPayload?.messages?.length,
|
||||||
|
0,
|
||||||
|
toolCallMessage,
|
||||||
|
...toolCallResult,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
onerror(e) {
|
options,
|
||||||
options.onError?.(e);
|
);
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
openWhenHidden: true,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
const res = await fetch(chatPath, chatPayload);
|
const res = await fetch(chatPath, chatPayload);
|
||||||
clearTimeout(requestTimeoutId);
|
clearTimeout(requestTimeoutId);
|
||||||
|
@ -17,7 +17,10 @@ import {
|
|||||||
usePluginStore,
|
usePluginStore,
|
||||||
ChatMessageTool,
|
ChatMessageTool,
|
||||||
} from "@/app/store";
|
} from "@/app/store";
|
||||||
import { stream } from "@/app/utils/chat";
|
import {
|
||||||
|
preProcessImageAndWebReferenceContent,
|
||||||
|
stream,
|
||||||
|
} from "@/app/utils/chat";
|
||||||
import { getClientConfig } from "@/app/config/client";
|
import { getClientConfig } from "@/app/config/client";
|
||||||
import { GEMINI_BASE_URL } from "@/app/constant";
|
import { GEMINI_BASE_URL } from "@/app/constant";
|
||||||
|
|
||||||
@ -88,7 +91,7 @@ export class GeminiProApi implements LLMApi {
|
|||||||
// try get base64image from local cache image_url
|
// try get base64image from local cache image_url
|
||||||
const _messages: ChatOptions["messages"] = [];
|
const _messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = await preProcessImageContent(v.content);
|
const content = await preProcessImageAndWebReferenceContent(v);
|
||||||
_messages.push({ role: v.role, content });
|
_messages.push({ role: v.role, content });
|
||||||
}
|
}
|
||||||
const messages = _messages.map((v) => {
|
const messages = _messages.map((v) => {
|
||||||
|
@ -17,7 +17,11 @@ import {
|
|||||||
usePluginStore,
|
usePluginStore,
|
||||||
} from "@/app/store";
|
} from "@/app/store";
|
||||||
import { collectModelsWithDefaultModel } from "@/app/utils/model";
|
import { collectModelsWithDefaultModel } from "@/app/utils/model";
|
||||||
import { preProcessImageContent, stream } from "@/app/utils/chat";
|
import {
|
||||||
|
preProcessImageAndWebReferenceContent,
|
||||||
|
preProcessImageContent,
|
||||||
|
stream,
|
||||||
|
} from "@/app/utils/chat";
|
||||||
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
|
||||||
import { DalleSize, DalleQuality, DalleStyle } from "@/app/typing";
|
import { DalleSize, DalleQuality, DalleStyle } from "@/app/typing";
|
||||||
|
|
||||||
@ -239,7 +243,7 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
const messages: ChatOptions["messages"] = [];
|
const messages: ChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = visionModel
|
const content = visionModel
|
||||||
? await preProcessImageContent(v.content)
|
? await preProcessImageAndWebReferenceContent(v)
|
||||||
: getWebReferenceMessageTextContent(v);
|
: getWebReferenceMessageTextContent(v);
|
||||||
if (!(isO1 && v.role === "system"))
|
if (!(isO1 && v.role === "system"))
|
||||||
messages.push({ role: v.role, content });
|
messages.push({ role: v.role, content });
|
||||||
@ -429,7 +433,7 @@ export class ChatGPTApi implements LLMApi {
|
|||||||
const messages: AgentChatOptions["messages"] = [];
|
const messages: AgentChatOptions["messages"] = [];
|
||||||
for (const v of options.messages) {
|
for (const v of options.messages) {
|
||||||
const content = visionModel
|
const content = visionModel
|
||||||
? await preProcessImageContent(v.content)
|
? await preProcessImageAndWebReferenceContent(v)
|
||||||
: getMessageTextContent(v);
|
: getMessageTextContent(v);
|
||||||
messages.push({ role: v.role, content });
|
messages.push({ role: v.role, content });
|
||||||
}
|
}
|
||||||
|
@ -105,6 +105,7 @@ export const UNFINISHED_INPUT = (id: string) => "unfinished-input-" + id;
|
|||||||
export const STORAGE_KEY = "chatgpt-next-web";
|
export const STORAGE_KEY = "chatgpt-next-web";
|
||||||
|
|
||||||
export const REQUEST_TIMEOUT_MS = 60000;
|
export const REQUEST_TIMEOUT_MS = 60000;
|
||||||
|
export const REQUEST_TIMEOUT_MS_FOR_THINKING = REQUEST_TIMEOUT_MS * 5;
|
||||||
|
|
||||||
export const EXPORT_MESSAGE_CLASS_NAME = "export-markdown";
|
export const EXPORT_MESSAGE_CLASS_NAME = "export-markdown";
|
||||||
|
|
||||||
@ -417,6 +418,10 @@ const bytedanceModels = [
|
|||||||
"Doubao-pro-4k",
|
"Doubao-pro-4k",
|
||||||
"Doubao-pro-32k",
|
"Doubao-pro-32k",
|
||||||
"Doubao-pro-128k",
|
"Doubao-pro-128k",
|
||||||
|
"deepseek-v3-241226",
|
||||||
|
"deepseek-r1-250120",
|
||||||
|
"deepseek-r1-distill-qwen-7b-250120",
|
||||||
|
"deepseek-r1-distill-qwen-32b-250120",
|
||||||
];
|
];
|
||||||
|
|
||||||
const alibabaModes = [
|
const alibabaModes = [
|
||||||
|
20
app/utils.ts
20
app/utils.ts
@ -2,7 +2,11 @@ import { useEffect, useState } from "react";
|
|||||||
import { showToast } from "./components/ui-lib";
|
import { showToast } from "./components/ui-lib";
|
||||||
import Locale, { getLang } from "./locales";
|
import Locale, { getLang } from "./locales";
|
||||||
import { RequestMessage } from "./client/api";
|
import { RequestMessage } from "./client/api";
|
||||||
import { DEFAULT_MODELS } from "./constant";
|
import {
|
||||||
|
DEFAULT_MODELS,
|
||||||
|
REQUEST_TIMEOUT_MS,
|
||||||
|
REQUEST_TIMEOUT_MS_FOR_THINKING,
|
||||||
|
} from "./constant";
|
||||||
import { ServiceProvider } from "./constant";
|
import { ServiceProvider } from "./constant";
|
||||||
// import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http";
|
// import { fetch as tauriFetch, ResponseType } from "@tauri-apps/api/http";
|
||||||
import { fetch as tauriStreamFetch } from "./utils/stream";
|
import { fetch as tauriStreamFetch } from "./utils/stream";
|
||||||
@ -340,6 +344,20 @@ export function isDalle3(model: string) {
|
|||||||
return "dall-e-3" === model;
|
return "dall-e-3" === model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getTimeoutMSByModel(model: string) {
|
||||||
|
model = model.toLowerCase();
|
||||||
|
if (
|
||||||
|
model.startsWith("dall-e") ||
|
||||||
|
model.startsWith("dalle") ||
|
||||||
|
model.startsWith("o1") ||
|
||||||
|
model.startsWith("o3") ||
|
||||||
|
model.includes("deepseek-r") ||
|
||||||
|
model.includes("-thinking")
|
||||||
|
)
|
||||||
|
return REQUEST_TIMEOUT_MS_FOR_THINKING;
|
||||||
|
return REQUEST_TIMEOUT_MS;
|
||||||
|
}
|
||||||
|
|
||||||
export function showPlugins(provider: ServiceProvider, model: string) {
|
export function showPlugins(provider: ServiceProvider, model: string) {
|
||||||
if (
|
if (
|
||||||
provider == ServiceProvider.OpenAI ||
|
provider == ServiceProvider.OpenAI ||
|
||||||
|
@ -11,6 +11,7 @@ import {
|
|||||||
} from "@fortaine/fetch-event-source";
|
} from "@fortaine/fetch-event-source";
|
||||||
import { prettyObject } from "./format";
|
import { prettyObject } from "./format";
|
||||||
import { fetch as tauriFetch } from "./stream";
|
import { fetch as tauriFetch } from "./stream";
|
||||||
|
import { getWebReferenceMessageTextContent } from "../utils";
|
||||||
|
|
||||||
export function compressImage(file: Blob, maxSize: number): Promise<string> {
|
export function compressImage(file: Blob, maxSize: number): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -92,6 +93,16 @@ export async function preProcessImageContent(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function preProcessImageAndWebReferenceContent(
|
||||||
|
message: RequestMessage,
|
||||||
|
) {
|
||||||
|
const content = message.content;
|
||||||
|
if (typeof content === "string") {
|
||||||
|
return getWebReferenceMessageTextContent(message);
|
||||||
|
}
|
||||||
|
return preProcessImageContent(content);
|
||||||
|
}
|
||||||
|
|
||||||
const imageCaches: Record<string, string> = {};
|
const imageCaches: Record<string, string> = {};
|
||||||
export function cacheImageToBase64Image(imageUrl: string) {
|
export function cacheImageToBase64Image(imageUrl: string) {
|
||||||
if (imageUrl.includes(CACHE_URL_PREFIX)) {
|
if (imageUrl.includes(CACHE_URL_PREFIX)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user