mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2026-03-06 20:24:25 +08:00
Compare commits
42 Commits
afbf5eb541
...
1998cf5ced
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1998cf5ced | ||
|
|
1164e1bdf6 | ||
|
|
d55c752e1e | ||
|
|
e3c18bb123 | ||
|
|
00d6cb27f7 | ||
|
|
f532731e2a | ||
|
|
58837f6dec | ||
|
|
e49fe976d9 | ||
|
|
14f751965f | ||
|
|
0ec423389f | ||
|
|
820ab54e2d | ||
|
|
a6c1eb27a8 | ||
|
|
0dc4071ccc | ||
|
|
4d3949718a | ||
|
|
aef535f1a7 | ||
|
|
686a80e727 | ||
|
|
e49466fa05 | ||
|
|
4b93370814 | ||
|
|
5733e3c588 | ||
|
|
44fc5b5cbf | ||
|
|
2d3f7c922f | ||
|
|
fe8cca3730 | ||
|
|
fbb7a1e853 | ||
|
|
fb2c15567d | ||
|
|
c2c52a1f60 | ||
|
|
106ddc17cd | ||
|
|
17d5209738 | ||
|
|
d66bfc6352 | ||
|
|
4d75b23ed1 | ||
|
|
36bfa2ef7c | ||
|
|
afe12c212e | ||
|
|
adf97c6d8b | ||
|
|
7a8d557ea3 | ||
|
|
d3f0a77830 | ||
|
|
7c466c9b9c | ||
|
|
d357b45e84 | ||
|
|
b0d28eb77e | ||
|
|
801dc412f9 | ||
|
|
064e964d75 | ||
|
|
47fb40d572 | ||
|
|
9e18cc260b | ||
|
|
03268ce4d8 |
13
README.md
13
README.md
@@ -31,7 +31,7 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4
|
||||
[MacOS-image]: https://img.shields.io/badge/-MacOS-black?logo=apple
|
||||
[Linux-image]: https://img.shields.io/badge/-Linux-333?logo=ubuntu
|
||||
|
||||
[<img src="https://vercel.com/button" alt="Deploy on Zeabur" height="30">](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [<img src="https://zeabur.com/button.svg" alt="Deploy on Zeabur" height="30">](https://zeabur.com/templates/ZBUEFA) [<img src="https://gitpod.io/button/open-in-gitpod.svg" alt="Open in Gitpod" height="30">](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) [<img src="https://img.shields.io/badge/BT_Deploy-Install-20a53a" alt="Open in Gitpod" height="30">](https://www.bt.cn/new/download.html)
|
||||
[<img src="https://vercel.com/button" alt="Deploy on Vercel" height="30">](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2FChatGPTNextWeb%2FChatGPT-Next-Web&env=OPENAI_API_KEY&env=CODE&project-name=nextchat&repository-name=NextChat) [<img src="https://zeabur.com/button.svg" alt="Deploy on Zeabur" height="30">](https://zeabur.com/templates/ZBUEFA) [<img src="https://gitpod.io/button/open-in-gitpod.svg" alt="Open in Gitpod" height="30">](https://gitpod.io/#https://github.com/Yidadaa/ChatGPT-Next-Web) [<img src="https://img.shields.io/badge/BT_Deploy-Install-20a53a" alt="BT Deply Install" height="30">](https://www.bt.cn/new/download.html) [<img src="https://svgshare.com/i/1AVg.svg" alt="Deploy to Alibaba Cloud" height="30">](https://computenest.aliyun.com/market/service-f1c9b75e59814dc49d52)
|
||||
|
||||
[<img src="https://github.com/user-attachments/assets/903482d4-3e87-4134-9af1-f2588fa90659" height="60" width="288" >](https://monica.im/?utm=nxcrp)
|
||||
|
||||
@@ -301,6 +301,14 @@ iflytek Api Key.
|
||||
|
||||
iflytek Api Secret.
|
||||
|
||||
### `CHATGLM_API_KEY` (optional)
|
||||
|
||||
ChatGLM Api Key.
|
||||
|
||||
### `CHATGLM_URL` (optional)
|
||||
|
||||
ChatGLM Api Url.
|
||||
|
||||
### `HIDE_USER_API_KEY` (optional)
|
||||
|
||||
> Default: Empty
|
||||
@@ -397,6 +405,9 @@ yarn dev
|
||||
|
||||
> [简体中文 > 如何部署到私人服务器](./README_CN.md#部署)
|
||||
|
||||
### BT Install
|
||||
> [简体中文 > 如何通过宝塔一键部署](./docs/bt-cn.md)
|
||||
|
||||
### Docker (Recommended)
|
||||
|
||||
```shell
|
||||
|
||||
10
README_CN.md
10
README_CN.md
@@ -184,6 +184,13 @@ ByteDance Api Url.
|
||||
|
||||
讯飞星火Api Secret.
|
||||
|
||||
### `CHATGLM_API_KEY` (可选)
|
||||
|
||||
ChatGLM Api Key.
|
||||
|
||||
### `CHATGLM_URL` (可选)
|
||||
|
||||
ChatGLM Api Url.
|
||||
|
||||
|
||||
### `HIDE_USER_API_KEY` (可选)
|
||||
@@ -264,6 +271,9 @@ BASE_URL=https://b.nextweb.fun/api/proxy
|
||||
|
||||
## 部署
|
||||
|
||||
### 宝塔面板部署
|
||||
> [简体中文 > 如何通过宝塔一键部署](./docs/bt-cn.md)
|
||||
|
||||
### 容器部署 (推荐)
|
||||
|
||||
> Docker 版本需要在 20 及其以上,否则会提示找不到镜像。
|
||||
|
||||
@@ -12,6 +12,7 @@ import { handle as moonshotHandler } from "../../moonshot";
|
||||
import { handle as stabilityHandler } from "../../stability";
|
||||
import { handle as iflytekHandler } from "../../iflytek";
|
||||
import { handle as xaiHandler } from "../../xai";
|
||||
import { handle as chatglmHandler } from "../../glm";
|
||||
import { handle as proxyHandler } from "../../proxy";
|
||||
|
||||
async function handle(
|
||||
@@ -45,6 +46,8 @@ async function handle(
|
||||
return iflytekHandler(req, { params });
|
||||
case ApiPath.XAI:
|
||||
return xaiHandler(req, { params });
|
||||
case ApiPath.ChatGLM:
|
||||
return chatglmHandler(req, { params });
|
||||
case ApiPath.OpenAI:
|
||||
return openaiHandler(req, { params });
|
||||
default:
|
||||
|
||||
@@ -117,6 +117,9 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
|
||||
case ModelProvider.XAI:
|
||||
systemApiKey = serverConfig.xaiApiKey;
|
||||
break;
|
||||
case ModelProvider.ChatGLM:
|
||||
systemApiKey = serverConfig.chatglmApiKey;
|
||||
break;
|
||||
case ModelProvider.GPT:
|
||||
default:
|
||||
if (req.nextUrl.pathname.includes("azure/deployments")) {
|
||||
|
||||
@@ -5,17 +5,13 @@ import {
|
||||
BedrockRuntimeClient,
|
||||
ConverseStreamCommand,
|
||||
ConverseStreamCommandInput,
|
||||
Message,
|
||||
ContentBlock,
|
||||
ConverseStreamOutput,
|
||||
ModelStreamErrorException,
|
||||
type Message,
|
||||
type ContentBlock,
|
||||
type SystemContentBlock,
|
||||
type Tool,
|
||||
type ToolChoice,
|
||||
type ToolResultContentBlock,
|
||||
} from "@aws-sdk/client-bedrock-runtime";
|
||||
|
||||
// 解密函数
|
||||
const ALLOWED_PATH = new Set(["converse"]);
|
||||
|
||||
function decrypt(str: string): string {
|
||||
try {
|
||||
return Buffer.from(str, "base64").toString().split("").reverse().join("");
|
||||
@@ -24,14 +20,11 @@ function decrypt(str: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
// Constants and Types
|
||||
const ALLOWED_PATH = new Set(["converse"]);
|
||||
|
||||
export interface ConverseRequest {
|
||||
modelId: string;
|
||||
messages: {
|
||||
role: "user" | "assistant" | "system";
|
||||
content: string | ContentItem[];
|
||||
content: string | any[];
|
||||
}[];
|
||||
inferenceConfig?: {
|
||||
maxTokens?: number;
|
||||
@@ -39,324 +32,117 @@ export interface ConverseRequest {
|
||||
topP?: number;
|
||||
stopSequences?: string[];
|
||||
};
|
||||
toolConfig?: {
|
||||
tools: Tool[];
|
||||
toolChoice?: ToolChoice;
|
||||
};
|
||||
}
|
||||
|
||||
interface ContentItem {
|
||||
type: "text" | "image_url" | "document" | "tool_use" | "tool_result";
|
||||
text?: string;
|
||||
image_url?: {
|
||||
url: string; // base64 data URL
|
||||
};
|
||||
document?: {
|
||||
format: DocumentFormat;
|
||||
tools?: {
|
||||
name: string;
|
||||
source: {
|
||||
bytes: string; // base64
|
||||
};
|
||||
};
|
||||
tool_use?: {
|
||||
tool_use_id: string;
|
||||
name: string;
|
||||
input: any;
|
||||
};
|
||||
tool_result?: {
|
||||
tool_use_id: string;
|
||||
content: ToolResultItem[];
|
||||
status: "success" | "error";
|
||||
};
|
||||
description?: string;
|
||||
input_schema: any;
|
||||
}[];
|
||||
}
|
||||
|
||||
interface ToolResultItem {
|
||||
type: "text" | "image" | "document" | "json";
|
||||
text?: string;
|
||||
image?: {
|
||||
format: "png" | "jpeg" | "gif" | "webp";
|
||||
source: {
|
||||
bytes: string; // base64
|
||||
};
|
||||
};
|
||||
document?: {
|
||||
format: DocumentFormat;
|
||||
name: string;
|
||||
source: {
|
||||
bytes: string; // base64
|
||||
};
|
||||
};
|
||||
json?: any;
|
||||
}
|
||||
|
||||
type DocumentFormat =
|
||||
| "pdf"
|
||||
| "csv"
|
||||
| "doc"
|
||||
| "docx"
|
||||
| "xls"
|
||||
| "xlsx"
|
||||
| "html"
|
||||
| "txt"
|
||||
| "md";
|
||||
|
||||
function validateImageSize(base64Data: string): boolean {
|
||||
const sizeInBytes = (base64Data.length * 3) / 4;
|
||||
const maxSize = 3.75 * 1024 * 1024;
|
||||
if (sizeInBytes > maxSize) {
|
||||
throw new Error("Image size exceeds 3.75 MB limit");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Content Processing Functions
|
||||
function convertContentToAWSBlock(item: ContentItem): ContentBlock | null {
|
||||
if (item.type === "text" && item.text) {
|
||||
return { text: item.text };
|
||||
}
|
||||
|
||||
if (item.type === "image_url" && item.image_url?.url) {
|
||||
const base64Match = item.image_url.url.match(
|
||||
/^data:image\/([a-zA-Z]*);base64,([^"]*)/,
|
||||
);
|
||||
if (base64Match) {
|
||||
const format = base64Match[1].toLowerCase();
|
||||
if (["png", "jpeg", "gif", "webp"].includes(format)) {
|
||||
validateImageSize(base64Match[2]);
|
||||
return {
|
||||
image: {
|
||||
format: format as "png" | "jpeg" | "gif" | "webp",
|
||||
source: {
|
||||
bytes: Uint8Array.from(Buffer.from(base64Match[2], "base64")),
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (item.type === "tool_use" && item.tool_use) {
|
||||
return {
|
||||
toolUse: {
|
||||
toolUseId: item.tool_use.tool_use_id,
|
||||
name: item.tool_use.name,
|
||||
input: item.tool_use.input,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (item.type === "tool_result" && item.tool_result) {
|
||||
const toolResultContent = item.tool_result.content
|
||||
.map((resultItem) => {
|
||||
if (resultItem.type === "text" && resultItem.text) {
|
||||
return { text: resultItem.text } as ToolResultContentBlock;
|
||||
}
|
||||
if (resultItem.type === "image" && resultItem.image) {
|
||||
return {
|
||||
image: {
|
||||
format: resultItem.image.format,
|
||||
source: {
|
||||
bytes: Uint8Array.from(
|
||||
Buffer.from(resultItem.image.source.bytes, "base64"),
|
||||
),
|
||||
},
|
||||
},
|
||||
} as ToolResultContentBlock;
|
||||
}
|
||||
if (resultItem.type === "document" && resultItem.document) {
|
||||
return {
|
||||
document: {
|
||||
format: resultItem.document.format,
|
||||
name: resultItem.document.name,
|
||||
source: {
|
||||
bytes: Uint8Array.from(
|
||||
Buffer.from(resultItem.document.source.bytes, "base64"),
|
||||
),
|
||||
},
|
||||
},
|
||||
} as ToolResultContentBlock;
|
||||
}
|
||||
if (resultItem.type === "json" && resultItem.json) {
|
||||
return { json: resultItem.json } as ToolResultContentBlock;
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.filter((content): content is ToolResultContentBlock => content !== null);
|
||||
|
||||
if (toolResultContent.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
toolResult: {
|
||||
toolUseId: item.tool_result.tool_use_id,
|
||||
content: toolResultContent,
|
||||
status: item.tool_result.status,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function convertContentToAWS(content: string | ContentItem[]): ContentBlock[] {
|
||||
if (typeof content === "string") {
|
||||
return [{ text: content }];
|
||||
}
|
||||
|
||||
const blocks = content
|
||||
.map(convertContentToAWSBlock)
|
||||
.filter((block): block is ContentBlock => block !== null);
|
||||
|
||||
return blocks.length > 0 ? blocks : [{ text: "" }];
|
||||
}
|
||||
|
||||
function formatMessages(messages: ConverseRequest["messages"]): {
|
||||
messages: Message[];
|
||||
systemPrompt?: SystemContentBlock[];
|
||||
} {
|
||||
const systemMessages = messages.filter((msg) => msg.role === "system");
|
||||
const nonSystemMessages = messages.filter((msg) => msg.role !== "system");
|
||||
|
||||
const systemPrompt =
|
||||
systemMessages.length > 0
|
||||
? systemMessages.map((msg) => {
|
||||
if (typeof msg.content === "string") {
|
||||
return { text: msg.content } as SystemContentBlock;
|
||||
}
|
||||
const blocks = convertContentToAWS(msg.content);
|
||||
return blocks[0] as SystemContentBlock;
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const formattedMessages = nonSystemMessages.reduce(
|
||||
(acc: Message[], curr, idx) => {
|
||||
if (idx > 0 && curr.role === nonSystemMessages[idx - 1].role) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
const content = convertContentToAWS(curr.content);
|
||||
if (content.length > 0) {
|
||||
acc.push({
|
||||
role: curr.role as "user" | "assistant",
|
||||
content,
|
||||
});
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
if (formattedMessages.length === 0 || formattedMessages[0].role !== "user") {
|
||||
formattedMessages.unshift({
|
||||
role: "user",
|
||||
content: [{ text: "Hello" }],
|
||||
});
|
||||
}
|
||||
|
||||
if (formattedMessages[formattedMessages.length - 1].role !== "user") {
|
||||
formattedMessages.push({
|
||||
role: "user",
|
||||
content: [{ text: "Continue" }],
|
||||
});
|
||||
}
|
||||
|
||||
return { messages: formattedMessages, systemPrompt };
|
||||
function supportsToolUse(modelId: string): boolean {
|
||||
// llama和mistral模型不支持工具调用
|
||||
return modelId.toLowerCase().includes("claude-3");
|
||||
}
|
||||
|
||||
function formatRequestBody(
|
||||
request: ConverseRequest,
|
||||
): ConverseStreamCommandInput {
|
||||
const { messages, systemPrompt } = formatMessages(request.messages);
|
||||
const messages: Message[] = request.messages.map((msg) => ({
|
||||
role: msg.role === "system" ? "user" : msg.role,
|
||||
content: Array.isArray(msg.content)
|
||||
? msg.content.map((item) => {
|
||||
if (item.type === "tool_use") {
|
||||
return {
|
||||
toolUse: {
|
||||
toolUseId: item.id,
|
||||
name: item.name,
|
||||
input: item.input || "{}",
|
||||
},
|
||||
} as ContentBlock;
|
||||
}
|
||||
if (item.type === "tool_result") {
|
||||
return {
|
||||
toolResult: {
|
||||
toolUseId: item.tool_use_id,
|
||||
content: [{ text: item.content || ";" }],
|
||||
status: "success",
|
||||
},
|
||||
} as ContentBlock;
|
||||
}
|
||||
if (item.type === "text") {
|
||||
return { text: item.text || ";" } as ContentBlock;
|
||||
}
|
||||
if (item.type === "image") {
|
||||
return {
|
||||
image: {
|
||||
format: item.source.media_type.split("/")[1] as
|
||||
| "png"
|
||||
| "jpeg"
|
||||
| "gif"
|
||||
| "webp",
|
||||
source: {
|
||||
bytes: Uint8Array.from(
|
||||
Buffer.from(item.source.data, "base64"),
|
||||
),
|
||||
},
|
||||
},
|
||||
} as ContentBlock;
|
||||
}
|
||||
return { text: ";" } as ContentBlock;
|
||||
})
|
||||
: [{ text: msg.content || ";" } as ContentBlock],
|
||||
}));
|
||||
|
||||
const input: ConverseStreamCommandInput = {
|
||||
modelId: request.modelId,
|
||||
messages,
|
||||
...(systemPrompt && { system: systemPrompt }),
|
||||
...(request.inferenceConfig && {
|
||||
inferenceConfig: request.inferenceConfig,
|
||||
}),
|
||||
};
|
||||
|
||||
if (request.inferenceConfig) {
|
||||
input.inferenceConfig = {
|
||||
maxTokens: request.inferenceConfig.maxTokens,
|
||||
temperature: request.inferenceConfig.temperature,
|
||||
topP: request.inferenceConfig.topP,
|
||||
stopSequences: request.inferenceConfig.stopSequences,
|
||||
};
|
||||
}
|
||||
|
||||
if (request.toolConfig) {
|
||||
// 只有在支持工具调用的模型上才添加toolConfig
|
||||
if (request.tools?.length && supportsToolUse(request.modelId)) {
|
||||
input.toolConfig = {
|
||||
tools: request.toolConfig.tools,
|
||||
toolChoice: request.toolConfig.toolChoice,
|
||||
tools: request.tools.map((tool) => ({
|
||||
toolSpec: {
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
inputSchema: {
|
||||
json: tool.input_schema,
|
||||
},
|
||||
},
|
||||
})),
|
||||
toolChoice: { auto: {} },
|
||||
};
|
||||
}
|
||||
|
||||
const logInput = {
|
||||
...input,
|
||||
messages: messages.map((msg) => ({
|
||||
role: msg.role,
|
||||
content: msg.content?.map((content) => {
|
||||
if ("image" in content && content.image) {
|
||||
return {
|
||||
image: {
|
||||
format: content.image.format,
|
||||
source: { bytes: "[BINARY]" },
|
||||
},
|
||||
};
|
||||
}
|
||||
if ("document" in content && content.document) {
|
||||
return {
|
||||
document: { ...content.document, source: { bytes: "[BINARY]" } },
|
||||
};
|
||||
}
|
||||
return content;
|
||||
}),
|
||||
})),
|
||||
};
|
||||
|
||||
console.log(
|
||||
"[Bedrock] Formatted request:",
|
||||
JSON.stringify(logInput, null, 2),
|
||||
);
|
||||
return input;
|
||||
}
|
||||
|
||||
// Main Request Handler
|
||||
export async function handle(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { path: string[] } },
|
||||
) {
|
||||
console.log("[Bedrock Route] params ", params);
|
||||
|
||||
if (req.method === "OPTIONS") {
|
||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||
}
|
||||
|
||||
const subpath = params.path.join("/");
|
||||
|
||||
if (!ALLOWED_PATH.has(subpath)) {
|
||||
console.log("[Bedrock Route] forbidden path ", subpath);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: true,
|
||||
msg: "you are not allowed to request " + subpath,
|
||||
},
|
||||
{
|
||||
status: 403,
|
||||
},
|
||||
{ error: true, msg: "Path not allowed: " + subpath },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
const serverConfig = getServerSideConfig();
|
||||
|
||||
// 首先尝试使用环境变量中的凭证
|
||||
let region = serverConfig.awsRegion;
|
||||
let accessKeyId = serverConfig.awsAccessKey;
|
||||
let secretAccessKey = serverConfig.awsSecretKey;
|
||||
let sessionToken = undefined;
|
||||
|
||||
// 如果环境变量中没有配置,则尝试使用前端传来的加密凭证
|
||||
if (!region || !accessKeyId || !secretAccessKey) {
|
||||
// 解密前端传来的凭证
|
||||
region = decrypt(req.headers.get("X-Region") ?? "");
|
||||
accessKeyId = decrypt(req.headers.get("X-Access-Key") ?? "");
|
||||
secretAccessKey = decrypt(req.headers.get("X-Secret-Key") ?? "");
|
||||
@@ -367,51 +153,18 @@ export async function handle(
|
||||
|
||||
if (!region || !accessKeyId || !secretAccessKey) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: true,
|
||||
msg: "AWS credentials not found in environment variables or request headers",
|
||||
},
|
||||
{
|
||||
status: 401,
|
||||
},
|
||||
{ error: true, msg: "Missing AWS credentials" },
|
||||
{ status: 401 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const client = new BedrockRuntimeClient({
|
||||
region,
|
||||
credentials: {
|
||||
accessKeyId,
|
||||
secretAccessKey,
|
||||
sessionToken,
|
||||
},
|
||||
credentials: { accessKeyId, secretAccessKey, sessionToken },
|
||||
});
|
||||
|
||||
const response = await handleConverseRequest(req, client);
|
||||
return response;
|
||||
} catch (e) {
|
||||
console.error("[Bedrock] ", e);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: true,
|
||||
message: e instanceof Error ? e.message : "Unknown error",
|
||||
details: prettyObject(e),
|
||||
},
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleConverseRequest(
|
||||
req: NextRequest,
|
||||
client: BedrockRuntimeClient,
|
||||
) {
|
||||
try {
|
||||
const body = (await req.json()) as ConverseRequest;
|
||||
const { modelId } = body;
|
||||
|
||||
console.log("[Bedrock] Invoking model:", modelId);
|
||||
|
||||
const command = new ConverseStreamCommand(formatRequestBody(body));
|
||||
const response = await client.send(command);
|
||||
|
||||
@@ -422,128 +175,71 @@ async function handleConverseRequest(
|
||||
const stream = new ReadableStream({
|
||||
async start(controller) {
|
||||
try {
|
||||
const responseStream = response.stream;
|
||||
if (!responseStream) {
|
||||
throw new Error("No stream in response");
|
||||
}
|
||||
|
||||
const responseStream =
|
||||
response.stream as AsyncIterable<ConverseStreamOutput>;
|
||||
for await (const event of responseStream) {
|
||||
const output = event as ConverseStreamOutput;
|
||||
if (
|
||||
"contentBlockStart" in event &&
|
||||
event.contentBlockStart?.start?.toolUse &&
|
||||
event.contentBlockStart.contentBlockIndex !== undefined
|
||||
) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
type: "content_block",
|
||||
content_block: {
|
||||
type: "tool_use",
|
||||
id: event.contentBlockStart.start.toolUse.toolUseId,
|
||||
name: event.contentBlockStart.start.toolUse.name,
|
||||
},
|
||||
index: event.contentBlockStart.contentBlockIndex,
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if (
|
||||
"contentBlockDelta" in event &&
|
||||
event.contentBlockDelta?.delta &&
|
||||
event.contentBlockDelta.contentBlockIndex !== undefined
|
||||
) {
|
||||
const delta = event.contentBlockDelta.delta;
|
||||
|
||||
if ("messageStart" in output && output.messageStart?.role) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
messageStart: { role: output.messageStart.role },
|
||||
},
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if (
|
||||
"contentBlockStart" in output &&
|
||||
output.contentBlockStart
|
||||
) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
contentBlockStart: {
|
||||
contentBlockIndex:
|
||||
output.contentBlockStart.contentBlockIndex,
|
||||
start: output.contentBlockStart.start,
|
||||
},
|
||||
},
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if (
|
||||
"contentBlockDelta" in output &&
|
||||
output.contentBlockDelta?.delta
|
||||
) {
|
||||
if ("text" in output.contentBlockDelta.delta) {
|
||||
if ("text" in delta && delta.text) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
contentBlockDelta: {
|
||||
delta: { text: output.contentBlockDelta.delta.text },
|
||||
contentBlockIndex:
|
||||
output.contentBlockDelta.contentBlockIndex,
|
||||
},
|
||||
type: "content_block_delta",
|
||||
delta: {
|
||||
type: "text_delta",
|
||||
text: delta.text,
|
||||
},
|
||||
index: event.contentBlockDelta.contentBlockIndex,
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if ("toolUse" in output.contentBlockDelta.delta) {
|
||||
} else if ("toolUse" in delta && delta.toolUse?.input) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
contentBlockDelta: {
|
||||
delta: {
|
||||
toolUse: {
|
||||
input:
|
||||
output.contentBlockDelta.delta.toolUse?.input,
|
||||
},
|
||||
},
|
||||
contentBlockIndex:
|
||||
output.contentBlockDelta.contentBlockIndex,
|
||||
},
|
||||
type: "content_block_delta",
|
||||
delta: {
|
||||
type: "input_json_delta",
|
||||
partial_json: delta.toolUse.input,
|
||||
},
|
||||
index: event.contentBlockDelta.contentBlockIndex,
|
||||
})}\n\n`,
|
||||
);
|
||||
}
|
||||
} else if (
|
||||
"contentBlockStop" in output &&
|
||||
output.contentBlockStop
|
||||
"contentBlockStop" in event &&
|
||||
event.contentBlockStop?.contentBlockIndex !== undefined
|
||||
) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
contentBlockStop: {
|
||||
contentBlockIndex:
|
||||
output.contentBlockStop.contentBlockIndex,
|
||||
},
|
||||
},
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if ("messageStop" in output && output.messageStop) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
messageStop: {
|
||||
stopReason: output.messageStop.stopReason,
|
||||
additionalModelResponseFields:
|
||||
output.messageStop.additionalModelResponseFields,
|
||||
},
|
||||
},
|
||||
})}\n\n`,
|
||||
);
|
||||
} else if ("metadata" in output && output.metadata) {
|
||||
controller.enqueue(
|
||||
`data: ${JSON.stringify({
|
||||
stream: {
|
||||
metadata: {
|
||||
usage: output.metadata.usage,
|
||||
metrics: output.metadata.metrics,
|
||||
trace: output.metadata.trace,
|
||||
},
|
||||
},
|
||||
type: "content_block_stop",
|
||||
index: event.contentBlockStop.contentBlockIndex,
|
||||
})}\n\n`,
|
||||
);
|
||||
}
|
||||
}
|
||||
controller.close();
|
||||
} catch (error) {
|
||||
const errorResponse = {
|
||||
stream: {
|
||||
error:
|
||||
error instanceof Error
|
||||
? error.constructor.name
|
||||
: "UnknownError",
|
||||
message: error instanceof Error ? error.message : "Unknown error",
|
||||
...(error instanceof ModelStreamErrorException && {
|
||||
originalStatusCode: error.originalStatusCode,
|
||||
originalMessage: error.originalMessage,
|
||||
}),
|
||||
},
|
||||
};
|
||||
controller.enqueue(`data: ${JSON.stringify(errorResponse)}\n\n`);
|
||||
controller.close();
|
||||
console.error("[Bedrock] Stream error:", error);
|
||||
controller.error(error);
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -555,8 +251,15 @@ async function handleConverseRequest(
|
||||
Connection: "keep-alive",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("[Bedrock] Request error:", error);
|
||||
throw error;
|
||||
} catch (e) {
|
||||
console.error("[Bedrock] Error:", e);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: true,
|
||||
message: e instanceof Error ? e.message : "Unknown error",
|
||||
details: prettyObject(e),
|
||||
},
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
129
app/api/glm.ts
Normal file
129
app/api/glm.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import { getServerSideConfig } from "@/app/config/server";
|
||||
import {
|
||||
CHATGLM_BASE_URL,
|
||||
ApiPath,
|
||||
ModelProvider,
|
||||
ServiceProvider,
|
||||
} from "@/app/constant";
|
||||
import { prettyObject } from "@/app/utils/format";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/app/api/auth";
|
||||
import { isModelAvailableInServer } from "@/app/utils/model";
|
||||
|
||||
const serverConfig = getServerSideConfig();
|
||||
|
||||
export async function handle(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { path: string[] } },
|
||||
) {
|
||||
console.log("[GLM Route] params ", params);
|
||||
|
||||
if (req.method === "OPTIONS") {
|
||||
return NextResponse.json({ body: "OK" }, { status: 200 });
|
||||
}
|
||||
|
||||
const authResult = auth(req, ModelProvider.ChatGLM);
|
||||
if (authResult.error) {
|
||||
return NextResponse.json(authResult, {
|
||||
status: 401,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await request(req);
|
||||
return response;
|
||||
} catch (e) {
|
||||
console.error("[GLM] ", e);
|
||||
return NextResponse.json(prettyObject(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function request(req: NextRequest) {
|
||||
const controller = new AbortController();
|
||||
|
||||
// alibaba use base url or just remove the path
|
||||
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.ChatGLM, "");
|
||||
|
||||
let baseUrl = serverConfig.chatglmUrl || CHATGLM_BASE_URL;
|
||||
|
||||
if (!baseUrl.startsWith("http")) {
|
||||
baseUrl = `https://${baseUrl}`;
|
||||
}
|
||||
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.slice(0, -1);
|
||||
}
|
||||
|
||||
console.log("[Proxy] ", path);
|
||||
console.log("[Base Url]", baseUrl);
|
||||
|
||||
const timeoutId = setTimeout(
|
||||
() => {
|
||||
controller.abort();
|
||||
},
|
||||
10 * 60 * 1000,
|
||||
);
|
||||
|
||||
const fetchUrl = `${baseUrl}${path}`;
|
||||
console.log("[Fetch Url] ", fetchUrl);
|
||||
const fetchOptions: RequestInit = {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: req.headers.get("Authorization") ?? "",
|
||||
},
|
||||
method: req.method,
|
||||
body: req.body,
|
||||
redirect: "manual",
|
||||
// @ts-ignore
|
||||
duplex: "half",
|
||||
signal: controller.signal,
|
||||
};
|
||||
|
||||
// #1815 try to refuse some request to some models
|
||||
if (serverConfig.customModels && req.body) {
|
||||
try {
|
||||
const clonedBody = await req.text();
|
||||
fetchOptions.body = clonedBody;
|
||||
|
||||
const jsonBody = JSON.parse(clonedBody) as { model?: string };
|
||||
|
||||
// not undefined and is false
|
||||
if (
|
||||
isModelAvailableInServer(
|
||||
serverConfig.customModels,
|
||||
jsonBody?.model as string,
|
||||
ServiceProvider.ChatGLM as string,
|
||||
)
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: true,
|
||||
message: `you are not allowed to use ${jsonBody?.model} model`,
|
||||
},
|
||||
{
|
||||
status: 403,
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[GLM] filter`, e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
const res = await fetch(fetchUrl, fetchOptions);
|
||||
|
||||
// to prevent browser prompt for credentials
|
||||
const newHeaders = new Headers(res.headers);
|
||||
newHeaders.delete("www-authenticate");
|
||||
// to disable nginx buffering
|
||||
newHeaders.set("X-Accel-Buffering", "no");
|
||||
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: newHeaders,
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { HunyuanApi } from "./platforms/tencent";
|
||||
import { MoonshotApi } from "./platforms/moonshot";
|
||||
import { SparkApi } from "./platforms/iflytek";
|
||||
import { XAIApi } from "./platforms/xai";
|
||||
import { ChatGLMApi } from "./platforms/glm";
|
||||
|
||||
export const ROLES = ["system", "user", "assistant"] as const;
|
||||
export type MessageRole = (typeof ROLES)[number];
|
||||
@@ -78,7 +79,7 @@ export interface ChatOptions {
|
||||
config: LLMConfig;
|
||||
|
||||
onUpdate?: (message: string, chunk: string) => void;
|
||||
onFinish: (message: string) => void;
|
||||
onFinish: (message: string, responseRes: Response) => void;
|
||||
onError?: (err: Error) => void;
|
||||
onController?: (controller: AbortController) => void;
|
||||
onBeforeTool?: (tool: ChatMessageTool) => void;
|
||||
@@ -168,6 +169,9 @@ export class ClientApi {
|
||||
case ModelProvider.XAI:
|
||||
this.llm = new XAIApi();
|
||||
break;
|
||||
case ModelProvider.ChatGLM:
|
||||
this.llm = new ChatGLMApi();
|
||||
break;
|
||||
default:
|
||||
this.llm = new ChatGPTApi();
|
||||
}
|
||||
@@ -257,6 +261,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
|
||||
const isMoonshot = modelConfig.providerName === ServiceProvider.Moonshot;
|
||||
const isIflytek = modelConfig.providerName === ServiceProvider.Iflytek;
|
||||
const isXAI = modelConfig.providerName === ServiceProvider.XAI;
|
||||
const isChatGLM = modelConfig.providerName === ServiceProvider.ChatGLM;
|
||||
const isEnabledAccessControl = accessStore.enabledAccessControl();
|
||||
const apiKey = isGoogle
|
||||
? accessStore.googleApiKey
|
||||
@@ -274,6 +279,8 @@ export function getHeaders(ignoreHeaders: boolean = false) {
|
||||
? accessStore.moonshotApiKey
|
||||
: isXAI
|
||||
? accessStore.xaiApiKey
|
||||
: isChatGLM
|
||||
? accessStore.chatglmApiKey
|
||||
: isIflytek
|
||||
? accessStore.iflytekApiKey && accessStore.iflytekApiSecret
|
||||
? accessStore.iflytekApiKey + ":" + accessStore.iflytekApiSecret
|
||||
@@ -290,6 +297,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
|
||||
isMoonshot,
|
||||
isIflytek,
|
||||
isXAI,
|
||||
isChatGLM,
|
||||
apiKey,
|
||||
isEnabledAccessControl,
|
||||
};
|
||||
@@ -372,6 +380,8 @@ export function getClientApi(provider: ServiceProvider): ClientApi {
|
||||
return new ClientApi(ModelProvider.Iflytek);
|
||||
case ServiceProvider.XAI:
|
||||
return new ClientApi(ModelProvider.XAI);
|
||||
case ServiceProvider.ChatGLM:
|
||||
return new ClientApi(ModelProvider.ChatGLM);
|
||||
default:
|
||||
return new ClientApi(ModelProvider.GPT);
|
||||
}
|
||||
|
||||
@@ -143,6 +143,7 @@ export class QwenApi implements LLMApi {
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
let responseRes: Response;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
function animateResponseText() {
|
||||
@@ -172,7 +173,7 @@ export class QwenApi implements LLMApi {
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -188,6 +189,7 @@ export class QwenApi implements LLMApi {
|
||||
"[Alibaba] request response content type: ",
|
||||
contentType,
|
||||
);
|
||||
responseRes = res;
|
||||
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
@@ -254,7 +256,7 @@ export class QwenApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -317,13 +317,14 @@ export class ClaudeApi implements LLMApi {
|
||||
};
|
||||
|
||||
try {
|
||||
controller.signal.onabort = () => options.onFinish("");
|
||||
controller.signal.onabort = () =>
|
||||
options.onFinish("", new Response(null, { status: 400 }));
|
||||
|
||||
const res = await fetch(path, payload);
|
||||
const resJson = await res.json();
|
||||
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
} catch (e) {
|
||||
console.error("failed to chat", e);
|
||||
options.onError?.(e as Error);
|
||||
|
||||
@@ -162,6 +162,7 @@ export class ErnieApi implements LLMApi {
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
let responseRes: Response;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
function animateResponseText() {
|
||||
@@ -191,7 +192,7 @@ export class ErnieApi implements LLMApi {
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -204,7 +205,7 @@ export class ErnieApi implements LLMApi {
|
||||
clearTimeout(requestTimeoutId);
|
||||
const contentType = res.headers.get("content-type");
|
||||
console.log("[Baidu] request response content type: ", contentType);
|
||||
|
||||
responseRes = res;
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
@@ -267,7 +268,7 @@ export class ErnieApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = resJson?.result;
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
import { ApiPath } from "../../constant";
|
||||
import {
|
||||
ChatOptions,
|
||||
getHeaders,
|
||||
LLMApi,
|
||||
LLMModel,
|
||||
LLMUsage,
|
||||
SpeechOptions,
|
||||
} from "../api";
|
||||
import { ChatOptions, getHeaders, LLMApi, SpeechOptions } from "../api";
|
||||
import {
|
||||
useAppConfig,
|
||||
usePluginStore,
|
||||
@@ -16,6 +9,7 @@ import {
|
||||
import { getMessageTextContent, isVisionModel } from "../../utils";
|
||||
import { fetch } from "../../utils/stream";
|
||||
import { preProcessImageContent, stream } from "../../utils/chat";
|
||||
import { RequestPayload } from "./openai";
|
||||
|
||||
export type MultiBlockContent = {
|
||||
type: "image" | "text";
|
||||
@@ -39,22 +33,14 @@ const ClaudeMapper = {
|
||||
} as const;
|
||||
|
||||
export class BedrockApi implements LLMApi {
|
||||
usage(): Promise<LLMUsage> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
models(): Promise<LLMModel[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
speech(options: SpeechOptions): Promise<ArrayBuffer> {
|
||||
throw new Error("Speech not implemented for Bedrock.");
|
||||
}
|
||||
|
||||
extractMessage(res: any) {
|
||||
console.log("[Response] bedrock response: ", res);
|
||||
if (Array.isArray(res?.content)) {
|
||||
return res.content;
|
||||
}
|
||||
return res;
|
||||
console.log("[Response] claude response: ", res);
|
||||
|
||||
return res?.content?.[0]?.text;
|
||||
}
|
||||
|
||||
async chat(options: ChatOptions): Promise<void> {
|
||||
@@ -149,34 +135,15 @@ export class BedrockApi implements LLMApi {
|
||||
});
|
||||
}
|
||||
|
||||
const [tools, funcs] = usePluginStore
|
||||
.getState()
|
||||
.getAsTools(useChatStore.getState().currentSession().mask?.plugin || []);
|
||||
|
||||
const requestBody = {
|
||||
modelId: options.config.model,
|
||||
messages: messages.filter((msg) => msg.content.length > 0),
|
||||
messages: prompt,
|
||||
inferenceConfig: {
|
||||
maxTokens: modelConfig.max_tokens,
|
||||
temperature: modelConfig.temperature,
|
||||
topP: modelConfig.top_p,
|
||||
stopSequences: [],
|
||||
},
|
||||
toolConfig:
|
||||
Array.isArray(tools) && tools.length > 0
|
||||
? {
|
||||
tools: tools.map((tool: any) => ({
|
||||
toolSpec: {
|
||||
name: tool?.function?.name,
|
||||
description: tool?.function?.description,
|
||||
inputSchema: {
|
||||
json: tool?.function?.parameters,
|
||||
},
|
||||
},
|
||||
})),
|
||||
toolChoice: { auto: {} },
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
|
||||
const conversePath = `${ApiPath.Bedrock}/converse`;
|
||||
@@ -185,83 +152,79 @@ export class BedrockApi implements LLMApi {
|
||||
|
||||
if (shouldStream) {
|
||||
let currentToolUse: ChatMessageTool | null = null;
|
||||
let index = -1;
|
||||
const [tools, funcs] = usePluginStore
|
||||
.getState()
|
||||
.getAsTools(
|
||||
useChatStore.getState().currentSession().mask?.plugin || [],
|
||||
);
|
||||
return stream(
|
||||
conversePath,
|
||||
requestBody,
|
||||
getHeaders(),
|
||||
Array.isArray(tools)
|
||||
? tools.map((tool: any) => ({
|
||||
name: tool?.function?.name,
|
||||
description: tool?.function?.description,
|
||||
input_schema: tool?.function?.parameters,
|
||||
}))
|
||||
: [],
|
||||
// @ts-ignore
|
||||
tools.map((tool) => ({
|
||||
name: tool?.function?.name,
|
||||
description: tool?.function?.description,
|
||||
input_schema: tool?.function?.parameters,
|
||||
})),
|
||||
funcs,
|
||||
controller,
|
||||
// parseSSE
|
||||
(text: string, runTools: ChatMessageTool[]) => {
|
||||
const parsed = JSON.parse(text);
|
||||
const event = parsed.stream;
|
||||
// console.log("parseSSE", text, runTools);
|
||||
let chunkJson:
|
||||
| undefined
|
||||
| {
|
||||
type: "content_block_delta" | "content_block_stop";
|
||||
content_block?: {
|
||||
type: "tool_use";
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
delta?: {
|
||||
type: "text_delta" | "input_json_delta";
|
||||
text?: string;
|
||||
partial_json?: string;
|
||||
};
|
||||
index: number;
|
||||
};
|
||||
chunkJson = JSON.parse(text);
|
||||
|
||||
if (!event) {
|
||||
console.warn("[Bedrock] Unexpected event format:", parsed);
|
||||
return "";
|
||||
}
|
||||
|
||||
if (event.messageStart) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (event.contentBlockStart?.start?.toolUse) {
|
||||
const { toolUseId, name } = event.contentBlockStart.start.toolUse;
|
||||
currentToolUse = {
|
||||
id: toolUseId,
|
||||
if (chunkJson?.content_block?.type == "tool_use") {
|
||||
index += 1;
|
||||
const id = chunkJson?.content_block.id;
|
||||
const name = chunkJson?.content_block.name;
|
||||
runTools.push({
|
||||
id,
|
||||
type: "function",
|
||||
function: {
|
||||
name,
|
||||
arguments: "",
|
||||
},
|
||||
};
|
||||
runTools.push(currentToolUse);
|
||||
return "";
|
||||
});
|
||||
}
|
||||
|
||||
if (event.contentBlockDelta?.delta?.text) {
|
||||
return event.contentBlockDelta.delta.text;
|
||||
}
|
||||
|
||||
if (
|
||||
event.contentBlockDelta?.delta?.toolUse?.input &&
|
||||
currentToolUse?.function
|
||||
chunkJson?.delta?.type == "input_json_delta" &&
|
||||
chunkJson?.delta?.partial_json
|
||||
) {
|
||||
currentToolUse.function.arguments +=
|
||||
event.contentBlockDelta.delta.toolUse.input;
|
||||
return "";
|
||||
// @ts-ignore
|
||||
runTools[index]["function"]["arguments"] +=
|
||||
chunkJson?.delta?.partial_json;
|
||||
}
|
||||
|
||||
if (
|
||||
event.internalServerException ||
|
||||
event.modelStreamErrorException ||
|
||||
event.validationException ||
|
||||
event.throttlingException ||
|
||||
event.serviceUnavailableException
|
||||
) {
|
||||
const errorMessage =
|
||||
event.internalServerException?.message ||
|
||||
event.modelStreamErrorException?.message ||
|
||||
event.validationException?.message ||
|
||||
event.throttlingException?.message ||
|
||||
event.serviceUnavailableException?.message ||
|
||||
"Unknown error";
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
return "";
|
||||
return chunkJson?.delta?.text;
|
||||
},
|
||||
// processToolMessage
|
||||
(requestPayload: any, toolCallMessage: any, toolCallResult: any[]) => {
|
||||
currentToolUse = null;
|
||||
// processToolMessage, include tool_calls message and tool call results
|
||||
(
|
||||
requestPayload: RequestPayload,
|
||||
toolCallMessage: any,
|
||||
toolCallResult: any[],
|
||||
) => {
|
||||
// reset index value
|
||||
index = -1;
|
||||
// @ts-ignore
|
||||
requestPayload?.messages?.splice(
|
||||
// @ts-ignore
|
||||
requestPayload?.messages?.length,
|
||||
0,
|
||||
{
|
||||
@@ -277,6 +240,7 @@ export class BedrockApi implements LLMApi {
|
||||
}),
|
||||
),
|
||||
},
|
||||
// @ts-ignore
|
||||
...toolCallResult.map((result) => ({
|
||||
role: "user",
|
||||
content: [
|
||||
@@ -292,26 +256,37 @@ export class BedrockApi implements LLMApi {
|
||||
options,
|
||||
);
|
||||
} else {
|
||||
const payload = {
|
||||
method: "POST",
|
||||
body: JSON.stringify(requestBody),
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
...getHeaders(), // get common headers
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(conversePath, {
|
||||
method: "POST",
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(requestBody),
|
||||
signal: controller.signal,
|
||||
});
|
||||
controller.signal.onabort = () =>
|
||||
options.onFinish("", new Response(null, { status: 400 }));
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.text();
|
||||
throw new Error(`Bedrock API error: ${error}`);
|
||||
}
|
||||
const res = await fetch(conversePath, payload);
|
||||
const resJson = await res.json();
|
||||
|
||||
const responseBody = await response.json();
|
||||
const content = this.extractMessage(responseBody);
|
||||
options.onFinish(content);
|
||||
} catch (e: any) {
|
||||
console.error("[Bedrock] Chat error:", e);
|
||||
throw e;
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message, res);
|
||||
} catch (e) {
|
||||
console.error("failed to chat", e);
|
||||
options.onError?.(e as Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
async usage() {
|
||||
return {
|
||||
used: 0,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
async models() {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +130,7 @@ export class DoubaoApi implements LLMApi {
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
let responseRes: Response;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
function animateResponseText() {
|
||||
@@ -159,7 +160,7 @@ export class DoubaoApi implements LLMApi {
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -175,7 +176,7 @@ export class DoubaoApi implements LLMApi {
|
||||
"[ByteDance] request response content type: ",
|
||||
contentType,
|
||||
);
|
||||
|
||||
responseRes = res;
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
@@ -241,7 +242,7 @@ export class DoubaoApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
197
app/client/platforms/glm.ts
Normal file
197
app/client/platforms/glm.ts
Normal file
@@ -0,0 +1,197 @@
|
||||
"use client";
|
||||
import {
|
||||
ApiPath,
|
||||
CHATGLM_BASE_URL,
|
||||
ChatGLM,
|
||||
REQUEST_TIMEOUT_MS,
|
||||
} from "@/app/constant";
|
||||
import {
|
||||
useAccessStore,
|
||||
useAppConfig,
|
||||
useChatStore,
|
||||
ChatMessageTool,
|
||||
usePluginStore,
|
||||
} from "@/app/store";
|
||||
import { stream } from "@/app/utils/chat";
|
||||
import {
|
||||
ChatOptions,
|
||||
getHeaders,
|
||||
LLMApi,
|
||||
LLMModel,
|
||||
SpeechOptions,
|
||||
} from "../api";
|
||||
import { getClientConfig } from "@/app/config/client";
|
||||
import { getMessageTextContent } from "@/app/utils";
|
||||
import { RequestPayload } from "./openai";
|
||||
import { fetch } from "@/app/utils/stream";
|
||||
|
||||
export class ChatGLMApi implements LLMApi {
|
||||
private disableListModels = true;
|
||||
|
||||
path(path: string): string {
|
||||
const accessStore = useAccessStore.getState();
|
||||
|
||||
let baseUrl = "";
|
||||
|
||||
if (accessStore.useCustomConfig) {
|
||||
baseUrl = accessStore.chatglmUrl;
|
||||
}
|
||||
|
||||
if (baseUrl.length === 0) {
|
||||
const isApp = !!getClientConfig()?.isApp;
|
||||
const apiPath = ApiPath.ChatGLM;
|
||||
baseUrl = isApp ? CHATGLM_BASE_URL : apiPath;
|
||||
}
|
||||
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.slice(0, baseUrl.length - 1);
|
||||
}
|
||||
if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.ChatGLM)) {
|
||||
baseUrl = "https://" + baseUrl;
|
||||
}
|
||||
|
||||
console.log("[Proxy Endpoint] ", baseUrl, path);
|
||||
|
||||
return [baseUrl, path].join("/");
|
||||
}
|
||||
|
||||
extractMessage(res: any) {
|
||||
return res.choices?.at(0)?.message?.content ?? "";
|
||||
}
|
||||
|
||||
speech(options: SpeechOptions): Promise<ArrayBuffer> {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
async chat(options: ChatOptions) {
|
||||
const messages: ChatOptions["messages"] = [];
|
||||
for (const v of options.messages) {
|
||||
const content = getMessageTextContent(v);
|
||||
messages.push({ role: v.role, content });
|
||||
}
|
||||
|
||||
const modelConfig = {
|
||||
...useAppConfig.getState().modelConfig,
|
||||
...useChatStore.getState().currentSession().mask.modelConfig,
|
||||
...{
|
||||
model: options.config.model,
|
||||
providerName: options.config.providerName,
|
||||
},
|
||||
};
|
||||
|
||||
const requestPayload: RequestPayload = {
|
||||
messages,
|
||||
stream: options.config.stream,
|
||||
model: modelConfig.model,
|
||||
temperature: modelConfig.temperature,
|
||||
presence_penalty: modelConfig.presence_penalty,
|
||||
frequency_penalty: modelConfig.frequency_penalty,
|
||||
top_p: modelConfig.top_p,
|
||||
};
|
||||
|
||||
console.log("[Request] glm payload: ", requestPayload);
|
||||
|
||||
const shouldStream = !!options.config.stream;
|
||||
const controller = new AbortController();
|
||||
options.onController?.(controller);
|
||||
|
||||
try {
|
||||
const chatPath = this.path(ChatGLM.ChatPath);
|
||||
const chatPayload = {
|
||||
method: "POST",
|
||||
body: JSON.stringify(requestPayload),
|
||||
signal: controller.signal,
|
||||
headers: getHeaders(),
|
||||
};
|
||||
|
||||
// make a fetch request
|
||||
const requestTimeoutId = setTimeout(
|
||||
() => controller.abort(),
|
||||
REQUEST_TIMEOUT_MS,
|
||||
);
|
||||
|
||||
if (shouldStream) {
|
||||
const [tools, funcs] = usePluginStore
|
||||
.getState()
|
||||
.getAsTools(
|
||||
useChatStore.getState().currentSession().mask?.plugin || [],
|
||||
);
|
||||
return stream(
|
||||
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;
|
||||
tool_calls: ChatMessageTool[];
|
||||
};
|
||||
}>;
|
||||
const tool_calls = choices[0]?.delta?.tool_calls;
|
||||
if (tool_calls?.length > 0) {
|
||||
const index = tool_calls[0]?.index;
|
||||
const id = tool_calls[0]?.id;
|
||||
const args = tool_calls[0]?.function?.arguments;
|
||||
if (id) {
|
||||
runTools.push({
|
||||
id,
|
||||
type: tool_calls[0]?.type,
|
||||
function: {
|
||||
name: tool_calls[0]?.function?.name as string,
|
||||
arguments: args,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// @ts-ignore
|
||||
runTools[index]["function"]["arguments"] += args;
|
||||
}
|
||||
}
|
||||
return choices[0]?.delta?.content;
|
||||
},
|
||||
// processToolMessage, include tool_calls message and tool call results
|
||||
(
|
||||
requestPayload: RequestPayload,
|
||||
toolCallMessage: any,
|
||||
toolCallResult: any[],
|
||||
) => {
|
||||
// @ts-ignore
|
||||
requestPayload?.messages?.splice(
|
||||
// @ts-ignore
|
||||
requestPayload?.messages?.length,
|
||||
0,
|
||||
toolCallMessage,
|
||||
...toolCallResult,
|
||||
);
|
||||
},
|
||||
options,
|
||||
);
|
||||
} else {
|
||||
const res = await fetch(chatPath, chatPayload);
|
||||
clearTimeout(requestTimeoutId);
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
options.onError?.(e as Error);
|
||||
}
|
||||
}
|
||||
async usage() {
|
||||
return {
|
||||
used: 0,
|
||||
total: 0,
|
||||
};
|
||||
}
|
||||
|
||||
async models(): Promise<LLMModel[]> {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ export class GeminiProApi implements LLMApi {
|
||||
);
|
||||
}
|
||||
const message = apiClient.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -117,6 +117,7 @@ export class SparkApi implements LLMApi {
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
let responseRes: Response;
|
||||
|
||||
// Animate response text to make it look smooth
|
||||
function animateResponseText() {
|
||||
@@ -143,7 +144,7 @@ export class SparkApi implements LLMApi {
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -156,7 +157,7 @@ export class SparkApi implements LLMApi {
|
||||
clearTimeout(requestTimeoutId);
|
||||
const contentType = res.headers.get("content-type");
|
||||
console.log("[Spark] request response content type: ", contentType);
|
||||
|
||||
responseRes = res;
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
@@ -231,7 +232,7 @@ export class SparkApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -180,7 +180,7 @@ export class MoonshotApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -361,7 +361,7 @@ export class ChatGPTApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = await this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -142,6 +142,7 @@ export class HunyuanApi implements LLMApi {
|
||||
let responseText = "";
|
||||
let remainText = "";
|
||||
let finished = false;
|
||||
let responseRes: Response;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
function animateResponseText() {
|
||||
@@ -171,7 +172,7 @@ export class HunyuanApi implements LLMApi {
|
||||
const finish = () => {
|
||||
if (!finished) {
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -187,7 +188,7 @@ export class HunyuanApi implements LLMApi {
|
||||
"[Tencent] request response content type: ",
|
||||
contentType,
|
||||
);
|
||||
|
||||
responseRes = res;
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
return finish();
|
||||
@@ -253,7 +254,7 @@ export class HunyuanApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -173,7 +173,7 @@ export class XAIApi implements LLMApi {
|
||||
|
||||
const resJson = await res.json();
|
||||
const message = this.extractMessage(resJson);
|
||||
options.onFinish(message);
|
||||
options.onFinish(message, res);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[Request] failed to make a chat request", e);
|
||||
|
||||
@@ -1607,7 +1607,7 @@ function _Chat() {
|
||||
title={Locale.Chat.Actions.RefreshTitle}
|
||||
onClick={() => {
|
||||
showToast(Locale.Chat.Actions.RefreshToast);
|
||||
chatStore.summarizeSession(true);
|
||||
chatStore.summarizeSession(true, session);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -72,6 +72,7 @@ import {
|
||||
Stability,
|
||||
Iflytek,
|
||||
SAAS_CHAT_URL,
|
||||
ChatGLM,
|
||||
} from "../constant";
|
||||
import { Prompt, SearchService, usePromptStore } from "../store/prompt";
|
||||
import { ErrorBoundary } from "./error";
|
||||
@@ -1302,6 +1303,47 @@ export function Settings() {
|
||||
</>
|
||||
);
|
||||
|
||||
const chatglmConfigComponent = accessStore.provider ===
|
||||
ServiceProvider.ChatGLM && (
|
||||
<>
|
||||
<ListItem
|
||||
title={Locale.Settings.Access.ChatGLM.Endpoint.Title}
|
||||
subTitle={
|
||||
Locale.Settings.Access.ChatGLM.Endpoint.SubTitle +
|
||||
ChatGLM.ExampleEndpoint
|
||||
}
|
||||
>
|
||||
<input
|
||||
aria-label={Locale.Settings.Access.ChatGLM.Endpoint.Title}
|
||||
type="text"
|
||||
value={accessStore.chatglmUrl}
|
||||
placeholder={ChatGLM.ExampleEndpoint}
|
||||
onChange={(e) =>
|
||||
accessStore.update(
|
||||
(access) => (access.chatglmUrl = e.currentTarget.value),
|
||||
)
|
||||
}
|
||||
></input>
|
||||
</ListItem>
|
||||
<ListItem
|
||||
title={Locale.Settings.Access.ChatGLM.ApiKey.Title}
|
||||
subTitle={Locale.Settings.Access.ChatGLM.ApiKey.SubTitle}
|
||||
>
|
||||
<PasswordInput
|
||||
aria-label={Locale.Settings.Access.ChatGLM.ApiKey.Title}
|
||||
value={accessStore.chatglmApiKey}
|
||||
type="text"
|
||||
placeholder={Locale.Settings.Access.ChatGLM.ApiKey.Placeholder}
|
||||
onChange={(e) => {
|
||||
accessStore.update(
|
||||
(access) => (access.chatglmApiKey = e.currentTarget.value),
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</ListItem>
|
||||
</>
|
||||
);
|
||||
|
||||
const stabilityConfigComponent = accessStore.provider ===
|
||||
ServiceProvider.Stability && (
|
||||
<>
|
||||
@@ -1762,6 +1804,7 @@ export function Settings() {
|
||||
{stabilityConfigComponent}
|
||||
{lflytekConfigComponent}
|
||||
{XAIConfigComponent}
|
||||
{chatglmConfigComponent}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -80,6 +80,10 @@ declare global {
|
||||
XAI_URL?: string;
|
||||
XAI_API_KEY?: string;
|
||||
|
||||
// chatglm only
|
||||
CHATGLM_URL?: string;
|
||||
CHATGLM_API_KEY?: string;
|
||||
|
||||
// custom template for preprocessing user input
|
||||
DEFAULT_INPUT_TEMPLATE?: string;
|
||||
}
|
||||
@@ -156,6 +160,7 @@ export const getServerSideConfig = () => {
|
||||
const isMoonshot = !!process.env.MOONSHOT_API_KEY;
|
||||
const isIflytek = !!process.env.IFLYTEK_API_KEY;
|
||||
const isXAI = !!process.env.XAI_API_KEY;
|
||||
const isChatGLM = !!process.env.CHATGLM_API_KEY;
|
||||
// const apiKeyEnvVar = process.env.OPENAI_API_KEY ?? "";
|
||||
// const apiKeys = apiKeyEnvVar.split(",").map((v) => v.trim());
|
||||
// const randomIndex = Math.floor(Math.random() * apiKeys.length);
|
||||
@@ -227,6 +232,10 @@ export const getServerSideConfig = () => {
|
||||
xaiUrl: process.env.XAI_URL,
|
||||
xaiApiKey: getApiKey(process.env.XAI_API_KEY),
|
||||
|
||||
isChatGLM,
|
||||
chatglmUrl: process.env.CHATGLM_URL,
|
||||
chatglmApiKey: getApiKey(process.env.CHATGLM_API_KEY),
|
||||
|
||||
cloudflareAccountId: process.env.CLOUDFLARE_ACCOUNT_ID,
|
||||
cloudflareKVNamespaceId: process.env.CLOUDFLARE_KV_NAMESPACE_ID,
|
||||
cloudflareKVApiKey: getApiKey(process.env.CLOUDFLARE_KV_API_KEY),
|
||||
|
||||
@@ -32,6 +32,8 @@ export const IFLYTEK_BASE_URL = "https://spark-api-open.xf-yun.com";
|
||||
|
||||
export const XAI_BASE_URL = "https://api.x.ai";
|
||||
|
||||
export const CHATGLM_BASE_URL = "https://open.bigmodel.cn";
|
||||
|
||||
export const CACHE_URL_PREFIX = "/api/cache";
|
||||
export const UPLOAD_URL = `${CACHE_URL_PREFIX}/upload`;
|
||||
|
||||
@@ -65,6 +67,7 @@ export enum ApiPath {
|
||||
Stability = "/api/stability",
|
||||
Artifacts = "/api/artifacts",
|
||||
XAI = "/api/xai",
|
||||
ChatGLM = "/api/chatglm",
|
||||
}
|
||||
|
||||
export enum SlotID {
|
||||
@@ -119,6 +122,7 @@ export enum ServiceProvider {
|
||||
Iflytek = "Iflytek",
|
||||
XAI = "XAI",
|
||||
Bedrock = "Bedrock",
|
||||
ChatGLM = "ChatGLM",
|
||||
}
|
||||
|
||||
// Google API safety settings, see https://ai.google.dev/gemini-api/docs/safety-settings
|
||||
@@ -143,6 +147,7 @@ export enum ModelProvider {
|
||||
Moonshot = "Moonshot",
|
||||
Iflytek = "Iflytek",
|
||||
XAI = "XAI",
|
||||
ChatGLM = "ChatGLM",
|
||||
}
|
||||
|
||||
export const Stability = {
|
||||
@@ -230,6 +235,11 @@ export const XAI = {
|
||||
ChatPath: "v1/chat/completions",
|
||||
};
|
||||
|
||||
export const ChatGLM = {
|
||||
ExampleEndpoint: CHATGLM_BASE_URL,
|
||||
ChatPath: "/api/paas/v4/chat/completions",
|
||||
};
|
||||
|
||||
export const Bedrock = {
|
||||
ChatPath: "converse",
|
||||
};
|
||||
@@ -342,11 +352,12 @@ const anthropicModels = [
|
||||
"claude-2.1",
|
||||
"claude-3-sonnet-20240229",
|
||||
"claude-3-opus-20240229",
|
||||
"claude-3-opus-latest",
|
||||
"claude-3-haiku-20240307",
|
||||
"claude-3-5-sonnet-20240620",
|
||||
"claude-3-5-sonnet-20241022",
|
||||
"claude-3-5-sonnet-latest",
|
||||
"claude-3-opus-latest",
|
||||
"claude-3-5-haiku-latest",
|
||||
];
|
||||
|
||||
const baiduModels = [
|
||||
@@ -404,6 +415,17 @@ const iflytekModels = [
|
||||
|
||||
const xAIModes = ["grok-beta"];
|
||||
|
||||
const chatglmModels = [
|
||||
"glm-4-plus",
|
||||
"glm-4-0520",
|
||||
"glm-4",
|
||||
"glm-4-air",
|
||||
"glm-4-airx",
|
||||
"glm-4-long",
|
||||
"glm-4-flashx",
|
||||
"glm-4-flash",
|
||||
];
|
||||
|
||||
let seq = 1000; // 内置的模型序号生成器从1000开始
|
||||
export const DEFAULT_MODELS = [
|
||||
...openaiModels.map((name) => ({
|
||||
@@ -527,6 +549,19 @@ export const DEFAULT_MODELS = [
|
||||
sorted: 11,
|
||||
},
|
||||
})),
|
||||
|
||||
...chatglmModels.map((name) => ({
|
||||
name,
|
||||
available: true,
|
||||
sorted: seq++,
|
||||
provider: {
|
||||
id: "chatglm",
|
||||
providerName: "ChatGLM",
|
||||
providerType: "chatglm",
|
||||
sorted: 12,
|
||||
},
|
||||
})),
|
||||
|
||||
...bedrockModels.map((name) => ({
|
||||
name,
|
||||
available: true,
|
||||
|
||||
@@ -500,6 +500,17 @@ const cn = {
|
||||
SubTitle: "样例:",
|
||||
},
|
||||
},
|
||||
ChatGLM: {
|
||||
ApiKey: {
|
||||
Title: "接口密钥",
|
||||
SubTitle: "使用自定义 ChatGLM API Key",
|
||||
Placeholder: "ChatGLM API Key",
|
||||
},
|
||||
Endpoint: {
|
||||
Title: "接口地址",
|
||||
SubTitle: "样例:",
|
||||
},
|
||||
},
|
||||
Stability: {
|
||||
ApiKey: {
|
||||
Title: "接口密钥",
|
||||
|
||||
@@ -484,6 +484,17 @@ const en: LocaleType = {
|
||||
SubTitle: "Example: ",
|
||||
},
|
||||
},
|
||||
ChatGLM: {
|
||||
ApiKey: {
|
||||
Title: "ChatGLM API Key",
|
||||
SubTitle: "Use a custom ChatGLM API Key",
|
||||
Placeholder: "ChatGLM API Key",
|
||||
},
|
||||
Endpoint: {
|
||||
Title: "Endpoint Address",
|
||||
SubTitle: "Example: ",
|
||||
},
|
||||
},
|
||||
Stability: {
|
||||
ApiKey: {
|
||||
Title: "Stability API Key",
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
STABILITY_BASE_URL,
|
||||
IFLYTEK_BASE_URL,
|
||||
XAI_BASE_URL,
|
||||
CHATGLM_BASE_URL,
|
||||
} from "../constant";
|
||||
import { getHeaders } from "../client/api";
|
||||
import { getClientConfig } from "../config/client";
|
||||
@@ -49,6 +50,8 @@ const DEFAULT_IFLYTEK_URL = isApp ? IFLYTEK_BASE_URL : ApiPath.Iflytek;
|
||||
|
||||
const DEFAULT_XAI_URL = isApp ? XAI_BASE_URL : ApiPath.XAI;
|
||||
|
||||
const DEFAULT_CHATGLM_URL = isApp ? CHATGLM_BASE_URL : ApiPath.ChatGLM;
|
||||
|
||||
const DEFAULT_ACCESS_STATE = {
|
||||
accessCode: "",
|
||||
useCustomConfig: false,
|
||||
@@ -117,6 +120,10 @@ const DEFAULT_ACCESS_STATE = {
|
||||
xaiUrl: DEFAULT_XAI_URL,
|
||||
xaiApiKey: "",
|
||||
|
||||
// chatglm
|
||||
chatglmUrl: DEFAULT_CHATGLM_URL,
|
||||
chatglmApiKey: "",
|
||||
|
||||
// server config
|
||||
needCode: true,
|
||||
hideUserApiKey: false,
|
||||
@@ -193,6 +200,10 @@ export const useAccessStore = createPersistStore(
|
||||
return ensure(get(), ["xaiApiKey"]);
|
||||
},
|
||||
|
||||
isValidChatGLM() {
|
||||
return ensure(get(), ["chatglmApiKey"]);
|
||||
},
|
||||
|
||||
isAuthorized() {
|
||||
this.fetch();
|
||||
|
||||
@@ -210,6 +221,7 @@ export const useAccessStore = createPersistStore(
|
||||
this.isValidMoonshot() ||
|
||||
this.isValidIflytek() ||
|
||||
this.isValidXAI() ||
|
||||
this.isValidChatGLM() ||
|
||||
!this.enabledAccessControl() ||
|
||||
(this.enabledAccessControl() && ensure(get(), ["accessCode"]))
|
||||
);
|
||||
|
||||
@@ -352,13 +352,13 @@ export const useChatStore = createPersistStore(
|
||||
return session;
|
||||
},
|
||||
|
||||
onNewMessage(message: ChatMessage) {
|
||||
get().updateCurrentSession((session) => {
|
||||
onNewMessage(message: ChatMessage, targetSession: ChatSession) {
|
||||
get().updateTargetSession(targetSession, (session) => {
|
||||
session.messages = session.messages.concat();
|
||||
session.lastUpdate = Date.now();
|
||||
});
|
||||
get().updateStat(message);
|
||||
get().summarizeSession();
|
||||
get().summarizeSession(false, targetSession);
|
||||
},
|
||||
|
||||
async onUserInput(content: string, attachImages?: string[]) {
|
||||
@@ -428,7 +428,7 @@ export const useChatStore = createPersistStore(
|
||||
botMessage.streaming = false;
|
||||
if (message) {
|
||||
botMessage.content = message;
|
||||
get().onNewMessage(botMessage);
|
||||
get().onNewMessage(botMessage, session);
|
||||
}
|
||||
ChatControllerPool.remove(session.id, botMessage.id);
|
||||
},
|
||||
@@ -598,9 +598,12 @@ export const useChatStore = createPersistStore(
|
||||
});
|
||||
},
|
||||
|
||||
summarizeSession(refreshTitle: boolean = false) {
|
||||
summarizeSession(
|
||||
refreshTitle: boolean = false,
|
||||
targetSession: ChatSession,
|
||||
) {
|
||||
const config = useAppConfig.getState();
|
||||
const session = get().currentSession();
|
||||
const session = targetSession;
|
||||
const modelConfig = session.mask.modelConfig;
|
||||
// skip summarize when using dalle3?
|
||||
if (isDalle3(modelConfig.model)) {
|
||||
@@ -649,13 +652,15 @@ export const useChatStore = createPersistStore(
|
||||
stream: false,
|
||||
providerName,
|
||||
},
|
||||
onFinish(message) {
|
||||
if (!isValidMessage(message)) return;
|
||||
get().updateCurrentSession(
|
||||
(session) =>
|
||||
(session.topic =
|
||||
message.length > 0 ? trimTopic(message) : DEFAULT_TOPIC),
|
||||
);
|
||||
onFinish(message, responseRes) {
|
||||
if (responseRes?.status === 200) {
|
||||
get().updateTargetSession(
|
||||
session,
|
||||
(session) =>
|
||||
(session.topic =
|
||||
message.length > 0 ? trimTopic(message) : DEFAULT_TOPIC),
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -669,7 +674,7 @@ export const useChatStore = createPersistStore(
|
||||
|
||||
const historyMsgLength = countMessages(toBeSummarizedMsgs);
|
||||
|
||||
if (historyMsgLength > modelConfig?.max_tokens ?? 4000) {
|
||||
if (historyMsgLength > (modelConfig?.max_tokens || 4000)) {
|
||||
const n = toBeSummarizedMsgs.length;
|
||||
toBeSummarizedMsgs = toBeSummarizedMsgs.slice(
|
||||
Math.max(0, n - modelConfig.historyMessageCount),
|
||||
@@ -715,22 +720,20 @@ export const useChatStore = createPersistStore(
|
||||
onUpdate(message) {
|
||||
session.memoryPrompt = message;
|
||||
},
|
||||
onFinish(message) {
|
||||
console.log("[Memory] ", message);
|
||||
get().updateCurrentSession((session) => {
|
||||
session.lastSummarizeIndex = lastSummarizeIndex;
|
||||
session.memoryPrompt = message; // Update the memory prompt for stored it in local storage
|
||||
});
|
||||
onFinish(message, responseRes) {
|
||||
if (responseRes?.status === 200) {
|
||||
console.log("[Memory] ", message);
|
||||
get().updateTargetSession(session, (session) => {
|
||||
session.lastSummarizeIndex = lastSummarizeIndex;
|
||||
session.memoryPrompt = message; // Update the memory prompt for stored it in local storage
|
||||
});
|
||||
}
|
||||
},
|
||||
onError(err) {
|
||||
console.error("[Summarize] ", err);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function isValidMessage(message: any): boolean {
|
||||
return typeof message === "string" && !message.startsWith("```json");
|
||||
}
|
||||
},
|
||||
|
||||
updateStat(message: ChatMessage) {
|
||||
@@ -746,7 +749,16 @@ export const useChatStore = createPersistStore(
|
||||
updater(sessions[index]);
|
||||
set(() => ({ sessions }));
|
||||
},
|
||||
|
||||
updateTargetSession(
|
||||
targetSession: ChatSession,
|
||||
updater: (session: ChatSession) => void,
|
||||
) {
|
||||
const sessions = get().sessions;
|
||||
const index = sessions.findIndex((s) => s.id === targetSession.id);
|
||||
if (index < 0) return;
|
||||
updater(sessions[index]);
|
||||
set(() => ({ sessions }));
|
||||
},
|
||||
async clearAllData() {
|
||||
await indexedDBStorage.clear();
|
||||
localStorage.clear();
|
||||
|
||||
@@ -266,7 +266,9 @@ export function isVisionModel(model: string) {
|
||||
model.includes("gpt-4-turbo") && !model.includes("preview");
|
||||
|
||||
return (
|
||||
visionKeywords.some((keyword) => model.includes(keyword)) || isGpt4Turbo
|
||||
visionKeywords.some((keyword) => model.includes(keyword)) ||
|
||||
isGpt4Turbo ||
|
||||
isDalle3(model)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -278,14 +280,15 @@ export function showPlugins(provider: ServiceProvider, model: string) {
|
||||
if (
|
||||
provider == ServiceProvider.OpenAI ||
|
||||
provider == ServiceProvider.Azure ||
|
||||
provider == ServiceProvider.Moonshot
|
||||
provider == ServiceProvider.Moonshot ||
|
||||
provider == ServiceProvider.ChatGLM
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (provider == ServiceProvider.Anthropic && !model.includes("claude-2")) {
|
||||
return true;
|
||||
}
|
||||
if (provider == ServiceProvider.Bedrock && !model.includes("claude-2")) {
|
||||
if (provider == ServiceProvider.Bedrock && model.includes("claude-3")) {
|
||||
return true;
|
||||
}
|
||||
if (provider == ServiceProvider.Google && !model.includes("vision")) {
|
||||
|
||||
@@ -174,6 +174,7 @@ export function stream(
|
||||
let finished = false;
|
||||
let running = false;
|
||||
let runTools: any[] = [];
|
||||
let responseRes: Response;
|
||||
|
||||
// animate response to make it looks smooth
|
||||
function animateResponseText() {
|
||||
@@ -272,7 +273,7 @@ export function stream(
|
||||
}
|
||||
console.debug("[ChatAPI] end");
|
||||
finished = true;
|
||||
options.onFinish(responseText + remainText);
|
||||
options.onFinish(responseText + remainText, responseRes); // 将res传递给onFinish
|
||||
}
|
||||
};
|
||||
|
||||
@@ -304,6 +305,7 @@ export function stream(
|
||||
clearTimeout(requestTimeoutId);
|
||||
const contentType = res.headers.get("content-type");
|
||||
console.log("[Request] response content type: ", contentType);
|
||||
responseRes = res;
|
||||
|
||||
if (contentType?.startsWith("text/plain")) {
|
||||
responseText = await res.clone().text();
|
||||
|
||||
@@ -19,7 +19,7 @@ type StreamResponse = {
|
||||
headers: Record<string, string>;
|
||||
};
|
||||
|
||||
export function fetch(url: string, options?: RequestInit): Promise<any> {
|
||||
export function fetch(url: string, options?: RequestInit): Promise<Response> {
|
||||
if (window.__TAURI__) {
|
||||
const {
|
||||
signal,
|
||||
|
||||
29
docs/bt-cn.md
Normal file
29
docs/bt-cn.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# 宝塔面板 的部署说明
|
||||
|
||||
## 拥有自己的宝塔
|
||||
当你需要通过 宝塔面板 部署本项目之前,需要在服务器上先安装好 宝塔面板工具。 接下来的 部署流程 都建立在已有宝塔面板的前提下。宝塔安装请参考 ([宝塔官网](https://www.bt.cn/new/download.html))
|
||||
|
||||
> 注意:本项目需要宝塔面板版本 9.2.0 及以上
|
||||
|
||||
## 一键安装
|
||||

|
||||
1. 在 宝塔面板 -> Docker -> 应用商店 页面,搜索 ChatGPT-Next-Web 找到本项目的docker应用;
|
||||
2. 点击 安装 开始部署本项目
|
||||
|
||||

|
||||
1. 在项目配置页,根据要求开始配置环境变量;
|
||||
2. 如勾选 允许外部访问 配置,请注意为配置的 web端口 开放安全组端口访问权限;
|
||||
3. 请确保你添加了正确的 Open Api Key,否则无法使用;当配置 OpenAI官方 提供的key(国内无法访问),请配置代理地址;
|
||||
4. 建议配置 访问权限密码,否则部署后所有人均可使用已配置的 Open Api Key(当允许外部访问时);
|
||||
5. 点击 确认 开始自动部署。
|
||||
|
||||
## 如何访问
|
||||

|
||||
通过根据 服务器IP地址 和配置的 web端口 http://$(host):$(port),在浏览器中打开 ChatGPT-Next-Web。
|
||||
|
||||

|
||||
若配置了 访问权限密码,访问大模型前需要登录,请点击 登录,获取访问权限。
|
||||
|
||||

|
||||
|
||||

|
||||
BIN
docs/images/bt/bt-install-1.jpeg
Normal file
BIN
docs/images/bt/bt-install-1.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 161 KiB |
BIN
docs/images/bt/bt-install-2.jpeg
Normal file
BIN
docs/images/bt/bt-install-2.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 196 KiB |
BIN
docs/images/bt/bt-install-3.jpeg
Normal file
BIN
docs/images/bt/bt-install-3.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 117 KiB |
BIN
docs/images/bt/bt-install-4.jpeg
Normal file
BIN
docs/images/bt/bt-install-4.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 159 KiB |
BIN
docs/images/bt/bt-install-5.jpeg
Normal file
BIN
docs/images/bt/bt-install-5.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
BIN
docs/images/bt/bt-install-6.jpeg
Normal file
BIN
docs/images/bt/bt-install-6.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 146 KiB |
@@ -59,7 +59,7 @@
|
||||
"@tauri-apps/cli": "1.5.11",
|
||||
"@testing-library/dom": "^10.4.0",
|
||||
"@testing-library/jest-dom": "^6.6.2",
|
||||
"@testing-library/react": "^16.0.0",
|
||||
"@testing-library/react": "^16.0.1",
|
||||
"@types/jest": "^29.5.14",
|
||||
"@types/js-yaml": "4.0.9",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"package": {
|
||||
"productName": "NextChat",
|
||||
"version": "2.15.6"
|
||||
"version": "2.15.7"
|
||||
},
|
||||
"tauri": {
|
||||
"allowlist": {
|
||||
|
||||
17
yarn.lock
17
yarn.lock
@@ -1201,14 +1201,7 @@
|
||||
resolved "https://registry.yarnpkg.com/@babel/regjsgen/-/regjsgen-0.8.0.tgz#f0ba69b075e1f05fb2825b7fad991e7adbb18310"
|
||||
integrity sha512-x/rqGMdzj+fWZvCOYForTghzbtqPDZ5gPwaoNGHdgDfF2QA/XZbCBp4Moo5scrkAMPhB7z26XM/AaHuIJdgauA==
|
||||
|
||||
"@babel/runtime@^7.12.1", "@babel/runtime@^7.20.7", "@babel/runtime@^7.23.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
|
||||
version "7.23.6"
|
||||
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.6.tgz#c05e610dc228855dc92ef1b53d07389ed8ab521d"
|
||||
integrity sha512-zHd0eUrf5GZoOWVCXp6koAKQTfZV07eit6bGPmJgnZdnSAvvZee6zniW2XMF7Cmc4ISOOnPy3QaSiIJGJkVEDQ==
|
||||
dependencies:
|
||||
regenerator-runtime "^0.14.0"
|
||||
|
||||
"@babel/runtime@^7.12.5", "@babel/runtime@^7.21.0":
|
||||
"@babel/runtime@^7.12.1", "@babel/runtime@^7.12.5", "@babel/runtime@^7.20.7", "@babel/runtime@^7.21.0", "@babel/runtime@^7.23.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
|
||||
version "7.25.0"
|
||||
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.25.0.tgz#3af9a91c1b739c569d5d80cc917280919c544ecb"
|
||||
integrity sha512-7dRy4DwXwtzBrPbZflqxnvfxLF8kdZXPkhymtDeFoFqE6ldzjQFgYTtYIFARcLEYDrqfBfYcZt1WqFxRoyC9Rw==
|
||||
@@ -2134,10 +2127,10 @@
|
||||
lodash "^4.17.21"
|
||||
redent "^3.0.0"
|
||||
|
||||
"@testing-library/react@^16.0.0":
|
||||
version "16.0.0"
|
||||
resolved "https://registry.npmmirror.com/@testing-library/react/-/react-16.0.0.tgz#0a1e0c7a3de25841c3591b8cb7fb0cf0c0a27321"
|
||||
integrity sha512-guuxUKRWQ+FgNX0h0NS0FIq3Q3uLtWVpBzcLOggmfMoUpgBnzBzvLLd4fbm6yS8ydJd94cIfY4yP9qUQjM2KwQ==
|
||||
"@testing-library/react@^16.0.1":
|
||||
version "16.0.1"
|
||||
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-16.0.1.tgz#29c0ee878d672703f5e7579f239005e4e0faa875"
|
||||
integrity sha512-dSmwJVtJXmku+iocRhWOUFbrERC76TX2Mnf0ATODz8brzAZrMBbzLwQixlBSanZxR6LddK3eiwpSFZgDET1URg==
|
||||
dependencies:
|
||||
"@babel/runtime" "^7.12.5"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user