perf: sse response

This commit is contained in:
archer
2023-06-23 23:11:22 +08:00
parent 6787f19d78
commit 986206b691
13 changed files with 150 additions and 132 deletions

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { authUser, getApiKey } from '@/service/utils/auth';
import { authUser, getApiKey, getSystemOpenAiKey } from '@/service/utils/auth';
import { withNextCors } from '@/service/utils/tools';
import { getOpenAIApi } from '@/service/utils/chat/openai';
import { embeddingModel } from '@/constants/model';
@@ -39,14 +39,10 @@ export async function openaiEmbedding({
input,
mustPay = false
}: { userId: string; mustPay?: boolean } & Props) {
const { userOpenAiKey, systemAuthKey } = await getApiKey({
model: OpenAiChatEnum.GPT35,
userId,
mustPay
});
const apiKey = getSystemOpenAiKey();
// 获取 chatAPI
const chatAPI = getOpenAIApi();
const chatAPI = getOpenAIApi(apiKey);
// 把输入的内容转成向量
const result = await chatAPI
@@ -57,16 +53,22 @@ export async function openaiEmbedding({
},
{
timeout: 60000,
...axiosConfig(userOpenAiKey || systemAuthKey)
...axiosConfig(apiKey)
}
)
.then((res) => ({
tokenLen: res.data?.usage?.total_tokens || 0,
vectors: res.data.data.map((item) => item.embedding)
}));
.then((res) => {
if (!res.data?.usage?.total_tokens) {
// @ts-ignore
return Promise.reject(res.data?.error?.message || 'Embedding Error');
}
return {
tokenLen: res.data.usage.total_tokens || 0,
vectors: res.data.data.map((item) => item.embedding)
};
});
pushGenerateVectorBill({
isPay: !userOpenAiKey,
isPay: mustPay,
userId,
text: input.join(''),
tokenLen: result.tokenLen