Skip to content

Commit

Permalink
(DOCSP-33419) [ANALYTICS] Operational Views (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlarew authored Oct 3, 2023
1 parent fbf2bbf commit aae8259
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 1 deletion.
2 changes: 2 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"createQualityTestsYaml": "npm run build && node ./build/createQualityTestsYaml.js",
"scrubMessages": "npm run build && node ./build/scrubMessages.js",
"analyzeMessages": "npm run build && node ./build/analyzeMessages.js",
"createView/messages_by_rating": "npm run build && node ./build/createView/messages_by_rating.js",
"createView/top_250_references": "npm run build && node ./build/createView/top_250_references.js",
"build": "npm run clean && tsc",
"postbuild": "cp ./src/MessageAnalysis.d.ts ./build/",
"clean": "rm -rf ./build",
Expand Down
90 changes: 90 additions & 0 deletions scripts/src/createView/messages_by_rating.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { MongoClient, ObjectId } from "mongodb";
import { assertEnvVars } from "chat-core";
import { Message } from "chat-server";

import "dotenv/config";

const { MONGODB_DATABASE_NAME, MONGODB_CONNECTION_URI } = assertEnvVars({
MONGODB_DATABASE_NAME: "",
MONGODB_CONNECTION_URI: "",
});

type Rating = true | false | null;

export type MessageByRating = {
_id: Rating;
rating: Rating;
count: number;
uniqueIps: string[];
conversationIds: ObjectId[];
messages: (Message & {
conversationId: ObjectId;
messageId: ObjectId;
index: number;
})[];
};

(async function main() {
const client = await MongoClient.connect(MONGODB_CONNECTION_URI);
try {
const db = client.db(MONGODB_DATABASE_NAME);
await db.createCollection<MessageByRating>("messages_by_rating", {
viewOn: "conversations",
pipeline: [
{
$match: {
messages: {
$elemMatch: {
role: "user",
},
},
},
},
{
$unwind: {
path: "$messages",
includeArrayIndex: "messageIndex",
},
},
{
$match: {
"messages.role": "assistant",
},
},
{
$group: {
_id: "$messages.rating",
count: {
$sum: 1,
},
uniqueIps: {
$addToSet: "$ipAddress",
},
conversationIds: {
$addToSet: "$_id",
},
messages: {
$push: {
$mergeObjects: [
"$messages",
{
conversationId: "$_id",
messageId: "$messages.id",
index: "$messageIndex",
},
],
},
},
},
},
{
$addFields: {
rating: "$_id",
},
},
],
});
} finally {
await client.close();
}
})();
101 changes: 101 additions & 0 deletions scripts/src/createView/top_250_references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { MongoClient, ObjectId } from "mongodb";
import { assertEnvVars } from "chat-core";
import { Message } from "chat-server";

import "dotenv/config";

const { MONGODB_DATABASE_NAME, MONGODB_CONNECTION_URI } = assertEnvVars({
MONGODB_DATABASE_NAME: "",
MONGODB_CONNECTION_URI: "",
});

const NUM_REFERENCES = 250;

export type TopReference = {
_id: string;
count: number;
conversationIds: ObjectId[];
messages: (Message & {
conversationId: ObjectId;
messageId: ObjectId;
index: number;
})[];
};

(async function main() {
const client = await MongoClient.connect(MONGODB_CONNECTION_URI);
try {
const db = client.db(MONGODB_DATABASE_NAME);
await db.createCollection<TopReference>(
`top_${NUM_REFERENCES}_references`,
{
viewOn: "conversations",
pipeline: [
{
$match: {
"messages.role": "user",
},
},
{
$unwind: {
path: "$messages",
includeArrayIndex: "messageIndex",
},
},
{
$match: {
"messages.role": "assistant",
"messages.references.0": {
$exists: true,
},
},
},
{
$addFields: {
references: "$messages.references",
},
},
{
$unwind: {
path: "$references",
includeArrayIndex: "referenceIndex",
},
},
{
$group: {
_id: "$references.title",
count: {
$sum: 1,
},
conversationIds: {
$addToSet: "$_id",
},
messages: {
$push: {
$mergeObjects: [
"$messages",
{
conversationId: "$_id",
messageId: "$messages.id",
index: "$messageIndex",
},
],
},
},
},
},
{
$sort: {
count: -1,
},
},
{
$limit: NUM_REFERENCES,
},
],
}
);
} finally {
await client.close();
}
})();
1 change: 0 additions & 1 deletion scripts/trigger.me

This file was deleted.

0 comments on commit aae8259

Please sign in to comment.