diff --git a/src/api/openapi.ts b/src/api/openapi.ts index 01af4f976..83ba6ff8c 100644 --- a/src/api/openapi.ts +++ b/src/api/openapi.ts @@ -13,4 +13,4 @@ export const getOpenApiKeys = () => GET('/openapi/getKeys'); /** * delete api by id */ -export const delOpenApiById = (id: string) => DELETE(`/openapi/delKet?id=${id}`); +export const delOpenApiById = (id: string) => DELETE(`/openapi/delKey?id=${id}`); diff --git a/src/pages/api/openapi/chat/vectorGpt.ts b/src/pages/api/openapi/chat/vectorGpt.ts index 5a04f2ff1..b0038e972 100644 --- a/src/pages/api/openapi/chat/vectorGpt.ts +++ b/src/pages/api/openapi/chat/vectorGpt.ts @@ -123,7 +123,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) formatRedisPrompt.unshift(prompts.shift()?.value || ''); } - // textArr 筛选,最多 2800 tokens + // 系统提示词筛选,最多 2800 tokens const systemPrompt = systemPromptFilter(formatRedisPrompt, 2800); prompts.unshift({ diff --git a/src/pages/api/openapi/delKet.ts b/src/pages/api/openapi/delKey.ts similarity index 100% rename from src/pages/api/openapi/delKet.ts rename to src/pages/api/openapi/delKey.ts diff --git a/src/pages/model/detail/components/ModelEditForm.tsx b/src/pages/model/detail/components/ModelEditForm.tsx index c29fc9d0d..30d289a24 100644 --- a/src/pages/model/detail/components/ModelEditForm.tsx +++ b/src/pages/model/detail/components/ModelEditForm.tsx @@ -53,6 +53,12 @@ const ModelEditForm = ({ })} > + + + modelId: + + {getValues('_id')} + diff --git a/src/pages/openapi/index.tsx b/src/pages/openapi/index.tsx index a88fc580e..603c36aa4 100644 --- a/src/pages/openapi/index.tsx +++ b/src/pages/openapi/index.tsx @@ -1,4 +1,5 @@ import React, { useState } from 'react'; +import Link from 'next/link'; import { Card, Box, @@ -54,10 +55,21 @@ const OpenApi = () => { <> - Open Api Key + Open Api - 请注意保管你的 key,不要泄露! + Open Api 允许你将 Fast Gpt 的部分功能通过 api 的形式接入到自己的应用中。请注意保管你的 Api + Key,不要泄露! + + + 点击查看文档 @@ -93,16 +105,17 @@ const OpenApi = () => { ))}
-
+
setApiKey('')}> diff --git a/src/service/errorCode.ts b/src/service/errorCode.ts index 73d087ff2..933fa97f3 100644 --- a/src/service/errorCode.ts +++ b/src/service/errorCode.ts @@ -14,3 +14,30 @@ export const proxyError: Record = { ECONNABORTED: true, ECONNRESET: true }; + +export enum ERROR_ENUM { + unAuthorization = 'unAuthorization', + insufficientQuota = 'insufficientQuota' +} +export const ERROR_RESPONSE: Record< + any, + { + code: number; + statusText: string; + message: string; + data?: any; + } +> = { + [ERROR_ENUM.unAuthorization]: { + code: 403, + statusText: ERROR_ENUM.unAuthorization, + message: '凭证错误', + data: null + }, + [ERROR_ENUM.insufficientQuota]: { + code: 403, + statusText: ERROR_ENUM.insufficientQuota, + message: '账号余额不足', + data: null + } +}; diff --git a/src/service/response.ts b/src/service/response.ts index f1b8ca27e..48a9ca133 100644 --- a/src/service/response.ts +++ b/src/service/response.ts @@ -1,5 +1,5 @@ import { NextApiResponse } from 'next'; -import { openaiError, openaiError2, proxyError } from './errorCode'; +import { openaiError, openaiError2, proxyError, ERROR_RESPONSE } from './errorCode'; export interface ResponseType { code: number; @@ -18,7 +18,14 @@ export const jsonRes = ( ) => { const { code = 200, message = '', data = null, error } = props || {}; - let msg = message; + const errResponseKey = typeof error === 'string' ? error : error?.message; + // Specified error + if (ERROR_RESPONSE[errResponseKey]) { + return res.json(ERROR_RESPONSE[errResponseKey]); + } + + // another error + let msg = message || error?.message; if ((code < 200 || code >= 400) && !message) { msg = error?.message || '请求错误'; if (typeof error === 'string') { @@ -40,7 +47,8 @@ export const jsonRes = ( res.json({ code, + statusText: '', message: msg, - data + data: data || error || null }); }; diff --git a/src/service/utils/tools.ts b/src/service/utils/tools.ts index 6cfb82d69..1d02cc41f 100644 --- a/src/service/utils/tools.ts +++ b/src/service/utils/tools.ts @@ -1,11 +1,11 @@ import type { NextApiRequest } from 'next'; import crypto from 'crypto'; import jwt from 'jsonwebtoken'; -import tunnel from 'tunnel'; import { ChatItemType } from '@/types/chat'; import { encode } from 'gpt-token-utils'; import { OpenApi, User } from '../mongo'; import { formatPrice } from '@/utils/user'; +import { ERROR_ENUM } from '../errorCode'; /* 密码加密 */ export const hashPassword = (psw: string) => { @@ -49,20 +49,20 @@ export const authOpenApiKey = async (req: NextApiRequest) => { const { apikey: apiKey } = req.headers; if (!apiKey) { - return Promise.reject('api key is empty'); + return Promise.reject(ERROR_ENUM.unAuthorization); } try { const openApi = await OpenApi.findOne({ apiKey }); if (!openApi) { - return Promise.reject('api key is error'); + return Promise.reject(ERROR_ENUM.unAuthorization); } const userId = String(openApi.userId); // 余额校验 const user = await User.findById(userId); if (!user) { - return Promise.reject('user is empty'); + return Promise.reject(ERROR_ENUM.unAuthorization); } if (formatPrice(user.balance) <= 0) { return Promise.reject('Insufficient account balance');