From 48fbc74168c30a770bf9d7d184e0038dd80b54dc Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Mon, 5 Jun 2023 01:23:40 +0800 Subject: [PATCH] feat: embedding --- .../api/openapi/plugin/openaiEmbedding.ts | 19 ++++++------------- src/pages/api/user/getPayCode.ts | 1 - src/pages/chat/index.tsx | 2 ++ src/pages/number/components/PayModal.tsx | 6 +++--- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/pages/api/openapi/plugin/openaiEmbedding.ts b/src/pages/api/openapi/plugin/openaiEmbedding.ts index 6952ffd82..4923c723c 100644 --- a/src/pages/api/openapi/plugin/openaiEmbedding.ts +++ b/src/pages/api/openapi/plugin/openaiEmbedding.ts @@ -1,8 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { jsonRes } from '@/service/response'; -import { authUser } from '@/service/utils/auth'; +import { authUser, getSystemOpenAiKey } from '@/service/utils/auth'; import { withNextCors } from '@/service/utils/tools'; -import { getApiKey } from '@/service/utils/auth'; import { getOpenAIApi } from '@/service/utils/chat/openai'; import { embeddingModel } from '@/constants/model'; import { axiosConfig } from '@/service/utils/tools'; @@ -25,7 +24,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex } jsonRes(res, { - data: await openaiEmbedding({ userId, input, mustPay: true, type }) + data: await openaiEmbedding({ userId, input, type }) }); } catch (err) { console.log(err); @@ -39,15 +38,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex export async function openaiEmbedding({ userId, input, - mustPay = false, type = 'chat' -}: { userId: string; mustPay?: boolean } & Props) { - const { userOpenAiKey, systemAuthKey } = await getApiKey({ - model: 'gpt-3.5-turbo', - userId, - mustPay, - type - }); +}: { userId: string } & Props) { + const apiKey = getSystemOpenAiKey(type); // 获取 chatAPI const chatAPI = getOpenAIApi(); @@ -61,7 +54,7 @@ export async function openaiEmbedding({ }, { timeout: 60000, - ...axiosConfig(userOpenAiKey || systemAuthKey) + ...axiosConfig(apiKey) } ) .then((res) => ({ @@ -70,7 +63,7 @@ export async function openaiEmbedding({ })); pushGenerateVectorBill({ - isPay: !userOpenAiKey, + isPay: false, userId, text: input.join(''), tokenLen: result.tokenLen diff --git a/src/pages/api/user/getPayCode.ts b/src/pages/api/user/getPayCode.ts index e0aacfe2e..09e32c478 100644 --- a/src/pages/api/user/getPayCode.ts +++ b/src/pages/api/user/getPayCode.ts @@ -35,7 +35,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) } }); } catch (err) { - console.log(err, '=='); jsonRes(res, { code: 500, error: err diff --git a/src/pages/chat/index.tsx b/src/pages/chat/index.tsx index e70cd3b7f..abd377177 100644 --- a/src/pages/chat/index.tsx +++ b/src/pages/chat/index.tsx @@ -211,6 +211,8 @@ const Chat = ({ modelId, chatId }: { modelId: string; chatId: string }) => { router.replace(`/chat?modelId=${modelId}&chatId=${newChatId}`); } + abortSignal.signal.aborted && (await delay(500)); + // 设置聊天内容为完成状态 setChatData((state) => ({ ...state, diff --git a/src/pages/number/components/PayModal.tsx b/src/pages/number/components/PayModal.tsx index df72a59b9..f33ed6f79 100644 --- a/src/pages/number/components/PayModal.tsx +++ b/src/pages/number/components/PayModal.tsx @@ -16,6 +16,7 @@ import { getPayCode, checkPayResult } from '@/api/user'; import { useToast } from '@/hooks/useToast'; import { useQuery } from '@tanstack/react-query'; import { useRouter } from 'next/router'; +import { getErrText } from '@/utils/tools'; const PayModal = ({ onClose }: { onClose: () => void }) => { const router = useRouter(); @@ -39,12 +40,11 @@ const PayModal = ({ onClose }: { onClose: () => void }) => { correctLevel: QRCode.CorrectLevel.H }); setPayId(res.payId); - } catch (error) { + } catch (err) { toast({ - title: '出现了一些意外...', + title: getErrText(err), status: 'error' }); - console.log(error); } setLoading(false); }, [inputVal, toast]);