Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix: increase decimal places for exports and unify across UI (#889)

Max Deichmann33dc18356a96
diff --git a/src/components/trace/ObservationPreview.tsx b/src/components/trace/ObservationPreview.tsx
index 3ff2adb8b3..adaeb45d4c 100644
--- a/src/components/trace/ObservationPreview.tsx
+++ b/src/components/trace/ObservationPreview.tsx
@@ -23,6 +23,7 @@ import { api } from "@/src/utils/api";
import { IOPreview } from "@/src/components/trace/IOPreview";
import { formatInterval } from "@/src/utils/dates";
import Link from "next/link";
+import { usdFormatter } from "@/src/utils/numbers";
export const ObservationPreview = (props: {
observations: Array<ObservationReturnType>;
@@ -98,7 +99,7 @@ export const ObservationPreview = (props: {
) : null}
{preloadedObservation.price ? (
<Badge variant="outline">
- {preloadedObservation.price.toString()} USD
+ {usdFormatter(preloadedObservation.price.toNumber())}
</Badge>
) : undefined}
diff --git a/src/features/dashboard/components/MetricTable.tsx b/src/features/dashboard/components/MetricTable.tsx
index 5a4875b98f..e1c39d7ec4 100644
--- a/src/features/dashboard/components/MetricTable.tsx
+++ b/src/features/dashboard/components/MetricTable.tsx
@@ -61,7 +61,7 @@ export const MetricTable = ({
</RightAlignedCell>,
<RightAlignedCell key={`${i}-cost`}>
{item.totalTokenCost
- ? usdFormatter(item.totalTokenCost as number)
+ ? usdFormatter(item.totalTokenCost as number, 2, 2)
: "$0"}
</RightAlignedCell>,
])
@@ -83,7 +83,7 @@ export const MetricTable = ({
collapse={{ collapsed: 5, expanded: 20 }}
>
<TotalMetric
- metric={totalTokens ? usdFormatter(totalTokens) : "$0"}
+ metric={totalTokens ? usdFormatter(totalTokens, 2, 2) : "$0"}
description="Total cost"
>
<DocPopup
diff --git a/src/features/dashboard/components/ModelUsageChart.tsx b/src/features/dashboard/components/ModelUsageChart.tsx
index 4499f275c1..9ef158ebb7 100644
--- a/src/features/dashboard/components/ModelUsageChart.tsx
+++ b/src/features/dashboard/components/ModelUsageChart.tsx
@@ -94,13 +94,19 @@ export const ModelUsageChart = ({
0,
);
+ // had to add this function as tremor under the hodd adds more variables
+ // to the function call which would break usdFormatter.
+ const oneValueUsdFormatter = (value: number) => {
+ return usdFormatter(value, 2, 2);
+ };
+
const data = [
{
tabTitle: "Total cost",
data: transformedModelCost,
- totalMetric: totalCost ? usdFormatter(totalCost) : usdFormatter(0),
+ totalMetric: totalCost ? usdFormatter(totalCost, 2, 2) : usdFormatter(0),
metricDescription: `Token cost`,
- formatter: usdFormatter,
+ formatter: oneValueUsdFormatter,
},
{
tabTitle: "Total tokens",
@@ -115,7 +121,7 @@ export const ModelUsageChart = ({
return (
<DashboardCard
className={className}
- title={"Model Usage"}
+ title="Model Usage"
isLoading={tokens.isLoading}
>
<TabComponent
diff --git a/src/features/dashboard/components/UserChart.tsx b/src/features/dashboard/components/UserChart.tsx
index 4a4925cae9..ad2736f28e 100644
--- a/src/features/dashboard/components/UserChart.tsx
+++ b/src/features/dashboard/components/UserChart.tsx
@@ -114,15 +114,17 @@ export const UserChart = ({
const maxNumberOfEntries = { collapsed: 5, expanded: 20 } as const;
+ const localUsdFormatter = (value: number) => usdFormatter(value, 2, 2);
+
const data = [
{
tabTitle: "Token cost",
data: isExpanded
? transformedCost.slice(0, maxNumberOfEntries.expanded)
: transformedCost.slice(0, maxNumberOfEntries.collapsed),
- totalMetric: totalCost ? usdFormatter(totalCost) : usdFormatter(0),
+ totalMetric: totalCost ? usdFormatter(totalCost, 2, 2) : usdFormatter(0),
metricDescription: "Total cost",
- formatter: usdFormatter,
+ formatter: localUsdFormatter,
},
{
tabTitle: "Count of Traces",
diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx
index d0fbe0c3e1..2c5c14afdc 100644
--- a/src/pages/_app.tsx
+++ b/src/pages/_app.tsx
@@ -29,6 +29,8 @@ import "core-js/features/array/to-sorted";
// Other CSS
import "react18-json-view/src/style.css";
import { DetailPageListsProvider } from "@/src/features/navigate-detail-pages/context";
+import Decimal from "decimal.js";
+import SuperJSON from "superjson";
const setProjectInPosthog = () => {
// project
const url = window.location.href;
@@ -41,6 +43,17 @@ const setProjectInPosthog = () => {
}
};
+// https://github.com/blitz-js/superjson?tab=readme-ov-file#recipes
+// support for decimal.js in superjson
+SuperJSON.registerCustom<Decimal, string>(
+ {
+ isApplicable: (v): v is Decimal => Decimal.isDecimal(v),
+ serialize: (v) => v.toJSON(),
+ deserialize: (v) => new Decimal(v),
+ },
+ "decimal.js",
+);
+
// Check that PostHog is client-side (used to handle Next.js SSR) and that env vars are set
if (
typeof window !== "undefined" &&
diff --git a/src/pages/project/[projectId]/index.tsx b/src/pages/project/[projectId]/index.tsx
index 4508dc848c..e4fd30f9b2 100644
--- a/src/pages/project/[projectId]/index.tsx
+++ b/src/pages/project/[projectId]/index.tsx
@@ -105,6 +105,7 @@ export default function Start() {
projectId={projectId}
globalFilterState={globalFilterState}
/>
+
<MetricTable
className="col-span-1 xl:col-span-2"
projectId={projectId}
diff --git a/src/server/api/routers/generations.ts b/src/server/api/routers/generations.ts
index 2722c32e18..874f29af7a 100644
--- a/src/server/api/routers/generations.ts
+++ b/src/server/api/routers/generations.ts
@@ -253,7 +253,7 @@ export const generationsRouter = createTRPCRouter({
generation.startTime.toISOString(),
generation.endTime?.toISOString() ?? "",
generation.cost
- ? usdFormatter(generation.cost.toNumber())
+ ? usdFormatter(generation.cost.toNumber(), 2, 4)
: "",
JSON.stringify(generation.input),
JSON.stringify(generation.output),
diff --git a/src/server/api/routers/observations.ts b/src/server/api/routers/observations.ts
index 340a0d0d12..00176bf5a7 100644
--- a/src/server/api/routers/observations.ts
+++ b/src/server/api/routers/observations.ts
@@ -20,7 +20,6 @@ export const observationsRouter = createTRPCRouter({
traceId: input.traceId,
},
});
-
const scores = generation.traceId
? await ctx.prisma.score.findMany({
where: {
diff --git a/src/utils/numbers.ts b/src/utils/numbers.ts
index 5a82d24ec1..4795cf95cb 100644
--- a/src/utils/numbers.ts
+++ b/src/utils/numbers.ts
@@ -14,12 +14,18 @@ export const numberFormatter = (number: number) => {
}).format(number);
};
-export const usdFormatter = (number: number) =>
- new Intl.NumberFormat("en-US", {
+export const usdFormatter = (
+ number: number,
+ minimumFractionDigits: number = 2,
+ maximumFractionDigits: number = 4,
+) => {
+ return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
- // These options are needed to round to whole numbers if that's what you want.
- //minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
- //maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits
+ minimumFractionDigits,
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#maximumfractiondigits
+ maximumFractionDigits,
}).format(number);
+};