Skip to content

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

fix: correctly sample evals (#2562)

Max Deichmanne0f605a0f279
diff --git a/worker/src/__tests__/eval-service.test.ts b/worker/src/__tests__/eval-service.test.ts
index af8cbb8cab..f532b2eb8b 100644
--- a/worker/src/__tests__/eval-service.test.ts
+++ b/worker/src/__tests__/eval-service.test.ts
@@ -199,6 +199,104 @@ describe("create eval jobs", () => {
expect(jobs[0].end_time).to.be.null;
}, 10_000);
+ test("does not create job for inactive config", async () => {
+ await pruneDatabase();
+ const traceId = randomUUID();
+
+ await kyselyPrisma.$kysely
+ .insertInto("traces")
+ .values({
+ id: traceId,
+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ })
+ .execute();
+
+ await prisma.jobConfiguration.create({
+ data: {
+ id: randomUUID(),
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ filter: JSON.parse("[]"),
+ jobType: "EVAL",
+ delay: 0,
+ sampling: new Decimal("1"),
+ targetObject: "traces",
+ scoreName: "score",
+ variableMapping: JSON.parse("[]"),
+ status: "INACTIVE",
+ },
+ });
+
+ const payload = {
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ traceId: traceId,
+ };
+
+ await createEvalJobs({ event: payload });
+
+ const jobs = await kyselyPrisma.$kysely
+ .selectFrom("job_executions")
+ .selectAll()
+ .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
+ .execute();
+
+ expect(jobs.length).toBe(0);
+ }, 10_000);
+
+ test("does not create eval job for 0 sample rate", async () => {
+ await pruneDatabase();
+ const traceId = randomUUID();
+
+ await kyselyPrisma.$kysely
+ .insertInto("traces")
+ .values({
+ id: traceId,
+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ })
+ .execute();
+
+ await kyselyPrisma.$kysely
+ .insertInto("llm_api_keys")
+ .values({
+ id: randomUUID(),
+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ secret_key: encrypt(String(OPENAI_API_KEY)),
+ provider: "openai",
+ adapter: LLMAdapter.OpenAI,
+ custom_models: [],
+ display_secret_key: "123456",
+ })
+ .execute();
+
+ await prisma.jobConfiguration.create({
+ data: {
+ id: randomUUID(),
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ filter: JSON.parse("[]"),
+ jobType: "EVAL",
+ delay: 0,
+ sampling: new Decimal("0"),
+ targetObject: "traces",
+ scoreName: "score",
+ variableMapping: JSON.parse("[]"),
+ },
+ });
+
+ const payload = {
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ traceId: traceId,
+ };
+
+ await createEvalJobs({ event: payload });
+
+ const jobs = await kyselyPrisma.$kysely
+ .selectFrom("job_executions")
+ .selectAll()
+ .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
+ .execute();
+
+ expect(jobs.length).toBe(0);
+ }, 10_000);
+
test("cancels a job if the second event deselects", async () => {
await pruneDatabase();
const traceId = randomUUID();
diff --git a/worker/src/features/evaluation/eval-service.ts b/worker/src/features/evaluation/eval-service.ts
index aeed74f41f..5d741c3233 100644
--- a/worker/src/features/evaluation/eval-service.ts
+++ b/worker/src/features/evaluation/eval-service.ts
@@ -98,6 +98,19 @@ export const createEvalJobs = async ({
continue;
}
+ // apply sampling. Only if the job is sampled, we create a job
+ // user supplies a number between 0 and 1, which is the probability of sampling
+
+ if (parseFloat(config.sampling) !== 1) {
+ const random = Math.random();
+ if (random > parseFloat(config.sampling)) {
+ logger.info(
+ `Eval job for config ${config.id} and trace ${event.traceId} was sampled out`
+ );
+ continue;
+ }
+ }
+
logger.info(
`Creating eval job for config ${config.id} and trace ${event.traceId}`
);