Skip to content

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

feat: map environment to score within eval service (#5749)

Steffen Schmitz346e5fe7b621
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index d688529ecc..9396e1fbbe 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -178,6 +178,7 @@ export const getObservationForTraceIdByName = async (
project_id,
type,
parent_observation_id,
+ environment,
start_time,
end_time,
name,
diff --git a/worker/src/__tests__/evalService.test.ts b/worker/src/__tests__/evalService.test.ts
index 3cd6958b1e..4501d7e05e 100644
--- a/worker/src/__tests__/evalService.test.ts
+++ b/worker/src/__tests__/evalService.test.ts
@@ -732,99 +732,101 @@ describe("eval service tests", () => {
expect(jobs.length).toBe(0);
}, 10_000);
- });
- test("does create eval for trace which is way in the past if timestamp is provided", async () => {
- const traceId = randomUUID();
+ test("does create eval for trace which is way in the past if timestamp is provided", async () => {
+ const traceId = randomUUID();
- const timestamp = new Date(Date.now() - 1000 * 60 * 60 * 24 * 365 * 1);
- const trace = createTrace({
- project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
- id: traceId,
- timestamp: timestamp.getTime(),
- });
+ const timestamp = new Date(Date.now() - 1000 * 60 * 60 * 24 * 365 * 1);
+ const trace = createTrace({
+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ id: traceId,
+ timestamp: timestamp.getTime(),
+ });
- await createTracesCh([trace]);
+ await createTracesCh([trace]);
- const jobConfiguration = 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: "trace",
- scoreName: "score",
- variableMapping: JSON.parse("[]"),
- timeScope: ["NEW"],
- },
- });
+ const jobConfiguration = 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: "trace",
+ scoreName: "score",
+ variableMapping: JSON.parse("[]"),
+ timeScope: ["NEW"],
+ },
+ });
- const payload = {
- projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
- traceId: traceId,
- configId: jobConfiguration.id,
- timestamp: timestamp,
- };
+ const payload = {
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ traceId: traceId,
+ configId: jobConfiguration.id,
+ timestamp: timestamp,
+ };
- await createEvalJobs({
- event: payload,
- enforcedJobTimeScope: "NEW", // the config must contain NEW
- });
+ await createEvalJobs({
+ event: payload,
+ enforcedJobTimeScope: "NEW", // the config must contain NEW
+ });
- const jobs = await kyselyPrisma.$kysely
- .selectFrom("job_executions")
- .selectAll()
- .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
- .where("job_configuration_id", "in", [jobConfiguration.id])
- .where("job_input_trace_id", "=", traceId)
- .execute();
+ const jobs = await kyselyPrisma.$kysely
+ .selectFrom("job_executions")
+ .selectAll()
+ .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
+ .where("job_configuration_id", "in", [jobConfiguration.id])
+ .where("job_input_trace_id", "=", traceId)
+ .execute();
- expect(jobs.length).toBe(1);
- }, 10_000);
+ expect(jobs.length).toBe(1);
+ }, 10_000);
- test("creates eval for trace with timestamp in the future", async () => {
- const traceId = randomUUID();
+ test("creates eval for trace with timestamp in the future", async () => {
+ const traceId = randomUUID();
- 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: "trace",
- scoreName: "score",
- variableMapping: JSON.parse("[]"),
- timeScope: ["NEW"],
- },
- });
+ 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: "trace",
+ scoreName: "score",
+ variableMapping: JSON.parse("[]"),
+ timeScope: ["NEW"],
+ },
+ });
- const trace = createTrace({
- project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
- id: traceId,
- timestamp: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365 * 1).getTime(),
- });
+ const trace = createTrace({
+ project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ id: traceId,
+ timestamp: new Date(
+ Date.now() + 1000 * 60 * 60 * 24 * 365 * 1,
+ ).getTime(),
+ });
- await createTracesCh([trace]);
+ await createTracesCh([trace]);
- await createEvalJobs({
- event: {
- projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
- traceId: traceId,
- },
- });
+ await createEvalJobs({
+ event: {
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ traceId: traceId,
+ },
+ });
- const jobs = await kyselyPrisma.$kysely
- .selectFrom("job_executions")
- .selectAll()
- .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
- .execute();
+ const jobs = await kyselyPrisma.$kysely
+ .selectFrom("job_executions")
+ .selectAll()
+ .where("project_id", "=", "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a")
+ .execute();
- expect(jobs.length).toBe(1);
- }, 10_000);
+ expect(jobs.length).toBe(1);
+ }, 10_000);
+ });
describe("execute evals", () => {
test("evals a valid 'trace' event", async () => {
@@ -1362,6 +1364,7 @@ describe("eval service tests", () => {
id: traceId,
project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
user_id: "a",
+ environment: "production",
input: JSON.stringify({ input: "This is a great prompt" }),
output: JSON.stringify({ output: "This is a great response" }),
timestamp: convertDateToClickhouseDateTime(new Date()),
@@ -1393,10 +1396,12 @@ describe("eval service tests", () => {
{
value: '{"input":"This is a great prompt"}',
var: "input",
+ environment: "production",
},
{
value: '{"output":"This is a great response"}',
var: "output",
+ environment: "production",
},
]);
}, 10_000);
@@ -1421,6 +1426,7 @@ describe("eval service tests", () => {
project_id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
name: "great-llm-name",
type: "GENERATION",
+ environment: "production",
input: JSON.stringify({ huhu: "This is a great prompt" }),
output: JSON.stringify({ haha: "This is a great response" }),
start_time: convertDateToClickhouseDateTime(new Date()),
@@ -1454,10 +1460,12 @@ describe("eval service tests", () => {
{
value: '{"huhu":"This is a great prompt"}',
var: "input",
+ environment: "production",
},
{
value: '{"haha":"This is a great response"}',
var: "output",
+ environment: "production",
},
]);
}, 10_000);
@@ -1544,10 +1552,12 @@ describe("eval service tests", () => {
expect(result).toEqual([
{
+ environment: "default",
value: "",
var: "input",
},
{
+ environment: "default",
value: "",
var: "output",
},
@@ -1622,10 +1632,12 @@ describe("eval service tests", () => {
expect(result).toEqual([
{
+ environment: "default",
value: '{"huhu":"This is a great prompt again"}',
var: "input",
},
{
+ environment: "default",
value: '{"haha":"This is a great response again"}',
var: "output",
},
diff --git a/worker/src/ee/evaluation/evalService.ts b/worker/src/ee/evaluation/evalService.ts
index d6d0787035..881c7e67f7 100644
--- a/worker/src/ee/evaluation/evalService.ts
+++ b/worker/src/ee/evaluation/evalService.ts
@@ -371,6 +371,9 @@ export const evaluate = async ({
`Evaluating job ${event.jobExecutionId} extracted variables ${JSON.stringify(mappingResult)} `,
);
+ // Get environment from trace or observation variables
+ const environment = mappingResult.find((r) => r.environment)?.environment;
+
// compile the prompt and send out the LLM request
let prompt;
try {
@@ -461,6 +464,7 @@ export const evaluate = async ({
value: parsedLLMOutput.score,
comment: parsedLLMOutput.reasoning,
source: ScoreSource.EVAL,
+ environment: environment ?? "default",
};
// Write score to S3 and ingest into queue for Clickhouse processing
@@ -542,7 +546,7 @@ export async function extractVariablesFromTracingData({
// this here are variables which were inserted by users. Need to validate before DB query.
variableMapping: z.infer<typeof variableMappingList>;
datasetItemId?: string;
-}): Promise<{ var: string; value: string }[]> {
+}): Promise<{ var: string; value: string; environment?: string }[]> {
return Promise.all(
variables.map(async (variable) => {
const mapping = variableMapping.find(
@@ -616,10 +620,7 @@ export async function extractVariablesFromTracingData({
return { var: variable, value: "" };
}
- const trace: Record<string, unknown> | undefined = await getTraceById(
- traceId,
- projectId,
- );
+ const trace = await getTraceById(traceId, projectId);
// user facing errors
if (!trace) {
@@ -634,6 +635,7 @@ export async function extractVariablesFromTracingData({
return {
var: variable,
value: parseDatabaseRowToString(trace, mapping),
+ environment: trace.environment,
};
}
@@ -656,7 +658,7 @@ export async function extractVariablesFromTracingData({
return { var: variable, value: "" };
}
- const observation: Record<string, unknown> | undefined = (
+ const observation = (
await getObservationForTraceIdByName(
traceId,
projectId,
@@ -678,7 +680,10 @@ export async function extractVariablesFromTracingData({
return {
var: variable,
- value: parseUnknownToString(observation[mapping.selectedColumnId]),
+ value: parseUnknownToString(
+ (observation as Record<string, unknown>)[mapping.selectedColumnId],
+ ),
+ environment: observation.environment,
};
}