Skip to content

Instantly share code, notes, and snippets.

@davidkaneda
Last active December 29, 2022 03:40
Show Gist options
  • Save davidkaneda/7412156a980b45241fb84fe6ca2019e2 to your computer and use it in GitHub Desktop.
Save davidkaneda/7412156a980b45241fb84fe6ca2019e2 to your computer and use it in GitHub Desktop.

Revisions

  1. davidkaneda revised this gist Dec 29, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion users-by-cost.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    await this.aggregate([
    await AiSuggestion.aggregate([
    {
    $group: {
    _id: "$user",
  2. davidkaneda created this gist Dec 29, 2022.
    99 changes: 99 additions & 0 deletions users-by-cost.ts
    Original 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,
    },
    },
    ]);