mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
perf: bill
This commit is contained in:
@@ -39,7 +39,6 @@ export const useAppRoute = (app) => {
|
||||
userId: app.userId,
|
||||
name: app.name,
|
||||
intro: app.intro,
|
||||
app: app.chat?.chatModel,
|
||||
relatedKbs: kbNames, // 将relatedKbs的id转换为相应的Kb名称
|
||||
systemPrompt: app.chat?.systemPrompt || '',
|
||||
temperature: app.chat?.temperature || 0,
|
||||
|
@@ -62,12 +62,6 @@ const appSchema = new mongoose.Schema({
|
||||
avatar: String,
|
||||
status: String,
|
||||
intro: String,
|
||||
chat: {
|
||||
relatedKbs: [mongoose.Schema.Types.ObjectId],
|
||||
systemPrompt: String,
|
||||
temperature: Number,
|
||||
chatModel: String
|
||||
},
|
||||
share: {
|
||||
topNum: Number,
|
||||
isShare: Boolean,
|
||||
|
26
client/data/ChatModels.json
Normal file
26
client/data/ChatModels.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"Gpt35-4k": {
|
||||
"model": "gpt-3.5-turbo",
|
||||
"name": "Gpt35-4k",
|
||||
"contextMaxToken": 4000,
|
||||
"systemMaxToken": 2400,
|
||||
"maxTemperature": 1.2,
|
||||
"price": 1.5
|
||||
},
|
||||
"Gpt35-16k": {
|
||||
"model": "gpt-3.5-turbo-16k",
|
||||
"name": "Gpt35-16k",
|
||||
"contextMaxToken": 16000,
|
||||
"systemMaxToken": 8000,
|
||||
"maxTemperature": 1.2,
|
||||
"price": 3
|
||||
},
|
||||
"Gpt4": {
|
||||
"model": "gpt-4",
|
||||
"name": "Gpt4",
|
||||
"contextMaxToken": 8000,
|
||||
"systemMaxToken": 4000,
|
||||
"maxTemperature": 1.2,
|
||||
"price": 45
|
||||
}
|
||||
}
|
8
client/data/QAModels.json
Normal file
8
client/data/QAModels.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Gpt35-16k": {
|
||||
"model": "gpt-3.5-turbo-16k",
|
||||
"name": "Gpt35-16k",
|
||||
"maxToken": 16000,
|
||||
"price": 3
|
||||
}
|
||||
}
|
6
client/data/SystemParams.json
Normal file
6
client/data/SystemParams.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"vectorMaxProcess": 10,
|
||||
"qaMaxProcess": 10,
|
||||
"pgIvfflatProbe": 10,
|
||||
"sensitiveCheck": false
|
||||
}
|
7
client/data/VectorModels.json
Normal file
7
client/data/VectorModels.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"text-embedding-ada-002": {
|
||||
"model": "text-embedding-ada-002",
|
||||
"name": "Embedding-2",
|
||||
"price": 0.2
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
var _hmt = _hmt || [];
|
||||
|
||||
(function () {
|
||||
const hm = document.createElement('script');
|
||||
hm.src = 'https://hm.baidu.com/hm.js?a5357e9dab086658bac0b6faf148882e';
|
||||
const s = document.getElementsByTagName('script')[0];
|
||||
s.parentNode.insertBefore(hm, s);
|
||||
})();
|
@@ -2,7 +2,6 @@ import { GET, POST, PUT, DELETE } from '../request';
|
||||
import type { KbItemType } from '@/types/plugin';
|
||||
import { RequestPaging } from '@/types/index';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import { type QuoteItemType } from '@/pages/api/openapi/kb/appKbSearch';
|
||||
import {
|
||||
Props as PushDataProps,
|
||||
Response as PushDateResponse
|
||||
@@ -60,7 +59,7 @@ export const getTrainingData = (data: { kbId: string; init: boolean }) =>
|
||||
}>(`/plugins/kb/data/getTrainingData`, data);
|
||||
|
||||
export const getKbDataItemById = (dataId: string) =>
|
||||
GET<QuoteItemType>(`/plugins/kb/data/getDataById`, { dataId });
|
||||
GET(`/plugins/kb/data/getDataById`, { dataId });
|
||||
|
||||
/**
|
||||
* 直接push数据
|
||||
|
@@ -1,9 +1,6 @@
|
||||
import { GET, POST, PUT } from './request';
|
||||
import type { ChatModelItemType } from '@/constants/model';
|
||||
import type { InitDateResponse } from '@/pages/api/system/getInitData';
|
||||
|
||||
export const getInitData = () => GET<InitDateResponse>('/system/getInitData');
|
||||
|
||||
export const getSystemModelList = () => GET<ChatModelItemType[]>('/system/getModels');
|
||||
|
||||
export const uploadImg = (base64Img: string) => POST<string>('/system/uploadImage', { base64Img });
|
||||
|
@@ -10,7 +10,7 @@ import React, {
|
||||
import { throttle } from 'lodash';
|
||||
import { ChatItemType, ChatSiteItemType, ExportChatType } from '@/types/chat';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { useCopyData, voiceBroadcast, hasVoiceApi } from '@/utils/tools';
|
||||
import { useCopyData, voiceBroadcast, hasVoiceApi, getErrText } from '@/utils/tools';
|
||||
import { Box, Card, Flex, Input, Textarea, Button, useTheme } from '@chakra-ui/react';
|
||||
import { useUserStore } from '@/store/user';
|
||||
|
||||
@@ -241,33 +241,34 @@ const ChatBox = (
|
||||
variables: data
|
||||
});
|
||||
|
||||
// 设置聊天内容为完成状态
|
||||
setChatHistory((state) =>
|
||||
state.map((item, index) => {
|
||||
if (index !== state.length - 1) return item;
|
||||
return {
|
||||
...item,
|
||||
status: 'finish'
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
setTimeout(() => {
|
||||
generatingScroll();
|
||||
TextareaDom.current?.focus();
|
||||
}, 100);
|
||||
} catch (err: any) {
|
||||
toast({
|
||||
title: typeof err === 'string' ? err : err?.message || '聊天出错了~',
|
||||
status: 'warning',
|
||||
title: getErrText(err, '聊天出错了~'),
|
||||
status: 'error',
|
||||
duration: 5000,
|
||||
isClosable: true
|
||||
});
|
||||
|
||||
resetInputVal(value);
|
||||
|
||||
setChatHistory(newChatList.slice(0, newChatList.length - 2));
|
||||
if (!err?.responseText) {
|
||||
resetInputVal(value);
|
||||
setChatHistory(newChatList.slice(0, newChatList.length - 2));
|
||||
}
|
||||
}
|
||||
|
||||
// set finish status
|
||||
setChatHistory((state) =>
|
||||
state.map((item, index) => {
|
||||
if (index !== state.length - 1) return item;
|
||||
return {
|
||||
...item,
|
||||
status: 'finish'
|
||||
};
|
||||
})
|
||||
);
|
||||
},
|
||||
[
|
||||
isChatting,
|
||||
@@ -404,7 +405,7 @@ const ChatBox = (
|
||||
py={4}
|
||||
_hover={{
|
||||
'& .control': {
|
||||
display: 'flex'
|
||||
display: item.status === 'finish' ? 'flex' : 'none'
|
||||
}
|
||||
}}
|
||||
>
|
||||
|
@@ -965,8 +965,8 @@ export const appTemplates: (AppItemType & { avatar: string; intro: string })[] =
|
||||
name: '意图识别',
|
||||
intro: '可以判断用户问题属于哪方面问题,从而执行不同的操作。',
|
||||
type: 'http',
|
||||
url: '/openapi/modules/agent/classifyQuestion',
|
||||
flowType: 'classifyQuestionNode',
|
||||
url: '/openapi/modules/agent/recognizeIntention',
|
||||
flowType: 'recognizeIntention',
|
||||
inputs: [
|
||||
{
|
||||
key: 'systemPrompt',
|
||||
|
@@ -1,12 +0,0 @@
|
||||
export enum ChatModelEnum {
|
||||
'GPT35' = 'gpt-3.5-turbo',
|
||||
'GPT3516k' = 'gpt-3.5-turbo-16k',
|
||||
'GPT4' = 'gpt-4',
|
||||
'GPT432k' = 'gpt-4-32k'
|
||||
}
|
||||
|
||||
export const chatModelList = [
|
||||
{ label: 'Gpt35-16k', value: ChatModelEnum.GPT3516k },
|
||||
{ label: 'Gpt35-4k', value: ChatModelEnum.GPT35 },
|
||||
{ label: 'Gpt4-8k', value: ChatModelEnum.GPT4 }
|
||||
];
|
@@ -1,7 +1,7 @@
|
||||
import { AppModuleItemTypeEnum, SystemInputEnum, SpecificInputEnum } from '../app';
|
||||
import { FlowModuleTypeEnum, FlowInputItemTypeEnum, FlowOutputItemTypeEnum } from './index';
|
||||
import type { AppModuleTemplateItemType } from '@/types/app';
|
||||
import { chatModelList } from '../data';
|
||||
import { chatModelList } from '@/store/static';
|
||||
import {
|
||||
Input_Template_History,
|
||||
Input_Template_TFSwitch,
|
||||
@@ -96,8 +96,8 @@ export const ChatModule: AppModuleTemplateItemType = {
|
||||
key: 'model',
|
||||
type: FlowInputItemTypeEnum.select,
|
||||
label: '对话模型',
|
||||
value: chatModelList[0].value,
|
||||
list: chatModelList
|
||||
value: chatModelList[0]?.model,
|
||||
list: chatModelList.map((item) => ({ label: item.name, value: item.model }))
|
||||
},
|
||||
{
|
||||
key: 'temperature',
|
||||
@@ -278,13 +278,13 @@ export const TFSwitchModule: AppModuleTemplateItemType = {
|
||||
}
|
||||
]
|
||||
};
|
||||
export const ClassifyQuestionModule: AppModuleTemplateItemType = {
|
||||
export const RecognizeIntentionModule: AppModuleTemplateItemType = {
|
||||
logo: '/imgs/module/cq.png',
|
||||
name: '意图识别',
|
||||
intro: '可以判断用户问题属于哪方面问题,从而执行不同的操作。',
|
||||
type: AppModuleItemTypeEnum.http,
|
||||
url: '/openapi/modules/agent/classifyQuestion',
|
||||
flowType: FlowModuleTypeEnum.classifyQuestionNode,
|
||||
url: '/openapi/modules/agent/recognizeIntention',
|
||||
flowType: FlowModuleTypeEnum.recognizeIntention,
|
||||
inputs: [
|
||||
{
|
||||
key: 'systemPrompt',
|
||||
@@ -348,6 +348,6 @@ export const ModuleTemplates = [
|
||||
},
|
||||
{
|
||||
label: 'Agent',
|
||||
list: [ClassifyQuestionModule]
|
||||
list: [RecognizeIntentionModule]
|
||||
}
|
||||
];
|
||||
|
@@ -26,7 +26,7 @@ export enum FlowModuleTypeEnum {
|
||||
kbSearchNode = 'kbSearchNode',
|
||||
tfSwitchNode = 'tfSwitchNode',
|
||||
answerNode = 'answerNode',
|
||||
classifyQuestionNode = 'classifyQuestionNode'
|
||||
recognizeIntention = 'recognizeIntention'
|
||||
}
|
||||
|
||||
export const edgeOptions = {
|
||||
|
@@ -1,11 +1,6 @@
|
||||
import { getSystemModelList } from '@/api/system';
|
||||
import type { ShareChatEditType } from '@/types/app';
|
||||
import type { AppSchema } from '@/types/mongoSchema';
|
||||
|
||||
export const embeddingModel = 'text-embedding-ada-002';
|
||||
export const embeddingPrice = 0.1;
|
||||
export type EmbeddingModelType = 'text-embedding-ada-002';
|
||||
|
||||
export enum OpenAiChatEnum {
|
||||
'GPT35' = 'gpt-3.5-turbo',
|
||||
'GPT3516k' = 'gpt-3.5-turbo-16k',
|
||||
@@ -13,58 +8,6 @@ export enum OpenAiChatEnum {
|
||||
'GPT432k' = 'gpt-4-32k'
|
||||
}
|
||||
|
||||
export type ChatModelType = `${OpenAiChatEnum}`;
|
||||
|
||||
export type ChatModelItemType = {
|
||||
chatModel: ChatModelType;
|
||||
name: string;
|
||||
contextMaxToken: number;
|
||||
systemMaxToken: number;
|
||||
maxTemperature: number;
|
||||
price: number;
|
||||
};
|
||||
|
||||
export const ChatModelMap = {
|
||||
[OpenAiChatEnum.GPT35]: {
|
||||
chatModel: OpenAiChatEnum.GPT35,
|
||||
name: 'Gpt35-4k',
|
||||
contextMaxToken: 4000,
|
||||
systemMaxToken: 2400,
|
||||
maxTemperature: 1.2,
|
||||
price: 1.5
|
||||
},
|
||||
[OpenAiChatEnum.GPT3516k]: {
|
||||
chatModel: OpenAiChatEnum.GPT3516k,
|
||||
name: 'Gpt35-16k',
|
||||
contextMaxToken: 16000,
|
||||
systemMaxToken: 8000,
|
||||
maxTemperature: 1.2,
|
||||
price: 3
|
||||
},
|
||||
[OpenAiChatEnum.GPT4]: {
|
||||
chatModel: OpenAiChatEnum.GPT4,
|
||||
name: 'Gpt4',
|
||||
contextMaxToken: 8000,
|
||||
systemMaxToken: 4000,
|
||||
maxTemperature: 1.2,
|
||||
price: 45
|
||||
},
|
||||
[OpenAiChatEnum.GPT432k]: {
|
||||
chatModel: OpenAiChatEnum.GPT432k,
|
||||
name: 'Gpt4-32k',
|
||||
contextMaxToken: 32000,
|
||||
systemMaxToken: 8000,
|
||||
maxTemperature: 1.2,
|
||||
price: 90
|
||||
}
|
||||
};
|
||||
|
||||
export const chatModelList: ChatModelItemType[] = [
|
||||
ChatModelMap[OpenAiChatEnum.GPT3516k],
|
||||
ChatModelMap[OpenAiChatEnum.GPT35],
|
||||
ChatModelMap[OpenAiChatEnum.GPT4]
|
||||
];
|
||||
|
||||
export const defaultApp: AppSchema = {
|
||||
_id: '',
|
||||
userId: 'userId',
|
||||
@@ -72,17 +15,6 @@ export const defaultApp: AppSchema = {
|
||||
avatar: '/icon/logo.png',
|
||||
intro: '',
|
||||
updateTime: Date.now(),
|
||||
chat: {
|
||||
relatedKbs: [],
|
||||
searchSimilarity: 0.2,
|
||||
searchLimit: 5,
|
||||
searchEmptyText: '',
|
||||
systemPrompt: '',
|
||||
limitPrompt: '',
|
||||
temperature: 0,
|
||||
maxToken: 4000,
|
||||
chatModel: OpenAiChatEnum.GPT35
|
||||
},
|
||||
share: {
|
||||
isShare: false,
|
||||
isShareDetail: false,
|
||||
|
@@ -1,9 +1,6 @@
|
||||
export enum BillTypeEnum {
|
||||
chat = 'chat',
|
||||
openapiChat = 'openapiChat',
|
||||
QA = 'QA',
|
||||
vector = 'vector',
|
||||
return = 'return'
|
||||
export enum BillSourceEnum {
|
||||
fastgpt = 'fastgpt',
|
||||
api = 'api'
|
||||
}
|
||||
export enum PageTypeEnum {
|
||||
login = 'login',
|
||||
@@ -11,12 +8,9 @@ export enum PageTypeEnum {
|
||||
forgetPassword = 'forgetPassword'
|
||||
}
|
||||
|
||||
export const BillTypeMap: Record<`${BillTypeEnum}`, string> = {
|
||||
[BillTypeEnum.chat]: '对话',
|
||||
[BillTypeEnum.openapiChat]: 'api 对话',
|
||||
[BillTypeEnum.QA]: 'QA拆分',
|
||||
[BillTypeEnum.vector]: '索引生成',
|
||||
[BillTypeEnum.return]: '退款'
|
||||
export const BillSourceMap: Record<`${BillSourceEnum}`, string> = {
|
||||
[BillSourceEnum.fastgpt]: 'FastGpt 平台',
|
||||
[BillSourceEnum.api]: 'Api'
|
||||
};
|
||||
|
||||
export enum PromotionEnum {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { useRef, useState, useCallback, useLayoutEffect, useMemo, useEffect } from 'react';
|
||||
import { useRef, useState, useCallback, useMemo, useEffect } from 'react';
|
||||
import type { PagingData } from '../types/index';
|
||||
import { IconButton, Flex, Box, Input } from '@chakra-ui/react';
|
||||
import { ArrowBackIcon, ArrowForwardIcon } from '@chakra-ui/icons';
|
||||
@@ -144,7 +144,7 @@ export const usePagination = <T = any,>({
|
||||
[data.length, isLoading, mutate, pageNum, total]
|
||||
);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
useEffect(() => {
|
||||
if (!elementRef.current || type !== 'scroll') return;
|
||||
|
||||
const scrolling = throttle((e: Event) => {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import type { AppProps } from 'next/app';
|
||||
import Script from 'next/script';
|
||||
import Head from 'next/head';
|
||||
@@ -8,9 +8,9 @@ import { theme } from '@/constants/theme';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import NProgress from 'nprogress'; //nprogress module
|
||||
import Router from 'next/router';
|
||||
import { useGlobalStore } from '@/store/global';
|
||||
import 'nprogress/nprogress.css';
|
||||
import '@/styles/reset.scss';
|
||||
import { clientInitData } from '@/store/static';
|
||||
|
||||
//Binding events.
|
||||
Router.events.on('routeChangeStart', () => NProgress.start());
|
||||
@@ -29,13 +29,15 @@ const queryClient = new QueryClient({
|
||||
});
|
||||
|
||||
function App({ Component, pageProps }: AppProps) {
|
||||
const {
|
||||
loadInitData,
|
||||
initData: { googleVerKey, baiduTongji }
|
||||
} = useGlobalStore();
|
||||
const [googleVerKey, setGoogleVerKey] = useState<string>();
|
||||
const [baiduTongji, setBaiduTongji] = useState<string>();
|
||||
|
||||
useEffect(() => {
|
||||
loadInitData();
|
||||
(async () => {
|
||||
const { googleVerKey, baiduTongji } = await clientInitData();
|
||||
setGoogleVerKey(googleVerKey);
|
||||
setBaiduTongji(baiduTongji);
|
||||
})();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
@@ -53,7 +55,7 @@ function App({ Component, pageProps }: AppProps) {
|
||||
<Script src="/js/qrcode.min.js" strategy="lazyOnload"></Script>
|
||||
<Script src="/js/pdf.js" strategy="lazyOnload"></Script>
|
||||
<Script src="/js/html2pdf.bundle.min.js" strategy="lazyOnload"></Script>
|
||||
{baiduTongji && <Script src="/js/baidutongji.js" strategy="lazyOnload"></Script>}
|
||||
{baiduTongji && <Script src={baiduTongji} strategy="lazyOnload"></Script>}
|
||||
{googleVerKey && (
|
||||
<>
|
||||
<Script
|
||||
@@ -75,5 +77,4 @@ function App({ Component, pageProps }: AppProps) {
|
||||
);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
export default App;
|
||||
|
@@ -8,6 +8,8 @@ import { type ChatCompletionRequestMessage } from 'openai';
|
||||
import { AppModuleItemType } from '@/types/app';
|
||||
import { dispatchModules } from '../openapi/v1/chat/completions';
|
||||
import { gptMessage2ChatType } from '@/utils/adapt';
|
||||
import { createTaskBill, delTaskBill, finishTaskBill } from '@/service/events/pushBill';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
|
||||
export type MessageItemType = ChatCompletionRequestMessage & { _id?: string };
|
||||
export type Props = {
|
||||
@@ -15,10 +17,8 @@ export type Props = {
|
||||
prompt: string;
|
||||
modules: AppModuleItemType[];
|
||||
variables: Record<string, any>;
|
||||
};
|
||||
export type ChatResponseType = {
|
||||
newChatId: string;
|
||||
quoteLen?: number;
|
||||
appId: string;
|
||||
appName: string;
|
||||
};
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
@@ -30,8 +30,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
res.end();
|
||||
});
|
||||
|
||||
let { modules = [], history = [], prompt, variables = {} } = req.body as Props;
|
||||
|
||||
let { modules = [], history = [], prompt, variables = {}, appName, appId } = req.body as Props;
|
||||
let billId = '';
|
||||
try {
|
||||
if (!history || !modules || !prompt) {
|
||||
throw new Error('Prams Error');
|
||||
@@ -45,6 +45,13 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
/* user auth */
|
||||
const { userId } = await authUser({ req });
|
||||
|
||||
billId = await createTaskBill({
|
||||
userId,
|
||||
appName,
|
||||
appId,
|
||||
source: BillSourceEnum.fastgpt
|
||||
});
|
||||
|
||||
/* start process */
|
||||
const { responseData } = await dispatchModules({
|
||||
res,
|
||||
@@ -54,7 +61,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
history: gptMessage2ChatType(history),
|
||||
userChatInput: prompt
|
||||
},
|
||||
stream: true
|
||||
stream: true,
|
||||
billId
|
||||
});
|
||||
|
||||
sseResponse({
|
||||
@@ -70,7 +78,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
res.end();
|
||||
|
||||
// bill
|
||||
finishTaskBill({
|
||||
billId
|
||||
});
|
||||
} catch (err: any) {
|
||||
delTaskBill(billId);
|
||||
res.status(500);
|
||||
sseErrRes(res, err);
|
||||
res.end();
|
||||
|
@@ -53,14 +53,6 @@ export async function saveChat({
|
||||
await connectToDatabase();
|
||||
const { app } = await authApp({ appId, userId, authOwner: false });
|
||||
|
||||
const content = prompts.map((item) => ({
|
||||
_id: item._id,
|
||||
obj: item.obj,
|
||||
value: item.value,
|
||||
systemPrompt: item.systemPrompt || '',
|
||||
quote: item.quote || []
|
||||
}));
|
||||
|
||||
if (String(app.userId) === userId) {
|
||||
await App.findByIdAndUpdate(appId, {
|
||||
updateTime: new Date()
|
||||
@@ -73,12 +65,11 @@ export async function saveChat({
|
||||
Chat.findByIdAndUpdate(historyId, {
|
||||
$push: {
|
||||
content: {
|
||||
$each: content
|
||||
$each: prompts
|
||||
}
|
||||
},
|
||||
variables,
|
||||
title: content[0].value.slice(0, 20),
|
||||
latestChat: content[1].value,
|
||||
title: prompts[0].value.slice(0, 20),
|
||||
updateTime: new Date()
|
||||
}).then(() => ({
|
||||
newHistoryId: ''
|
||||
@@ -90,9 +81,8 @@ export async function saveChat({
|
||||
userId,
|
||||
appId,
|
||||
variables,
|
||||
content,
|
||||
title: content[0].value.slice(0, 20),
|
||||
latestChat: content[1].value
|
||||
content: prompts,
|
||||
title: prompts[0].value.slice(0, 20)
|
||||
}).then((res) => ({
|
||||
newHistoryId: String(res._id)
|
||||
}))
|
||||
|
@@ -1,186 +0,0 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
import { PgClient } from '@/service/pg';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import type { AppSchema } from '@/types/mongoSchema';
|
||||
import { authApp } from '@/service/utils/auth';
|
||||
import { ChatModelMap } from '@/constants/model';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { openaiEmbedding } from '../plugin/openaiEmbedding';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
|
||||
export type QuoteItemType = {
|
||||
id: string;
|
||||
q: string;
|
||||
a: string;
|
||||
source?: string;
|
||||
};
|
||||
type Props = {
|
||||
prompts: ChatItemType[];
|
||||
similarity: number;
|
||||
limit: number;
|
||||
appId: string;
|
||||
};
|
||||
type Response = {
|
||||
rawSearch: QuoteItemType[];
|
||||
userSystemPrompt: {
|
||||
obj: ChatRoleEnum;
|
||||
value: string;
|
||||
}[];
|
||||
userLimitPrompt: {
|
||||
obj: ChatRoleEnum;
|
||||
value: string;
|
||||
}[];
|
||||
quotePrompt: {
|
||||
obj: ChatRoleEnum;
|
||||
value: string;
|
||||
};
|
||||
};
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const { userId } = await authUser({ req });
|
||||
|
||||
if (!userId) {
|
||||
throw new Error('userId is empty');
|
||||
}
|
||||
|
||||
const { prompts, similarity, limit, appId } = req.body as Props;
|
||||
|
||||
if (!similarity || !Array.isArray(prompts) || !appId) {
|
||||
throw new Error('params is error');
|
||||
}
|
||||
|
||||
// auth app
|
||||
const { app } = await authApp({
|
||||
appId,
|
||||
userId
|
||||
});
|
||||
|
||||
const result = await appKbSearch({
|
||||
app,
|
||||
userId,
|
||||
fixedQuote: [],
|
||||
prompt: prompts[prompts.length - 1],
|
||||
similarity,
|
||||
limit
|
||||
});
|
||||
|
||||
jsonRes<Response>(res, {
|
||||
data: result
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export async function appKbSearch({
|
||||
app,
|
||||
userId,
|
||||
fixedQuote = [],
|
||||
prompt,
|
||||
similarity = 0.8,
|
||||
limit = 5
|
||||
}: {
|
||||
app: AppSchema;
|
||||
userId: string;
|
||||
fixedQuote?: QuoteItemType[];
|
||||
prompt: ChatItemType;
|
||||
similarity: number;
|
||||
limit: number;
|
||||
}): Promise<Response> {
|
||||
const modelConstantsData = ChatModelMap[app.chat.chatModel];
|
||||
|
||||
// get vector
|
||||
const promptVector = await openaiEmbedding({
|
||||
userId,
|
||||
input: [prompt.value]
|
||||
});
|
||||
|
||||
// search kb
|
||||
const res: any = await PgClient.query(
|
||||
`BEGIN;
|
||||
SET LOCAL ivfflat.probes = ${global.systemEnv.pgIvfflatProbe || 10};
|
||||
select id,q,a,source from modelData where kb_id IN (${app.chat.relatedKbs
|
||||
.map((item) => `'${item}'`)
|
||||
.join(',')}) AND vector <#> '[${promptVector[0]}]' < -${similarity} order by vector <#> '[${
|
||||
promptVector[0]
|
||||
}]' limit ${limit};
|
||||
COMMIT;`
|
||||
);
|
||||
|
||||
const searchRes: QuoteItemType[] = res?.[2]?.rows || [];
|
||||
|
||||
// filter same search result
|
||||
const idSet = new Set<string>();
|
||||
const filterSearch = [
|
||||
...searchRes.slice(0, 3),
|
||||
...fixedQuote.slice(0, 2),
|
||||
...searchRes.slice(3),
|
||||
...fixedQuote.slice(2, Math.floor(fixedQuote.length * 0.4))
|
||||
].filter((item) => {
|
||||
if (idSet.has(item.id)) {
|
||||
return false;
|
||||
}
|
||||
idSet.add(item.id);
|
||||
return true;
|
||||
});
|
||||
|
||||
// 计算固定提示词的 token 数量
|
||||
const userSystemPrompt = app.chat.systemPrompt // user system prompt
|
||||
? [
|
||||
{
|
||||
obj: ChatRoleEnum.System,
|
||||
value: app.chat.systemPrompt
|
||||
}
|
||||
]
|
||||
: [];
|
||||
const userLimitPrompt = [
|
||||
{
|
||||
obj: ChatRoleEnum.Human,
|
||||
value: app.chat.limitPrompt
|
||||
? app.chat.limitPrompt
|
||||
: `知识库是关于 ${app.name} 的内容,参考知识库回答问题。与 "${app.name}" 无关内容,直接回复: "我不知道"。`
|
||||
}
|
||||
];
|
||||
|
||||
const fixedSystemTokens = modelToolMap.countTokens({
|
||||
model: app.chat.chatModel,
|
||||
messages: [...userSystemPrompt, ...userLimitPrompt]
|
||||
});
|
||||
|
||||
// filter part quote by maxToken
|
||||
const sliceResult = modelToolMap
|
||||
.tokenSlice({
|
||||
model: app.chat.chatModel,
|
||||
maxToken: modelConstantsData.systemMaxToken - fixedSystemTokens,
|
||||
messages: filterSearch.map((item, i) => ({
|
||||
obj: ChatRoleEnum.System,
|
||||
value: `${i + 1}: [${item.q}\n${item.a}]`
|
||||
}))
|
||||
})
|
||||
.map((item) => item.value)
|
||||
.join('\n')
|
||||
.trim();
|
||||
|
||||
// slice filterSearch
|
||||
const rawSearch = filterSearch.slice(0, sliceResult.length);
|
||||
|
||||
const quoteText = sliceResult ? `知识库:\n${sliceResult}` : '';
|
||||
|
||||
return {
|
||||
rawSearch,
|
||||
userSystemPrompt,
|
||||
userLimitPrompt,
|
||||
quotePrompt: {
|
||||
obj: ChatRoleEnum.System,
|
||||
value: quoteText
|
||||
}
|
||||
};
|
||||
}
|
@@ -15,6 +15,7 @@ type DateItemType = { a: string; q: string; source?: string };
|
||||
export type Props = {
|
||||
kbId: string;
|
||||
data: DateItemType[];
|
||||
model: string;
|
||||
mode: `${TrainingModeEnum}`;
|
||||
prompt?: string;
|
||||
};
|
||||
@@ -25,14 +26,14 @@ export type Response = {
|
||||
|
||||
const modeMaxToken = {
|
||||
[TrainingModeEnum.index]: 6000,
|
||||
[TrainingModeEnum.qa]: 10000
|
||||
[TrainingModeEnum.qa]: 12000
|
||||
};
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const { kbId, data, mode, prompt } = req.body as Props;
|
||||
const { kbId, data, mode, prompt, model } = req.body as Props;
|
||||
|
||||
if (!kbId || !Array.isArray(data)) {
|
||||
if (!kbId || !Array.isArray(data) || !model) {
|
||||
throw new Error('缺少参数');
|
||||
}
|
||||
await connectToDatabase();
|
||||
@@ -46,7 +47,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
data,
|
||||
userId,
|
||||
mode,
|
||||
prompt
|
||||
prompt,
|
||||
model
|
||||
})
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -62,7 +64,8 @@ export async function pushDataToKb({
|
||||
kbId,
|
||||
data,
|
||||
mode,
|
||||
prompt
|
||||
prompt,
|
||||
model
|
||||
}: { userId: string } & Props): Promise<Response> {
|
||||
await authKb({
|
||||
userId,
|
||||
@@ -79,7 +82,7 @@ export async function pushDataToKb({
|
||||
if (mode === TrainingModeEnum.qa) {
|
||||
// count token
|
||||
const token = modelToolMap.countTokens({
|
||||
model: OpenAiChatEnum.GPT3516k,
|
||||
model: 'gpt-3.5-turbo-16k',
|
||||
messages: [{ obj: 'System', value: item.q }]
|
||||
});
|
||||
if (token > modeMaxToken[TrainingModeEnum.qa]) {
|
||||
@@ -144,6 +147,7 @@ export async function pushDataToKb({
|
||||
insertData.map((item) => ({
|
||||
q: item.q,
|
||||
a: item.a,
|
||||
model,
|
||||
source: item.source,
|
||||
userId,
|
||||
kbId,
|
||||
|
@@ -3,7 +3,7 @@ import { jsonRes } from '@/service/response';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
import { PgClient } from '@/service/pg';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import { openaiEmbedding } from '../plugin/openaiEmbedding';
|
||||
import { getVector } from '../plugin/vector';
|
||||
import type { KbTestItemType } from '@/types/plugin';
|
||||
|
||||
export type Props = {
|
||||
@@ -27,7 +27,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
throw new Error('缺少用户ID');
|
||||
}
|
||||
|
||||
const vector = await openaiEmbedding({
|
||||
const vector = await getVector({
|
||||
userId,
|
||||
input: [text]
|
||||
});
|
||||
|
@@ -3,7 +3,7 @@ import { jsonRes } from '@/service/response';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
import { PgClient } from '@/service/pg';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import { openaiEmbedding } from '../plugin/openaiEmbedding';
|
||||
import { getVector } from '../plugin/vector';
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
@@ -19,7 +19,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
// get vector
|
||||
const vector = await (async () => {
|
||||
if (q) {
|
||||
return openaiEmbedding({
|
||||
return getVector({
|
||||
userId,
|
||||
input: [q]
|
||||
});
|
||||
|
@@ -6,12 +6,12 @@ import { ChatContextFilter } from '@/service/utils/chat/index';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import type { ClassifyQuestionAgentItemType } from '@/types/app';
|
||||
import type { RecognizeIntentionAgentItemType } from '@/types/app';
|
||||
|
||||
export type Props = {
|
||||
history?: ChatItemType[];
|
||||
userChatInput: string;
|
||||
agents: ClassifyQuestionAgentItemType[];
|
||||
agents: RecognizeIntentionAgentItemType[];
|
||||
description: string;
|
||||
};
|
||||
export type Response = { history: ChatItemType[] };
|
||||
|
@@ -6,29 +6,30 @@ import { ChatContextFilter } from '@/service/utils/chat/index';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import type { ClassifyQuestionAgentItemType } from '@/types/app';
|
||||
import type { RecognizeIntentionAgentItemType } from '@/types/app';
|
||||
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
||||
|
||||
export type Props = {
|
||||
systemPrompt?: string;
|
||||
history?: ChatItemType[];
|
||||
userChatInput: string;
|
||||
agents: ClassifyQuestionAgentItemType[];
|
||||
agents: RecognizeIntentionAgentItemType[];
|
||||
billId?: string;
|
||||
};
|
||||
export type Response = { history: ChatItemType[] };
|
||||
|
||||
const agentModel = 'gpt-3.5-turbo-16k';
|
||||
const agentModel = 'gpt-3.5-turbo';
|
||||
const agentFunName = 'agent_user_question';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
let { systemPrompt, agents, history = [], userChatInput } = req.body as Props;
|
||||
let { userChatInput } = req.body as Props;
|
||||
|
||||
const response = await classifyQuestion({
|
||||
systemPrompt,
|
||||
history,
|
||||
userChatInput,
|
||||
agents
|
||||
});
|
||||
if (!userChatInput) {
|
||||
throw new Error('userChatInput is empty');
|
||||
}
|
||||
|
||||
const response = await classifyQuestion(req.body);
|
||||
|
||||
jsonRes(res, {
|
||||
data: response
|
||||
@@ -46,7 +47,8 @@ export async function classifyQuestion({
|
||||
agents,
|
||||
systemPrompt,
|
||||
history = [],
|
||||
userChatInput
|
||||
userChatInput,
|
||||
billId
|
||||
}: Props) {
|
||||
const messages: ChatItemType[] = [
|
||||
...(systemPrompt
|
||||
@@ -106,8 +108,19 @@ export async function classifyQuestion({
|
||||
if (!arg.type) {
|
||||
throw new Error('');
|
||||
}
|
||||
|
||||
const totalTokens = response.data.usage?.total_tokens || 0;
|
||||
|
||||
await pushTaskBillListItem({
|
||||
billId,
|
||||
moduleName: 'Recognize Intention',
|
||||
amount: countModelPrice({ model: agentModel, tokens: totalTokens }),
|
||||
model: agentModel,
|
||||
tokenLen: totalTokens
|
||||
});
|
||||
|
||||
console.log(
|
||||
'意图结果',
|
||||
'CQ',
|
||||
agents.findIndex((item) => item.key === arg.type)
|
||||
);
|
||||
|
@@ -1,9 +1,9 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { jsonRes, sseErrRes } from '@/service/response';
|
||||
import { sseResponse } from '@/service/utils/tools';
|
||||
import { ChatModelMap, OpenAiChatEnum } from '@/constants/model';
|
||||
import { adaptChatItem_openAI } from '@/utils/plugin/openai';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
import { adaptChatItem_openAI, countOpenAIToken } from '@/utils/plugin/openai';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
import { ChatContextFilter } from '@/service/utils/chat/index';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
@@ -11,6 +11,8 @@ import { ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
|
||||
import { parseStreamChunk, textAdaptGptResponse } from '@/utils/adapt';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { SpecificInputEnum } from '@/constants/app';
|
||||
import { getChatModel } from '@/service/utils/data';
|
||||
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
||||
|
||||
export type Props = {
|
||||
model: `${OpenAiChatEnum}`;
|
||||
@@ -22,39 +24,28 @@ export type Props = {
|
||||
quotePrompt?: string;
|
||||
systemPrompt?: string;
|
||||
limitPrompt?: string;
|
||||
billId?: string;
|
||||
};
|
||||
export type Response = { [SpecificInputEnum.answerText]: string };
|
||||
export type Response = { [SpecificInputEnum.answerText]: string; totalTokens: number };
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
let { model, temperature = 0, stream } = req.body as Props;
|
||||
try {
|
||||
let {
|
||||
model,
|
||||
stream = false,
|
||||
temperature = 0,
|
||||
maxToken = 4000,
|
||||
history = [],
|
||||
quotePrompt,
|
||||
userChatInput,
|
||||
systemPrompt,
|
||||
limitPrompt
|
||||
} = req.body as Props;
|
||||
|
||||
// temperature adapt
|
||||
const modelConstantsData = ChatModelMap[model];
|
||||
const modelConstantsData = getChatModel(model);
|
||||
|
||||
if (!modelConstantsData) {
|
||||
throw new Error('The chat model is undefined');
|
||||
}
|
||||
|
||||
// FastGpt temperature range: 1~10
|
||||
temperature = +(modelConstantsData.maxTemperature * (temperature / 10)).toFixed(2);
|
||||
|
||||
const response = await chatCompletion({
|
||||
...req.body,
|
||||
res,
|
||||
model,
|
||||
temperature,
|
||||
maxToken,
|
||||
stream,
|
||||
history,
|
||||
userChatInput,
|
||||
systemPrompt,
|
||||
limitPrompt,
|
||||
quotePrompt
|
||||
temperature
|
||||
});
|
||||
|
||||
if (stream) {
|
||||
@@ -70,25 +61,32 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
if (stream) {
|
||||
res.status(500);
|
||||
sseErrRes(res, err);
|
||||
res.end();
|
||||
} else {
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* request openai chat */
|
||||
export async function chatCompletion({
|
||||
res,
|
||||
model = OpenAiChatEnum.GPT35,
|
||||
temperature,
|
||||
model,
|
||||
temperature = 0,
|
||||
maxToken = 4000,
|
||||
stream,
|
||||
stream = false,
|
||||
history = [],
|
||||
quotePrompt,
|
||||
quotePrompt = '',
|
||||
userChatInput,
|
||||
systemPrompt,
|
||||
limitPrompt
|
||||
systemPrompt = '',
|
||||
limitPrompt = '',
|
||||
billId
|
||||
}: Props & { res: NextApiResponse }): Promise<Response> {
|
||||
const messages: ChatItemType[] = [
|
||||
...(quotePrompt
|
||||
@@ -121,7 +119,7 @@ export async function chatCompletion({
|
||||
value: userChatInput
|
||||
}
|
||||
];
|
||||
const modelTokenLimit = ChatModelMap[model]?.contextMaxToken || 4000;
|
||||
const modelTokenLimit = getChatModel(model)?.contextMaxToken || 4000;
|
||||
|
||||
const filterMessages = ChatContextFilter({
|
||||
model,
|
||||
@@ -157,37 +155,47 @@ export async function chatCompletion({
|
||||
}
|
||||
);
|
||||
|
||||
const { answer } = await (async () => {
|
||||
const { answer, totalTokens } = await (async () => {
|
||||
if (stream) {
|
||||
// sse response
|
||||
const { answer } = await streamResponse({ res, response });
|
||||
// count tokens
|
||||
// const finishMessages = filterMessages.concat({
|
||||
// obj: ChatRoleEnum.AI,
|
||||
// value: answer
|
||||
// });
|
||||
const finishMessages = filterMessages.concat({
|
||||
obj: ChatRoleEnum.AI,
|
||||
value: answer
|
||||
});
|
||||
|
||||
// const totalTokens = modelToolMap[model].countTokens({
|
||||
// messages: finishMessages
|
||||
// });
|
||||
const totalTokens = countOpenAIToken({
|
||||
messages: finishMessages,
|
||||
model: 'gpt-3.5-turbo-16k'
|
||||
});
|
||||
|
||||
return {
|
||||
answer
|
||||
// totalTokens
|
||||
answer,
|
||||
totalTokens
|
||||
};
|
||||
} else {
|
||||
const answer = stream ? '' : response.data.choices?.[0].message?.content || '';
|
||||
// const totalTokens = stream ? 0 : response.data.usage?.total_tokens || 0;
|
||||
const totalTokens = stream ? 0 : response.data.usage?.total_tokens || 0;
|
||||
|
||||
return {
|
||||
answer
|
||||
// totalTokens
|
||||
answer,
|
||||
totalTokens
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
await pushTaskBillListItem({
|
||||
billId,
|
||||
moduleName: 'AI Chat',
|
||||
amount: countModelPrice({ model, tokens: totalTokens }),
|
||||
model,
|
||||
tokenLen: totalTokens
|
||||
});
|
||||
|
||||
return {
|
||||
answerText: answer
|
||||
answerText: answer,
|
||||
totalTokens
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -4,8 +4,9 @@ import { PgClient } from '@/service/pg';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { openaiEmbedding_system } from '../../plugin/openaiEmbedding';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
import { getVector } from '../../plugin/vector';
|
||||
import { countModelPrice, pushTaskBillListItem } from '@/service/events/pushBill';
|
||||
|
||||
export type QuoteItemType = {
|
||||
id: string;
|
||||
@@ -21,6 +22,7 @@ type Props = {
|
||||
maxToken: number;
|
||||
userChatInput: string;
|
||||
stream?: boolean;
|
||||
billId?: string;
|
||||
};
|
||||
type Response = {
|
||||
rawSearch: QuoteItemType[];
|
||||
@@ -30,25 +32,15 @@ type Response = {
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const {
|
||||
kb_ids = [],
|
||||
history = [],
|
||||
similarity,
|
||||
limit,
|
||||
maxToken,
|
||||
userChatInput
|
||||
} = req.body as Props;
|
||||
const { kb_ids = [], userChatInput } = req.body as Props;
|
||||
|
||||
if (!similarity || !Array.isArray(kb_ids)) {
|
||||
if (!userChatInput || !Array.isArray(kb_ids)) {
|
||||
throw new Error('params is error');
|
||||
}
|
||||
|
||||
const result = await kbSearch({
|
||||
...req.body,
|
||||
kb_ids,
|
||||
history,
|
||||
similarity,
|
||||
limit,
|
||||
maxToken,
|
||||
userChatInput
|
||||
});
|
||||
|
||||
@@ -70,7 +62,8 @@ export async function kbSearch({
|
||||
similarity = 0.8,
|
||||
limit = 5,
|
||||
maxToken = 2500,
|
||||
userChatInput
|
||||
userChatInput,
|
||||
billId
|
||||
}: Props): Promise<Response> {
|
||||
if (kb_ids.length === 0)
|
||||
return {
|
||||
@@ -78,22 +71,34 @@ export async function kbSearch({
|
||||
rawSearch: [],
|
||||
quotePrompt: undefined
|
||||
};
|
||||
|
||||
// get vector
|
||||
const promptVector = await openaiEmbedding_system({
|
||||
const vectorModel = global.vectorModels[0].model;
|
||||
const { vectors, tokenLen } = await getVector({
|
||||
model: vectorModel,
|
||||
input: [userChatInput]
|
||||
});
|
||||
|
||||
// search kb
|
||||
const res: any = await PgClient.query(
|
||||
`BEGIN;
|
||||
const [res]: any = await Promise.all([
|
||||
PgClient.query(
|
||||
`BEGIN;
|
||||
SET LOCAL ivfflat.probes = ${global.systemEnv.pgIvfflatProbe || 10};
|
||||
select id,q,a,source from modelData where kb_id IN (${kb_ids
|
||||
.map((item) => `'${item}'`)
|
||||
.join(',')}) AND vector <#> '[${promptVector[0]}]' < -${similarity} order by vector <#> '[${
|
||||
promptVector[0]
|
||||
}]' limit ${limit};
|
||||
.join(',')}) AND vector <#> '[${vectors[0]}]' < -${similarity} order by vector <#> '[${
|
||||
vectors[0]
|
||||
}]' limit ${limit};
|
||||
COMMIT;`
|
||||
);
|
||||
),
|
||||
pushTaskBillListItem({
|
||||
billId,
|
||||
moduleName: 'Vector Generate',
|
||||
amount: countModelPrice({ model: vectorModel, tokens: tokenLen }),
|
||||
model: vectorModel,
|
||||
tokenLen
|
||||
})
|
||||
]);
|
||||
|
||||
const searchRes: QuoteItemType[] = res?.[2]?.rows || [];
|
||||
|
||||
|
@@ -1,115 +0,0 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
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';
|
||||
import { axiosConfig } from '@/service/utils/tools';
|
||||
import { pushGenerateVectorBill } from '@/service/events/pushBill';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
|
||||
type Props = {
|
||||
input: string[];
|
||||
};
|
||||
type Response = number[][];
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const { userId } = await authUser({ req });
|
||||
let { input } = req.query as Props;
|
||||
|
||||
if (!Array.isArray(input)) {
|
||||
throw new Error('缺少参数');
|
||||
}
|
||||
|
||||
jsonRes<Response>(res, {
|
||||
data: await openaiEmbedding({ userId, input, mustPay: true })
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export async function openaiEmbedding({
|
||||
userId,
|
||||
input,
|
||||
mustPay = false
|
||||
}: { userId: string; mustPay?: boolean } & Props) {
|
||||
const { userOpenAiKey, systemAuthKey } = await getApiKey({
|
||||
model: 'gpt-3.5-turbo',
|
||||
userId,
|
||||
mustPay
|
||||
});
|
||||
const apiKey = userOpenAiKey || systemAuthKey;
|
||||
|
||||
// 获取 chatAPI
|
||||
const chatAPI = getOpenAIApi(apiKey);
|
||||
|
||||
// 把输入的内容转成向量
|
||||
const result = await chatAPI
|
||||
.createEmbedding(
|
||||
{
|
||||
model: embeddingModel,
|
||||
input
|
||||
},
|
||||
{
|
||||
timeout: 60000,
|
||||
...axiosConfig(apiKey)
|
||||
}
|
||||
)
|
||||
.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,
|
||||
userId,
|
||||
text: input.join(''),
|
||||
tokenLen: result.tokenLen
|
||||
});
|
||||
|
||||
return result.vectors;
|
||||
}
|
||||
|
||||
export async function openaiEmbedding_system({ input }: Props) {
|
||||
const apiKey = getSystemOpenAiKey();
|
||||
|
||||
// 获取 chatAPI
|
||||
const chatAPI = getOpenAIApi(apiKey);
|
||||
|
||||
// 把输入的内容转成向量
|
||||
const result = await chatAPI
|
||||
.createEmbedding(
|
||||
{
|
||||
model: embeddingModel,
|
||||
input
|
||||
},
|
||||
{
|
||||
timeout: 20000,
|
||||
...axiosConfig(apiKey)
|
||||
}
|
||||
)
|
||||
.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)
|
||||
};
|
||||
});
|
||||
|
||||
return result.vectors;
|
||||
}
|
79
client/src/pages/api/openapi/plugin/vector.ts
Normal file
79
client/src/pages/api/openapi/plugin/vector.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { authBalanceByUid, authUser } from '@/service/utils/auth';
|
||||
import { withNextCors } from '@/service/utils/tools';
|
||||
import { getOpenAIApi, axiosConfig } from '@/service/ai/openai';
|
||||
import { pushGenerateVectorBill } from '@/service/events/pushBill';
|
||||
|
||||
type Props = {
|
||||
model: string;
|
||||
input: string[];
|
||||
};
|
||||
type Response = {
|
||||
tokenLen: number;
|
||||
vectors: number[][];
|
||||
};
|
||||
|
||||
export default withNextCors(async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
|
||||
try {
|
||||
const { userId } = await authUser({ req });
|
||||
let { input, model } = req.query as Props;
|
||||
|
||||
if (!Array.isArray(input)) {
|
||||
throw new Error('缺少参数');
|
||||
}
|
||||
|
||||
jsonRes<Response>(res, {
|
||||
data: await getVector({ userId, input, model })
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
jsonRes(res, {
|
||||
code: 500,
|
||||
error: err
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export async function getVector({
|
||||
model = 'text-embedding-ada-002',
|
||||
userId,
|
||||
input
|
||||
}: { userId?: string } & Props) {
|
||||
userId && (await authBalanceByUid(userId));
|
||||
|
||||
// 获取 chatAPI
|
||||
const chatAPI = getOpenAIApi();
|
||||
|
||||
// 把输入的内容转成向量
|
||||
const result = await chatAPI
|
||||
.createEmbedding(
|
||||
{
|
||||
model,
|
||||
input
|
||||
},
|
||||
{
|
||||
timeout: 60000,
|
||||
...axiosConfig()
|
||||
}
|
||||
)
|
||||
.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)
|
||||
};
|
||||
});
|
||||
|
||||
userId &&
|
||||
pushGenerateVectorBill({
|
||||
userId,
|
||||
tokenLen: result.tokenLen,
|
||||
model
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
@@ -15,8 +15,8 @@ import { Types } from 'mongoose';
|
||||
import { moduleFetch } from '@/service/api/request';
|
||||
import { AppModuleItemType, RunningModuleItemType } from '@/types/app';
|
||||
import { FlowInputItemTypeEnum } from '@/constants/flow';
|
||||
import { pushChatBill } from '@/service/events/pushBill';
|
||||
import { BillTypeEnum } from '@/constants/user';
|
||||
import { finishTaskBill, createTaskBill } from '@/service/events/pushBill';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
|
||||
export type MessageItemType = ChatCompletionRequestMessage & { _id?: string };
|
||||
type FastGptWebChatProps = {
|
||||
@@ -108,6 +108,13 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
res.setHeader('newHistoryId', String(newHistoryId));
|
||||
}
|
||||
|
||||
const billId = await createTaskBill({
|
||||
userId,
|
||||
appName: app.name,
|
||||
appId,
|
||||
source: BillSourceEnum.fastgpt
|
||||
});
|
||||
|
||||
/* start process */
|
||||
const { responseData, answerText } = await dispatchModules({
|
||||
res,
|
||||
@@ -117,7 +124,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
history: prompts,
|
||||
userChatInput: prompt.value
|
||||
},
|
||||
stream
|
||||
stream,
|
||||
billId: ''
|
||||
});
|
||||
|
||||
// save chat
|
||||
@@ -171,14 +179,9 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
|
||||
});
|
||||
}
|
||||
|
||||
pushChatBill({
|
||||
isPay: true,
|
||||
chatModel: 'gpt-3.5-turbo',
|
||||
userId,
|
||||
appId,
|
||||
textLen: 1,
|
||||
tokens: 100,
|
||||
type: BillTypeEnum.chat
|
||||
// bill
|
||||
finishTaskBill({
|
||||
billId
|
||||
});
|
||||
} catch (err: any) {
|
||||
if (stream) {
|
||||
@@ -199,18 +202,21 @@ export async function dispatchModules({
|
||||
modules,
|
||||
params = {},
|
||||
variables = {},
|
||||
stream = false
|
||||
stream = false,
|
||||
billId
|
||||
}: {
|
||||
res: NextApiResponse;
|
||||
modules: AppModuleItemType[];
|
||||
params?: Record<string, any>;
|
||||
variables?: Record<string, any>;
|
||||
billId: string;
|
||||
stream?: boolean;
|
||||
}) {
|
||||
const runningModules = loadModules(modules, variables);
|
||||
let storeData: Record<string, any> = {};
|
||||
let responseData: Record<string, any> = {};
|
||||
let answerText = '';
|
||||
|
||||
let storeData: Record<string, any> = {}; // after module used
|
||||
let responseData: Record<string, any> = {}; // response request and save to database
|
||||
let answerText = ''; // AI answer
|
||||
|
||||
function pushStore({
|
||||
isResponse = false,
|
||||
@@ -327,6 +333,7 @@ export async function dispatchModules({
|
||||
});
|
||||
const data = {
|
||||
stream,
|
||||
billId,
|
||||
...params
|
||||
};
|
||||
|
||||
|
@@ -1,19 +1,114 @@
|
||||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import {
|
||||
type QAModelItemType,
|
||||
type ChatModelItemType,
|
||||
type VectorModelItemType
|
||||
} from '@/types/model';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
export type InitDateResponse = {
|
||||
beianText: string;
|
||||
googleVerKey: string;
|
||||
baiduTongji: boolean;
|
||||
baiduTongji: string;
|
||||
chatModels: ChatModelItemType[];
|
||||
qaModels: QAModelItemType[];
|
||||
vectorModels: VectorModelItemType[];
|
||||
};
|
||||
|
||||
const defaultmodels = {
|
||||
'Gpt35-4k': {
|
||||
model: 'gpt-3.5-turbo',
|
||||
name: 'Gpt35-4k',
|
||||
contextMaxToken: 4000,
|
||||
systemMaxToken: 2400,
|
||||
maxTemperature: 1.2,
|
||||
price: 1.5
|
||||
},
|
||||
'Gpt35-16k': {
|
||||
model: 'gpt-3.5-turbo',
|
||||
name: 'Gpt35-16k',
|
||||
contextMaxToken: 16000,
|
||||
systemMaxToken: 8000,
|
||||
maxTemperature: 1.2,
|
||||
price: 3
|
||||
},
|
||||
Gpt4: {
|
||||
model: 'gpt-4',
|
||||
name: 'Gpt4',
|
||||
contextMaxToken: 8000,
|
||||
systemMaxToken: 4000,
|
||||
maxTemperature: 1.2,
|
||||
price: 45
|
||||
}
|
||||
};
|
||||
const defaultQaModels = {
|
||||
'Gpt35-16k': {
|
||||
model: 'gpt-3.5-turbo',
|
||||
name: 'Gpt35-16k',
|
||||
maxToken: 16000,
|
||||
price: 3
|
||||
}
|
||||
};
|
||||
const defaultVectorModels = {
|
||||
'text-embedding-ada-002': {
|
||||
model: 'text-embedding-ada-002',
|
||||
name: 'Embedding-2',
|
||||
price: 0.2
|
||||
}
|
||||
};
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const envs = {
|
||||
beianText: process.env.SAFE_BEIAN_TEXT || '',
|
||||
googleVerKey: process.env.CLIENT_GOOGLE_VER_TOKEN || '',
|
||||
baiduTongji: process.env.BAIDU_TONGJI || ''
|
||||
};
|
||||
|
||||
jsonRes<InitDateResponse>(res, {
|
||||
data: {
|
||||
beianText: process.env.SAFE_BEIAN_TEXT || '',
|
||||
googleVerKey: process.env.CLIENT_GOOGLE_VER_TOKEN || '',
|
||||
baiduTongji: process.env.BAIDU_TONGJI === '1'
|
||||
...envs,
|
||||
...initSystemModels()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function initSystemModels() {
|
||||
const { chatModels, qaModels, vectorModels } = (() => {
|
||||
try {
|
||||
const chatModels = Object.values(JSON.parse(readFileSync('data/ChatModels.json', 'utf-8')));
|
||||
const qaModels = Object.values(JSON.parse(readFileSync('data/QAModels.json', 'utf-8')));
|
||||
const vectorModels = Object.values(
|
||||
JSON.parse(readFileSync('data/VectorModels.json', 'utf-8'))
|
||||
);
|
||||
|
||||
return {
|
||||
chatModels,
|
||||
qaModels,
|
||||
vectorModels
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
return {
|
||||
chatModels: Object.values(defaultmodels),
|
||||
qaModels: Object.values(defaultQaModels),
|
||||
vectorModels: Object.values(defaultVectorModels)
|
||||
};
|
||||
}
|
||||
})() as {
|
||||
chatModels: ChatModelItemType[];
|
||||
qaModels: QAModelItemType[];
|
||||
vectorModels: VectorModelItemType[];
|
||||
};
|
||||
global.chatModels = chatModels;
|
||||
global.qaModels = qaModels;
|
||||
global.vectorModels = vectorModels;
|
||||
|
||||
return {
|
||||
chatModels,
|
||||
qaModels,
|
||||
vectorModels
|
||||
};
|
||||
}
|
||||
|
@@ -1,31 +1,22 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { jsonRes } from '@/service/response';
|
||||
import { System } from '@/service/models/system';
|
||||
import { authUser } from '@/service/utils/auth';
|
||||
|
||||
export type InitDateResponse = {
|
||||
beianText: string;
|
||||
googleVerKey: string;
|
||||
};
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
await authUser({ req, authRoot: true });
|
||||
updateSystemEnv();
|
||||
jsonRes<InitDateResponse>(res);
|
||||
jsonRes(res);
|
||||
}
|
||||
|
||||
export async function updateSystemEnv() {
|
||||
try {
|
||||
const mongoData = await System.findOne();
|
||||
const res = JSON.parse(readFileSync('data/SystemParams.json', 'utf-8'));
|
||||
|
||||
if (mongoData) {
|
||||
const obj = mongoData.toObject();
|
||||
global.systemEnv = {
|
||||
...global.systemEnv,
|
||||
...obj
|
||||
};
|
||||
}
|
||||
console.log('update env', global.systemEnv);
|
||||
global.systemEnv = {
|
||||
...global.systemEnv,
|
||||
...res
|
||||
};
|
||||
} catch (error) {
|
||||
console.log('update system env error');
|
||||
}
|
||||
|
@@ -15,6 +15,8 @@ import { SystemInputEnum } from '@/constants/app';
|
||||
import { streamFetch } from '@/api/fetch';
|
||||
import MyTooltip from '@/components/MyTooltip';
|
||||
import ChatBox, { type ComponentRef, type StartChatFnProps } from '@/components/ChatBox';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { getErrText } from '@/utils/tools';
|
||||
|
||||
export type ChatTestComponentRef = {
|
||||
resetChatTest: () => void;
|
||||
@@ -34,6 +36,7 @@ const ChatTest = (
|
||||
) => {
|
||||
const BoxRef = useRef(null);
|
||||
const ChatBoxRef = useRef<ComponentRef>(null);
|
||||
const { toast } = useToast();
|
||||
const isOpen = useMemo(() => modules && modules.length > 0, [modules]);
|
||||
|
||||
const variableModules = useMemo(
|
||||
@@ -60,21 +63,30 @@ const ChatTest = (
|
||||
const history = messages.slice(-historyMaxLen - 2, -2);
|
||||
|
||||
// 流请求,获取数据
|
||||
const { responseText } = await streamFetch({
|
||||
const { responseText, errMsg } = await streamFetch({
|
||||
url: '/api/chat/chatTest',
|
||||
data: {
|
||||
history,
|
||||
prompt: messages[messages.length - 2].content,
|
||||
modules,
|
||||
variables
|
||||
variables,
|
||||
appId: app._id,
|
||||
appName: `调试-${app.name}`
|
||||
},
|
||||
onMessage: generatingMessage,
|
||||
abortSignal: controller
|
||||
});
|
||||
|
||||
if (errMsg) {
|
||||
return Promise.reject({
|
||||
message: errMsg,
|
||||
responseText
|
||||
});
|
||||
}
|
||||
|
||||
return { responseText };
|
||||
},
|
||||
[modules]
|
||||
[app._id, app.name, modules]
|
||||
);
|
||||
|
||||
useOutsideClick({
|
||||
|
@@ -6,14 +6,14 @@ import { FlowModuleItemType } from '@/types/flow';
|
||||
import Divider from './modules/Divider';
|
||||
import Container from './modules/Container';
|
||||
import RenderInput from './render/RenderInput';
|
||||
import type { ClassifyQuestionAgentItemType } from '@/types/app';
|
||||
import type { RecognizeIntentionAgentItemType } from '@/types/app';
|
||||
import { Handle, Position } from 'reactflow';
|
||||
import { customAlphabet } from 'nanoid';
|
||||
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 4);
|
||||
import MyIcon from '@/components/Icon';
|
||||
import { FlowOutputItemTypeEnum } from '@/constants/flow';
|
||||
|
||||
const NodeCQNode = ({
|
||||
const NodeRINode = ({
|
||||
data: { moduleId, inputs, outputs, onChangeNode, ...props }
|
||||
}: NodeProps<FlowModuleItemType>) => {
|
||||
return (
|
||||
@@ -30,7 +30,7 @@ const NodeCQNode = ({
|
||||
value: agents = []
|
||||
}: {
|
||||
key: string;
|
||||
value?: ClassifyQuestionAgentItemType[];
|
||||
value?: RecognizeIntentionAgentItemType[];
|
||||
}) => (
|
||||
<Box>
|
||||
{agents.map((item, i) => (
|
||||
@@ -133,4 +133,4 @@ const NodeCQNode = ({
|
||||
</NodeCard>
|
||||
);
|
||||
};
|
||||
export default React.memo(NodeCQNode);
|
||||
export default React.memo(NodeRINode);
|
@@ -49,7 +49,7 @@ const NodeAnswer = dynamic(() => import('./components/NodeAnswer'), {
|
||||
const NodeQuestionInput = dynamic(() => import('./components/NodeQuestionInput'), {
|
||||
ssr: false
|
||||
});
|
||||
const NodeCQNode = dynamic(() => import('./components/NodeCQNode'), {
|
||||
const NodeRINode = dynamic(() => import('./components/NodeRINode'), {
|
||||
ssr: false
|
||||
});
|
||||
const NodeUserGuide = dynamic(() => import('./components/NodeUserGuide'), {
|
||||
@@ -70,7 +70,7 @@ const nodeTypes = {
|
||||
[FlowModuleTypeEnum.kbSearchNode]: NodeKbSearch,
|
||||
[FlowModuleTypeEnum.tfSwitchNode]: NodeTFSwitch,
|
||||
[FlowModuleTypeEnum.answerNode]: NodeAnswer,
|
||||
[FlowModuleTypeEnum.classifyQuestionNode]: NodeCQNode
|
||||
[FlowModuleTypeEnum.recognizeIntention]: NodeRINode
|
||||
};
|
||||
const edgeTypes = {
|
||||
buttonedge: ButtonEdge
|
||||
|
@@ -9,7 +9,6 @@ import {
|
||||
Box,
|
||||
useTheme
|
||||
} from '@chakra-ui/react';
|
||||
import { QuoteItemType } from '@/pages/api/openapi/kb/appKbSearch';
|
||||
import MyIcon from '@/components/Icon';
|
||||
import InputDataModal from '@/pages/kb/components/InputDataModal';
|
||||
import { getKbDataItemById } from '@/api/plugins/kb';
|
||||
|
@@ -4,6 +4,7 @@ import Markdown from '@/components/Markdown';
|
||||
import { useMarkdown } from '@/hooks/useMarkdown';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useGlobalStore } from '@/store/global';
|
||||
import { beianText } from '@/store/static';
|
||||
|
||||
import styles from './index.module.scss';
|
||||
import axios from 'axios';
|
||||
@@ -13,10 +14,7 @@ const Home = () => {
|
||||
const router = useRouter();
|
||||
const { inviterId } = router.query as { inviterId: string };
|
||||
const { data } = useMarkdown({ url: '/intro.md' });
|
||||
const {
|
||||
isPc,
|
||||
initData: { beianText }
|
||||
} = useGlobalStore();
|
||||
const { isPc } = useGlobalStore();
|
||||
const [star, setStar] = useState(1500);
|
||||
|
||||
useEffect(() => {
|
||||
|
@@ -17,6 +17,7 @@ import { useToast } from '@/hooks/useToast';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import { getErrText } from '@/utils/tools';
|
||||
import MyIcon from '@/components/Icon';
|
||||
import { vectorModelList } from '@/store/static';
|
||||
|
||||
export type FormData = { dataId?: string; a: string; q: string };
|
||||
|
||||
@@ -65,6 +66,7 @@ const InputDataModal = ({
|
||||
};
|
||||
const { insertLen } = await postKbDataFromList({
|
||||
kbId,
|
||||
model: vectorModelList[0].model,
|
||||
mode: TrainingModeEnum.index,
|
||||
data: [data]
|
||||
});
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useCallback, useRef } from 'react';
|
||||
import React, { useState, useCallback } from 'react';
|
||||
import {
|
||||
Box,
|
||||
Flex,
|
||||
@@ -22,9 +22,9 @@ import Radio from '@/components/Radio';
|
||||
import { splitText_token } from '@/utils/file';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import { getErrText } from '@/utils/tools';
|
||||
import { ChatModelMap, OpenAiChatEnum, embeddingPrice } from '@/constants/model';
|
||||
import { formatPrice } from '@/utils/user';
|
||||
import MySlider from '@/components/Slider';
|
||||
import { qaModelList, vectorModelList } from '@/store/static';
|
||||
|
||||
const fileExtension = '.txt,.doc,.docx,.pdf,.md';
|
||||
|
||||
@@ -39,12 +39,14 @@ const SelectFileModal = ({
|
||||
}) => {
|
||||
const [modeMap, setModeMap] = useState({
|
||||
[TrainingModeEnum.qa]: {
|
||||
maxLen: 8000,
|
||||
price: ChatModelMap[OpenAiChatEnum.GPT3516k].price
|
||||
model: qaModelList[0].model,
|
||||
maxLen: (qaModelList[0]?.maxToken || 16000) * 0.5,
|
||||
price: qaModelList[0]?.price || 3
|
||||
},
|
||||
[TrainingModeEnum.index]: {
|
||||
model: vectorModelList[0].model,
|
||||
maxLen: 600,
|
||||
price: embeddingPrice
|
||||
price: vectorModelList[0]?.price || 0.2
|
||||
}
|
||||
});
|
||||
const [btnLoading, setBtnLoading] = useState(false);
|
||||
@@ -111,6 +113,7 @@ const SelectFileModal = ({
|
||||
},
|
||||
[toast]
|
||||
);
|
||||
console.log({ model: modeMap[mode].model });
|
||||
|
||||
const { mutate, isLoading: uploading } = useMutation({
|
||||
mutationFn: async () => {
|
||||
@@ -122,6 +125,7 @@ const SelectFileModal = ({
|
||||
for (let i = 0; i < splitRes.chunks.length; i += step) {
|
||||
const { insertLen } = await postKbDataFromList({
|
||||
kbId,
|
||||
model: modeMap[mode].model,
|
||||
data: splitRes.chunks
|
||||
.slice(i, i + step)
|
||||
.map((item) => ({ q: item.value, a: '', source: item.filename })),
|
||||
@@ -275,8 +279,8 @@ const SelectFileModal = ({
|
||||
setModeMap((state) => ({
|
||||
...state,
|
||||
[TrainingModeEnum.index]: {
|
||||
maxLen: val,
|
||||
price: embeddingPrice
|
||||
...modeMap[TrainingModeEnum.index],
|
||||
maxLen: val
|
||||
}
|
||||
}));
|
||||
}}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import React, { useState } from 'react';
|
||||
import { Table, Thead, Tbody, Tr, Th, Td, TableContainer, Flex, Box } from '@chakra-ui/react';
|
||||
import { BillTypeMap } from '@/constants/user';
|
||||
import { BillSourceMap } from '@/constants/user';
|
||||
import { getUserBills } from '@/api/user';
|
||||
import type { UserBillType } from '@/types/user';
|
||||
import { usePagination } from '@/hooks/usePagination';
|
||||
@@ -39,10 +39,8 @@ const BillTable = () => {
|
||||
<Thead>
|
||||
<Tr>
|
||||
<Th>时间</Th>
|
||||
<Th>类型</Th>
|
||||
<Th>模型</Th>
|
||||
<Th>内容长度</Th>
|
||||
<Th>Tokens 长度</Th>
|
||||
<Th>来源</Th>
|
||||
<Th>应用名</Th>
|
||||
<Th>金额</Th>
|
||||
</Tr>
|
||||
</Thead>
|
||||
@@ -50,11 +48,9 @@ const BillTable = () => {
|
||||
{bills.map((item) => (
|
||||
<Tr key={item.id}>
|
||||
<Td>{dayjs(item.time).format('YYYY/MM/DD HH:mm:ss')}</Td>
|
||||
<Td>{BillTypeMap[item.type] || '-'}</Td>
|
||||
<Td>{item.modelName}</Td>
|
||||
<Td>{item.textLen}</Td>
|
||||
<Td>{item.tokenLen}</Td>
|
||||
<Td>{item.price}元</Td>
|
||||
<Td>{BillSourceMap[item.source]}</Td>
|
||||
<Td>{item.appName || '-'}</Td>
|
||||
<Td>{item.total}元</Td>
|
||||
</Tr>
|
||||
))}
|
||||
</Tbody>
|
||||
|
114
client/src/service/api/axios.ts
Normal file
114
client/src/service/api/axios.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import axios, { Method, InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
|
||||
interface ConfigType {
|
||||
headers?: { [key: string]: string };
|
||||
hold?: boolean;
|
||||
timeout?: number;
|
||||
}
|
||||
interface ResponseDataType {
|
||||
code: number;
|
||||
message: string;
|
||||
data: any;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求开始
|
||||
*/
|
||||
function requestStart(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig {
|
||||
if (config.headers) {
|
||||
// config.headers.Authorization = getToken();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求成功,检查请求头
|
||||
*/
|
||||
function responseSuccess(response: AxiosResponse<ResponseDataType>) {
|
||||
return response;
|
||||
}
|
||||
/**
|
||||
* 响应数据检查
|
||||
*/
|
||||
function checkRes(data: ResponseDataType) {
|
||||
if (data === undefined) {
|
||||
console.log('error->', data, 'data is empty');
|
||||
return Promise.reject('服务器异常');
|
||||
} else if (data.code < 200 || data.code >= 400) {
|
||||
return Promise.reject(data);
|
||||
}
|
||||
return data.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应错误
|
||||
*/
|
||||
function responseError(err: any) {
|
||||
console.log('error->', '请求错误', err);
|
||||
|
||||
if (!err) {
|
||||
return Promise.reject({ message: '未知错误' });
|
||||
}
|
||||
if (typeof err === 'string') {
|
||||
return Promise.reject({ message: err });
|
||||
}
|
||||
return Promise.reject(err);
|
||||
}
|
||||
|
||||
/* 创建请求实例 */
|
||||
const instance = axios.create({
|
||||
timeout: 60000, // 超时时间
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
/* 请求拦截 */
|
||||
instance.interceptors.request.use(requestStart, (err) => Promise.reject(err));
|
||||
/* 响应拦截 */
|
||||
instance.interceptors.response.use(responseSuccess, (err) => Promise.reject(err));
|
||||
|
||||
function request(url: string, data: any, config: ConfigType, method: Method): any {
|
||||
/* 去空 */
|
||||
for (const key in data) {
|
||||
if (data[key] === null || data[key] === undefined) {
|
||||
delete data[key];
|
||||
}
|
||||
}
|
||||
|
||||
return instance
|
||||
.request({
|
||||
baseURL: `http://localhost:${process.env.PORT || 3000}/api`,
|
||||
url,
|
||||
method,
|
||||
data: ['POST', 'PUT'].includes(method) ? data : null,
|
||||
params: !['POST', 'PUT'].includes(method) ? data : null,
|
||||
...config // 用户自定义配置,可以覆盖前面的配置
|
||||
})
|
||||
.then((res) => checkRes(res.data))
|
||||
.catch((err) => responseError(err));
|
||||
}
|
||||
|
||||
/**
|
||||
* api请求方式
|
||||
* @param {String} url
|
||||
* @param {Any} params
|
||||
* @param {Object} config
|
||||
* @returns
|
||||
*/
|
||||
export function GET<T>(url: string, params = {}, config: ConfigType = {}): Promise<T> {
|
||||
return request(url, params, config, 'GET');
|
||||
}
|
||||
|
||||
export function POST<T>(url: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
return request(url, data, config, 'POST');
|
||||
}
|
||||
|
||||
export function PUT<T>(url: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
return request(url, data, config, 'PUT');
|
||||
}
|
||||
|
||||
export function DELETE<T>(url: string, data = {}, config: ConfigType = {}): Promise<T> {
|
||||
return request(url, data, config, 'DELETE');
|
||||
}
|
@@ -93,6 +93,8 @@ export const moduleFetch = ({ url, data, res }: Props) =>
|
||||
event: sseResponseEventEnum.answer,
|
||||
data: JSON.stringify(data)
|
||||
});
|
||||
} else if (item.event === sseResponseEventEnum.error) {
|
||||
return reject(getErrText(data, '流响应错误'));
|
||||
}
|
||||
});
|
||||
read();
|
||||
|
@@ -1,15 +1,16 @@
|
||||
import { TrainingData } from '@/service/mongo';
|
||||
import { getApiKey } from '../utils/auth';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
import { pushSplitDataBill } from '@/service/events/pushBill';
|
||||
import { openaiAccountError } from '../errorCode';
|
||||
import { modelServiceToolMap } from '../utils/chat';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { BillTypeEnum } from '@/constants/user';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
import { pushDataToKb } from '@/pages/api/openapi/kb/pushData';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import { ERROR_ENUM } from '../errorCode';
|
||||
import { sendInform } from '@/pages/api/user/inform/send';
|
||||
import { authBalanceByUid } from '../utils/auth';
|
||||
import { axiosConfig, getOpenAIApi } from '../ai/openai';
|
||||
import { ChatCompletionRequestMessage } from 'openai';
|
||||
|
||||
const reduceQueue = () => {
|
||||
global.qaQueueLen = global.qaQueueLen > 0 ? global.qaQueueLen - 1 : 0;
|
||||
@@ -37,7 +38,8 @@ export async function generateQA(): Promise<any> {
|
||||
kbId: 1,
|
||||
prompt: 1,
|
||||
q: 1,
|
||||
source: 1
|
||||
source: 1,
|
||||
model: 1
|
||||
});
|
||||
|
||||
// task preemption
|
||||
@@ -51,54 +53,59 @@ export async function generateQA(): Promise<any> {
|
||||
userId = String(data.userId);
|
||||
const kbId = String(data.kbId);
|
||||
|
||||
// 余额校验并获取 openapi Key
|
||||
const { systemAuthKey } = await getApiKey({
|
||||
model: OpenAiChatEnum.GPT35,
|
||||
userId,
|
||||
mustPay: true
|
||||
});
|
||||
await authBalanceByUid(userId);
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
const chatAPI = getOpenAIApi();
|
||||
|
||||
// 请求 chatgpt 获取回答
|
||||
const response = await Promise.all(
|
||||
[data.q].map((text) =>
|
||||
modelServiceToolMap
|
||||
.chatCompletion({
|
||||
model: OpenAiChatEnum.GPT3516k,
|
||||
apiKey: systemAuthKey,
|
||||
temperature: 0.8,
|
||||
messages: [
|
||||
{
|
||||
obj: ChatRoleEnum.System,
|
||||
value: `你是出题人.
|
||||
[data.q].map((text) => {
|
||||
const messages: ChatCompletionRequestMessage[] = [
|
||||
{
|
||||
role: 'system',
|
||||
content: `你是出题人.
|
||||
${data.prompt || '用户会发送一段长文本'}.
|
||||
从中选出 25 个问题和答案. 答案详细完整. 按格式回答: Q1:
|
||||
A1:
|
||||
Q2:
|
||||
A2:
|
||||
...`
|
||||
},
|
||||
{
|
||||
obj: 'Human',
|
||||
value: text
|
||||
}
|
||||
],
|
||||
stream: false
|
||||
})
|
||||
.then(({ totalTokens, responseText, responseMessages }) => {
|
||||
const result = formatSplitText(responseText); // 格式化后的QA对
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: text
|
||||
}
|
||||
];
|
||||
return chatAPI
|
||||
.createChatCompletion(
|
||||
{
|
||||
model: data.model,
|
||||
temperature: 0.8,
|
||||
messages,
|
||||
stream: false
|
||||
},
|
||||
{
|
||||
timeout: 480000,
|
||||
...axiosConfig()
|
||||
}
|
||||
)
|
||||
.then((res) => {
|
||||
const answer = res.data.choices?.[0].message?.content;
|
||||
const totalTokens = res.data.usage?.total_tokens || 0;
|
||||
|
||||
const result = formatSplitText(answer || ''); // 格式化后的QA对
|
||||
console.log(`split result length: `, result.length);
|
||||
// 计费
|
||||
pushSplitDataBill({
|
||||
isPay: result.length > 0,
|
||||
userId: data.userId,
|
||||
type: BillTypeEnum.QA,
|
||||
textLen: responseMessages.map((item) => item.value).join('').length,
|
||||
totalTokens
|
||||
totalTokens,
|
||||
model: data.model,
|
||||
appName: 'QA 拆分'
|
||||
});
|
||||
return {
|
||||
rawContent: responseText,
|
||||
rawContent: answer,
|
||||
result
|
||||
};
|
||||
})
|
||||
@@ -106,8 +113,8 @@ A2:
|
||||
console.log('QA拆分错误');
|
||||
console.log(err.response?.status, err.response?.statusText, err.response?.data);
|
||||
return Promise.reject(err);
|
||||
})
|
||||
)
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const responseList = response.map((item) => item.result).flat();
|
||||
@@ -120,6 +127,7 @@ A2:
|
||||
source: data.source
|
||||
})),
|
||||
userId,
|
||||
model: global.vectorModels[0].model,
|
||||
mode: TrainingModeEnum.index
|
||||
});
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { openaiAccountError } from '../errorCode';
|
||||
import { insertKbItem } from '@/service/pg';
|
||||
import { openaiEmbedding } from '@/pages/api/openapi/plugin/openaiEmbedding';
|
||||
import { getVector } from '@/pages/api/openapi/plugin/vector';
|
||||
import { TrainingData } from '../models/trainingData';
|
||||
import { ERROR_ENUM } from '../errorCode';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
@@ -33,7 +33,8 @@ export async function generateVector(): Promise<any> {
|
||||
kbId: 1,
|
||||
q: 1,
|
||||
a: 1,
|
||||
source: 1
|
||||
source: 1,
|
||||
model: 1
|
||||
});
|
||||
|
||||
// task preemption
|
||||
@@ -55,10 +56,10 @@ export async function generateVector(): Promise<any> {
|
||||
];
|
||||
|
||||
// 生成词向量
|
||||
const vectors = await openaiEmbedding({
|
||||
const vectors = await getVector({
|
||||
model: data.model,
|
||||
input: dataItems.map((item) => item.q),
|
||||
userId,
|
||||
mustPay: true
|
||||
userId
|
||||
});
|
||||
|
||||
// 生成结果插入到 pg
|
||||
|
@@ -1,66 +1,85 @@
|
||||
import { connectToDatabase, Bill, User, ShareChat } from '../mongo';
|
||||
import {
|
||||
ChatModelMap,
|
||||
OpenAiChatEnum,
|
||||
ChatModelType,
|
||||
embeddingModel,
|
||||
embeddingPrice
|
||||
} from '@/constants/model';
|
||||
import { BillTypeEnum } from '@/constants/user';
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
import { getModel } from '../utils/data';
|
||||
import type { BillListItemType } from '@/types/mongoSchema';
|
||||
|
||||
export const pushChatBill = async ({
|
||||
isPay,
|
||||
chatModel,
|
||||
userId,
|
||||
export const createTaskBill = async ({
|
||||
appName,
|
||||
appId,
|
||||
textLen,
|
||||
tokens,
|
||||
type
|
||||
userId,
|
||||
source
|
||||
}: {
|
||||
isPay: boolean;
|
||||
chatModel: ChatModelType;
|
||||
userId: string;
|
||||
appName: string;
|
||||
appId: string;
|
||||
textLen: number;
|
||||
tokens: number;
|
||||
type: BillTypeEnum.chat | BillTypeEnum.openapiChat;
|
||||
userId: string;
|
||||
source: `${BillSourceEnum}`;
|
||||
}) => {
|
||||
console.log(`chat generate success. text len: ${textLen}. token len: ${tokens}. pay:${isPay}`);
|
||||
if (!isPay) return;
|
||||
const res = await Bill.create({
|
||||
userId,
|
||||
appName,
|
||||
appId,
|
||||
total: 0,
|
||||
source,
|
||||
list: []
|
||||
});
|
||||
return String(res._id);
|
||||
};
|
||||
|
||||
let billId = '';
|
||||
export const pushTaskBillListItem = async ({
|
||||
billId,
|
||||
moduleName,
|
||||
amount,
|
||||
model,
|
||||
tokenLen
|
||||
}: { billId?: string } & BillListItemType) => {
|
||||
if (!billId) return;
|
||||
try {
|
||||
await Bill.findByIdAndUpdate(billId, {
|
||||
$push: {
|
||||
list: {
|
||||
moduleName,
|
||||
amount,
|
||||
model,
|
||||
tokenLen
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {}
|
||||
};
|
||||
export const finishTaskBill = async ({ billId }: { billId: string }) => {
|
||||
try {
|
||||
// update bill
|
||||
const res = await Bill.findByIdAndUpdate(billId, [
|
||||
{
|
||||
$set: {
|
||||
total: {
|
||||
$sum: '$list.amount'
|
||||
},
|
||||
time: new Date()
|
||||
}
|
||||
}
|
||||
]);
|
||||
if (!res) return;
|
||||
const total = res.list.reduce((sum, item) => sum + item.amount, 0) || 0;
|
||||
|
||||
console.log('finish bill:', total);
|
||||
|
||||
// 账号扣费
|
||||
await User.findByIdAndUpdate(res.userId, {
|
||||
$inc: { balance: -total }
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('Finish bill failed:', error);
|
||||
billId && Bill.findByIdAndDelete(billId);
|
||||
}
|
||||
};
|
||||
|
||||
export const delTaskBill = async (billId?: string) => {
|
||||
if (!billId) return;
|
||||
|
||||
try {
|
||||
await connectToDatabase();
|
||||
|
||||
// 计算价格
|
||||
const unitPrice = ChatModelMap[chatModel]?.price || 3;
|
||||
const price = unitPrice * tokens;
|
||||
|
||||
try {
|
||||
// 插入 Bill 记录
|
||||
const res = await Bill.create({
|
||||
userId,
|
||||
type,
|
||||
modelName: chatModel,
|
||||
appId,
|
||||
textLen,
|
||||
tokenLen: tokens,
|
||||
price
|
||||
});
|
||||
billId = res._id;
|
||||
|
||||
// 账号扣费
|
||||
await User.findByIdAndUpdate(userId, {
|
||||
$inc: { balance: -price }
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('创建账单失败:', error);
|
||||
billId && Bill.findByIdAndDelete(billId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
await Bill.findByIdAndRemove(billId);
|
||||
} catch (error) {}
|
||||
};
|
||||
|
||||
export const updateShareChatBill = async ({
|
||||
@@ -81,22 +100,17 @@ export const updateShareChatBill = async ({
|
||||
};
|
||||
|
||||
export const pushSplitDataBill = async ({
|
||||
isPay,
|
||||
userId,
|
||||
totalTokens,
|
||||
textLen,
|
||||
type
|
||||
model,
|
||||
appName
|
||||
}: {
|
||||
isPay: boolean;
|
||||
model: string;
|
||||
userId: string;
|
||||
totalTokens: number;
|
||||
textLen: number;
|
||||
type: BillTypeEnum.QA;
|
||||
appName: string;
|
||||
}) => {
|
||||
console.log(
|
||||
`splitData generate success. text len: ${textLen}. token len: ${totalTokens}. pay:${isPay}`
|
||||
);
|
||||
if (!isPay) return;
|
||||
console.log(`splitData generate success. token len: ${totalTokens}.`);
|
||||
|
||||
let billId;
|
||||
|
||||
@@ -104,24 +118,22 @@ export const pushSplitDataBill = async ({
|
||||
await connectToDatabase();
|
||||
|
||||
// 获取模型单价格, 都是用 gpt35 拆分
|
||||
const unitPrice = ChatModelMap[OpenAiChatEnum.GPT3516k].price || 3;
|
||||
const unitPrice = global.chatModels.find((item) => item.model === model)?.price || 3;
|
||||
// 计算价格
|
||||
const price = unitPrice * totalTokens;
|
||||
const total = unitPrice * totalTokens;
|
||||
|
||||
// 插入 Bill 记录
|
||||
const res = await Bill.create({
|
||||
userId,
|
||||
type,
|
||||
modelName: OpenAiChatEnum.GPT3516k,
|
||||
textLen,
|
||||
appName,
|
||||
tokenLen: totalTokens,
|
||||
price
|
||||
total
|
||||
});
|
||||
billId = res._id;
|
||||
|
||||
// 账号扣费
|
||||
await User.findByIdAndUpdate(userId, {
|
||||
$inc: { balance: -price }
|
||||
$inc: { balance: -total }
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('创建账单失败:', error);
|
||||
@@ -130,21 +142,14 @@ export const pushSplitDataBill = async ({
|
||||
};
|
||||
|
||||
export const pushGenerateVectorBill = async ({
|
||||
isPay,
|
||||
userId,
|
||||
text,
|
||||
tokenLen
|
||||
tokenLen,
|
||||
model
|
||||
}: {
|
||||
isPay: boolean;
|
||||
userId: string;
|
||||
text: string;
|
||||
tokenLen: number;
|
||||
model: string;
|
||||
}) => {
|
||||
// console.log(
|
||||
// `vector generate success. text len: ${text.length}. token len: ${tokenLen}. pay:${isPay}`
|
||||
// );
|
||||
if (!isPay) return;
|
||||
|
||||
let billId;
|
||||
|
||||
try {
|
||||
@@ -152,23 +157,22 @@ export const pushGenerateVectorBill = async ({
|
||||
|
||||
try {
|
||||
// 计算价格. 至少为1
|
||||
let price = embeddingPrice * tokenLen;
|
||||
price = price > 1 ? price : 1;
|
||||
const unitPrice = global.vectorModels.find((item) => item.model === model)?.price || 0.2;
|
||||
let total = unitPrice * tokenLen;
|
||||
total = total > 1 ? total : 1;
|
||||
|
||||
// 插入 Bill 记录
|
||||
const res = await Bill.create({
|
||||
userId,
|
||||
type: BillTypeEnum.vector,
|
||||
modelName: embeddingModel,
|
||||
textLen: text.length,
|
||||
tokenLen,
|
||||
price
|
||||
model,
|
||||
appName: '索引生成',
|
||||
total
|
||||
});
|
||||
billId = res._id;
|
||||
|
||||
// 账号扣费
|
||||
await User.findByIdAndUpdate(userId, {
|
||||
$inc: { balance: -price }
|
||||
$inc: { balance: -total }
|
||||
});
|
||||
} catch (error) {
|
||||
console.log('创建账单失败:', error);
|
||||
@@ -178,3 +182,9 @@ export const pushGenerateVectorBill = async ({
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const countModelPrice = ({ model, tokens }: { model: string; tokens: number }) => {
|
||||
const modelData = getModel(model);
|
||||
if (!modelData) return 0;
|
||||
return modelData.price * tokens;
|
||||
};
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import { Schema, model, models, Model } from 'mongoose';
|
||||
import { AppSchema as AppType } from '@/types/mongoSchema';
|
||||
import { ChatModelMap, OpenAiChatEnum } from '@/constants/model';
|
||||
|
||||
const AppSchema = new Schema({
|
||||
userId: {
|
||||
@@ -24,50 +23,6 @@ const AppSchema = new Schema({
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
chat: {
|
||||
relatedKbs: {
|
||||
type: [Schema.Types.ObjectId],
|
||||
ref: 'kb',
|
||||
default: []
|
||||
},
|
||||
searchSimilarity: {
|
||||
type: Number,
|
||||
default: 0.8
|
||||
},
|
||||
searchLimit: {
|
||||
type: Number,
|
||||
default: 5
|
||||
},
|
||||
searchEmptyText: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
systemPrompt: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
limitPrompt: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
maxToken: {
|
||||
type: Number,
|
||||
default: 4000,
|
||||
min: 100
|
||||
},
|
||||
temperature: {
|
||||
type: Number,
|
||||
min: 0,
|
||||
max: 10,
|
||||
default: 0
|
||||
},
|
||||
chatModel: {
|
||||
// 聊天时使用的模型
|
||||
type: String,
|
||||
enum: Object.keys(ChatModelMap),
|
||||
default: OpenAiChatEnum.GPT3516k
|
||||
}
|
||||
},
|
||||
share: {
|
||||
topNum: {
|
||||
type: Number,
|
||||
|
@@ -1,7 +1,6 @@
|
||||
import { Schema, model, models, Model } from 'mongoose';
|
||||
import { ChatModelMap, embeddingModel } from '@/constants/model';
|
||||
import { BillSchema as BillType } from '@/types/mongoSchema';
|
||||
import { BillTypeMap } from '@/constants/user';
|
||||
import { BillSourceEnum, BillSourceMap } from '@/constants/user';
|
||||
|
||||
const BillSchema = new Schema({
|
||||
userId: {
|
||||
@@ -9,36 +8,48 @@ const BillSchema = new Schema({
|
||||
ref: 'user',
|
||||
required: true
|
||||
},
|
||||
type: {
|
||||
appName: {
|
||||
type: String,
|
||||
enum: Object.keys(BillTypeMap),
|
||||
required: true
|
||||
},
|
||||
modelName: {
|
||||
type: String,
|
||||
enum: [...Object.keys(ChatModelMap), embeddingModel]
|
||||
default: ''
|
||||
},
|
||||
appId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'app'
|
||||
ref: 'app',
|
||||
required: false
|
||||
},
|
||||
time: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
textLen: {
|
||||
// 提示词+响应的总字数
|
||||
total: {
|
||||
type: Number,
|
||||
required: true
|
||||
},
|
||||
tokenLen: {
|
||||
// 折算成 token 的数量
|
||||
type: Number,
|
||||
required: true
|
||||
source: {
|
||||
type: String,
|
||||
enum: Object.keys(BillSourceMap),
|
||||
default: BillSourceEnum.fastgpt
|
||||
},
|
||||
price: {
|
||||
type: Number,
|
||||
required: true
|
||||
list: {
|
||||
type: [
|
||||
{
|
||||
moduleName: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
amount: {
|
||||
type: Number,
|
||||
required: true
|
||||
},
|
||||
model: {
|
||||
type: String
|
||||
},
|
||||
tokenLen: {
|
||||
type: Number
|
||||
}
|
||||
}
|
||||
],
|
||||
default: []
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -1,22 +0,0 @@
|
||||
import { Schema, model, models } from 'mongoose';
|
||||
|
||||
const SystemSchema = new Schema({
|
||||
vectorMaxProcess: {
|
||||
type: Number,
|
||||
default: 10
|
||||
},
|
||||
qaMaxProcess: {
|
||||
type: Number,
|
||||
default: 10
|
||||
},
|
||||
pgIvfflatProbe: {
|
||||
type: Number,
|
||||
default: 10
|
||||
},
|
||||
sensitiveCheck: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
});
|
||||
|
||||
export const System = models['system'] || model('system', SystemSchema);
|
@@ -28,13 +28,16 @@ const TrainingDataSchema = new Schema({
|
||||
enum: Object.keys(TrainingTypeMap),
|
||||
required: true
|
||||
},
|
||||
model: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
prompt: {
|
||||
// 拆分时的提示词
|
||||
// qa split prompt
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
q: {
|
||||
// 如果是
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
|
@@ -2,6 +2,7 @@ import mongoose from 'mongoose';
|
||||
import tunnel from 'tunnel';
|
||||
import { startQueue } from './utils/tools';
|
||||
import { updateSystemEnv } from '@/pages/api/system/updateEnv';
|
||||
import { initSystemModels } from '@/pages/api/system/getInitData';
|
||||
|
||||
/**
|
||||
* 连接 MongoDB 数据库
|
||||
@@ -10,6 +11,7 @@ export async function connectToDatabase(): Promise<void> {
|
||||
if (global.mongodb) {
|
||||
return;
|
||||
}
|
||||
global.mongodb = 'connecting';
|
||||
|
||||
// init global data
|
||||
global.qaQueueLen = 0;
|
||||
@@ -31,8 +33,9 @@ export async function connectToDatabase(): Promise<void> {
|
||||
}
|
||||
});
|
||||
}
|
||||
initSystemModels();
|
||||
updateSystemEnv();
|
||||
|
||||
global.mongodb = 'connecting';
|
||||
try {
|
||||
mongoose.set('strictQuery', true);
|
||||
global.mongodb = await mongoose.connect(process.env.MONGODB_URI as string, {
|
||||
@@ -49,7 +52,6 @@ export async function connectToDatabase(): Promise<void> {
|
||||
}
|
||||
|
||||
// init function
|
||||
updateSystemEnv();
|
||||
startQueue();
|
||||
}
|
||||
|
||||
@@ -66,5 +68,4 @@ export * from './models/collection';
|
||||
export * from './models/shareChat';
|
||||
export * from './models/kb';
|
||||
export * from './models/inform';
|
||||
export * from './models/system';
|
||||
export * from './models/image';
|
||||
|
@@ -92,7 +92,7 @@ export const sseErrRes = (res: NextApiResponse, error: any) => {
|
||||
} else if (openaiError[error?.response?.statusText]) {
|
||||
msg = openaiError[error.response.statusText];
|
||||
}
|
||||
console.log('sse error', error);
|
||||
console.log('sse error => ', error);
|
||||
|
||||
sseResponse({
|
||||
res,
|
||||
|
@@ -1,15 +1,11 @@
|
||||
import type { NextApiRequest } from 'next';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import Cookie from 'cookie';
|
||||
import { Chat, App, OpenApi, User, ShareChat, KB } from '../mongo';
|
||||
import { App, OpenApi, User, ShareChat, KB } from '../mongo';
|
||||
import type { AppSchema } from '@/types/mongoSchema';
|
||||
import type { ChatItemType } from '@/types/chat';
|
||||
import mongoose from 'mongoose';
|
||||
import { defaultApp } from '@/constants/model';
|
||||
import { formatPrice } from '@/utils/user';
|
||||
import { ERROR_ENUM } from '../errorCode';
|
||||
import { ChatModelType, OpenAiChatEnum } from '@/constants/model';
|
||||
import { hashPassword } from '@/service/utils/tools';
|
||||
|
||||
export type AuthType = 'token' | 'root' | 'apikey';
|
||||
|
||||
@@ -35,6 +31,19 @@ export const parseCookie = (cookie?: string): Promise<string> => {
|
||||
});
|
||||
};
|
||||
|
||||
/* auth balance */
|
||||
export const authBalanceByUid = async (uid: string) => {
|
||||
const user = await User.findById(uid);
|
||||
if (!user) {
|
||||
return Promise.reject(ERROR_ENUM.unAuthorization);
|
||||
}
|
||||
|
||||
if (!user.openaiKey && formatPrice(user.balance) <= 0) {
|
||||
return Promise.reject(ERROR_ENUM.insufficientQuota);
|
||||
}
|
||||
return user;
|
||||
};
|
||||
|
||||
/* uniform auth user */
|
||||
export const authUser = async ({
|
||||
req,
|
||||
@@ -144,14 +153,7 @@ export const authUser = async ({
|
||||
|
||||
// balance check
|
||||
if (authBalance) {
|
||||
const user = await User.findById(uid);
|
||||
if (!user) {
|
||||
return Promise.reject(ERROR_ENUM.unAuthorization);
|
||||
}
|
||||
|
||||
if (!user.openaiKey && formatPrice(user.balance) <= 0) {
|
||||
return Promise.reject(ERROR_ENUM.insufficientQuota);
|
||||
}
|
||||
await authBalanceByUid(uid);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -166,43 +168,6 @@ export const getSystemOpenAiKey = () => {
|
||||
return process.env.ONEAPI_KEY || process.env.OPENAIKEY || '';
|
||||
};
|
||||
|
||||
/* 获取 api 请求的 key */
|
||||
export const getApiKey = async ({
|
||||
model,
|
||||
userId,
|
||||
mustPay = false
|
||||
}: {
|
||||
model: ChatModelType;
|
||||
userId: string;
|
||||
mustPay?: boolean;
|
||||
}) => {
|
||||
const user = await User.findById(userId, 'openaiKey balance');
|
||||
if (!user) {
|
||||
return Promise.reject(ERROR_ENUM.unAuthorization);
|
||||
}
|
||||
|
||||
const userOpenAiKey = user.openaiKey || '';
|
||||
const systemAuthKey = getSystemOpenAiKey();
|
||||
|
||||
// 有自己的key
|
||||
if (!mustPay && userOpenAiKey) {
|
||||
return {
|
||||
userOpenAiKey,
|
||||
systemAuthKey: ''
|
||||
};
|
||||
}
|
||||
|
||||
// 平台账号余额校验
|
||||
if (formatPrice(user.balance) <= 0) {
|
||||
return Promise.reject(ERROR_ENUM.insufficientQuota);
|
||||
}
|
||||
|
||||
return {
|
||||
userOpenAiKey: '',
|
||||
systemAuthKey
|
||||
};
|
||||
};
|
||||
|
||||
// 模型使用权校验
|
||||
export const authApp = async ({
|
||||
appId,
|
||||
@@ -232,14 +197,6 @@ export const authApp = async ({
|
||||
if (userId !== String(app.userId)) return Promise.reject(ERROR_ENUM.unAuthModel);
|
||||
}
|
||||
|
||||
// do not share detail info
|
||||
if (!reserveDetail && !app.share.isShareDetail && userId !== String(app.userId)) {
|
||||
app.chat = {
|
||||
...defaultApp.chat,
|
||||
chatModel: app.chat.chatModel
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
app,
|
||||
showModelDetail: userId === String(app.userId)
|
||||
|
@@ -1,13 +1,8 @@
|
||||
import { ChatItemType } from '@/types/chat';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
import type { ChatModelType } from '@/constants/model';
|
||||
import { ChatRoleEnum, sseResponseEventEnum } from '@/constants/chat';
|
||||
import { sseResponse } from '../tools';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { OpenAiChatEnum } from '@/constants/model';
|
||||
import { chatResponse, openAiStreamResponse } from './openai';
|
||||
import type { NextApiResponse } from 'next';
|
||||
import { textAdaptGptResponse } from '@/utils/adapt';
|
||||
import { parseStreamChunk } from '@/utils/adapt';
|
||||
|
||||
export type ChatCompletionType = {
|
||||
apiKey: string;
|
||||
@@ -36,11 +31,6 @@ export type StreamResponseReturnType = {
|
||||
finishMessages: ChatItemType[];
|
||||
};
|
||||
|
||||
export const modelServiceToolMap = {
|
||||
chatCompletion: chatResponse,
|
||||
streamResponse: openAiStreamResponse
|
||||
};
|
||||
|
||||
/* delete invalid symbol */
|
||||
const simplifyStr = (str = '') =>
|
||||
str
|
||||
@@ -54,7 +44,7 @@ export const ChatContextFilter = ({
|
||||
prompts,
|
||||
maxTokens
|
||||
}: {
|
||||
model: ChatModelType;
|
||||
model: string;
|
||||
prompts: ChatItemType[];
|
||||
maxTokens: number;
|
||||
}) => {
|
||||
@@ -111,126 +101,3 @@ export const ChatContextFilter = ({
|
||||
|
||||
return [...systemPrompts, ...chats];
|
||||
};
|
||||
|
||||
/* stream response */
|
||||
export const resStreamResponse = async ({
|
||||
model,
|
||||
res,
|
||||
chatResponse,
|
||||
prompts
|
||||
}: StreamResponseType & {
|
||||
model: ChatModelType;
|
||||
}) => {
|
||||
// 创建响应流
|
||||
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('X-Accel-Buffering', 'no');
|
||||
res.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
|
||||
const { responseContent, totalTokens, finishMessages } = await modelServiceToolMap.streamResponse(
|
||||
{
|
||||
chatResponse,
|
||||
prompts,
|
||||
res,
|
||||
model
|
||||
}
|
||||
);
|
||||
|
||||
return { responseContent, totalTokens, finishMessages };
|
||||
};
|
||||
|
||||
/* stream response */
|
||||
export const V2_StreamResponse = async ({
|
||||
model,
|
||||
res,
|
||||
chatResponse,
|
||||
prompts
|
||||
}: StreamResponseType & {
|
||||
model: ChatModelType;
|
||||
}) => {
|
||||
let responseContent = '';
|
||||
let error: any = null;
|
||||
let truncateData = '';
|
||||
const clientRes = async (data: string) => {
|
||||
//部分代理会导致流式传输时的数据被截断,不为json格式,这里做一个兼容
|
||||
const { content = '' } = (() => {
|
||||
try {
|
||||
if (truncateData) {
|
||||
try {
|
||||
//判断是否为json,如果是的话直接跳过后续拼装操作,注意极端情况下可能出现截断成3截以上情况也可以兼容
|
||||
JSON.parse(data);
|
||||
} catch (e) {
|
||||
data = truncateData + data;
|
||||
}
|
||||
truncateData = '';
|
||||
}
|
||||
const json = JSON.parse(data);
|
||||
const content: string = json?.choices?.[0].delta.content || '';
|
||||
error = json.error;
|
||||
responseContent += content;
|
||||
return { content };
|
||||
} catch (error) {
|
||||
truncateData = data;
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
if (res.closed || error) return;
|
||||
|
||||
if (data === '[DONE]') {
|
||||
sseResponse({
|
||||
res,
|
||||
event: sseResponseEventEnum.answer,
|
||||
data: textAdaptGptResponse({
|
||||
text: null,
|
||||
finish_reason: 'stop'
|
||||
})
|
||||
});
|
||||
sseResponse({
|
||||
res,
|
||||
event: sseResponseEventEnum.answer,
|
||||
data: '[DONE]'
|
||||
});
|
||||
} else {
|
||||
sseResponse({
|
||||
res,
|
||||
event: sseResponseEventEnum.answer,
|
||||
data: textAdaptGptResponse({
|
||||
text: content
|
||||
})
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
for await (const chunk of chatResponse.data as any) {
|
||||
if (res.closed) break;
|
||||
const parse = parseStreamChunk(chunk);
|
||||
parse.forEach((item) => clientRes(item.data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('pipe error', error);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
console.log(error);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
// count tokens
|
||||
const finishMessages = prompts.concat({
|
||||
obj: ChatRoleEnum.AI,
|
||||
value: responseContent
|
||||
});
|
||||
|
||||
const totalTokens = modelToolMap.countTokens({
|
||||
model,
|
||||
messages: finishMessages
|
||||
});
|
||||
|
||||
return {
|
||||
responseContent,
|
||||
totalTokens,
|
||||
finishMessages
|
||||
};
|
||||
};
|
||||
|
@@ -1,133 +0,0 @@
|
||||
import { Configuration, OpenAIApi } from 'openai';
|
||||
import { axiosConfig } from '../tools';
|
||||
import { ChatModelMap, OpenAiChatEnum } from '@/constants/model';
|
||||
import { adaptChatItem_openAI } from '@/utils/plugin/openai';
|
||||
import { modelToolMap } from '@/utils/plugin';
|
||||
import { ChatCompletionType, ChatContextFilter, StreamResponseType } from './index';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import { parseStreamChunk } from '@/utils/adapt';
|
||||
|
||||
export const getOpenAIApi = (apiKey: string) => {
|
||||
const openaiBaseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
|
||||
return new OpenAIApi(
|
||||
new Configuration({
|
||||
basePath: apiKey === process.env.ONEAPI_KEY ? process.env.ONEAPI_URL : openaiBaseUrl
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
/* 模型对话 */
|
||||
export const chatResponse = async ({
|
||||
model,
|
||||
apiKey,
|
||||
temperature,
|
||||
maxToken = 4000,
|
||||
messages,
|
||||
stream
|
||||
}: ChatCompletionType & { model: `${OpenAiChatEnum}` }) => {
|
||||
const modelTokenLimit = ChatModelMap[model]?.contextMaxToken || 4000;
|
||||
const filterMessages = ChatContextFilter({
|
||||
model,
|
||||
prompts: messages,
|
||||
maxTokens: Math.ceil(modelTokenLimit - 300) // filter token. not response maxToken
|
||||
});
|
||||
|
||||
const adaptMessages = adaptChatItem_openAI({ messages: filterMessages, reserveId: false });
|
||||
const chatAPI = getOpenAIApi(apiKey);
|
||||
|
||||
const promptsToken = modelToolMap.countTokens({
|
||||
model,
|
||||
messages: filterMessages
|
||||
});
|
||||
|
||||
maxToken = maxToken + promptsToken > modelTokenLimit ? modelTokenLimit - promptsToken : maxToken;
|
||||
|
||||
const response = await chatAPI.createChatCompletion(
|
||||
{
|
||||
model,
|
||||
temperature: Number(temperature || 0),
|
||||
max_tokens: maxToken,
|
||||
messages: adaptMessages,
|
||||
frequency_penalty: 0.5, // 越大,重复内容越少
|
||||
presence_penalty: -0.5, // 越大,越容易出现新内容
|
||||
stream
|
||||
// stop: ['.!?。']
|
||||
},
|
||||
{
|
||||
timeout: stream ? 60000 : 480000,
|
||||
responseType: stream ? 'stream' : 'json',
|
||||
...axiosConfig(apiKey)
|
||||
}
|
||||
);
|
||||
|
||||
const responseText = stream ? '' : response.data.choices?.[0].message?.content || '';
|
||||
const totalTokens = stream ? 0 : response.data.usage?.total_tokens || 0;
|
||||
|
||||
return {
|
||||
streamResponse: response,
|
||||
responseMessages: filterMessages.concat({ obj: 'AI', value: responseText }),
|
||||
responseText,
|
||||
totalTokens
|
||||
};
|
||||
};
|
||||
|
||||
/* openai stream response */
|
||||
export const openAiStreamResponse = async ({
|
||||
res,
|
||||
model,
|
||||
chatResponse,
|
||||
prompts
|
||||
}: StreamResponseType & {
|
||||
model: `${OpenAiChatEnum}`;
|
||||
}) => {
|
||||
try {
|
||||
let responseContent = '';
|
||||
|
||||
const clientRes = async (data: string) => {
|
||||
const { content = '' } = (() => {
|
||||
try {
|
||||
const json = JSON.parse(data);
|
||||
const content: string = json?.choices?.[0].delta.content || '';
|
||||
responseContent += content;
|
||||
return { content };
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
if (data === '[DONE]') return;
|
||||
|
||||
!res.closed && content && res.write(content);
|
||||
};
|
||||
|
||||
try {
|
||||
for await (const chunk of chatResponse.data as any) {
|
||||
if (res.closed) break;
|
||||
|
||||
const parse = parseStreamChunk(chunk);
|
||||
parse.forEach((item) => clientRes(item.data));
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('pipe error', error);
|
||||
}
|
||||
|
||||
// count tokens
|
||||
const finishMessages = prompts.concat({
|
||||
obj: ChatRoleEnum.AI,
|
||||
value: responseContent
|
||||
});
|
||||
|
||||
const totalTokens = modelToolMap.countTokens({
|
||||
model,
|
||||
messages: finishMessages
|
||||
});
|
||||
|
||||
return {
|
||||
responseContent,
|
||||
totalTokens,
|
||||
finishMessages
|
||||
};
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
};
|
14
client/src/service/utils/data.ts
Normal file
14
client/src/service/utils/data.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export const getChatModel = (model: string) => {
|
||||
return global.chatModels.find((item) => item.model === model);
|
||||
};
|
||||
export const getVectorModel = (model: string) => {
|
||||
return global.vectorModels.find((item) => item.model === model);
|
||||
};
|
||||
export const getQAModel = (model: string) => {
|
||||
return global.qaModels.find((item) => item.model === model);
|
||||
};
|
||||
export const getModel = (model: string) => {
|
||||
return [...global.chatModels, ...global.vectorModels, ...global.qaModels].find(
|
||||
(item) => item.model === model
|
||||
);
|
||||
};
|
@@ -4,7 +4,6 @@ import crypto from 'crypto';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { generateQA } from '../events/generateQA';
|
||||
import { generateVector } from '../events/generateVector';
|
||||
import { sseResponseEventEnum } from '@/constants/chat';
|
||||
|
||||
/* 密码加密 */
|
||||
export const hashPassword = (psw: string) => {
|
||||
@@ -33,20 +32,6 @@ export const clearCookie = (res: NextApiResponse) => {
|
||||
res.setHeader('Set-Cookie', 'token=; Path=/; Max-Age=0');
|
||||
};
|
||||
|
||||
/* openai axios config */
|
||||
export const axiosConfig = (apikey: string) => {
|
||||
const openaiBaseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
|
||||
|
||||
return {
|
||||
baseURL: apikey === process.env.ONEAPI_KEY ? process.env.ONEAPI_URL : openaiBaseUrl, // 此处仅对非 npm 模块有效
|
||||
httpsAgent: global.httpsAgent,
|
||||
headers: {
|
||||
Authorization: `Bearer ${apikey}`,
|
||||
auth: process.env.OPENAI_BASE_URL_AUTH || ''
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export function withNextCors(handler: NextApiHandler): NextApiHandler {
|
||||
return async function nextApiHandlerWrappedWithNextCors(
|
||||
req: NextApiRequest,
|
||||
|
@@ -1,12 +1,8 @@
|
||||
import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
import type { InitDateResponse } from '@/pages/api/system/getInitData';
|
||||
import { getInitData } from '@/api/system';
|
||||
|
||||
type State = {
|
||||
initData: InitDateResponse;
|
||||
loadInitData: () => Promise<void>;
|
||||
loading: boolean;
|
||||
setLoading: (val: boolean) => null;
|
||||
screenWidth: number;
|
||||
@@ -17,19 +13,6 @@ type State = {
|
||||
export const useGlobalStore = create<State>()(
|
||||
devtools(
|
||||
immer((set, get) => ({
|
||||
initData: {
|
||||
beianText: '',
|
||||
googleVerKey: '',
|
||||
baiduTongji: false
|
||||
},
|
||||
async loadInitData() {
|
||||
try {
|
||||
const res = await getInitData();
|
||||
set((state) => {
|
||||
state.initData = res;
|
||||
});
|
||||
} catch (error) {}
|
||||
},
|
||||
loading: false,
|
||||
setLoading: (val: boolean) => {
|
||||
set((state) => {
|
||||
|
36
client/src/store/static.ts
Normal file
36
client/src/store/static.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
type QAModelItemType,
|
||||
type ChatModelItemType,
|
||||
type VectorModelItemType
|
||||
} from '@/types/model';
|
||||
import type { InitDateResponse } from '@/pages/api/system/getInitData';
|
||||
import { getInitData } from '@/api/system';
|
||||
import { delay } from '@/utils/tools';
|
||||
|
||||
export let beianText = '';
|
||||
export let googleVerKey = '';
|
||||
export let baiduTongji = '';
|
||||
export let chatModelList: ChatModelItemType[] = [];
|
||||
export let qaModelList: QAModelItemType[] = [];
|
||||
export let vectorModelList: VectorModelItemType[] = [];
|
||||
|
||||
let retryTimes = 3;
|
||||
|
||||
export const clientInitData = async (): Promise<InitDateResponse> => {
|
||||
try {
|
||||
const res = await getInitData();
|
||||
|
||||
chatModelList = res.chatModels;
|
||||
qaModelList = res.qaModels;
|
||||
vectorModelList = res.vectorModels;
|
||||
beianText = res.beianText;
|
||||
googleVerKey = res.googleVerKey;
|
||||
baiduTongji = res.baiduTongji;
|
||||
|
||||
return res;
|
||||
} catch (error) {
|
||||
retryTimes--;
|
||||
await delay(500);
|
||||
return clientInitData();
|
||||
}
|
||||
};
|
2
client/src/types/app.d.ts
vendored
2
client/src/types/app.d.ts
vendored
@@ -42,7 +42,7 @@ export type ShareChatEditType = {
|
||||
|
||||
/* agent */
|
||||
/* question classify */
|
||||
export type ClassifyQuestionAgentItemType = {
|
||||
export type RecognizeIntentionAgentItemType = {
|
||||
value: string;
|
||||
key: string;
|
||||
};
|
||||
|
3
client/src/types/chat.d.ts
vendored
3
client/src/types/chat.d.ts
vendored
@@ -8,9 +8,6 @@ export type ChatItemType = {
|
||||
_id?: string;
|
||||
obj: `${ChatRoleEnum}`;
|
||||
value: string;
|
||||
quoteLen?: number;
|
||||
quote?: QuoteItemType[];
|
||||
systemPrompt?: string;
|
||||
[key: string]: any;
|
||||
};
|
||||
|
||||
|
10
client/src/types/index.d.ts
vendored
10
client/src/types/index.d.ts
vendored
@@ -2,6 +2,7 @@ import type { Mongoose } from 'mongoose';
|
||||
import type { Agent } from 'http';
|
||||
import type { Pool } from 'pg';
|
||||
import type { Tiktoken } from '@dqbd/tiktoken';
|
||||
import { ChatModelItemType, QAModelItemType, VectorModelItemType } from './model';
|
||||
|
||||
export type PagingData<T> = {
|
||||
pageNum: number;
|
||||
@@ -16,9 +17,6 @@ declare global {
|
||||
var mongodb: Mongoose | string | null;
|
||||
var pgClient: Pool | null;
|
||||
var httpsAgent: Agent;
|
||||
var particlesJS: any;
|
||||
var grecaptcha: any;
|
||||
var QRCode: any;
|
||||
var qaQueueLen: number;
|
||||
var vectorQueueLen: number;
|
||||
var OpenAiEncMap: Tiktoken;
|
||||
@@ -30,8 +28,14 @@ declare global {
|
||||
pgIvfflatProbe: number;
|
||||
sensitiveCheck: boolean;
|
||||
};
|
||||
var chatModels: ChatModelItemType[];
|
||||
var qaModels: QAModelItemType[];
|
||||
var vectorModels: VectorModelItemType[];
|
||||
|
||||
interface Window {
|
||||
['pdfjs-dist/build/pdf']: any;
|
||||
particlesJS: any;
|
||||
grecaptcha: any;
|
||||
QRCode: any;
|
||||
}
|
||||
}
|
||||
|
19
client/src/types/model.d.ts
vendored
Normal file
19
client/src/types/model.d.ts
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
export type ChatModelItemType = {
|
||||
model: string;
|
||||
name: string;
|
||||
contextMaxToken: number;
|
||||
systemMaxToken: number;
|
||||
maxTemperature: number;
|
||||
price: number;
|
||||
};
|
||||
export type QAModelItemType = {
|
||||
model: string;
|
||||
name: string;
|
||||
maxToken: number;
|
||||
price: number;
|
||||
};
|
||||
export type VectorModelItemType = {
|
||||
model: string;
|
||||
name: string;
|
||||
price: number;
|
||||
};
|
29
client/src/types/mongoSchema.d.ts
vendored
29
client/src/types/mongoSchema.d.ts
vendored
@@ -1,7 +1,7 @@
|
||||
import type { ChatItemType } from './chat';
|
||||
import { ModelNameEnum, ChatModelType, EmbeddingModelType } from '@/constants/model';
|
||||
import type { DataType } from './data';
|
||||
import { BillTypeEnum, InformTypeEnum } from '@/constants/user';
|
||||
import { BillSourceEnum, InformTypeEnum } from '@/constants/user';
|
||||
import { TrainingModeEnum } from '@/constants/plugin';
|
||||
import type { AppModuleItemType } from './app';
|
||||
|
||||
@@ -38,17 +38,6 @@ export interface AppSchema {
|
||||
avatar: string;
|
||||
intro: string;
|
||||
updateTime: number;
|
||||
chat: {
|
||||
relatedKbs: string[];
|
||||
searchSimilarity: number;
|
||||
searchLimit: number;
|
||||
searchEmptyText: string;
|
||||
systemPrompt: string;
|
||||
limitPrompt: string;
|
||||
temperature: number;
|
||||
maxToken: number;
|
||||
chatModel: ChatModelType; // 聊天时用的模型,训练后就是训练的模型
|
||||
};
|
||||
share: {
|
||||
isShare: boolean;
|
||||
isShareDetail: boolean;
|
||||
@@ -68,6 +57,7 @@ export interface TrainingDataSchema {
|
||||
kbId: string;
|
||||
expireAt: Date;
|
||||
lockTime: Date;
|
||||
model: string;
|
||||
mode: `${TrainingModeEnum}`;
|
||||
prompt: string;
|
||||
q: string;
|
||||
@@ -87,16 +77,21 @@ export interface ChatSchema {
|
||||
content: ChatItemType[];
|
||||
}
|
||||
|
||||
export type BillListItemType = {
|
||||
moduleName: string;
|
||||
amount: number;
|
||||
model?: string;
|
||||
tokenLen?: number;
|
||||
};
|
||||
export interface BillSchema {
|
||||
_id: string;
|
||||
userId: string;
|
||||
type: `${BillTypeEnum}`;
|
||||
modelName?: ChatModelType | EmbeddingModelType;
|
||||
appName: string;
|
||||
appId?: string;
|
||||
source: `${BillSourceEnum}`;
|
||||
time: Date;
|
||||
textLen: number;
|
||||
tokenLen: number;
|
||||
price: number;
|
||||
total: number;
|
||||
list: BillListItemType[];
|
||||
}
|
||||
|
||||
export interface PaySchema {
|
||||
|
9
client/src/types/user.d.ts
vendored
9
client/src/types/user.d.ts
vendored
@@ -1,3 +1,4 @@
|
||||
import { BillSourceEnum } from '@/constants/user';
|
||||
import type { BillSchema } from './mongoSchema';
|
||||
export interface UserType {
|
||||
_id: string;
|
||||
@@ -19,9 +20,7 @@ export interface UserUpdateParams {
|
||||
export interface UserBillType {
|
||||
id: string;
|
||||
time: Date;
|
||||
modelName: string;
|
||||
type: BillSchema['type'];
|
||||
textLen: number;
|
||||
tokenLen: number;
|
||||
price: number;
|
||||
appName: string;
|
||||
source: BillSchema['source'];
|
||||
total: number;
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ import { ChatItemType } from '@/types/chat';
|
||||
import { ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||
import { ChatRoleEnum } from '@/constants/chat';
|
||||
import type { MessageItemType } from '@/pages/api/openapi/v1/chat/completions';
|
||||
import { ChatModelMap, OpenAiChatEnum } from '@/constants/model';
|
||||
import type { AppModuleItemType } from '@/types/app';
|
||||
import type { FlowModuleItemType } from '@/types/flow';
|
||||
import type { Edge, Node } from 'reactflow';
|
||||
@@ -16,12 +15,10 @@ const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz1234567890', 6);
|
||||
export const adaptBill = (bill: BillSchema): UserBillType => {
|
||||
return {
|
||||
id: bill._id,
|
||||
type: bill.type,
|
||||
modelName: ChatModelMap[bill.modelName as `${OpenAiChatEnum}`]?.name || bill.modelName,
|
||||
source: bill.source,
|
||||
time: bill.time,
|
||||
textLen: bill.textLen,
|
||||
tokenLen: bill.tokenLen,
|
||||
price: formatPrice(bill.price)
|
||||
total: formatPrice(bill.total),
|
||||
appName: bill.appName
|
||||
};
|
||||
};
|
||||
|
||||
|
@@ -50,10 +50,10 @@ export const adaptChatItem_openAI = ({
|
||||
|
||||
export function countOpenAIToken({
|
||||
messages,
|
||||
model
|
||||
model = 'gpt-3.5-turbo'
|
||||
}: {
|
||||
messages: ChatItemType[];
|
||||
model: `${OpenAiChatEnum}`;
|
||||
model?: string;
|
||||
}) {
|
||||
const diffVal = model.startsWith('gpt-3.5-turbo') ? 3 : 2;
|
||||
|
||||
@@ -74,7 +74,7 @@ export const openAiSliceTextByToken = ({
|
||||
text,
|
||||
length
|
||||
}: {
|
||||
model: `${OpenAiChatEnum}`;
|
||||
model: string;
|
||||
text: string;
|
||||
length: number;
|
||||
}) => {
|
||||
|
Reference in New Issue
Block a user