Skip to content

Commit

Permalink
add sentiment column
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Apr 24, 2024
1 parent a5d9b4c commit 4ca8dd7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/frontend/pages/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import {
costColumn,
durationColumn,
enrichmentColumn,
feedbackColumn,
inputColumn,
nameColumn,
Expand Down Expand Up @@ -59,13 +60,13 @@ import { useDebouncedState, useDidUpdate } from "@mantine/hooks"
import { ProjectContext } from "@/utils/context"
import { CheckLogic, deserializeLogic, serializeLogic } from "shared"
import { useRouter } from "next/router"
import useSWR from "swr"

const columns = {
llm: [
timeColumn("createdAt"),
nameColumn("Model"),
durationColumn(),
enrichmentColumn(),
userColumn(),
{
header: "Tokens",
Expand Down Expand Up @@ -103,6 +104,7 @@ const columns = {
const CHECKS_BY_TYPE = {
llm: [
"models",
// "enrichment",
"tags",
"users",
"status",
Expand Down
72 changes: 71 additions & 1 deletion packages/frontend/utils/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import AppUserAvatar from "@/components/blocks/AppUserAvatar"
import Feedback from "@/components/blocks/OldFeedback"
import ProtectedText from "@/components/blocks/ProtectedText"
import SmartViewer from "@/components/SmartViewer"
import { Badge, Group } from "@mantine/core"
import { Badge, Group, Tooltip } from "@mantine/core"
import { createColumnHelper } from "@tanstack/react-table"
import { useEffect } from "react"
import analytics from "./analytics"

import { formatCost, formatDateTime, msToTime } from "./format"
import { useProjectSWR } from "./dataHooks"
import {
IconMoodNeutral,
IconMoodSad,
IconMoodSmile,
} from "@tabler/icons-react"
const columnHelper = createColumnHelper<any>()

export function timeColumn(timeColumn, label = "Time") {
Expand Down Expand Up @@ -214,3 +219,68 @@ export function feedbackColumn(withRelatedRuns = false) {
cell,
})
}

const enrichRenderer = (data) => {
switch (data.id) {
case "sentiment":
let emoji
let type

if (data.value > 0.5) {
emoji = <IconMoodSmile color="teal" />
type = "positive"
} else if (data.value < -0.5) {
emoji = <IconMoodSad color="crimson" />
type = "negative"
} else {
emoji = <IconMoodNeutral color="gray" />
type = "neutral"
}

return {
element: (
<Group gap="xs">
{emoji} {data.value}
</Group>
),
help: "Sentiment: " + type,
}
case "pii":
return { element: data.value ? "Yes" : "No" }
default:
return { element: data.value, help: data.id }
}
}

// return a value between 0 and 1, static for a given seed runID
function valueBetween0and1forRunID(runID: string) {
const seed = parseInt(runID, 16)
return (Math.sin(seed) + 1) / 2
}

export function enrichmentColumn() {
return columnHelper.accessor("enrichment", {
header: "Enrichment",
size: 100,
cell: (props) => {
// for testing, random value between -1 and 1
const runID = props.row.original.id

const enrichedData = [
{
id: "sentiment",
value: (valueBetween0and1forRunID(runID) * 2 - 1).toFixed(2),
},
]

return enrichedData.map((data) => {
const { element, help } = enrichRenderer(data)
return (
<Tooltip key={data.id} label={help}>
<div key={data.id}>{element}</div>
</Tooltip>
)
})
},
})
}

0 comments on commit 4ca8dd7

Please sign in to comment.