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
b22c878c
authored
Aug 31, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: variable input and update chat time
parent
3420f677
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
30 additions
and
48 deletions
+30
-48
client/public/locales/en/common.json
+2
-1
client/public/locales/zh/common.json
+2
-1
client/src/components/ChatBox/index.tsx
+8
-5
client/src/constants/model.ts
+0
-7
client/src/pages/api/openapi/kb/pushData.ts
+0
-1
client/src/pages/api/openapi/plugin/vector.ts
+1
-1
client/src/pages/api/openapi/text/gptMessagesSlice.ts
+2
-8
client/src/pages/api/plugins/kb/data/insertData.ts
+0
-1
client/src/pages/app/detail/components/AdEdit/components/Nodes/NodeVariable.tsx
+2
-2
client/src/pages/app/detail/components/BasicEdit/index.tsx
+7
-0
client/src/pages/app/detail/components/VariableEditModal.tsx
+1
-1
client/src/service/moduleDispatch/chat/oneapi.ts
+0
-4
client/src/service/utils/chat/index.ts
+1
-4
client/src/service/utils/chat/saveChat.ts
+2
-2
client/src/utils/plugin/openai.ts
+2
-10
No files found.
client/public/locales/en/common.json
View file @
b22c878c
...
...
@@ -31,7 +31,8 @@
"Mark Count"
:
"Mark Count"
,
"My Apps"
:
"My Apps"
,
"Output Field Settings"
:
"Output Field Settings"
,
"Paste Config"
:
"Paste Config"
"Paste Config"
:
"Paste Config"
,
"Variable Key Repeat Tip"
:
"Variable Key Repeat"
},
"chat"
:
{
"Admin Mark Content"
:
"Corrected response"
,
...
...
client/public/locales/zh/common.json
View file @
b22c878c
...
...
@@ -31,7 +31,8 @@
"Mark Count"
:
"标注答案数量"
,
"My Apps"
:
"我的应用"
,
"Output Field Settings"
:
"输出字段编辑"
,
"Paste Config"
:
"粘贴配置"
"Paste Config"
:
"粘贴配置"
,
"Variable Key Repeat Tip"
:
"变量 key 重复"
},
"chat"
:
{
"Admin Mark Content"
:
"纠正后的回复"
,
...
...
client/src/components/ChatBox/index.tsx
View file @
b22c878c
...
...
@@ -198,6 +198,8 @@ const ChatBox = (
chatHistory
[
chatHistory
.
length
-
1
]?.
status
!==
'finish'
,
[
chatHistory
]
);
// compute variable input is finish.
const
[
variableInputFinish
,
setVariableInputFinish
]
=
useState
(
false
);
const
variableIsFinish
=
useMemo
(()
=>
{
if
(
!
variableModules
||
chatHistory
.
length
>
0
)
return
true
;
...
...
@@ -208,8 +210,8 @@ const ChatBox = (
}
}
return
true
;
},
[
chatHistory
.
length
,
variableModules
,
variables
]);
return
variableInputFinish
;
},
[
chatHistory
.
length
,
variable
InputFinish
,
variable
Modules
,
variables
]);
const
{
register
,
reset
,
getValues
,
setValue
,
handleSubmit
}
=
useForm
<
Record
<
string
,
any
>>
({
defaultValues
:
variables
...
...
@@ -408,6 +410,7 @@ const ChatBox = (
]
);
// output data
useImperativeHandle
(
ref
,
()
=>
({
getChatHistory
:
()
=>
chatHistory
,
resetVariables
(
e
)
{
...
...
@@ -420,6 +423,7 @@ const ChatBox = (
setVariables
(
e
||
defaultVal
);
},
resetHistory
(
e
)
{
setVariableInputFinish
(
!!
e
.
length
);
setChatHistory
(
e
);
},
scrollToBottom
...
...
@@ -554,9 +558,7 @@ const ChatBox = (
label
:
item
.
value
,
value
:
item
.
value
}))
}
{
...
register
(
item
.
key
,
{
required
:
item
.
required
})}
value=
{
getValues
(
item
.
key
)
}
onchange=
{
(
e
)
=>
{
setValue
(
item
.
key
,
e
);
setRefresh
(
!
refresh
);
...
...
@@ -574,6 +576,7 @@ const ChatBox = (
onClick=
{
handleSubmit
((
data
)
=>
{
onUpdateVariable
?.(
data
);
setVariables
(
data
);
setVariableInputFinish
(
true
);
})
}
>
{
'开始对话'
}
...
...
client/src/constants/model.ts
View file @
b22c878c
import
type
{
ShareChatEditType
}
from
'@/types/app'
;
import
type
{
AppSchema
}
from
'@/types/mongoSchema'
;
export
enum
OpenAiChatEnum
{
'GPT35'
=
'gpt-3.5-turbo'
,
'GPT3516k'
=
'gpt-3.5-turbo-16k'
,
'FastAI-Plus'
=
'gpt-4'
,
'FastAI-Plus32k'
=
'gpt-4-32k'
}
export
const
defaultApp
:
AppSchema
=
{
_id
:
''
,
userId
:
'userId'
,
...
...
client/src/pages/api/openapi/kb/pushData.ts
View file @
b22c878c
...
...
@@ -104,7 +104,6 @@ export async function pushDataToKb({
// count q token
const
token
=
modelToolMap
.
countTokens
({
model
:
'gpt-3.5-turbo'
,
messages
:
[{
obj
:
'System'
,
value
:
item
.
q
}]
});
...
...
client/src/pages/api/openapi/plugin/vector.ts
View file @
b22c878c
...
...
@@ -69,7 +69,7 @@ export async function getVector({
.
then
(
async
(
res
)
=>
{
if
(
!
res
.
data
?.
data
?.[
0
]?.
embedding
)
{
// @ts-ignore
return
Promise
.
reject
(
res
.
data
?.
error
?.
message
||
'Embedding Error'
);
return
Promise
.
reject
(
res
.
data
?.
error
?.
message
||
'Embedding
API
Error'
);
}
return
{
tokenLen
:
res
.
data
.
usage
.
total_tokens
||
0
,
...
...
client/src/pages/api/openapi/text/gptMessagesSlice.ts
View file @
b22c878c
...
...
@@ -4,13 +4,10 @@ import { jsonRes } from '@/service/response';
import
{
authUser
}
from
'@/service/utils/auth'
;
import
type
{
ChatItemType
}
from
'@/types/chat'
;
import
{
countOpenAIToken
}
from
'@/utils/plugin/openai'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
type
ModelType
=
`
${
OpenAiChatEnum
}
`
;
type
Props
=
{
messages
:
ChatItemType
[];
model
:
ModelType
;
model
:
string
;
maxLen
:
number
;
};
type
Response
=
ChatItemType
[];
...
...
@@ -28,7 +25,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return
jsonRes
<
Response
>
(
res
,
{
data
:
gpt_chatItemTokenSlice
({
messages
,
model
,
maxToken
:
maxLen
})
});
...
...
@@ -42,11 +38,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
export
function
gpt_chatItemTokenSlice
({
messages
,
model
=
'gpt-3.5-turbo'
,
maxToken
}:
{
messages
:
ChatItemType
[];
model
?:
string
;
maxToken
:
number
;
})
{
let
result
:
ChatItemType
[]
=
[];
...
...
@@ -54,7 +48,7 @@ export function gpt_chatItemTokenSlice({
for
(
let
i
=
0
;
i
<
messages
.
length
;
i
++
)
{
const
msgs
=
[...
result
,
messages
[
i
]];
const
tokens
=
countOpenAIToken
({
messages
:
msgs
,
model
});
const
tokens
=
countOpenAIToken
({
messages
:
msgs
});
if
(
tokens
<
maxToken
)
{
result
=
msgs
;
...
...
client/src/pages/api/plugins/kb/data/insertData.ts
View file @
b22c878c
...
...
@@ -35,7 +35,6 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
// token check
const
token
=
modelToolMap
.
countTokens
({
model
:
'gpt-3.5-turbo'
,
messages
:
[{
obj
:
'System'
,
value
:
q
}]
});
...
...
client/src/pages/app/detail/components/AdEdit/components/Nodes/NodeVariable.tsx
View file @
b22c878c
...
...
@@ -10,7 +10,7 @@ import type { VariableItemType } from '@/types/app';
import
MyIcon
from
'@/components/Icon'
;
import
{
customAlphabet
}
from
'nanoid'
;
const
nanoid
=
customAlphabet
(
'abcdefghijklmnopqrstuvwxyz1234567890'
,
6
);
import
VariableEditModal
from
'../../../VariableEditModal'
;
import
VariableEditModal
,
{
addVariable
}
from
'../../../VariableEditModal'
;
export
const
defaultVariable
:
VariableItemType
=
{
id
:
nanoid
(),
...
...
@@ -105,7 +105,7 @@ const NodeUserGuide = ({ data }: NodeProps<FlowModuleItemType>) => {
variant=
{
'base'
}
leftIcon=
{
<
AddIcon
fontSize=
{
'10px'
}
/>
}
onClick=
{
()
=>
{
const
newVariable
=
{
...
defaultVariable
,
id
:
nanoid
()
}
;
const
newVariable
=
addVariable
()
;
updateVariables
(
variables
.
concat
(
newVariable
));
setEditVariable
(
newVariable
);
}
}
...
...
client/src/pages/app/detail/components/BasicEdit/index.tsx
View file @
b22c878c
...
...
@@ -532,6 +532,13 @@ const Settings = ({ appId }: { appId: string }) => {
variables
.
map
((
item
)
=>
(
item
.
id
===
variable
.
id
?
variable
:
item
))
);
}
else
{
// auth same key
if
(
variables
.
find
((
item
)
=>
item
.
key
===
variable
.
key
))
{
return
toast
({
status
:
'warning'
,
title
:
t
(
'app.Variable Key Repeat Tip'
)
});
}
appendVariable
(
variable
);
}
...
...
client/src/pages/app/detail/components/VariableEditModal.tsx
View file @
b22c878c
...
...
@@ -204,6 +204,6 @@ export const defaultVariable: VariableItemType = {
enums
:
[{
value
:
''
}]
};
export
const
addVariable
=
()
=>
{
const
newVariable
=
{
...
defaultVariable
,
id
:
nanoid
()
};
const
newVariable
=
{
...
defaultVariable
,
key
:
nanoid
(),
id
:
nanoid
()
};
return
newVariable
;
};
client/src/service/moduleDispatch/chat/oneapi.ts
View file @
b22c878c
import
type
{
NextApiResponse
}
from
'next'
;
import
{
sseResponse
}
from
'@/service/utils/tools'
;
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'
;
...
...
@@ -198,7 +197,6 @@ function filterQuote({
model
:
ChatModelItemType
;
})
{
const
sliceResult
=
modelToolMap
.
tokenSlice
({
model
:
model
.
model
,
maxToken
:
model
.
quoteMaxToken
,
messages
:
quoteQA
.
map
((
item
)
=>
({
obj
:
ChatRoleEnum
.
System
,
...
...
@@ -312,7 +310,6 @@ function getMaxTokens({
/* count response max token */
const
promptsToken
=
modelToolMap
.
countTokens
({
model
:
model
.
model
,
messages
:
filterMessages
});
maxToken
=
maxToken
+
promptsToken
>
tokensLimit
?
tokensLimit
-
promptsToken
:
maxToken
;
...
...
@@ -383,7 +380,6 @@ async function streamResponse({
}
if
(
error
)
{
console
.
log
(
error
);
return
Promise
.
reject
(
error
);
}
...
...
client/src/service/utils/chat/index.ts
View file @
b22c878c
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
modelToolMap
}
from
'@/utils/plugin'
;
import
{
ChatRoleEnum
}
from
'@/constants/chat'
;
import
{
OpenAiChatEnum
}
from
'@/constants/model'
;
import
type
{
NextApiResponse
}
from
'next'
;
export
type
ChatCompletionResponseType
=
{
...
...
@@ -14,7 +13,7 @@ export type StreamResponseType = {
chatResponse
:
any
;
prompts
:
ChatItemType
[];
res
:
NextApiResponse
;
model
:
`
${
OpenAiChatEnum
}
`
;
model
:
string
;
[
key
:
string
]:
any
;
};
...
...
@@ -45,7 +44,6 @@ export const ChatContextFilter = ({
// reduce token of systemPrompt
maxTokens
-=
modelToolMap
.
countTokens
({
model
,
messages
:
systemPrompts
});
...
...
@@ -57,7 +55,6 @@ export const ChatContextFilter = ({
chats
.
unshift
(
chatPrompts
[
i
]);
const
tokens
=
modelToolMap
.
countTokens
({
model
,
messages
:
chats
});
...
...
client/src/service/utils/chat/saveChat.ts
View file @
b22c878c
...
...
@@ -44,7 +44,7 @@ export async function saveChat({
];
if
(
chatHistory
)
{
promise
.
push
(
[
promise
.
push
(
Chat
.
updateOne
(
{
chatId
,
userId
},
{
...
...
@@ -52,7 +52,7 @@ export async function saveChat({
updateTime
:
new
Date
()
}
)
]
);
);
}
else
{
promise
.
push
(
Chat
.
create
({
...
...
client/src/utils/plugin/openai.ts
View file @
b22c878c
...
...
@@ -47,21 +47,13 @@ export const adaptChatItem_openAI = ({
}));
};
export
function
countOpenAIToken
({
messages
,
model
=
'gpt-3.5-turbo'
}:
{
messages
:
ChatItemType
[];
model
?:
string
;
})
{
const
diffVal
=
model
.
startsWith
(
'gpt-3.5-turbo'
)
?
3
:
2
;
export
function
countOpenAIToken
({
messages
}:
{
messages
:
ChatItemType
[]
})
{
const
adaptMessages
=
adaptChatItem_openAI
({
messages
,
reserveId
:
true
});
const
token
=
adaptMessages
.
reduce
((
sum
,
item
)
=>
{
const
text
=
`
${
item
.
role
}
\n
${
item
.
content
}
`
;
const
enc
=
getOpenAiEncMap
();
const
encodeText
=
enc
.
encode
(
text
);
const
tokens
=
encodeText
.
length
+
diffVal
;
const
tokens
=
encodeText
.
length
+
3
;
// 补充估算值
return
sum
+
tokens
;
},
0
);
...
...
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