File tree Expand file tree Collapse file tree 4 files changed +34
-4
lines changed
migrations/20250103193523_token_stats_multiple_types Expand file tree Collapse file tree 4 files changed +34
-4
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Warnings:
3
+
4
+ - Added the required column `completionTokens` to the `token_stats` table without a default value. This is not possible if the table is not empty.
5
+ - Added the required column `promptTokens` to the `token_stats` table without a default value. This is not possible if the table is not empty.
6
+
7
+ */
8
+ -- AlterTable
9
+ ALTER TABLE " token_stats" ADD COLUMN " completionTokens" INTEGER NOT NULL ,
10
+ ADD COLUMN " promptTokens" INTEGER NOT NULL ;
Original file line number Diff line number Diff line change @@ -67,6 +67,8 @@ model TokenStat {
67
67
id String @id @default (cuid () )
68
68
userId String
69
69
messageIds String []
70
+ promptTokens Int
71
+ completionTokens Int
70
72
totalTokens Int
71
73
createdAt DateTime @default (now () )
72
74
updatedAt DateTime @updatedAt
Original file line number Diff line number Diff line change @@ -165,13 +165,25 @@ export async function POST(req: Request) {
165
165
} ) ;
166
166
167
167
// Save the token stats
168
- if ( messages && newUserMessage && ! isNaN ( usage . totalTokens ) ) {
168
+ if (
169
+ messages &&
170
+ newUserMessage &&
171
+ ! isNaN ( usage . promptTokens ) &&
172
+ ! isNaN ( usage . completionTokens ) &&
173
+ ! isNaN ( usage . totalTokens )
174
+ ) {
169
175
const messageIds = newUserMessage
170
176
. concat ( messages )
171
177
. map ( ( message ) => message . id ) ;
172
- const totalTokens = usage . totalTokens ;
173
-
174
- await dbCreateTokenStat ( { userId, messageIds, totalTokens } ) ;
178
+ const { promptTokens, completionTokens, totalTokens } = usage ;
179
+
180
+ await dbCreateTokenStat ( {
181
+ userId,
182
+ messageIds,
183
+ promptTokens,
184
+ completionTokens,
185
+ totalTokens,
186
+ } ) ;
175
187
}
176
188
177
189
revalidatePath ( '/api/conversations' ) ;
Original file line number Diff line number Diff line change @@ -167,17 +167,23 @@ export async function dbCreateTokenStat({
167
167
userId,
168
168
messageIds,
169
169
totalTokens,
170
+ promptTokens,
171
+ completionTokens,
170
172
} : {
171
173
userId : string ;
172
174
messageIds : string [ ] ;
173
175
totalTokens : number ;
176
+ promptTokens : number ;
177
+ completionTokens : number ;
174
178
} ) {
175
179
try {
176
180
return await prisma . tokenStat . create ( {
177
181
data : {
178
182
userId,
179
183
messageIds,
180
184
totalTokens,
185
+ promptTokens,
186
+ completionTokens,
181
187
} ,
182
188
} ) ;
183
189
} catch ( error ) {
You can’t perform that action at this time.
0 commit comments