Last active
December 29, 2022 03:40
-
-
Save davidkaneda/7412156a980b45241fb84fe6ca2019e2 to your computer and use it in GitHub Desktop.
Revisions
-
davidkaneda revised this gist
Dec 29, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ await AiSuggestion.aggregate([ { $group: { _id: "$user", -
davidkaneda created this gist
Dec 29, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ await this.aggregate([ { $group: { _id: "$user", models: { $push: { model: "$request.model", totalTokens: { $sum: "$response.usage.total_tokens" }, }, }, }, }, { $unwind: "$models", }, { $group: { _id: { user: "$_id", model: "$models.model" }, totalTokens: { $sum: "$models.totalTokens" }, choices: { $push: "$response.choices" }, }, }, { $group: { _id: "$_id.user", models: { $push: { model: "$_id.model", totalTokens: "$totalTokens", cost: { $multiply: [ "$totalTokens", { $cond: { if: { $eq: ["$_id.model", MODELS.davinci] }, then: COSTS[MODELS.davinci] / 1000, else: { $cond: { if: { $eq: ["$_id.model", MODELS.babbage] }, then: COSTS[MODELS.babbage] / 1000, else: { $cond: { if: { $eq: ["$_id.model", MODELS.curie] }, then: COSTS[MODELS.curie] / 1000, else: 0, }, }, }, }, }, }, ], }, wordCount: { $sum: { $map: { input: "$choices", as: "choice", in: { $size: { $split: ["$$choice.text", " "] } }, }, }, }, }, }, }, }, { $addFields: { totalCost: { $sum: "$models.cost" }, }, }, { $sort: { totalCost: -1, }, }, { $lookup: { from: "users", localField: "_id", foreignField: "_id", as: "user", }, }, { $unwind: "$user", }, { $addFields: { name: "$user.name", email: "$user.email", }, }, { $project: { user: 0, }, }, ]);