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