Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
2a45fe52
authored
Jul 04, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf: code and inform
parent
8635de86
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
77 additions
and
143 deletions
+77
-143
client/public/docs/chatProblem.md
+1
-1
client/public/docs/intro.md
+1
-1
client/src/constants/model.ts
+1
-1
client/src/pages/api/openapi/chat/chat.ts
+2
-1
client/src/pages/api/openapi/kb/appKbSearch.ts
+4
-2
client/src/pages/api/openapi/kb/pushData.ts
+2
-1
client/src/pages/api/openapi/v1/chat/completions.ts
+2
-1
client/src/pages/api/system/getModels.ts
+1
-0
client/src/pages/api/user/inform/send.ts
+5
-3
client/src/pages/number/components/PayModal.tsx
+1
-1
client/src/service/events/generateQA.ts
+2
-1
client/src/service/events/sendInform.ts
+16
-0
client/src/service/mongo.ts
+2
-0
client/src/service/utils/auth.ts
+5
-25
client/src/service/utils/chat/index.ts
+16
-51
client/src/service/utils/chat/openai.ts
+4
-2
client/src/types/index.d.ts
+3
-1
client/src/utils/file.ts
+1
-1
client/src/utils/plugin/index.ts
+4
-28
client/src/utils/plugin/openai.ts
+4
-22
No files found.
client/public/docs/chatProblem.md
View file @
2a45fe52
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| --- | --- |
| --- | --- |
| 知识库 - 索引 | 0.001 |
| 知识库 - 索引 | 0.001 |
| chatgpt - 对话 | 0.0
2
5 |
| chatgpt - 对话 | 0.0
1
5 |
| chatgpt16K - 对话 | 0.025 |
| chatgpt16K - 对话 | 0.025 |
| gpt4 - 对话 | 0.45 |
| gpt4 - 对话 | 0.45 |
| 文件拆分 | 0.025 |
| 文件拆分 | 0.025 |
...
...
client/public/docs/intro.md
View file @
2a45fe52
...
@@ -19,7 +19,7 @@ FastGpt 项目完全开源,可随意私有化部署,去除平台风险忧虑
...
@@ -19,7 +19,7 @@ FastGpt 项目完全开源,可随意私有化部署,去除平台风险忧虑
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| --- | --- |
| --- | --- |
| 知识库 - 索引 | 0.001 |
| 知识库 - 索引 | 0.001 |
| chatgpt - 对话 | 0.0
2
5 |
| chatgpt - 对话 | 0.0
1
5 |
| chatgpt16K - 对话 | 0.025 |
| chatgpt16K - 对话 | 0.025 |
| gpt4 - 对话 | 0.45 |
| gpt4 - 对话 | 0.45 |
| 文件拆分 | 0.025 |
| 文件拆分 | 0.025 |
...
...
client/src/constants/model.ts
View file @
2a45fe52
...
@@ -31,7 +31,7 @@ export const ChatModelMap = {
...
@@ -31,7 +31,7 @@ export const ChatModelMap = {
contextMaxToken
:
4000
,
contextMaxToken
:
4000
,
systemMaxToken
:
2400
,
systemMaxToken
:
2400
,
maxTemperature
:
1.2
,
maxTemperature
:
1.2
,
price
:
2
.5
price
:
1
.5
},
},
[
OpenAiChatEnum
.
GPT3516k
]:
{
[
OpenAiChatEnum
.
GPT3516k
]:
{
chatModel
:
OpenAiChatEnum
.
GPT3516k
,
chatModel
:
OpenAiChatEnum
.
GPT3516k
,
...
...
client/src/pages/api/openapi/chat/chat.ts
View file @
2a45fe52
...
@@ -129,7 +129,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...
@@ -129,7 +129,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// 发出请求
// 发出请求
const
{
streamResponse
,
responseMessages
,
responseText
,
totalTokens
}
=
const
{
streamResponse
,
responseMessages
,
responseText
,
totalTokens
}
=
await
modelServiceToolMap
[
model
.
chat
.
chatModel
].
chatCompletion
({
await
modelServiceToolMap
.
chatCompletion
({
model
:
model
.
chat
.
chatModel
,
apiKey
,
apiKey
,
temperature
:
+
temperature
,
temperature
:
+
temperature
,
messages
:
completePrompts
,
messages
:
completePrompts
,
...
...
client/src/pages/api/openapi/kb/appKbSearch.ts
View file @
2a45fe52
...
@@ -150,13 +150,15 @@ export async function appKbSearch({
...
@@ -150,13 +150,15 @@ export async function appKbSearch({
}
}
];
];
const
fixedSystemTokens
=
modelToolMap
[
model
.
chat
.
chatModel
].
countTokens
({
const
fixedSystemTokens
=
modelToolMap
.
countTokens
({
model
:
model
.
chat
.
chatModel
,
messages
:
[...
userSystemPrompt
,
...
userLimitPrompt
]
messages
:
[...
userSystemPrompt
,
...
userLimitPrompt
]
});
});
// filter part quote by maxToken
// filter part quote by maxToken
const
sliceResult
=
modelToolMap
[
model
.
chat
.
chatModel
]
const
sliceResult
=
modelToolMap
.
tokenSlice
({
.
tokenSlice
({
model
:
model
.
chat
.
chatModel
,
maxToken
:
modelConstantsData
.
systemMaxToken
-
fixedSystemTokens
,
maxToken
:
modelConstantsData
.
systemMaxToken
-
fixedSystemTokens
,
messages
:
filterSearch
.
map
((
item
,
i
)
=>
({
messages
:
filterSearch
.
map
((
item
,
i
)
=>
({
obj
:
ChatRoleEnum
.
System
,
obj
:
ChatRoleEnum
.
System
,
...
...
client/src/pages/api/openapi/kb/pushData.ts
View file @
2a45fe52
...
@@ -78,7 +78,8 @@ export async function pushDataToKb({
...
@@ -78,7 +78,8 @@ export async function pushDataToKb({
if
(
mode
===
TrainingModeEnum
.
qa
)
{
if
(
mode
===
TrainingModeEnum
.
qa
)
{
// count token
// count token
const
token
=
modelToolMap
[
OpenAiChatEnum
.
GPT3516k
].
countTokens
({
const
token
=
modelToolMap
.
countTokens
({
model
:
OpenAiChatEnum
.
GPT3516k
,
messages
:
[{
obj
:
'System'
,
value
:
item
.
q
}]
messages
:
[{
obj
:
'System'
,
value
:
item
.
q
}]
});
});
if
(
token
>
modeMaxToken
[
TrainingModeEnum
.
qa
])
{
if
(
token
>
modeMaxToken
[
TrainingModeEnum
.
qa
])
{
...
...
client/src/pages/api/openapi/v1/chat/completions.ts
View file @
2a45fe52
...
@@ -196,7 +196,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
...
@@ -196,7 +196,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// start model api. responseText and totalTokens: valid only if stream = false
// start model api. responseText and totalTokens: valid only if stream = false
const { streamResponse, responseMessages, responseText, totalTokens } =
const { streamResponse, responseMessages, responseText, totalTokens } =
await modelServiceToolMap[model.chat.chatModel].chatCompletion({
await modelServiceToolMap.chatCompletion({
model: model.chat.chatModel,
apiKey: userOpenAiKey || apiKey,
apiKey: userOpenAiKey || apiKey,
temperature: +temperature,
temperature: +temperature,
maxToken: model.chat.maxToken,
maxToken: model.chat.maxToken,
...
...
client/src/pages/api/system/getModels.ts
View file @
2a45fe52
...
@@ -9,6 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -9,6 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT3516k
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT3516k
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT35
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT35
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
textada001
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT4
]);
chatModelList
.
push
(
ChatModelMap
[
OpenAiChatEnum
.
GPT4
]);
jsonRes
(
res
,
{
jsonRes
(
res
,
{
...
...
client/src/pages/api/user/inform/send.ts
View file @
2a45fe52
...
@@ -4,6 +4,7 @@ import { jsonRes } from '@/service/response';
...
@@ -4,6 +4,7 @@ import { jsonRes } from '@/service/response';
import
{
connectToDatabase
,
Inform
,
User
}
from
'@/service/mongo'
;
import
{
connectToDatabase
,
Inform
,
User
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
{
InformTypeEnum
}
from
'@/constants/user'
;
import
{
InformTypeEnum
}
from
'@/constants/user'
;
import
{
startSendInform
}
from
'@/service/events/sendInform'
;
export
type
Props
=
{
export
type
Props
=
{
type
:
`
${
InformTypeEnum
}
`
;
type
:
`
${
InformTypeEnum
}
`
;
...
@@ -37,14 +38,14 @@ export async function sendInform({ type, title, content, userId }: Props) {
...
@@ -37,14 +38,14 @@ export async function sendInform({ type, title, content, userId }: Props) {
try
{
try
{
if
(
userId
)
{
if
(
userId
)
{
global
.
sendInformQueue
.
push
(
async
()
=>
{
// skip it if have same inform within 5 minutes
// skip it if have same inform within 5 minutes
const
inform
=
await
Inform
.
findOne
({
const
inform
=
await
Inform
.
findOne
({
type
,
type
,
title
,
title
,
content
,
content
,
userId
,
userId
,
read
:
false
,
time
:
{
$gte
:
new
Date
(
Date
.
now
()
-
5
*
60
*
1000
)
}
time
:
{
$lte
:
new
Date
(
Date
.
now
()
+
5
*
60
*
1000
)
}
});
});
if
(
inform
)
return
;
if
(
inform
)
return
;
...
@@ -55,7 +56,8 @@ export async function sendInform({ type, title, content, userId }: Props) {
...
@@ -55,7 +56,8 @@ export async function sendInform({ type, title, content, userId }: Props) {
content
,
content
,
userId
userId
});
});
});
startSendInform
();
return
;
return
;
}
}
...
...
client/src/pages/number/components/PayModal.tsx
View file @
2a45fe52
...
@@ -114,7 +114,7 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
...
@@ -114,7 +114,7 @@ const PayModal = ({ onClose }: { onClose: () => void }) => {
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| 计费项 | 价格: 元/ 1K tokens(包含上下文)|
| --- | --- |
| --- | --- |
| 知识库 - 索引 | 0.001 |
| 知识库 - 索引 | 0.001 |
| chatgpt - 对话 | 0.0
2
5 |
| chatgpt - 对话 | 0.0
1
5 |
| chatgpt16K - 对话 | 0.025 |
| chatgpt16K - 对话 | 0.025 |
| gpt4 - 对话 | 0.45 |
| gpt4 - 对话 | 0.45 |
| 文件拆分 | 0.025 |`
}
| 文件拆分 | 0.025 |`
}
...
...
client/src/service/events/generateQA.ts
View file @
2a45fe52
...
@@ -63,8 +63,9 @@ export async function generateQA(): Promise<any> {
...
@@ -63,8 +63,9 @@ export async function generateQA(): Promise<any> {
// 请求 chatgpt 获取回答
// 请求 chatgpt 获取回答
const
response
=
await
Promise
.
all
(
const
response
=
await
Promise
.
all
(
[
data
.
q
].
map
((
text
)
=>
[
data
.
q
].
map
((
text
)
=>
modelServiceToolMap
[
OpenAiChatEnum
.
GPT3516k
]
modelServiceToolMap
.
chatCompletion
({
.
chatCompletion
({
model
:
OpenAiChatEnum
.
GPT3516k
,
apiKey
:
systemAuthKey
,
apiKey
:
systemAuthKey
,
temperature
:
0.8
,
temperature
:
0.8
,
messages
:
[
messages
:
[
...
...
client/src/service/events/sendInform.ts
0 → 100644
View file @
2a45fe52
export
const
startSendInform
=
async
()
=>
{
if
(
global
.
sendInformQueue
.
length
===
0
||
global
.
sendInformQueueLen
>
0
)
return
;
global
.
sendInformQueueLen
++
;
try
{
const
fn
=
global
.
sendInformQueue
[
global
.
sendInformQueue
.
length
-
1
];
await
fn
();
global
.
sendInformQueue
.
pop
();
global
.
sendInformQueueLen
--
;
startSendInform
();
}
catch
(
error
)
{
global
.
sendInformQueueLen
--
;
startSendInform
();
}
};
client/src/service/mongo.ts
View file @
2a45fe52
...
@@ -20,6 +20,8 @@ export async function connectToDatabase(): Promise<void> {
...
@@ -20,6 +20,8 @@ export async function connectToDatabase(): Promise<void> {
pgIvfflatProbe
:
10
,
pgIvfflatProbe
:
10
,
sensitiveCheck
:
false
sensitiveCheck
:
false
};
};
global
.
sendInformQueue
=
[];
global
.
sendInformQueueLen
=
0
;
// proxy obj
// proxy obj
if
(
process
.
env
.
AXIOS_PROXY_HOST
&&
process
.
env
.
AXIOS_PROXY_PORT
)
{
if
(
process
.
env
.
AXIOS_PROXY_HOST
&&
process
.
env
.
AXIOS_PROXY_PORT
)
{
global
.
httpsAgent
=
tunnel
.
httpsOverHttp
({
global
.
httpsAgent
=
tunnel
.
httpsOverHttp
({
...
...
client/src/service/utils/auth.ts
View file @
2a45fe52
...
@@ -181,33 +181,13 @@ export const getApiKey = async ({
...
@@ -181,33 +181,13 @@ export const getApiKey = async ({
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
return
Promise
.
reject
(
ERROR_ENUM
.
unAuthorization
);
}
}
const
keyMap
=
{
const
userOpenAiKey
=
user
.
openaiKey
||
''
;
[
OpenAiChatEnum
.
GPT35
]:
{
const
systemAuthKey
=
getSystemOpenAiKey
();
userOpenAiKey
:
user
.
openaiKey
||
''
,
systemAuthKey
:
getSystemOpenAiKey
()
},
[
OpenAiChatEnum
.
GPT3516k
]:
{
userOpenAiKey
:
user
.
openaiKey
||
''
,
systemAuthKey
:
getSystemOpenAiKey
()
},
[
OpenAiChatEnum
.
GPT4
]:
{
userOpenAiKey
:
user
.
openaiKey
||
''
,
systemAuthKey
:
getSystemOpenAiKey
()
},
[
OpenAiChatEnum
.
GPT432k
]:
{
userOpenAiKey
:
user
.
openaiKey
||
''
,
systemAuthKey
:
getSystemOpenAiKey
()
}
};
if
(
!
keyMap
[
model
])
{
return
Promise
.
reject
(
'App model is exists'
);
}
// 有自己的key
// 有自己的key
if
(
!
mustPay
&&
keyMap
[
model
].
userOpenAiKey
)
{
if
(
!
mustPay
&&
userOpenAiKey
)
{
return
{
return
{
userOpenAiKey
:
keyMap
[
model
].
userOpenAiKey
,
userOpenAiKey
,
systemAuthKey
:
''
systemAuthKey
:
''
};
};
}
}
...
@@ -219,7 +199,7 @@ export const getApiKey = async ({
...
@@ -219,7 +199,7 @@ export const getApiKey = async ({
return
{
return
{
userOpenAiKey
:
''
,
userOpenAiKey
:
''
,
systemAuthKey
:
keyMap
[
model
].
systemAuthKey
systemAuthKey
};
};
};
};
...
...
client/src/service/utils/chat/index.ts
View file @
2a45fe52
...
@@ -27,6 +27,7 @@ export type StreamResponseType = {
...
@@ -27,6 +27,7 @@ export type StreamResponseType = {
chatResponse
:
any
;
chatResponse
:
any
;
prompts
:
ChatItemType
[];
prompts
:
ChatItemType
[];
res
:
NextApiResponse
;
res
:
NextApiResponse
;
model
:
`
${
OpenAiChatEnum
}
`
;
[
key
:
string
]:
any
;
[
key
:
string
]:
any
;
};
};
export
type
StreamResponseReturnType
=
{
export
type
StreamResponseReturnType
=
{
...
@@ -35,49 +36,9 @@ export type StreamResponseReturnType = {
...
@@ -35,49 +36,9 @@ export type StreamResponseReturnType = {
finishMessages
:
ChatItemType
[];
finishMessages
:
ChatItemType
[];
};
};
export
const
modelServiceToolMap
:
Record
<
export
const
modelServiceToolMap
=
{
ChatModelType
,
chatCompletion
:
chatResponse
,
{
streamResponse
:
openAiStreamResponse
chatCompletion
:
(
data
:
ChatCompletionType
)
=>
Promise
<
ChatCompletionResponseType
>
;
streamResponse
:
(
data
:
StreamResponseType
)
=>
Promise
<
StreamResponseReturnType
>
;
}
>
=
{
[
OpenAiChatEnum
.
GPT35
]:
{
chatCompletion
:
(
data
:
ChatCompletionType
)
=>
chatResponse
({
model
:
OpenAiChatEnum
.
GPT35
,
...
data
}),
streamResponse
:
(
data
:
StreamResponseType
)
=>
openAiStreamResponse
({
model
:
OpenAiChatEnum
.
GPT35
,
...
data
})
},
[
OpenAiChatEnum
.
GPT3516k
]:
{
chatCompletion
:
(
data
:
ChatCompletionType
)
=>
chatResponse
({
model
:
OpenAiChatEnum
.
GPT3516k
,
...
data
}),
streamResponse
:
(
data
:
StreamResponseType
)
=>
openAiStreamResponse
({
model
:
OpenAiChatEnum
.
GPT3516k
,
...
data
})
},
[
OpenAiChatEnum
.
GPT4
]:
{
chatCompletion
:
(
data
:
ChatCompletionType
)
=>
chatResponse
({
model
:
OpenAiChatEnum
.
GPT4
,
...
data
}),
streamResponse
:
(
data
:
StreamResponseType
)
=>
openAiStreamResponse
({
model
:
OpenAiChatEnum
.
GPT4
,
...
data
})
},
[
OpenAiChatEnum
.
GPT432k
]:
{
chatCompletion
:
(
data
:
ChatCompletionType
)
=>
chatResponse
({
model
:
OpenAiChatEnum
.
GPT432k
,
...
data
}),
streamResponse
:
(
data
:
StreamResponseType
)
=>
openAiStreamResponse
({
model
:
OpenAiChatEnum
.
GPT432k
,
...
data
})
}
};
};
/* delete invalid symbol */
/* delete invalid symbol */
...
@@ -124,7 +85,8 @@ export const ChatContextFilter = ({
...
@@ -124,7 +85,8 @@ export const ChatContextFilter = ({
}
}
// 去掉 system 的 token
// 去掉 system 的 token
maxTokens
-=
modelToolMap
[
model
].
countTokens
({
maxTokens
-=
modelToolMap
.
countTokens
({
model
,
messages
:
systemPrompts
messages
:
systemPrompts
});
});
...
@@ -135,7 +97,8 @@ export const ChatContextFilter = ({
...
@@ -135,7 +97,8 @@ export const ChatContextFilter = ({
for
(
let
i
=
chatPrompts
.
length
-
1
;
i
>=
0
;
i
--
)
{
for
(
let
i
=
chatPrompts
.
length
-
1
;
i
>=
0
;
i
--
)
{
chats
.
unshift
(
chatPrompts
[
i
]);
chats
.
unshift
(
chatPrompts
[
i
]);
const
tokens
=
modelToolMap
[
model
].
countTokens
({
const
tokens
=
modelToolMap
.
countTokens
({
model
,
messages
:
chats
messages
:
chats
});
});
...
@@ -164,13 +127,14 @@ export const resStreamResponse = async ({
...
@@ -164,13 +127,14 @@ export const resStreamResponse = async ({
res
.
setHeader
(
'X-Accel-Buffering'
,
'no'
);
res
.
setHeader
(
'X-Accel-Buffering'
,
'no'
);
res
.
setHeader
(
'Cache-Control'
,
'no-cache, no-transform'
);
res
.
setHeader
(
'Cache-Control'
,
'no-cache, no-transform'
);
const
{
responseContent
,
totalTokens
,
finishMessages
}
=
await
modelServiceToolMap
[
const
{
responseContent
,
totalTokens
,
finishMessages
}
=
await
modelServiceToolMap
.
streamResponse
(
model
{
].
streamResponse
({
chatResponse
,
chatResponse
,
prompts
,
prompts
,
res
res
,
});
model
}
);
return
{
responseContent
,
totalTokens
,
finishMessages
};
return
{
responseContent
,
totalTokens
,
finishMessages
};
};
};
...
@@ -259,7 +223,8 @@ export const V2_StreamResponse = async ({
...
@@ -259,7 +223,8 @@ export const V2_StreamResponse = async ({
value
:
responseContent
value
:
responseContent
});
});
const
totalTokens
=
modelToolMap
[
model
].
countTokens
({
const
totalTokens
=
modelToolMap
.
countTokens
({
model
,
messages
:
finishMessages
messages
:
finishMessages
});
});
...
...
client/src/service/utils/chat/openai.ts
View file @
2a45fe52
...
@@ -35,7 +35,8 @@ export const chatResponse = async ({
...
@@ -35,7 +35,8 @@ export const chatResponse = async ({
const
adaptMessages
=
adaptChatItem_openAI
({
messages
:
filterMessages
,
reserveId
:
false
});
const
adaptMessages
=
adaptChatItem_openAI
({
messages
:
filterMessages
,
reserveId
:
false
});
const
chatAPI
=
getOpenAIApi
(
apiKey
);
const
chatAPI
=
getOpenAIApi
(
apiKey
);
const
promptsToken
=
modelToolMap
[
model
].
countTokens
({
const
promptsToken
=
modelToolMap
.
countTokens
({
model
,
messages
:
filterMessages
messages
:
filterMessages
});
});
...
@@ -116,7 +117,8 @@ export const openAiStreamResponse = async ({
...
@@ -116,7 +117,8 @@ export const openAiStreamResponse = async ({
value
:
responseContent
value
:
responseContent
});
});
const
totalTokens
=
modelToolMap
[
model
].
countTokens
({
const
totalTokens
=
modelToolMap
.
countTokens
({
model
,
messages
:
finishMessages
messages
:
finishMessages
});
});
...
...
client/src/types/index.d.ts
View file @
2a45fe52
...
@@ -21,7 +21,9 @@ declare global {
...
@@ -21,7 +21,9 @@ declare global {
var
QRCode
:
any
;
var
QRCode
:
any
;
var
qaQueueLen
:
number
;
var
qaQueueLen
:
number
;
var
vectorQueueLen
:
number
;
var
vectorQueueLen
:
number
;
var
OpenAiEncMap
:
Record
<
string
,
Tiktoken
>
;
var
OpenAiEncMap
:
Tiktoken
;
var
sendInformQueue
:
(()
=>
Promise
<
void
>
)[];
var
sendInformQueueLen
:
number
;
var
systemEnv
:
{
var
systemEnv
:
{
vectorMaxProcess
:
number
;
vectorMaxProcess
:
number
;
qaMaxProcess
:
number
;
qaMaxProcess
:
number
;
...
...
client/src/utils/file.ts
View file @
2a45fe52
...
@@ -152,7 +152,7 @@ export const splitText_token = ({ text, maxLen }: { text: string; maxLen: number
...
@@ -152,7 +152,7 @@ export const splitText_token = ({ text, maxLen }: { text: string; maxLen: number
const
slideLen
=
Math
.
floor
(
maxLen
*
0.3
);
const
slideLen
=
Math
.
floor
(
maxLen
*
0.3
);
try
{
try
{
const
enc
=
getOpenAiEncMap
()
[
OpenAiChatEnum
.
GPT35
]
;
const
enc
=
getOpenAiEncMap
();
// filter empty text. encode sentence
// filter empty text. encode sentence
const
encodeText
=
enc
.
encode
(
text
);
const
encodeText
=
enc
.
encode
(
text
);
...
...
client/src/utils/plugin/index.ts
View file @
2a45fe52
...
@@ -4,32 +4,8 @@ import type { ChatItemType } from '@/types/chat';
...
@@ -4,32 +4,8 @@ import type { ChatItemType } from '@/types/chat';
import
{
countOpenAIToken
,
openAiSliceTextByToken
}
from
'./openai'
;
import
{
countOpenAIToken
,
openAiSliceTextByToken
}
from
'./openai'
;
import
{
gpt_chatItemTokenSlice
}
from
'@/pages/api/openapi/text/gptMessagesSlice'
;
import
{
gpt_chatItemTokenSlice
}
from
'@/pages/api/openapi/text/gptMessagesSlice'
;
export
const
modelToolMap
:
Record
<
export
const
modelToolMap
=
{
ChatModelType
,
countTokens
:
countOpenAIToken
,
{
sliceText
:
openAiSliceTextByToken
,
countTokens
:
(
data
:
{
messages
:
ChatItemType
[]
})
=>
number
;
tokenSlice
:
gpt_chatItemTokenSlice
sliceText
:
(
data
:
{
text
:
string
;
length
:
number
})
=>
string
;
tokenSlice
:
(
data
:
{
messages
:
ChatItemType
[];
maxToken
:
number
})
=>
ChatItemType
[];
}
>
=
{
[
OpenAiChatEnum
.
GPT35
]:
{
countTokens
:
({
messages
})
=>
countOpenAIToken
({
model
:
OpenAiChatEnum
.
GPT35
,
messages
}),
sliceText
:
(
data
)
=>
openAiSliceTextByToken
({
model
:
OpenAiChatEnum
.
GPT35
,
...
data
}),
tokenSlice
:
(
data
)
=>
gpt_chatItemTokenSlice
({
model
:
OpenAiChatEnum
.
GPT35
,
...
data
})
},
[
OpenAiChatEnum
.
GPT3516k
]:
{
countTokens
:
({
messages
})
=>
countOpenAIToken
({
model
:
OpenAiChatEnum
.
GPT3516k
,
messages
}),
sliceText
:
(
data
)
=>
openAiSliceTextByToken
({
model
:
OpenAiChatEnum
.
GPT3516k
,
...
data
}),
tokenSlice
:
(
data
)
=>
gpt_chatItemTokenSlice
({
model
:
OpenAiChatEnum
.
GPT3516k
,
...
data
})
},
[
OpenAiChatEnum
.
GPT4
]:
{
countTokens
:
({
messages
})
=>
countOpenAIToken
({
model
:
OpenAiChatEnum
.
GPT4
,
messages
}),
sliceText
:
(
data
)
=>
openAiSliceTextByToken
({
model
:
OpenAiChatEnum
.
GPT4
,
...
data
}),
tokenSlice
:
(
data
)
=>
gpt_chatItemTokenSlice
({
model
:
OpenAiChatEnum
.
GPT4
,
...
data
})
},
[
OpenAiChatEnum
.
GPT432k
]:
{
countTokens
:
({
messages
})
=>
countOpenAIToken
({
model
:
OpenAiChatEnum
.
GPT432k
,
messages
}),
sliceText
:
(
data
)
=>
openAiSliceTextByToken
({
model
:
OpenAiChatEnum
.
GPT432k
,
...
data
}),
tokenSlice
:
(
data
)
=>
gpt_chatItemTokenSlice
({
model
:
OpenAiChatEnum
.
GPT432k
,
...
data
})
}
};
};
client/src/utils/plugin/openai.ts
View file @
2a45fe52
...
@@ -4,7 +4,6 @@ import { ChatRoleEnum } from '@/constants/chat';
...
@@ -4,7 +4,6 @@ import { ChatRoleEnum } from '@/constants/chat';
import
{
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
import
axios
from
'axios'
;
import
axios
from
'axios'
;
import
dayjs
from
'dayjs'
;
import
type
{
MessageItemType
}
from
'@/pages/api/openapi/v1/chat/completions'
;
import
type
{
MessageItemType
}
from
'@/pages/api/openapi/v1/chat/completions'
;
export
const
getOpenAiEncMap
=
()
=>
{
export
const
getOpenAiEncMap
=
()
=>
{
...
@@ -14,28 +13,11 @@ export const getOpenAiEncMap = () => {
...
@@ -14,28 +13,11 @@ export const getOpenAiEncMap = () => {
if
(
typeof
global
!==
'undefined'
&&
global
.
OpenAiEncMap
)
{
if
(
typeof
global
!==
'undefined'
&&
global
.
OpenAiEncMap
)
{
return
global
.
OpenAiEncMap
;
return
global
.
OpenAiEncMap
;
}
}
const
enc
=
{
const
enc
=
encoding_for_model
(
'gpt-3.5-turbo'
,
{
[
OpenAiChatEnum
.
GPT35
]:
encoding_for_model
(
'gpt-3.5-turbo'
,
{
'<|im_start|>'
:
100264
,
'<|im_start|>'
:
100264
,
'<|im_end|>'
:
100265
,
'<|im_end|>'
:
100265
,
'<|im_sep|>'
:
100266
'<|im_sep|>'
:
100266
}),
});
[
OpenAiChatEnum
.
GPT3516k
]:
encoding_for_model
(
'gpt-3.5-turbo'
,
{
'<|im_start|>'
:
100264
,
'<|im_end|>'
:
100265
,
'<|im_sep|>'
:
100266
}),
[
OpenAiChatEnum
.
GPT4
]:
encoding_for_model
(
'gpt-4'
,
{
'<|im_start|>'
:
100264
,
'<|im_end|>'
:
100265
,
'<|im_sep|>'
:
100266
}),
[
OpenAiChatEnum
.
GPT432k
]:
encoding_for_model
(
'gpt-4-32k'
,
{
'<|im_start|>'
:
100264
,
'<|im_end|>'
:
100265
,
'<|im_sep|>'
:
100266
})
};
if
(
typeof
window
!==
'undefined'
)
{
if
(
typeof
window
!==
'undefined'
)
{
window
.
OpenAiEncMap
=
enc
;
window
.
OpenAiEncMap
=
enc
;
...
@@ -78,7 +60,7 @@ export function countOpenAIToken({
...
@@ -78,7 +60,7 @@ export function countOpenAIToken({
const
adaptMessages
=
adaptChatItem_openAI
({
messages
,
reserveId
:
true
});
const
adaptMessages
=
adaptChatItem_openAI
({
messages
,
reserveId
:
true
});
const
token
=
adaptMessages
.
reduce
((
sum
,
item
)
=>
{
const
token
=
adaptMessages
.
reduce
((
sum
,
item
)
=>
{
const
text
=
`
${
item
.
role
}
\n
${
item
.
content
}
`
;
const
text
=
`
${
item
.
role
}
\n
${
item
.
content
}
`
;
const
enc
=
getOpenAiEncMap
()
[
model
]
;
const
enc
=
getOpenAiEncMap
();
const
encodeText
=
enc
.
encode
(
text
);
const
encodeText
=
enc
.
encode
(
text
);
const
tokens
=
encodeText
.
length
+
diffVal
;
const
tokens
=
encodeText
.
length
+
diffVal
;
return
sum
+
tokens
;
return
sum
+
tokens
;
...
@@ -96,7 +78,7 @@ export const openAiSliceTextByToken = ({
...
@@ -96,7 +78,7 @@ export const openAiSliceTextByToken = ({
text
:
string
;
text
:
string
;
length
:
number
;
length
:
number
;
})
=>
{
})
=>
{
const
enc
=
getOpenAiEncMap
()
[
model
]
;
const
enc
=
getOpenAiEncMap
();
const
encodeText
=
enc
.
encode
(
text
);
const
encodeText
=
enc
.
encode
(
text
);
const
decoder
=
new
TextDecoder
();
const
decoder
=
new
TextDecoder
();
return
decoder
.
decode
(
enc
.
decode
(
encodeText
.
slice
(
0
,
length
)));
return
decoder
.
decode
(
enc
.
decode
(
encodeText
.
slice
(
0
,
length
)));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment