Skip to content

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

feat(evals): preview number of historic items to be evaluated (#6116)

marliessophie174d96c1003d
diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.ts
index 5f1bd106a7..d2132509e7 100644
--- a/packages/shared/src/server/index.ts
+++ b/packages/shared/src/server/index.ts
@@ -49,6 +49,7 @@ export * from "./queries";
export * from "./repositories";
export * from "./redis/evalExecutionQueue";
export * from "./services/sessions-ui-table-service";
+export * from "./services/datasets-ui-table-service";
// test utils
export * from "./test-utils";
diff --git a/packages/shared/src/server/services/datasets-ui-table-service.ts b/packages/shared/src/server/services/datasets-ui-table-service.ts
new file mode 100644
index 0000000000..4c9fc6a0c9
--- /dev/null
+++ b/packages/shared/src/server/services/datasets-ui-table-service.ts
@@ -0,0 +1,61 @@
+import { evalDatasetFormFilterCols } from "../../tableDefinitions/tracesTable";
+import { FilterState } from "../../types";
+import { tableColumnsToSqlFilterAndPrefix } from "../filterToPrisma";
+import { Prisma, prisma } from "../../db";
+
+type FetchDatasetItemsTableProps = {
+ select: "count";
+ projectId: string;
+ filter: FilterState;
+};
+
+const getDatasetItemsTableGeneric = async <T>(
+ props: FetchDatasetItemsTableProps,
+) => {
+ const { select, projectId, filter } = props;
+
+ let sqlSelect: Prisma.Sql;
+ switch (select) {
+ case "count":
+ sqlSelect = Prisma.sql`count(*) as count`;
+ break;
+ default:
+ // eslint-disable-next-line no-case-declarations, no-unused-vars
+ const exhaustiveCheckDefault: never = select;
+ throw new Error(`Unknown select type: ${select}`);
+ }
+
+ const datasetItemsFilter = tableColumnsToSqlFilterAndPrefix(
+ filter,
+ evalDatasetFormFilterCols,
+ "dataset_items",
+ );
+
+ const query = Prisma.sql`
+ SELECT
+ ${sqlSelect}
+ FROM dataset_items di
+ WHERE
+ di.project_id = ${projectId}
+ ${datasetItemsFilter}
+ `;
+
+ const res = await prisma.$queryRaw<T>(query);
+
+ return res;
+};
+
+export const getDatasetItemsTableCount = async (props: {
+ projectId: string;
+ filter: FilterState;
+}) => {
+ const res = await getDatasetItemsTableGeneric<Array<{ count: bigint }>>({
+ select: "count",
+ projectId: props.projectId,
+ filter: props.filter,
+ });
+
+ const totalCount = res.length > 0 ? Number(res[0].count) : 0;
+
+ return { totalCount };
+};
diff --git a/web/src/__tests__/async/datasets-ui-table.servertest.ts b/web/src/__tests__/async/datasets-ui-table.servertest.ts
new file mode 100644
index 0000000000..a3cba40d5d
--- /dev/null
+++ b/web/src/__tests__/async/datasets-ui-table.servertest.ts
@@ -0,0 +1,64 @@
+import {
+ createOrgProjectAndApiKey,
+ getDatasetItemsTableCount,
+} from "@langfuse/shared/src/server";
+import { v4 as uuidv4 } from "uuid";
+import { prisma } from "@langfuse/shared/src/db";
+import { type FilterState } from "@langfuse/shared";
+
+const generateFilter = (datasetIds: string[]): FilterState => {
+ return [
+ {
+ column: "Dataset",
+ operator: "any of",
+ type: "stringOptions",
+ value: datasetIds,
+ },
+ ];
+};
+
+describe("trpc.datasets", () => {
+ let projectId: string;
+ let datasetIds: string[];
+
+ beforeAll(async () => {
+ const { projectId: newProjectId } = await createOrgProjectAndApiKey();
+ projectId = newProjectId;
+ datasetIds = [uuidv4(), uuidv4()];
+
+ await prisma.dataset.createMany({
+ data: datasetIds.map((datasetId, index) => ({
+ id: datasetId,
+ projectId: projectId,
+ name: `test-${index}`,
+ })),
+ });
+
+ await prisma.datasetItem.createMany({
+ data: datasetIds.map((datasetId) => ({
+ id: uuidv4(),
+ projectId: projectId,
+ datasetId: datasetId,
+ })),
+ });
+ });
+ describe("GET datasetItems.countAll", () => {
+ it("should GET all dataset items with no filter", async () => {
+ const { totalCount } = await getDatasetItemsTableCount({
+ projectId: projectId,
+ filter: [],
+ });
+
+ expect(totalCount).toBe(2);
+ });
+
+ it("should GET all dataset items with filter", async () => {
+ const { totalCount } = await getDatasetItemsTableCount({
+ projectId: projectId,
+ filter: generateFilter([datasetIds[0]]),
+ });
+
+ expect(totalCount).toBe(1);
+ });
+ });
+});
diff --git a/web/src/ee/features/evals/components/eval-form-descriptions.tsx b/web/src/ee/features/evals/components/eval-form-descriptions.tsx
new file mode 100644
index 0000000000..c8720454a9
--- /dev/null
+++ b/web/src/ee/features/evals/components/eval-form-descriptions.tsx
@@ -0,0 +1,38 @@
+import DocPopup from "@/src/components/layouts/doc-popup";
+import { Label } from "@/src/components/ui/label";
+
+export function VariableMappingDescription(p: {
+ title: string;
+ description: string;
+ href: string;
+}) {
+ return (
+ <div className="flex w-1/2 items-center">
+ <Label className="muted-foreground text-sm font-light">{p.title}</Label>
+ <DocPopup description={p.description} href={p.href} />
+ </div>
+ );
+}
+
+export function TimeScopeDescription(props: {
+ projectId: string;
+ timeScope: ("NEW" | "EXISTING")[] | undefined;
+ target: "trace" | "dataset_item" | undefined;
+}) {
+ if (!props.timeScope || props.timeScope.length === 0) {
+ return "Select a time scope to run this configuration on.";
+ }
+
+ return (
+ <div>
+ This configuration will run on{" "}
+ {props.timeScope?.includes("NEW") && props.timeScope?.includes("EXISTING")
+ ? "all future and existing"
+ : props.timeScope?.includes("NEW")
+ ? "all future"
+ : "all existing"}{" "}
+ {props.target === "trace" ? "traces" : "dataset items"} that match these
+ filters.{" "}
+ </div>
+ );
+}
diff --git a/web/src/ee/features/evals/components/evaluator-form.tsx b/web/src/ee/features/evals/components/evaluator-form.tsx
index 4814fd39b7..12e1b7f9a5 100644
--- a/web/src/ee/features/evals/components/evaluator-form.tsx
+++ b/web/src/ee/features/evals/components/evaluator-form.tsx
@@ -1,52 +1,8 @@
-import { useFieldArray, useForm } from "react-hook-form";
-import { Input } from "@/src/components/ui/input";
import { Button } from "@/src/components/ui/button";
-import {
- Form,
- FormControl,
- FormDescription,
- FormField,
- FormItem,
- FormLabel,
- FormMessage,
-} from "@/src/components/ui/form";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/src/components/ui/select";
-import { zodResolver } from "@hookform/resolvers/zod";
-import { Tabs, TabsList, TabsTrigger } from "@/src/components/ui/tabs";
-import {
- tracesTableColsWithOptions,
- evalTraceTableCols,
- evalDatasetFormFilterCols,
- singleFilter,
- type JobConfiguration,
- availableTraceEvalVariables,
- datasetFormFilterColsWithOptions,
- availableDatasetEvalVariables,
- type langfuseObjects,
- TimeScopeSchema,
-} from "@langfuse/shared";
-import * as z from "zod";
-import { useEffect, useMemo, useState } from "react";
+import { type JobConfiguration } from "@langfuse/shared";
+import { useEffect, useState } from "react";
import { api } from "@/src/utils/api";
-import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";
-import {
- type EvalTemplate,
- variableMapping,
- wipVariableMapping,
-} from "@langfuse/shared";
-import router from "next/router";
-import { Slider } from "@/src/components/ui/slider";
-import { Card } from "@/src/components/ui/card";
-import { JSONView } from "@/src/components/ui/CodeJsonViewer";
-import { Label } from "@/src/components/ui/label";
-import DocPopup from "@/src/components/layouts/doc-popup";
-import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { type EvalTemplate } from "@langfuse/shared";
import { CheckIcon, ChevronDown, ExternalLink } from "lucide-react";
import {
Popover,
@@ -66,31 +22,7 @@ import { cn } from "@/src/utils/tailwind";
import { Dialog, DialogContent, DialogTitle } from "@/src/components/ui/dialog";
import { EvalTemplateForm } from "@/src/ee/features/evals/components/template-form";
import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";
-import { Checkbox } from "@/src/components/ui/checkbox";
-import { compactNumberFormatter } from "@/src/utils/numbers";
-
-export const fieldHasJsonSelectorOption = (
- selectedColumnId: string | undefined | null,
-): boolean =>
- selectedColumnId === "input" ||
- selectedColumnId === "output" ||
- selectedColumnId === "expected_output";
-
-const formSchema = z.object({
- scoreName: z.string(),
- target: z.string(),
- filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
- mapping: z.array(wipVariableMapping),
- sampling: z.coerce.number().gt(0).lte(1),
- delay: z.coerce.number().optional().default(10),
- timeScope: TimeScopeSchema,
-});
-
-type LangfuseObject = (typeof langfuseObjects)[number];
-
-const isTraceTarget = (target: string): boolean => target === "trace";
-const isTraceOrDatasetObject = (object: LangfuseObject): boolean =>
- object === "trace" || object === "dataset_item";
+import { InnerEvaluatorForm } from "@/src/ee/features/evals/components/inner-evaluator-form";
export const EvaluatorForm = (props: {
projectId: string;
@@ -286,7 +218,7 @@ export const EvaluatorForm = (props: {
</DialogContent>
</Dialog>
{evalTemplate && currentTemplate ? (
- <InnerEvalConfigForm
+ <InnerEvaluatorForm
key={evalTemplate}
projectId={props.projectId}
disabled={props.disabled}
@@ -302,775 +234,3 @@ export const EvaluatorForm = (props: {
</>
);
};
-
-export const InnerEvalConfigForm = (props: {
- projectId: string;
- evalTemplate: EvalTemplate;
- disabled?: boolean;
- existingEvaluator?: JobConfiguration;
- onFormSuccess?: () => void;
- shouldWrapVariables?: boolean;
- mode?: "create" | "edit";
-}) => {
- const [formError, setFormError] = useState<string | null>(null);
- const capture = usePostHogClientCapture();
-
- const form = useForm<z.infer<typeof formSchema>>({
- resolver: zodResolver(formSchema),
- disabled: props.disabled,
- defaultValues: {
- scoreName:
- props.existingEvaluator?.scoreName ?? `${props.evalTemplate.name}`,
- target: props.existingEvaluator?.targetObject ?? "trace",
- filter: props.existingEvaluator?.filter
- ? z.array(singleFilter).parse(props.existingEvaluator.filter)
- : [],
- mapping: props.existingEvaluator?.variableMapping
- ? z
- .array(variableMapping)
- .parse(props.existingEvaluator.variableMapping)
- : z.array(variableMapping).parse(
- props.evalTemplate
- ? props.evalTemplate.vars.map((v) => ({
- templateVariable: v,
- langfuseObject: "trace" as const,
- selectedColumnId: "input",
- }))
- : [],
- ),
- sampling: props.existingEvaluator?.sampling
- ? props.existingEvaluator.sampling.toNumber()
- : 1,
- delay: props.existingEvaluator?.delay
- ? props.existingEvaluator.delay / 1000
- : 10,
- timeScope: (props.existingEvaluator?.timeScope ?? ["NEW"]).filter(
- (option): option is "NEW" | "EXISTING" =>
- ["NEW", "EXISTING"].includes(option),
- ),
- },
- });
-
- const traceFilterOptions = api.traces.filterOptions.useQuery(
- {
- projectId: props.projectId,
- },
- {
- trpc: {
- context: {
- skipBatch: true,
- },
- },
- refetchOnMount: false,
- refetchOnWindowFocus: false,
- refetchOnReconnect: false,
- staleTime: Infinity,
- },
- );
-
- const datasets = api.datasets.allDatasetMeta.useQuery(
- {
- projectId: props.projectId,
- },
- {
- trpc: {
- context: {
- skipBatch: true,
- },
- },
- refetchOnMount: false,
- refetchOnWindowFocus: false,
- refetchOnReconnect: false,
- staleTime: Infinity,
- },
- );
-
- const datasetFilterOptions = useMemo(() => {
- if (!datasets.data) return undefined;
- return {
- datasetId: datasets.data?.map((d) => ({
- value: d.id,
- displayValue: d.name,
- })),
- };
- }, [datasets.data]);
-
- useEffect(() => {
- if (props.evalTemplate && form.getValues("mapping").length === 0) {
- form.setValue(
- "mapping",
- props.evalTemplate.vars.map((v) => ({
- templateVariable: v,
- langfuseObject: "trace" as const,
- selectedColumnId: "input",
- })),
- );
- form.setValue("scoreName", `${props.evalTemplate.name}`);
- }
- }, [form, props.evalTemplate]);
-
- const { fields } = useFieldArray({
- control: form.control,
- name: "mapping",
- });
-
- const utils = api.useUtils();
- const createJobMutation = api.evals.createJob.useMutation({
- onSuccess: () => utils.models.invalidate(),
- onError: (error) => setFormError(error.message),
- });
- const updateJobMutation = api.evals.updateEvalJob.useMutation({
- onSuccess: () => utils.evals.invalidate(),
- onError: (error) => setFormError(error.message),
- });
- const [availableVariables, setAvailableVariables] = useState<
- typeof availableTraceEvalVariables | typeof availableDatasetEvalVariables
- >(
- isTraceTarget(props.existingEvaluator?.targetObject ?? "trace")
- ? availableTraceEvalVariables
- : availableDatasetEvalVariables,
- );
-
- function onSubmit(values: z.infer<typeof formSchema>) {
- capture(
- props.mode === "edit"
- ? "eval_config:update"
- : "eval_config:new_form_submit",
- );
-
- const validatedFilter = z.array(singleFilter).safeParse(values.filter);
-
- if (
- props.existingEvaluator?.timeScope.includes("EXISTING") &&
- props.mode === "edit" &&
- !values.timeScope.includes("EXISTING")
- ) {
- form.setError("timeScope", {
- type: "manual",
- message:
- "The evaluator ran on existing traces already. This cannot be changed anymore.",
- });
- return;
- }
- if (form.getValues("timeScope").length === 0) {
- form.setError("timeScope", {
- type: "manual",
- message: "Please select at least one.",
- });
- return;
- }
-
- if (validatedFilter.success === false) {
- form.setError("filter", {
- type: "manual",
- message: "Please fill out all filter fields",
- });
- return;
- }
-
- const validatedVarMapping = z
- .array(variableMapping)
- .safeParse(values.mapping);
-
- if (validatedVarMapping.success === false) {
- console.log(validatedVarMapping.error);
- form.setError("mapping", {
- type: "manual",
- message: "Please fill out all variable mappings",
- });
- return;
- }
-
- const delay = values.delay * 1000; // convert to ms
- const sampling = values.sampling;
- const mapping = validatedVarMapping.data;
- const filter = validatedFilter.data;
- const scoreName = values.scoreName;
-
- (props.mode === "edit" && props.existingEvaluator
- ? updateJobMutation.mutateAsync({
- projectId: props.projectId,
- evalConfigId: props.existingEvaluator.id,
- config: {
- delay,
- filter,
- variableMapping: mapping,
- sampling,
- scoreName,
- timeScope: values.timeScope,
- },
- })
- : createJobMutation.mutateAsync({
- projectId: props.projectId,
- target: values.target,
- evalTemplateId: props.evalTemplate.id,
- scoreName,
- filter,
- mapping,
- sampling,
- delay,
- timeScope: values.timeScope,
- })
- )
- .then(() => {
- form.reset();
- props.onFormSuccess?.();
-
- if (props.mode !== "edit") {
- void router.push(`/project/${props.projectId}/evals`);
- }
- })
- .catch((error) => {
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
- if ("message" in error && typeof error.message === "string") {
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
- setFormError(error.message as string);
- return;
- } else {
- setFormError(JSON.stringify(error));
- console.error(error);
- }
- });
- }
-
- return (
- <Form {...form}>
- <form
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
- onSubmit={form.handleSubmit(onSubmit)}
- className="flex w-full flex-col gap-4"
- >
- <div className="grid gap-4">
- <FormField
- control={form.control}
- name="scoreName"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Score Name</FormLabel>
- <FormControl>
- <Input {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <Card className="flex max-w-full flex-col gap-6 overflow-y-auto p-4">
- <FormField
- control={form.control}
- name="target"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Target object</FormLabel>
- <FormControl>
- <Tabs
- defaultValue="trace"
- value={field.value}
- onValueChange={(value) => {
- const isTrace = isTraceTarget(value);
- const langfuseObject: LangfuseObject = isTrace
- ? "trace"
- : "dataset_item";
- const newMapping = form
- .getValues("mapping")
- .map((field) => ({ ...field, langfuseObject }));
- form.setValue("mapping", newMapping);
- form.setValue("delay", isTrace ? 10 : 20);
- setAvailableVariables(
- isTrace
- ? availableTraceEvalVariables
- : availableDatasetEvalVariables,
- );
- field.onChange(value);
- }}
- >
- <TabsList>
- <TabsTrigger
- value="trace"
- disabled={props.disabled || props.mode === "edit"}
- >
- Trace
- </TabsTrigger>
- <TabsTrigger
- value="dataset"
- disabled={props.disabled || props.mode === "edit"}
- >
- Dataset
- </TabsTrigger>
- </TabsList>
- </Tabs>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
-
- <div className="flex min-w-[300px]">
- <FormField
- control={form.control}
- name="timeScope"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Evaluator runs on</FormLabel>
- <FormControl>
- <div className="flex flex-col gap-2">
- <div className="items-top flex space-x-2">
- <Checkbox
- id="newObjects"
- checked={field.value.includes("NEW")}
- onCheckedChange={(checked) => {
- const newValue = checked
- ? [...field.value, "NEW"]
- : field.value.filter((v) => v !== "NEW");
- field.onChange(newValue);
- }}
- disabled={props.disabled}
- />
- <div className="grid gap-1.5 leading-none">
- <label
- htmlFor="newObjects"
- className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
- >
- New{" "}
- {form.watch("target") === "trace"
- ? "traces"
- : "dataset items"}
- </label>
- </div>
- </div>
- <div className="items-top flex space-x-2">
- <Checkbox
- id="existingObjects"
- checked={field.value.includes("EXISTING")}
- onCheckedChange={(checked) => {
- const newValue = checked
- ? [...field.value, "EXISTING"]
- : field.value.filter((v) => v !== "EXISTING");
- field.onChange(newValue);
- }}
- disabled={
- props.disabled ||
- (props.mode === "edit" &&
- field.value.includes("EXISTING"))
- }
- />
- <div className="grid gap-1.5 leading-none">
- <label
- htmlFor="existingObjects"
- className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
- >
- Existing{" "}
- {form.watch("target") === "trace"
- ? "traces"
- : "dataset items"}
- </label>
- </div>
- </div>
- </div>
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
-
- <FormField
- control={form.control}
- name="filter"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Target filter</FormLabel>
- {isTraceTarget(form.watch("target")) ? (
- <>
- <FormControl>
- <InlineFilterBuilder
- columns={tracesTableColsWithOptions(
- traceFilterOptions.data,
- evalTraceTableCols,
- )}
- filterState={field.value ?? []}
- onChange={(value) => field.onChange(value)}
- disabled={props.disabled}
- />
- </FormControl>
- <FormDescription>
- <TimeScopeDescription
- projectId={props.projectId}
- timeScope={form.watch("timeScope")}
- target="trace"
- />
- </FormDescription>
- <FormMessage />
- </>
- ) : (
- <>
- <FormControl>
- <InlineFilterBuilder
- columns={datasetFormFilterColsWithOptions(
- datasetFilterOptions,
- evalDatasetFormFilterCols,
- )}
- filterState={field.value ?? []}
- onChange={(value) => field.onChange(value)}
- disabled={props.disabled}
- />
- </FormControl>
- <FormDescription>
- <TimeScopeDescription
- projectId={props.projectId}
- timeScope={form.watch("timeScope")}
- target="dataset_item"
- />
- </FormDescription>
- <FormMessage />
- </>
- )}
- </FormItem>
- )}
- />
- </Card>
- <Card className="p-4">
- <FormField
- control={form.control}
- name="mapping"
- render={() => (
- <>
- <FormLabel className="">Variable mapping</FormLabel>
- <FormControl>
- Here will some variable mapping be added.
- </FormControl>
- <div
- className={cn(
- "my-2 flex flex-col gap-2",
- !props.shouldWrapVariables && "lg:flex-row",
- )}
- >
- <JSONView
- title={"Eval Template"}
- json={props.evalTemplate.prompt ?? null}
- className={cn(
- "min-h-48",
- !props.shouldWrapVariables && "lg:w-2/3",
- )}
- codeClassName="flex-1"
- />
- <div
- className={cn(
- "flex flex-col gap-2",
- !props.shouldWrapVariables && "lg:w-1/3",
- )}
- >
- {fields.map((mappingField, index) => (
- <Card className="flex flex-col gap-2 p-4" key={index}>
- <div className="text-sm font-semibold">
- {"{{"}
- {mappingField.templateVariable}
- {"}}"}
- <DocPopup
- description={
- "Variable in the template to be replaced with the trace data."
- }
- href={
- "https://langfuse.com/docs/scores/model-based-evals"
- }
- />
- </div>
- <FormField
- control={form.control}
- key={`${mappingField.id}-langfuseObject`}
- name={`mapping.${index}.langfuseObject`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title="Object"
- description={
- "Langfuse object to retrieve the data from."
- }
- href={
- "https://langfuse.com/docs/scores/model-based-evals"
- }
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Select
- disabled={props.disabled}
- defaultValue={field.value}
- onValueChange={field.onChange}
- >
- <SelectTrigger>
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {availableVariables.map(
- (evalObject) => (
- <SelectItem
- value={evalObject.id}
- key={evalObject.id}
- >
- {evalObject.display}
- </SelectItem>
- ),
- )}
- </SelectContent>
- </Select>
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
- />
-
- {!isTraceOrDatasetObject(
- form.watch(`mapping.${index}.langfuseObject`),
- ) ? (
- <FormField
- control={form.control}
- key={`${mappingField.id}-objectName`}
- name={`mapping.${index}.objectName`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title={"Object Name"}
- description={
- "Name of the Langfuse object to retrieve the data from."
- }
- href={
- "https://langfuse.com/docs/scores/model-based-evals"
- }
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- disabled={props.disabled}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
- />
- ) : undefined}
-
- <FormField
- control={form.control}
- key={`${mappingField.id}-selectedColumnId`}
- name={`mapping.${index}.selectedColumnId`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title={"Object Variable"}
- description={
- "Variable on the Langfuse object to insert into the template."
- }
- href={
- "https://langfuse.com/docs/scores/model-based-evals"
- }
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Select
- disabled={props.disabled}
- defaultValue={field.value ?? undefined}
- onValueChange={(value) => {
- const availableColumns =
- availableVariables.find(
- (evalObject) =>
- evalObject.id ===
- form.watch(
- `mapping.${index}.langfuseObject`,
- ),
- )?.availableColumns;
-
- const column = availableColumns?.find(
- (column) => column.id === value,
- );
-
- field.onChange(column?.id);
- }}
- >
- <SelectTrigger>
- <SelectValue placeholder="Object type" />
- </SelectTrigger>
- <SelectContent>
- {availableVariables
- .find(
- (evalObject) =>
- evalObject.id ===
- form.watch(
- `mapping.${index}.langfuseObject`,
- ),
- )
- ?.availableColumns.map((column) => (
- <SelectItem
- value={column.id}
- key={column.id}
- >
- {column.name}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
- />
- {fieldHasJsonSelectorOption(
- form.watch(`mapping.${index}.selectedColumnId`),
- ) ? (
- <FormField
- control={form.control}
- key={`${mappingField.id}-jsonSelector`}
- name={`mapping.${index}.jsonSelector`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title={"JsonPath"}
- description={
- "Optional selection: Use JsonPath syntax to select from a JSON object stored on a trace. If not selected, we will pass the entire object into the prompt."
- }
- href={
- "https://langfuse.com/docs/scores/model-based-evals"
- }
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- disabled={props.disabled}
- placeholder="Optional"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
- />
- ) : undefined}
- </Card>
- ))}
- </div>
- </div>
- <FormDescription>
- Insert trace data into the prompt template.
- </FormDescription>
- <FormMessage />
- </>
- )}
- />
- </Card>
- <Card className="flex flex-col gap-6 p-4">
- <FormField
- control={form.control}
- name="sampling"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Sampling</FormLabel>
- <FormControl>
- <Slider
- disabled={props.disabled}
- min={0}
- max={1}
- step={0.01}
- value={[field.value]}
- onValueChange={(value) => field.onChange(value[0])}
- />
- </FormControl>
- <div className="flex flex-col">
- <FormDescription className="flex justify-between">
- <span>0%</span>
- <span>100%</span>
- </FormDescription>
- <FormDescription className="mt-1 flex flex-row gap-1">
- <span>Percentage of traces to evaluate.</span>
- <span>
- Currently set to {(field.value * 100).toFixed(0)}%.
- </span>
- </FormDescription>
- </div>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="delay"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Delay (seconds)</FormLabel>
- <FormControl>
- <Input {...field} type="number" />
- </FormControl>
- <FormDescription>
- Time between first Trace/Dataset run event and evaluation
- execution to ensure all data is available
- </FormDescription>
- <FormMessage />
- </FormItem>
- )}
- />
- </Card>
- </div>
-
- {!props.disabled ? (
- <Button
- type="submit"
- loading={createJobMutation.isLoading || updateJobMutation.isLoading}
- className="mt-3"
- >
- Save
- </Button>
- ) : null}
- </form>
- {formError ? (
- <p className="text-red text-center">
- <span className="font-bold">Error:</span> {formError}
- </p>
- ) : null}
- </Form>
- );
-};
-function VariableMappingDescription(p: {
- title: string;
- description: string;
- href: string;
-}) {
- return (
- <div className="flex w-1/2 items-center">
- <Label className="muted-foreground text-sm font-light">{p.title}</Label>
- <DocPopup description={p.description} href={p.href} />
- </div>
- );
-}
-
-export const TimeScopeDescription = (props: {
- projectId: string;
- timeScope: ("NEW" | "EXISTING")[] | undefined;
- target: "trace" | "dataset_item" | undefined;
-}) => {
- if (!props.timeScope || props.timeScope.length === 0) {
- return "Select a time scope to run this configuration on.";
- }
-
- const globalConfig = api.evals.globalJobConfigs.useQuery({
- projectId: props.projectId,
- });
- return (
- <div>
- This configuration will run on{" "}
- {props.timeScope?.includes("NEW") && props.timeScope?.includes("EXISTING")
- ? "all future and existing"
- : props.timeScope?.includes("NEW")
- ? "all future"
- : "all existing"}{" "}
- {props.target === "trace" ? "traces" : "dataset items"} that match these
- filters.{" "}
- {globalConfig.data && props.timeScope?.includes("EXISTING")
- ? `We execute the evaluation on up to ${compactNumberFormatter(globalConfig.data)} historic evaluations.`
- : null}
- </div>
- );
-};
diff --git a/web/src/ee/features/evals/components/execution-count-tooltip.tsx b/web/src/ee/features/evals/components/execution-count-tooltip.tsx
new file mode 100644
index 0000000000..231568ef3f
--- /dev/null
+++ b/web/src/ee/features/evals/components/execution-count-tooltip.tsx
@@ -0,0 +1,86 @@
+import { InfoIcon, Loader } from "lucide-react";
+import {
+ type EvalFormType,
+ isTraceTarget,
+} from "@/src/ee/features/evals/utils/evaluator-form-utils";
+import { api } from "@/src/utils/api";
+import { useState } from "react";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/src/components/ui/tooltip";
+import { compactNumberFormatter } from "@/src/utils/numbers";
+
+type ExecutionCountTooltipProps = {
+ projectId: string;
+ item: string;
+ filter: EvalFormType["filter"];
+};
+
+export const ExecutionCountTooltip = ({
+ projectId,
+ item,
+ filter,
+}: ExecutionCountTooltipProps) => {
+ const [isOpen, setIsOpen] = useState(false);
+
+ const globalConfig = api.evals.globalJobConfigs.useQuery({
+ projectId,
+ });
+
+ const baseAllCountFilter = {
+ projectId,
+ filter,
+ };
+
+ const tracesAllCountFilter = {
+ ...baseAllCountFilter,
+ searchQuery: null,
+ orderBy: null,
+ };
+
+ // utilize `isOpen` to only query if user hovers over tooltip to avoid unnecessary queries
+ const totalCountQuery = api.traces.countAll.useQuery(tracesAllCountFilter, {
+ enabled: isOpen && isTraceTarget(item),
+ });
+
+ const datasetCountQuery = api.datasets.countAllDatasetItems.useQuery(
+ baseAllCountFilter,
+ {
+ enabled: isOpen && !isTraceTarget(item),
+ },
+ );
+
+ const loading = isTraceTarget(item)
+ ? totalCountQuery.isLoading
+ : datasetCountQuery.isLoading;
+
+ const totalCount = isTraceTarget(item)
+ ? totalCountQuery.data?.totalCount
+ : datasetCountQuery.data?.totalCount;
+
+ return (
+ <Tooltip open={isOpen} onOpenChange={setIsOpen}>
+ <TooltipTrigger>
+ <InfoIcon className="h-4 w-4" />
+ </TooltipTrigger>
+ <TooltipContent>
+ <div className="text-sm">
+ We execute the evaluation on{" "}
+ {loading ? (
+ <Loader className="inline-block h-4 w-4 animate-spin" />
+ ) : (
+ compactNumberFormatter(
+ !globalConfig.data ||
+ (totalCount && totalCount < globalConfig.data)
+ ? totalCount
+ : globalConfig.data,
+ )
+ )}{" "}
+ {isTraceTarget(item) ? "traces" : "dataset items"}.
+ </div>
+ </TooltipContent>
+ </Tooltip>
+ );
+};
diff --git a/web/src/ee/features/evals/components/inner-evaluator-form.tsx b/web/src/ee/features/evals/components/inner-evaluator-form.tsx
new file mode 100644
index 0000000000..c1bc009191
--- /dev/null
+++ b/web/src/ee/features/evals/components/inner-evaluator-form.tsx
@@ -0,0 +1,800 @@
+import { useFieldArray, useForm } from "react-hook-form";
+import { Input } from "@/src/components/ui/input";
+import { Button } from "@/src/components/ui/button";
+import {
+ Form,
+ FormControl,
+ FormDescription,
+ FormField,
+ FormItem,
+ FormLabel,
+ FormMessage,
+} from "@/src/components/ui/form";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/src/components/ui/select";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { Tabs, TabsList, TabsTrigger } from "@/src/components/ui/tabs";
+import {
+ tracesTableColsWithOptions,
+ evalTraceTableCols,
+ evalDatasetFormFilterCols,
+ singleFilter,
+ type JobConfiguration,
+ availableTraceEvalVariables,
+ datasetFormFilterColsWithOptions,
+ availableDatasetEvalVariables,
+} from "@langfuse/shared";
+import * as z from "zod";
+import { useEffect, useMemo, useState } from "react";
+import { api } from "@/src/utils/api";
+import { InlineFilterBuilder } from "@/src/features/filters/components/filter-builder";
+import { type EvalTemplate, variableMapping } from "@langfuse/shared";
+import router from "next/router";
+import { Slider } from "@/src/components/ui/slider";
+import { Card } from "@/src/components/ui/card";
+import { JSONView } from "@/src/components/ui/CodeJsonViewer";
+import DocPopup from "@/src/components/layouts/doc-popup";
+import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
+import { cn } from "@/src/utils/tailwind";
+import { Checkbox } from "@/src/components/ui/checkbox";
+import {
+ evalConfigFormSchema,
+ isTraceOrDatasetObject,
+ isTraceTarget,
+ type LangfuseObject,
+} from "@/src/ee/features/evals/utils/evaluator-form-utils";
+import { ExecutionCountTooltip } from "@/src/ee/features/evals/components/execution-count-tooltip";
+import {
+ TimeScopeDescription,
+ VariableMappingDescription,
+} from "@/src/ee/features/evals/components/eval-form-descriptions";
+
+const fieldHasJsonSelectorOption = (
+ selectedColumnId: string | undefined | null,
+): boolean =>
+ selectedColumnId === "input" ||
+ selectedColumnId === "output" ||
+ selectedColumnId === "expected_output";
+
+export const InnerEvaluatorForm = (props: {
+ projectId: string;
+ evalTemplate: EvalTemplate;
+ disabled?: boolean;
+ existingEvaluator?: JobConfiguration;
+ onFormSuccess?: () => void;
+ shouldWrapVariables?: boolean;
+ mode?: "create" | "edit";
+}) => {
+ const [formError, setFormError] = useState<string | null>(null);
+ const capture = usePostHogClientCapture();
+
+ const form = useForm<z.infer<typeof evalConfigFormSchema>>({
+ resolver: zodResolver(evalConfigFormSchema),
+ disabled: props.disabled,
+ defaultValues: {
+ scoreName:
+ props.existingEvaluator?.scoreName ?? `${props.evalTemplate.name}`,
+ target: props.existingEvaluator?.targetObject ?? "trace",
+ filter: props.existingEvaluator?.filter
+ ? z.array(singleFilter).parse(props.existingEvaluator.filter)
+ : [],
+ mapping: props.existingEvaluator?.variableMapping
+ ? z
+ .array(variableMapping)
+ .parse(props.existingEvaluator.variableMapping)
+ : z.array(variableMapping).parse(
+ props.evalTemplate
+ ? props.evalTemplate.vars.map((v) => ({
+ templateVariable: v,
+ langfuseObject: "trace" as const,
+ selectedColumnId: "input",
+ }))
+ : [],
+ ),
+ sampling: props.existingEvaluator?.sampling
+ ? props.existingEvaluator.sampling.toNumber()
+ : 1,
+ delay: props.existingEvaluator?.delay
+ ? props.existingEvaluator.delay / 1000
+ : 10,
+ timeScope: (props.existingEvaluator?.timeScope ?? ["NEW"]).filter(
+ (option): option is "NEW" | "EXISTING" =>
+ ["NEW", "EXISTING"].includes(option),
+ ),
+ },
+ });
+
+ const traceFilterOptions = api.traces.filterOptions.useQuery(
+ {
+ projectId: props.projectId,
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ refetchOnMount: false,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ staleTime: Infinity,
+ },
+ );
+
+ const datasets = api.datasets.allDatasetMeta.useQuery(
+ {
+ projectId: props.projectId,
+ },
+ {
+ trpc: {
+ context: {
+ skipBatch: true,
+ },
+ },
+ refetchOnMount: false,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ staleTime: Infinity,
+ },
+ );
+
+ const datasetFilterOptions = useMemo(() => {
+ if (!datasets.data) return undefined;
+ return {
+ datasetId: datasets.data?.map((d) => ({
+ value: d.id,
+ displayValue: d.name,
+ })),
+ };
+ }, [datasets.data]);
+
+ useEffect(() => {
+ if (props.evalTemplate && form.getValues("mapping").length === 0) {
+ form.setValue(
+ "mapping",
+ props.evalTemplate.vars.map((v) => ({
+ templateVariable: v,
+ langfuseObject: "trace" as const,
+ selectedColumnId: "input",
+ })),
+ );
+ form.setValue("scoreName", `${props.evalTemplate.name}`);
+ }
+ }, [form, props.evalTemplate]);
+
+ const { fields } = useFieldArray({
+ control: form.control,
+ name: "mapping",
+ });
+
+ const utils = api.useUtils();
+ const createJobMutation = api.evals.createJob.useMutation({
+ onSuccess: () => utils.models.invalidate(),
+ onError: (error) => setFormError(error.message),
+ });
+ const updateJobMutation = api.evals.updateEvalJob.useMutation({
+ onSuccess: () => utils.evals.invalidate(),
+ onError: (error) => setFormError(error.message),
+ });
+ const [availableVariables, setAvailableVariables] = useState<
+ typeof availableTraceEvalVariables | typeof availableDatasetEvalVariables
+ >(
+ isTraceTarget(props.existingEvaluator?.targetObject ?? "trace")
+ ? availableTraceEvalVariables
+ : availableDatasetEvalVariables,
+ );
+
+ function onSubmit(values: z.infer<typeof evalConfigFormSchema>) {
+ capture(
+ props.mode === "edit"
+ ? "eval_config:update"
+ : "eval_config:new_form_submit",
+ );
+
+ const validatedFilter = z.array(singleFilter).safeParse(values.filter);
+
+ if (
+ props.existingEvaluator?.timeScope.includes("EXISTING") &&
+ props.mode === "edit" &&
+ !values.timeScope.includes("EXISTING")
+ ) {
+ form.setError("timeScope", {
+ type: "manual",
+ message:
+ "The evaluator ran on existing traces already. This cannot be changed anymore.",
+ });
+ return;
+ }
+ if (form.getValues("timeScope").length === 0) {
+ form.setError("timeScope", {
+ type: "manual",
+ message: "Please select at least one.",
+ });
+ return;
+ }
+
+ if (validatedFilter.success === false) {
+ form.setError("filter", {
+ type: "manual",
+ message: "Please fill out all filter fields",
+ });
+ return;
+ }
+
+ const validatedVarMapping = z
+ .array(variableMapping)
+ .safeParse(values.mapping);
+
+ if (validatedVarMapping.success === false) {
+ form.setError("mapping", {
+ type: "manual",
+ message: "Please fill out all variable mappings",
+ });
+ return;
+ }
+
+ const delay = values.delay * 1000; // convert to ms
+ const sampling = values.sampling;
+ const mapping = validatedVarMapping.data;
+ const filter = validatedFilter.data;
+ const scoreName = values.scoreName;
+
+ (props.mode === "edit" && props.existingEvaluator
+ ? updateJobMutation.mutateAsync({
+ projectId: props.projectId,
+ evalConfigId: props.existingEvaluator.id,
+ config: {
+ delay,
+ filter,
+ variableMapping: mapping,
+ sampling,
+ scoreName,
+ timeScope: values.timeScope,
+ },
+ })
+ : createJobMutation.mutateAsync({
+ projectId: props.projectId,
+ target: values.target,
+ evalTemplateId: props.evalTemplate.id,
+ scoreName,
+ filter,
+ mapping,
+ sampling,
+ delay,
+ timeScope: values.timeScope,
+ })
+ )
+ .then(() => {
+ form.reset();
+ props.onFormSuccess?.();
+
+ if (props.mode !== "edit") {
+ void router.push(`/project/${props.projectId}/evals`);
+ }
+ })
+ .catch((error) => {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ if ("message" in error && typeof error.message === "string") {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ setFormError(error.message as string);
+ return;
+ } else {
+ setFormError(JSON.stringify(error));
+ console.error(error);
+ }
+ });
+ }
+
+ return (
+ <Form {...form}>
+ <form
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ onSubmit={form.handleSubmit(onSubmit)}
+ className="flex w-full flex-col gap-4"
+ >
+ <div className="grid gap-4">
+ <FormField
+ control={form.control}
+ name="scoreName"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Score Name</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <Card className="flex max-w-full flex-col gap-6 overflow-y-auto p-4">
+ <FormField
+ control={form.control}
+ name="target"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Target object</FormLabel>
+ <FormControl>
+ <Tabs
+ defaultValue="trace"
+ value={field.value}
+ onValueChange={(value) => {
+ const isTrace = isTraceTarget(value);
+ const langfuseObject: LangfuseObject = isTrace
+ ? "trace"
+ : "dataset_item";
+ const newMapping = form
+ .getValues("mapping")
+ .map((field) => ({ ...field, langfuseObject }));
+ form.setValue("mapping", newMapping);
+ form.setValue("delay", isTrace ? 10 : 20);
+ setAvailableVariables(
+ isTrace
+ ? availableTraceEvalVariables
+ : availableDatasetEvalVariables,
+ );
+ field.onChange(value);
+ }}
+ >
+ <TabsList>
+ <TabsTrigger
+ value="trace"
+ disabled={props.disabled || props.mode === "edit"}
+ >
+ Trace
+ </TabsTrigger>
+ <TabsTrigger
+ value="dataset"
+ disabled={props.disabled || props.mode === "edit"}
+ >
+ Dataset
+ </TabsTrigger>
+ </TabsList>
+ </Tabs>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+
+ <div className="flex min-w-[300px]">
+ <FormField
+ control={form.control}
+ name="timeScope"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Evaluator runs on</FormLabel>
+ <FormControl>
+ <div className="flex flex-col gap-2">
+ <div className="items-top flex space-x-2">
+ <Checkbox
+ id="newObjects"
+ checked={field.value.includes("NEW")}
+ onCheckedChange={(checked) => {
+ const newValue = checked
+ ? [...field.value, "NEW"]
+ : field.value.filter((v) => v !== "NEW");
+ field.onChange(newValue);
+ }}
+ disabled={props.disabled}
+ />
+ <div className="grid gap-1.5 leading-none">
+ <label
+ htmlFor="newObjects"
+ className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
+ >
+ New{" "}
+ {form.watch("target") === "trace"
+ ? "traces"
+ : "dataset items"}
+ </label>
+ </div>
+ </div>
+ <div className="items-top flex space-x-2">
+ <Checkbox
+ id="existingObjects"
+ checked={field.value.includes("EXISTING")}
+ onCheckedChange={(checked) => {
+ const newValue = checked
+ ? [...field.value, "EXISTING"]
+ : field.value.filter((v) => v !== "EXISTING");
+ field.onChange(newValue);
+ }}
+ disabled={
+ props.disabled ||
+ (props.mode === "edit" &&
+ field.value.includes("EXISTING"))
+ }
+ />
+ <div className="flex items-center gap-1.5 leading-none">
+ <label
+ htmlFor="existingObjects"
+ className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
+ >
+ Existing{" "}
+ {form.watch("target") === "trace"
+ ? "traces"
+ : "dataset items"}
+ </label>
+ {field.value.includes("EXISTING") &&
+ !props.disabled && (
+ <ExecutionCountTooltip
+ projectId={props.projectId}
+ item={form.watch("target")}
+ filter={form.watch("filter")}
+ />
+ )}
+ </div>
+ </div>
+ </div>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </div>
+
+ <FormField
+ control={form.control}
+ name="filter"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Target filter</FormLabel>
+ {isTraceTarget(form.watch("target")) ? (
+ <>
+ <FormControl>
+ <InlineFilterBuilder
+ columns={tracesTableColsWithOptions(
+ traceFilterOptions.data,
+ evalTraceTableCols,
+ )}
+ filterState={field.value ?? []}
+ onChange={(value) => field.onChange(value)}
+ disabled={props.disabled}
+ />
+ </FormControl>
+ <FormDescription>
+ <TimeScopeDescription
+ projectId={props.projectId}
+ timeScope={form.watch("timeScope")}
+ target="trace"
+ />
+ </FormDescription>
+ <FormMessage />
+ </>
+ ) : (
+ <>
+ <FormControl>
+ <InlineFilterBuilder
+ columns={datasetFormFilterColsWithOptions(
+ datasetFilterOptions,
+ evalDatasetFormFilterCols,
+ )}
+ filterState={field.value ?? []}
+ onChange={(value) => field.onChange(value)}
+ disabled={props.disabled}
+ />
+ </FormControl>
+ <FormDescription>
+ <TimeScopeDescription
+ projectId={props.projectId}
+ timeScope={form.watch("timeScope")}
+ target="dataset_item"
+ />
+ </FormDescription>
+ <FormMessage />
+ </>
+ )}
+ </FormItem>
+ )}
+ />
+ </Card>
+ <Card className="p-4">
+ <FormField
+ control={form.control}
+ name="mapping"
+ render={() => (
+ <>
+ <FormLabel className="">Variable mapping</FormLabel>
+ <FormControl>
+ Here will some variable mapping be added.
+ </FormControl>
+ <div
+ className={cn(
+ "my-2 flex flex-col gap-2",
+ !props.shouldWrapVariables && "lg:flex-row",
+ )}
+ >
+ <JSONView
+ title={"Eval Template"}
+ json={props.evalTemplate.prompt ?? null}
+ className={cn(
+ "min-h-48",
+ !props.shouldWrapVariables && "lg:w-2/3",
+ )}
+ codeClassName="flex-1"
+ />
+ <div
+ className={cn(
+ "flex flex-col gap-2",
+ !props.shouldWrapVariables && "lg:w-1/3",
+ )}
+ >
+ {fields.map((mappingField, index) => (
+ <Card className="flex flex-col gap-2 p-4" key={index}>
+ <div className="text-sm font-semibold">
+ {"{{"}
+ {mappingField.templateVariable}
+ {"}}"}
+ <DocPopup
+ description={
+ "Variable in the template to be replaced with the trace data."
+ }
+ href={
+ "https://langfuse.com/docs/scores/model-based-evals"
+ }
+ />
+ </div>
+ <FormField
+ control={form.control}
+ key={`${mappingField.id}-langfuseObject`}
+ name={`mapping.${index}.langfuseObject`}
+ render={({ field }) => (
+ <div className="flex items-center gap-2">
+ <VariableMappingDescription
+ title="Object"
+ description={
+ "Langfuse object to retrieve the data from."
+ }
+ href={
+ "https://langfuse.com/docs/scores/model-based-evals"
+ }
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Select
+ disabled={props.disabled}
+ defaultValue={field.value}
+ onValueChange={field.onChange}
+ >
+ <SelectTrigger>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {availableVariables.map(
+ (evalObject) => (
+ <SelectItem
+ value={evalObject.id}
+ key={evalObject.id}
+ >
+ {evalObject.display}
+ </SelectItem>
+ ),
+ )}
+ </SelectContent>
+ </Select>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ </div>
+ )}
+ />
+
+ {!isTraceOrDatasetObject(
+ form.watch(`mapping.${index}.langfuseObject`),
+ ) ? (
+ <FormField
+ control={form.control}
+ key={`${mappingField.id}-objectName`}
+ name={`mapping.${index}.objectName`}
+ render={({ field }) => (
+ <div className="flex items-center gap-2">
+ <VariableMappingDescription
+ title={"Object Name"}
+ description={
+ "Name of the Langfuse object to retrieve the data from."
+ }
+ href={
+ "https://langfuse.com/docs/scores/model-based-evals"
+ }
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ disabled={props.disabled}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ </div>
+ )}
+ />
+ ) : undefined}
+
+ <FormField
+ control={form.control}
+ key={`${mappingField.id}-selectedColumnId`}
+ name={`mapping.${index}.selectedColumnId`}
+ render={({ field }) => (
+ <div className="flex items-center gap-2">
+ <VariableMappingDescription
+ title={"Object Variable"}
+ description={
+ "Variable on the Langfuse object to insert into the template."
+ }
+ href={
+ "https://langfuse.com/docs/scores/model-based-evals"
+ }
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Select
+ disabled={props.disabled}
+ defaultValue={field.value ?? undefined}
+ onValueChange={(value) => {
+ const availableColumns =
+ availableVariables.find(
+ (evalObject) =>
+ evalObject.id ===
+ form.watch(
+ `mapping.${index}.langfuseObject`,
+ ),
+ )?.availableColumns;
+
+ const column = availableColumns?.find(
+ (column) => column.id === value,
+ );
+
+ field.onChange(column?.id);
+ }}
+ >
+ <SelectTrigger>
+ <SelectValue placeholder="Object type" />
+ </SelectTrigger>
+ <SelectContent>
+ {availableVariables
+ .find(
+ (evalObject) =>
+ evalObject.id ===
+ form.watch(
+ `mapping.${index}.langfuseObject`,
+ ),
+ )
+ ?.availableColumns.map((column) => (
+ <SelectItem
+ value={column.id}
+ key={column.id}
+ >
+ {column.name}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ </div>
+ )}
+ />
+ {fieldHasJsonSelectorOption(
+ form.watch(`mapping.${index}.selectedColumnId`),
+ ) ? (
+ <FormField
+ control={form.control}
+ key={`${mappingField.id}-jsonSelector`}
+ name={`mapping.${index}.jsonSelector`}
+ render={({ field }) => (
+ <div className="flex items-center gap-2">
+ <VariableMappingDescription
+ title={"JsonPath"}
+ description={
+ "Optional selection: Use JsonPath syntax to select from a JSON object stored on a trace. If not selected, we will pass the entire object into the prompt."
+ }
+ href={
+ "https://langfuse.com/docs/scores/model-based-evals"
+ }
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ disabled={props.disabled}
+ placeholder="Optional"
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ </div>
+ )}
+ />
+ ) : undefined}
+ </Card>
+ ))}
+ </div>
+ </div>
+ <FormDescription>
+ Insert trace data into the prompt template.
+ </FormDescription>
+ <FormMessage />
+ </>
+ )}
+ />
+ </Card>
+ <Card className="flex flex-col gap-6 p-4">
+ <FormField
+ control={form.control}
+ name="sampling"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Sampling</FormLabel>
+ <FormControl>
+ <Slider
+ disabled={props.disabled}
+ min={0}
+ max={1}
+ step={0.01}
+ value={[field.value]}
+ onValueChange={(value) => field.onChange(value[0])}
+ />
+ </FormControl>
+ <div className="flex flex-col">
+ <FormDescription className="flex justify-between">
+ <span>0%</span>
+ <span>100%</span>
+ </FormDescription>
+ <FormDescription className="mt-1 flex flex-row gap-1">
+ <span>Percentage of traces to evaluate.</span>
+ <span>
+ Currently set to {(field.value * 100).toFixed(0)}%.
+ </span>
+ </FormDescription>
+ </div>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="delay"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Delay (seconds)</FormLabel>
+ <FormControl>
+ <Input {...field} type="number" />
+ </FormControl>
+ <FormDescription>
+ Time between first Trace/Dataset run event and evaluation
+ execution to ensure all data is available
+ </FormDescription>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ </Card>
+ </div>
+
+ {!props.disabled ? (
+ <Button
+ type="submit"
+ loading={createJobMutation.isLoading || updateJobMutation.isLoading}
+ className="mt-3"
+ >
+ Save
+ </Button>
+ ) : null}
+ </form>
+ {formError ? (
+ <p className="text-red text-center">
+ <span className="font-bold">Error:</span> {formError}
+ </p>
+ ) : null}
+ </Form>
+ );
+};
diff --git a/web/src/ee/features/evals/utils/evaluator-form-utils.ts b/web/src/ee/features/evals/utils/evaluator-form-utils.ts
new file mode 100644
index 0000000000..272f23f00e
--- /dev/null
+++ b/web/src/ee/features/evals/utils/evaluator-form-utils.ts
@@ -0,0 +1,25 @@
+import { z } from "zod";
+import {
+ singleFilter,
+ type langfuseObjects,
+ TimeScopeSchema,
+} from "@langfuse/shared";
+import { wipVariableMapping } from "@langfuse/shared";
+
+export const isTraceTarget = (target: string): boolean => target === "trace";
+export const isTraceOrDatasetObject = (object: string): boolean =>
+ object === "trace" || object === "dataset_item";
+
+export const evalConfigFormSchema = z.object({
+ scoreName: z.string(),
+ target: z.string(),
+ filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
+ mapping: z.array(wipVariableMapping),
+ sampling: z.coerce.number().gt(0).lte(1),
+ delay: z.coerce.number().optional().default(10),
+ timeScope: TimeScopeSchema,
+});
+
+export type EvalFormType = z.infer<typeof evalConfigFormSchema>;
+
+export type LangfuseObject = (typeof langfuseObjects)[number];
diff --git a/web/src/features/datasets/server/dataset-router.ts b/web/src/features/datasets/server/dataset-router.ts
index 7f4eda4eca..56afa78bf2 100644
--- a/web/src/features/datasets/server/dataset-router.ts
+++ b/web/src/features/datasets/server/dataset-router.ts
@@ -7,7 +7,7 @@ import { Prisma, type Dataset } from "@langfuse/shared/src/db";
import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { DB } from "@/src/server/db";
-import { paginationZod, DatasetStatus } from "@langfuse/shared";
+import { paginationZod, DatasetStatus, singleFilter } from "@langfuse/shared";
import { TRPCError } from "@trpc/server";
import {
createDatasetRunsTable,
@@ -16,7 +16,7 @@ import {
fetchDatasetItems,
getRunItemsByRunIdOrItemId,
} from "@/src/features/datasets/server/service";
-import { logger } from "@langfuse/shared/src/server";
+import { getDatasetItemsTableCount, logger } from "@langfuse/shared/src/server";
import { createId as createCuid } from "@paralleldrive/cuid2";
const formatDatasetItemData = (data: string | null | undefined) => {
@@ -129,6 +129,21 @@ export const datasetRouter = createTRPCRouter({
datasets,
};
}),
+ countAllDatasetItems: protectedProjectProcedure
+ .input(
+ z.object({
+ projectId: z.string(), // Required for protectedProjectProcedure
+ filter: z.array(singleFilter).nullable(),
+ }),
+ )
+ .query(async ({ input }) => {
+ const count = await getDatasetItemsTableCount({
+ projectId: input.projectId,
+ filter: input.filter ?? [],
+ });
+
+ return count;
+ }),
byId: protectedProjectProcedure
.input(
z.object({