Skip to content

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

perf: do not load all models by default in the dashboards (#5387)

Max Deichmann727797e0ec71
diff --git a/packages/shared/scripts/prepareClickhouse.ts b/packages/shared/scripts/prepareClickhouse.ts
index 47ad5e09ba..d23d4af8a4 100644
--- a/packages/shared/scripts/prepareClickhouse.ts
+++ b/packages/shared/scripts/prepareClickhouse.ts
@@ -96,18 +96,14 @@ export const prepareClickhouse = async (
'version' AS version,
repeat('input', toInt64(randExponential(1 / 100))) AS input,
repeat('output', toInt64(randExponential(1 / 100))) AS output,
- if("type" = 'GENERATION',
- case
- when number % 2 = 0 then 'claude-3-haiku-20240307'
- else 'gpt-4'
- end,
- NULL) as provided_model_name,
- if("type" = 'GENERATION',
- case
- when number % 2 = 0 then 'cltr0w45b000008k1407o9qv1'
- else 'clrntkjgy000f08jx79v9g1xj'
- end,
- NULL) as internal_model_id,
+ case
+ when number % 2 = 0 then 'clause-3-haiku-20230407'
+ else 'gpt-4'
+ end as provided_model_name,
+ case
+ when number % 2 = 0 then 'cltra4wbs0000k1407g0ya3'
+ else '1cmtk9y0000y3y79x9jgxj'
+ end as internal_model_id,
if("type" = 'GENERATION',
'{"temperature": 0.7, "max_tokens": 150}',
'{}') AS model_parameters,
diff --git a/packages/shared/src/server/repositories/dashboards.ts b/packages/shared/src/server/repositories/dashboards.ts
index 5cc36c141f..509c8918ff 100644
--- a/packages/shared/src/server/repositories/dashboards.ts
+++ b/packages/shared/src/server/repositories/dashboards.ts
@@ -71,6 +71,7 @@ export const getObservationsCostGroupedByName = async (
AND ${appliedFilter.query}
GROUP BY provided_model_name
ORDER BY sumMap(cost_details)['total'] DESC
+ LIMIT 50
`;
const result = await queryClickhouse<{
@@ -280,12 +281,15 @@ export const getDistinctModels = async (
// No need for final as duplicates are caught by distinct anyway.
const query = `
- SELECT distinct(provided_model_name) as model
+ SELECT distinct(provided_model_name) as model, count(*) as count
FROM observations o
${tracesFilter ? "LEFT JOIN traces t ON o.trace_id = t.id AND o.project_id = t.project_id" : ""}
WHERE project_id = {projectId: String}
AND ${appliedFilter.query}
${timeFilter ? `AND t.timestamp >= {traceTimestamp: DateTime64(3)} - ${OBSERVATIONS_TO_TRACE_INTERVAL}` : ""}
+ GROUP BY provided_model_name
+ ORDER BY count(*) DESC
+ LIMIT 1000
`;
const result = await queryClickhouse<{ model: string }>({
diff --git a/web/src/features/dashboard/components/LatencyChart.tsx b/web/src/features/dashboard/components/LatencyChart.tsx
index a88673ed8d..227ea498c7 100644
--- a/web/src/features/dashboard/components/LatencyChart.tsx
+++ b/web/src/features/dashboard/components/LatencyChart.tsx
@@ -1,7 +1,6 @@
import { api } from "@/src/utils/api";
import { type FilterState } from "@langfuse/shared";
import {
- getAllModels,
extractTimeSeriesData,
fillMissingValuesAndTransform,
isEmptyTimeSeries,
@@ -15,6 +14,10 @@ import {
type DashboardDateRangeAggregationOption,
} from "@/src/utils/date-range-utils";
import { NoDataOrLoading } from "@/src/components/NoDataOrLoading";
+import {
+ ModelSelectorPopover,
+ useModelSelection,
+} from "@/src/features/dashboard/components/ModelSelector";
export const GenerationLatencyChart = ({
className,
@@ -27,6 +30,15 @@ export const GenerationLatencyChart = ({
globalFilterState: FilterState;
agg: DashboardDateRangeAggregationOption;
}) => {
+ const {
+ allModels,
+ selectedModels,
+ setSelectedModels,
+ isAllSelected,
+ buttonText,
+ handleSelectAll,
+ } = useModelSelection(projectId, globalFilterState);
+
const latencies = api.dashboard.chart.useQuery(
{
projectId,
@@ -47,6 +59,12 @@ export const GenerationLatencyChart = ({
operator: "=",
value: "GENERATION",
},
+ {
+ type: "stringOptions",
+ column: "model",
+ operator: "any of",
+ value: selectedModels,
+ } as const,
],
groupBy: [
{
@@ -59,6 +77,7 @@ export const GenerationLatencyChart = ({
queryName: "model-latencies-over-time",
},
{
+ enabled: selectedModels.length > 0 && allModels.length > 0,
trpc: {
context: {
skipBatch: true,
@@ -67,10 +86,8 @@ export const GenerationLatencyChart = ({
},
);
- const allModels = getAllModels(projectId, globalFilterState);
-
const getData = (valueColumn: string) => {
- return latencies.data && allModels.length > 0
+ return latencies.data && selectedModels.length > 0
? fillMissingValuesAndTransform(
extractTimeSeriesData(latencies.data, "startTime", [
{
@@ -78,7 +95,7 @@ export const GenerationLatencyChart = ({
valueColumn: valueColumn,
},
]),
- allModels,
+ selectedModels,
)
: [];
};
@@ -111,7 +128,19 @@ export const GenerationLatencyChart = ({
className={className}
title="Model latencies"
description="Latencies (seconds) per LLM generation"
- isLoading={latencies.isLoading}
+ isLoading={latencies.isLoading && selectedModels.length > 0}
+ headerRight={
+ <div className="flex items-center justify-end">
+ <ModelSelectorPopover
+ allModels={allModels}
+ selectedModels={selectedModels}
+ setSelectedModels={setSelectedModels}
+ buttonText={buttonText}
+ isAllSelected={isAllSelected}
+ handleSelectAll={handleSelectAll}
+ />
+ </div>
+ }
>
<TabComponent
tabs={data.map((item) => {
diff --git a/web/src/features/dashboard/components/ModelSelector.tsx b/web/src/features/dashboard/components/ModelSelector.tsx
new file mode 100644
index 0000000000..3ceb66e576
--- /dev/null
+++ b/web/src/features/dashboard/components/ModelSelector.tsx
@@ -0,0 +1,138 @@
+import { Button } from "@/src/components/ui/button";
+import {
+ InputCommand,
+ InputCommandEmpty,
+ InputCommandGroup,
+ InputCommandInput,
+ InputCommandItem,
+ InputCommandList,
+ InputCommandSeparator,
+} from "@/src/components/ui/input-command";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/src/components/ui/popover";
+import { getAllModels } from "@/src/features/dashboard/components/hooks";
+import { cn } from "@/src/utils/tailwind";
+import { type FilterState } from "@langfuse/shared";
+import { Check, ChevronsUpDown } from "lucide-react";
+import { useEffect, useState } from "react";
+
+export const ModelSelectorPopover = ({
+ allModels,
+ selectedModels,
+ setSelectedModels,
+ buttonText,
+ isAllSelected,
+ handleSelectAll,
+}: {
+ allModels: { model: string }[];
+ selectedModels: string[];
+ setSelectedModels: React.Dispatch<React.SetStateAction<string[]>>;
+ buttonText: string;
+ isAllSelected: boolean;
+ handleSelectAll: () => void;
+}) => {
+ const [open, setOpen] = useState(false);
+
+ return (
+ <Popover open={open} onOpenChange={setOpen}>
+ <PopoverTrigger asChild>
+ <Button
+ variant="outline"
+ role="combobox"
+ aria-expanded={open}
+ className="w-56 justify-between"
+ >
+ {buttonText}
+ <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent className="w-56 p-0">
+ <InputCommand>
+ <InputCommandInput placeholder="Search models..." />
+ <InputCommandEmpty>No model found.</InputCommandEmpty>
+ <InputCommandGroup>
+ <InputCommandItem onSelect={handleSelectAll}>
+ <Check
+ className={cn(
+ "mr-2 h-4 w-4",
+ isAllSelected ? "opacity-100" : "opacity-0",
+ )}
+ />
+ <span>
+ <p className="font-semibold">Select All</p>
+ </span>
+ </InputCommandItem>
+ <InputCommandSeparator className="my-1" />
+ <InputCommandList>
+ {allModels.map((model) => (
+ <InputCommandItem
+ key={model.model}
+ onSelect={() => {
+ setSelectedModels((prev) =>
+ prev.includes(model.model)
+ ? prev.filter((m) => m !== model.model)
+ : [...prev, model.model],
+ );
+ }}
+ >
+ <Check
+ className={cn(
+ "mr-2 h-4 w-4",
+ selectedModels.includes(model.model)
+ ? "opacity-100"
+ : "opacity-0",
+ )}
+ />
+ {!model.model || model.model === "" ? (
+ <i>none</i>
+ ) : (
+ model.model
+ )}
+ </InputCommandItem>
+ ))}
+ </InputCommandList>
+ </InputCommandGroup>
+ </InputCommand>
+ </PopoverContent>
+ </Popover>
+ );
+};
+
+export const useModelSelection = (
+ projectId: string,
+ globalFilterState: FilterState,
+) => {
+ const allModels = getAllModels(projectId, globalFilterState);
+
+ const [selectedModels, setSelectedModels] = useState<string[]>([]);
+ const [firstAllModelUpdate, setFirstAllModelUpdate] = useState(true);
+
+ const isAllSelected = selectedModels.length === allModels.length;
+
+ const buttonText = isAllSelected
+ ? "All models"
+ : `${selectedModels.length} selected`;
+
+ const handleSelectAll = () => {
+ setSelectedModels(isAllSelected ? [] : [...allModels.map((m) => m.model)]);
+ };
+
+ useEffect(() => {
+ if (firstAllModelUpdate && allModels.length > 0) {
+ setSelectedModels(allModels.slice(0, 10).map((model) => model.model));
+ setFirstAllModelUpdate(false);
+ }
+ }, [allModels, firstAllModelUpdate]);
+
+ return {
+ allModels,
+ selectedModels,
+ setSelectedModels,
+ isAllSelected,
+ buttonText,
+ handleSelectAll,
+ };
+};
diff --git a/web/src/features/dashboard/components/ModelUsageChart.tsx b/web/src/features/dashboard/components/ModelUsageChart.tsx
index 0259a97699..1c820d1504 100644
--- a/web/src/features/dashboard/components/ModelUsageChart.tsx
+++ b/web/src/features/dashboard/components/ModelUsageChart.tsx
@@ -1,29 +1,10 @@
-import { Check, ChevronsUpDown } from "lucide-react";
-import { useEffect, useState } from "react";
-
import { NoDataOrLoading } from "@/src/components/NoDataOrLoading";
-import { Button } from "@/src/components/ui/button";
-import {
- InputCommand,
- InputCommandEmpty,
- InputCommandGroup,
- InputCommandInput,
- InputCommandItem,
- InputCommandList,
- InputCommandSeparator,
-} from "@/src/components/ui/input-command";
-import {
- Popover,
- PopoverContent,
- PopoverTrigger,
-} from "@/src/components/ui/popover";
import { env } from "@/src/env.mjs";
import { BaseTimeSeriesChart } from "@/src/features/dashboard/components/BaseTimeSeriesChart";
import { DashboardCard } from "@/src/features/dashboard/components/cards/DashboardCard";
import {
extractTimeSeriesData,
fillMissingValuesAndTransform,
- getAllModels,
isEmptyTimeSeries,
} from "@/src/features/dashboard/components/hooks";
import { TabComponent } from "@/src/features/dashboard/components/TabsComponent";
@@ -35,8 +16,11 @@ import {
dashboardDateRangeAggregationSettings,
} from "@/src/utils/date-range-utils";
import { compactNumberFormatter } from "@/src/utils/numbers";
-import { cn } from "@/src/utils/tailwind";
import { type FilterState } from "@langfuse/shared";
+import {
+ ModelSelectorPopover,
+ useModelSelection,
+} from "@/src/features/dashboard/components/ModelSelector";
type ModelUsageReturnType = {
startTime: string;
@@ -56,20 +40,14 @@ export const ModelUsageChart = ({
globalFilterState: FilterState;
agg: DashboardDateRangeAggregationOption;
}) => {
- const allModels = getAllModels(projectId, globalFilterState);
-
- const [selectedModels, setSelectedModels] = useState<string[]>([]);
- const [open, setOpen] = useState(false);
- const [firstAllModelUpdate, setFirstAllModelUpdate] = useState(true);
-
- const isAllSelected = selectedModels.length === allModels.length;
- const buttonText = isAllSelected
- ? "All models"
- : `${selectedModels.length} selected`;
-
- const handleSelectAll = () => {
- setSelectedModels(isAllSelected ? [] : [...allModels]);
- };
+ const {
+ allModels,
+ selectedModels,
+ setSelectedModels,
+ isAllSelected,
+ buttonText,
+ handleSelectAll,
+ } = useModelSelection(projectId, globalFilterState);
const queryResult = api.dashboard.chart.useQuery(
{
@@ -85,16 +63,12 @@ export const ModelUsageChart = ({
filter: [
...globalFilterState,
{ type: "string", column: "type", operator: "=", value: "GENERATION" },
- ...(!isAllSelected
- ? [
- {
- type: "stringOptions",
- column: "model",
- operator: "any of",
- value: selectedModels,
- } as const,
- ]
- : []),
+ {
+ type: "stringOptions",
+ column: "model",
+ operator: "any of",
+ value: selectedModels,
+ } as const,
],
groupBy: [
{
@@ -113,7 +87,7 @@ export const ModelUsageChart = ({
queryName: "observations-usage-timeseries",
},
{
- enabled: selectedModels.length > 0,
+ enabled: selectedModels.length > 0 && allModels.length > 0,
trpc: {
context: {
skipBatch: true,
@@ -122,13 +96,6 @@ export const ModelUsageChart = ({
},
);
- useEffect(() => {
- if (firstAllModelUpdate && allModels.length > 0) {
- setSelectedModels(allModels);
- setFirstAllModelUpdate(false);
- }
- }, [allModels, firstAllModelUpdate]);
-
const typedData = (queryResult.data as ModelUsageReturnType[]) ?? [];
const allUsageUnits = [
@@ -148,7 +115,7 @@ export const ModelUsageChart = ({
>();
dates?.forEach((d) => {
- allModels.forEach((m) => {
+ selectedModels.forEach((m) => {
allUsageUnits.forEach((uu) => {
const existingEntry = typedData.find(
(td) =>
@@ -247,6 +214,7 @@ export const ModelUsageChart = ({
currentModels,
)
: [];
+ console.log("costByModel", costByModel.length);
const totalCost = usageData?.reduce(
(acc, curr) =>
@@ -312,63 +280,14 @@ export const ModelUsageChart = ({
isLoading={queryResult.isLoading && selectedModels.length > 0}
headerRight={
<div className="flex items-center justify-end">
- <Popover open={open} onOpenChange={setOpen}>
- <PopoverTrigger asChild>
- <Button
- variant="outline"
- role="combobox"
- aria-expanded={open}
- className="w-56 justify-between"
- >
- {buttonText}
- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
- </Button>
- </PopoverTrigger>
- <PopoverContent className="w-56 p-0">
- <InputCommand>
- <InputCommandInput placeholder="Search models..." />
- <InputCommandEmpty>No model found.</InputCommandEmpty>
- <InputCommandGroup>
- <InputCommandItem onSelect={handleSelectAll}>
- <Check
- className={cn(
- "mr-2 h-4 w-4",
- isAllSelected ? "opacity-100" : "opacity-0",
- )}
- />
- <span>
- <p className="font-semibold">Select All</p>
- </span>
- </InputCommandItem>
- <InputCommandSeparator className="my-1" />
- <InputCommandList>
- {allModels.map((model) => (
- <InputCommandItem
- key={model}
- onSelect={() => {
- setSelectedModels((prev) =>
- prev.includes(model)
- ? prev.filter((m) => m !== model)
- : [...prev, model],
- );
- }}
- >
- <Check
- className={cn(
- "mr-2 h-4 w-4",
- selectedModels.includes(model)
- ? "opacity-100"
- : "opacity-0",
- )}
- />
- {!model || model === "" ? <i>none</i> : model}
- </InputCommandItem>
- ))}
- </InputCommandList>
- </InputCommandGroup>
- </InputCommand>
- </PopoverContent>
- </Popover>
+ <ModelSelectorPopover
+ allModels={allModels}
+ selectedModels={selectedModels}
+ setSelectedModels={setSelectedModels}
+ buttonText={buttonText}
+ isAllSelected={isAllSelected}
+ handleSelectAll={handleSelectAll}
+ />
</div>
}
>
diff --git a/web/src/features/dashboard/components/hooks.ts b/web/src/features/dashboard/components/hooks.ts
index 2f93a74f60..c1d24b41c9 100644
--- a/web/src/features/dashboard/components/hooks.ts
+++ b/web/src/features/dashboard/components/hooks.ts
@@ -36,10 +36,15 @@ export const getAllModels = (
return allModels.data ? extractAllModels(allModels.data) : [];
};
-const extractAllModels = (data: DatabaseRow[]): string[] => {
+const extractAllModels = (
+ data: DatabaseRow[],
+): { model: string; count: number }[] => {
return data
.filter((item) => item.model !== null)
- .map((item) => item.model as string);
+ .map((item) => ({
+ model: item.model as string,
+ count: item.count as number,
+ }));
};
type ChartData = {