File tree Expand file tree Collapse file tree 4 files changed +20
-39
lines changed Expand file tree Collapse file tree 4 files changed +20
-39
lines changed Original file line number Diff line number Diff line change 4
4
5
5
> 2023-06-11
6
6
7
+ ### Fixed
8
+
9
+ - Change the backend token calculation to asynchronous, otherwise it will cause inaccurate calculations.
10
+
7
11
### Add
8
12
9
13
- Added backend capability to calculate token consumption
Original file line number Diff line number Diff line change 4
4
5
5
> 2023-06-11
6
6
7
+ ### Fixed
8
+
9
+ - 后端计算 token 修改为异步,不然会导致计算不准确
10
+
7
11
### Add
8
12
9
13
- 新增后端计算 token 消耗的能力
Original file line number Diff line number Diff line change @@ -73,26 +73,20 @@ const stream = async (
73
73
const tokenInfo = new GPTTokens ( { model, messages : final } ) ;
74
74
const { usedTokens, usedUSD } = tokenInfo ;
75
75
76
- prisma . user
77
- . findUnique ( {
76
+ const findUser = await prisma . user . findUnique ( {
77
+ where : { id : userId } ,
78
+ } ) ;
79
+ if ( findUser ) {
80
+ const costTokens = findUser . costTokens + usedTokens ;
81
+ const costUSD = Number ( ( findUser . costUSD + usedUSD ) . toFixed ( 5 ) ) ;
82
+ await prisma . user . update ( {
78
83
where : { id : userId } ,
79
- } )
80
- . then ( ( findUser ) => {
81
- if ( ! findUser ) return ;
82
-
83
- const costTokens = findUser . costTokens + usedTokens ;
84
- const costUSD = Number ( ( findUser . costUSD + usedUSD ) . toFixed ( 5 ) ) ;
85
-
86
- prisma . user
87
- . update ( {
88
- where : { id : userId } ,
89
- data : {
90
- costTokens,
91
- costUSD,
92
- } ,
93
- } )
94
- . then ( ) ;
84
+ data : {
85
+ costTokens,
86
+ costUSD,
87
+ } ,
95
88
} ) ;
89
+ }
96
90
}
97
91
98
92
if ( buffer ) {
Original file line number Diff line number Diff line change @@ -95,27 +95,6 @@ const stream = async (
95
95
} ,
96
96
} ) ;
97
97
}
98
-
99
- // prisma.user
100
- // .findUnique({
101
- // where: { id: userId },
102
- // })
103
- // .then((findUser) => {
104
- // if (!findUser) return;
105
-
106
- // const costTokens = findUser.costTokens + usedTokens;
107
- // const costUSD = Number((findUser.costUSD + usedUSD).toFixed(5));
108
-
109
- // prisma.user
110
- // .update({
111
- // where: { id: userId },
112
- // data: {
113
- // costTokens,
114
- // costUSD,
115
- // },
116
- // })
117
- // .then();
118
- // });
119
98
}
120
99
121
100
if ( buffer ) {
You can’t perform that action at this time.
0 commit comments