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
358c4716
authored
Jul 14, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: refresh max token
parent
f3715731
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
83 additions
and
27 deletions
+83
-27
client/src/api/app.ts
+2
-2
client/src/constants/flow/ModuleTemplate.ts
+9
-5
client/src/pages/api/app/data/totalUsage.ts
+2
-3
client/src/pages/api/openapi/v1/chat/completions.ts
+8
-4
client/src/pages/app/detail/components/Charts/TotalUsage.tsx
+7
-6
client/src/pages/app/detail/components/Settings.tsx
+5
-5
client/src/pages/app/detail/components/edit/components/NodeChat.tsx
+50
-2
No files found.
client/src/api/app.ts
View file @
358c4716
...
...
@@ -45,8 +45,8 @@ export const triggerModelCollection = (appId: string) =>
POST
<
number
>
(
`/app/share/collection?appId=
${
appId
}
`
);
// ====================== data
export
const
get
Token
Usage
=
(
data
:
{
appId
:
string
})
=>
POST
<
{
to
kenLen
:
number
;
date
:
Date
}[]
>
(
`/app/data/token
Usage`
,
{
export
const
get
AppTotal
Usage
=
(
data
:
{
appId
:
string
})
=>
POST
<
{
to
tal
:
number
;
date
:
Date
}[]
>
(
`/app/data/total
Usage`
,
{
...
data
,
start
:
addDays
(
new
Date
(),
-
7
),
end
:
new
Date
()
...
...
client/src/constants/flow/ModuleTemplate.ts
View file @
358c4716
...
...
@@ -84,6 +84,7 @@ export const HistoryModule: AppModuleTemplateItemType = {
]
};
const
defaultModel
=
chatModelList
[
0
];
export
const
ChatModule
:
AppModuleTemplateItemType
=
{
logo
:
'/imgs/module/AI.png'
,
name
:
'AI 对话'
,
...
...
@@ -94,9 +95,9 @@ export const ChatModule: AppModuleTemplateItemType = {
inputs
:
[
{
key
:
'model'
,
type
:
FlowInputItemTypeEnum
.
select
,
type
:
FlowInputItemTypeEnum
.
custom
,
label
:
'对话模型'
,
value
:
chatModelList
[
0
]
?.
model
,
value
:
defaultModel
?.
model
,
list
:
chatModelList
.
map
((
item
)
=>
({
label
:
item
.
name
,
value
:
item
.
model
}))
},
{
...
...
@@ -116,13 +117,16 @@ export const ChatModule: AppModuleTemplateItemType = {
key
:
'maxToken'
,
type
:
FlowInputItemTypeEnum
.
slider
,
label
:
'回复上限'
,
value
:
3
000
,
value
:
defaultModel
?
defaultModel
.
contextMaxToken
/
2
:
2
000
,
min
:
0
,
max
:
4000
,
max
:
defaultModel
?.
contextMaxToken
||
4000
,
step
:
50
,
markList
:
[
{
label
:
'0'
,
value
:
0
},
{
label
:
'4000'
,
value
:
4000
}
{
label
:
`
${
defaultModel
?.
contextMaxToken
||
4000
}
`,
value: defaultModel?.contextMaxToken || 4000
}
]
},
{
...
...
client/src/pages/api/app/data/to
ken
Usage.ts
→
client/src/pages/api/app/data/to
tal
Usage.ts
View file @
358c4716
...
...
@@ -2,7 +2,6 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import
{
jsonRes
}
from
'@/service/response'
;
import
{
connectToDatabase
,
Bill
}
from
'@/service/mongo'
;
import
{
authUser
}
from
'@/service/utils/auth'
;
import
type
{
ChatHistoryItemType
}
from
'@/types/chat'
;
import
{
Types
}
from
'mongoose'
;
/* get one app chat history content number. */
...
...
@@ -28,14 +27,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
month
:
{
$month
:
'$time'
},
day
:
{
$dayOfMonth
:
'$time'
}
},
to
kenLen
:
{
$sum
:
'$tokenLen'
}
// 对tokenLen的值求和
to
tal
:
{
$sum
:
'$total'
}
}
},
{
$project
:
{
_id
:
0
,
date
:
{
$dateFromParts
:
{
year
:
'$_id.year'
,
month
:
'$_id.month'
,
day
:
'$_id.day'
}
},
to
kenLen
:
1
to
tal
:
1
}
},
{
$sort
:
{
date
:
1
}
}
...
...
client/src/pages/api/openapi/v1/chat/completions.ts
View file @
358c4716
...
...
@@ -15,7 +15,7 @@ import { Types } from 'mongoose';
import
{
moduleFetch
}
from
'@/service/api/request'
;
import
{
AppModuleItemType
,
RunningModuleItemType
}
from
'@/types/app'
;
import
{
FlowInputItemTypeEnum
}
from
'@/constants/flow'
;
import
{
finishTaskBill
,
createTaskBill
}
from
'@/service/events/pushBill'
;
import
{
finishTaskBill
,
createTaskBill
,
delTaskBill
}
from
'@/service/events/pushBill'
;
import
{
BillSourceEnum
}
from
'@/constants/user'
;
export
type
MessageItemType
=
ChatCompletionRequestMessage
&
{
_id
?:
string
};
...
...
@@ -56,6 +56,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
variables
=
{}
}
=
req
.
body
as
Props
;
let
billId
=
''
;
try
{
if
(
!
messages
)
{
throw
new
Error
(
'Prams Error'
);
...
...
@@ -108,11 +110,11 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
res
.
setHeader
(
'newHistoryId'
,
String
(
newHistoryId
));
}
const
billId
=
await
createTaskBill
({
billId
=
await
createTaskBill
({
userId
,
appName
:
app
.
name
,
appId
,
source
:
BillSourceEnum
.
fastgpt
source
:
authType
===
'apikey'
?
BillSourceEnum
.
api
:
BillSourceEnum
.
fastgpt
});
/* start process */
...
...
@@ -125,7 +127,7 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
userChatInput
:
prompt
.
value
},
stream
,
billId
:
''
billId
});
// save chat
...
...
@@ -184,6 +186,8 @@ export default withNextCors(async function handler(req: NextApiRequest, res: Nex
billId
});
}
catch
(
err
:
any
)
{
delTaskBill
(
billId
);
if
(
stream
)
{
res
.
status
(
500
);
sseErrRes
(
res
,
err
);
...
...
client/src/pages/app/detail/components/Charts/To
ken
Usage.tsx
→
client/src/pages/app/detail/components/Charts/To
tal
Usage.tsx
View file @
358c4716
import
React
,
{
useEffect
,
useMemo
,
useRef
}
from
'react'
;
import
*
as
echarts
from
'echarts'
;
import
{
useGlobalStore
}
from
'@/store/global'
;
import
{
get
Token
Usage
}
from
'@/api/app'
;
import
{
get
AppTotal
Usage
}
from
'@/api/app'
;
import
{
useQuery
}
from
'@tanstack/react-query'
;
import
dayjs
from
'dayjs'
;
import
{
formatPrice
}
from
'@/utils/user'
;
const
map
=
{
blue
:
{
...
...
@@ -98,7 +99,7 @@ const TokenUsage = ({ appId }: { appId: string }) => {
const
Dom
=
useRef
<
HTMLDivElement
>
(
null
);
const
myChart
=
useRef
<
echarts
.
ECharts
>
();
const
{
data
=
[]
}
=
useQuery
([
'init'
],
()
=>
get
Token
Usage
({
appId
}));
const
{
data
=
[]
}
=
useQuery
([
'init'
],
()
=>
get
AppTotal
Usage
({
appId
}));
const
option
=
useMemo
(
()
=>
({
...
...
@@ -110,7 +111,7 @@ const TokenUsage = ({ appId }: { appId: string }) => {
},
yAxis
:
{
type
:
'value'
,
max
:
Math
.
max
(...
data
.
map
((
item
)
=>
item
.
to
kenLen
)),
max
:
Math
.
max
(...
data
.
map
((
item
)
=>
item
.
to
tal
)),
min
:
0
},
grid
:
{
...
...
@@ -132,14 +133,14 @@ const TokenUsage = ({ appId }: { appId: string }) => {
return
`
<div>
<div>
${
dayjs
(
data
.
axisValue
).
format
(
'YYYY/MM/DD'
)}
</div>
<div>
${
((
e
[
0
]?.
value
||
0
)
/
1000
).
toFixed
(
2
)}
k
Tokens
<
/div
>
<div>
${
formatPrice
(
e
[
0
]?.
value
||
0
)}
元
<
/div
>
<
/div
>
`;
}
},
series: [
{
data: data.map((item) => item.to
kenLen
),
data: data.map((item) => item.to
tal
),
type: 'line',
showSymbol: true,
animationDuration: 300,
...
...
@@ -170,7 +171,7 @@ const TokenUsage = ({ appId }: { appId: string }) => {
if (!Dom.current || myChart?.current?.getOption()) return;
myChart.current = echarts.init(Dom.current);
myChart.current && myChart.current.setOption(option);
}, [
Dom
]);
}, []);
// data changed, update
useEffect(() => {
...
...
client/src/pages/app/detail/components/Settings.tsx
View file @
358c4716
...
...
@@ -14,8 +14,8 @@ import Avatar from '@/components/Avatar';
import
MyIcon
from
'@/components/Icon'
;
const
InfoModal
=
dynamic
(()
=>
import
(
'./InfoModal'
));
const
To
kenUsage
=
dynamic
(()
=>
import
(
'./Charts/TokenUsage'
)
);
const
AppEdit
=
dynamic
(()
=>
import
(
'./edit'
));
const
To
talUsage
=
dynamic
(()
=>
import
(
'./Charts/TotalUsage'
),
{
ssr
:
false
}
);
const
AppEdit
=
dynamic
(()
=>
import
(
'./edit'
)
,
{
ssr
:
false
}
);
import
styles
from
'../../list/index.module.scss'
;
const
Settings
=
({
appId
}:
{
appId
:
string
})
=>
{
...
...
@@ -143,10 +143,10 @@ const Settings = ({ appId }: { appId: string }) => {
</
Box
>
<
Box
>
<
Box
mb=
{
2
}
fontSize=
{
[
'md'
,
'xl'
]
}
>
近 7 日
Tokens 消耗
近 7 日
消费
</
Box
>
<
Box
h=
{
'150px'
}
>
<
To
ken
Usage
appId=
{
appId
}
/>
<
Box
h=
{
'150px'
}
w=
{
'100%'
}
>
<
To
tal
Usage
appId=
{
appId
}
/>
</
Box
>
</
Box
>
</
Grid
>
...
...
client/src/pages/app/detail/components/edit/components/NodeChat.tsx
View file @
358c4716
...
...
@@ -7,6 +7,8 @@ import Container from './modules/Container';
import
RenderInput
from
'./render/RenderInput'
;
import
RenderOutput
from
'./render/RenderOutput'
;
import
{
FlowOutputItemTypeEnum
}
from
'@/constants/flow'
;
import
MySelect
from
'@/components/Select'
;
import
{
chatModelList
}
from
'@/store/static'
;
const
NodeChat
=
({
data
:
{
moduleId
,
inputs
,
outputs
,
onChangeNode
,
...
props
}
...
...
@@ -15,11 +17,57 @@ const NodeChat = ({
()
=>
outputs
.
filter
((
item
)
=>
item
.
type
!==
FlowOutputItemTypeEnum
.
hidden
).
length
,
[
outputs
]
);
return
(
<
NodeCard
minW=
{
'400px'
}
logo=
{
'/icon/logo.png'
}
name=
{
'对话'
}
moduleId=
{
moduleId
}
{
...
props
}
>
<
NodeCard
minW=
{
'400px'
}
moduleId=
{
moduleId
}
{
...
props
}
>
<
Divider
text=
"Input"
/>
<
Container
>
<
RenderInput
moduleId=
{
moduleId
}
onChangeNode=
{
onChangeNode
}
flowInputList=
{
inputs
}
/>
<
RenderInput
moduleId=
{
moduleId
}
onChangeNode=
{
onChangeNode
}
flowInputList=
{
inputs
}
CustomComponent=
{
{
model
:
(
inputItem
)
=>
(
<
MySelect
width=
{
'100%'
}
value=
{
inputItem
.
value
}
list=
{
inputItem
.
list
||
[]
}
onchange=
{
(
e
)
=>
{
onChangeNode
({
moduleId
,
key
:
inputItem
.
key
,
value
:
e
});
// update max tokens
const
model
=
chatModelList
.
find
((
item
)
=>
item
.
model
===
e
);
if
(
!
model
)
return
;
onChangeNode
({
moduleId
,
key
:
'maxToken'
,
valueKey
:
'markList'
,
value
:
[
{
label
:
'0'
,
value
:
0
},
{
label
:
`${model.contextMaxToken}`
,
value
:
model
.
contextMaxToken
}
]
});
onChangeNode
({
moduleId
,
key
:
'maxToken'
,
valueKey
:
'max'
,
value
:
model
.
contextMaxToken
});
onChangeNode
({
moduleId
,
key
:
'maxToken'
,
valueKey
:
'value'
,
value
:
model
.
contextMaxToken
/
2
});
}
}
/>
)
}
}
/>
</
Container
>
{
outputsLen
>
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