Skip to content

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

feat: add score `source` (#1365)

Marc Klingene98565ee9cdc
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index 41a61cbb66..f507d0805c 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -118,6 +118,7 @@ types:
traceId: string
name: string
value: double
+ source: ScoreSource
observationId: optional<string>
timestamp: datetime
comment: optional<string>
@@ -183,6 +184,10 @@ types:
enum:
- ACTIVE
- ARCHIVED
+ ScoreSource:
+ enum:
+ - API
+ - REVIEW
errors:
Error:
diff --git a/web/generated/openapi-server/openapi.yml b/web/generated/openapi-server/openapi.yml
index a63e442edd..370b636e5c 100644
--- a/web/generated/openapi-server/openapi.yml
+++ b/web/generated/openapi-server/openapi.yml
@@ -1376,6 +1376,8 @@ components:
value:
type: number
format: double
+ source:
+ $ref: '#/components/schemas/ScoreSource'
observationId:
type: string
nullable: true
@@ -1390,6 +1392,7 @@ components:
- traceId
- name
- value
+ - source
- timestamp
Dataset:
title: Dataset
@@ -1541,6 +1544,12 @@ components:
enum:
- ACTIVE
- ARCHIVED
+ ScoreSource:
+ title: ScoreSource
+ type: string
+ enum:
+ - API
+ - REVIEW
CreateDatasetItemRequest:
title: CreateDatasetItemRequest
type: object
diff --git a/web/prisma/generated/types.ts b/web/prisma/generated/types.ts
index 34b0193ff5..05968626e8 100644
--- a/web/prisma/generated/types.ts
+++ b/web/prisma/generated/types.ts
@@ -24,6 +24,11 @@ export const ObservationLevel = {
ERROR: "ERROR"
} as const;
export type ObservationLevel = (typeof ObservationLevel)[keyof typeof ObservationLevel];
+export const ScoreSource = {
+ API: "API",
+ REVIEW: "REVIEW"
+} as const;
+export type ScoreSource = (typeof ScoreSource)[keyof typeof ScoreSource];
export const PricingUnit = {
PER_1000_TOKENS: "PER_1000_TOKENS",
PER_1000_CHARS: "PER_1000_CHARS"
@@ -259,6 +264,7 @@ export type Score = {
timestamp: Generated<Timestamp>;
name: string;
value: number;
+ source: Generated<ScoreSource>;
comment: string | null;
trace_id: string;
observation_id: string | null;
diff --git a/web/prisma/migrations/20240307185543_score_add_source_nullable/migration.sql b/web/prisma/migrations/20240307185543_score_add_source_nullable/migration.sql
new file mode 100644
index 0000000000..78d80eaff9
--- /dev/null
+++ b/web/prisma/migrations/20240307185543_score_add_source_nullable/migration.sql
@@ -0,0 +1,5 @@
+-- CreateEnum
+CREATE TYPE "ScoreSource" AS ENUM ('API', 'REVIEW');
+
+-- AlterTable
+ALTER TABLE "scores" ADD COLUMN "source" "ScoreSource" NOT NULL DEFAULT 'API';
diff --git a/web/prisma/migrations/20240307185544_score_add_name_index/migration.sql b/web/prisma/migrations/20240307185544_score_add_name_index/migration.sql
new file mode 100644
index 0000000000..3b21082e2c
--- /dev/null
+++ b/web/prisma/migrations/20240307185544_score_add_name_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX CONCURRENTLY "scores_source_idx" ON "scores"("source");
diff --git a/web/prisma/migrations/20240307185725_backfill_score_source/migration.sql b/web/prisma/migrations/20240307185725_backfill_score_source/migration.sql
new file mode 100644
index 0000000000..8e70f97fa9
--- /dev/null
+++ b/web/prisma/migrations/20240307185725_backfill_score_source/migration.sql
@@ -0,0 +1,4 @@
+-- scores previously created in the Langfuse UI all had the name 'manual-score'
+UPDATE "scores"
+SET "source" = 'REVIEW'::"ScoreSource"
+WHERE "name" = 'manual-score';
diff --git a/web/prisma/schema.prisma b/web/prisma/schema.prisma
index bc4cfa487b..35ac0e57a9 100644
--- a/web/prisma/schema.prisma
+++ b/web/prisma/schema.prisma
@@ -296,6 +296,7 @@ model Score {
timestamp DateTime @default(now())
name String
value Float
+ source ScoreSource @default(API)
comment String?
traceId String @map("trace_id")
trace Trace @relation(fields: [traceId], references: [id], onDelete: Cascade)
@@ -307,9 +308,15 @@ model Score {
@@index([value])
@@index([traceId], type: Hash)
@@index([observationId], type: Hash)
+ @@index([source])
@@map("scores")
}
+enum ScoreSource {
+ API
+ REVIEW
+}
+
enum PricingUnit {
PER_1000_TOKENS
PER_1000_CHARS
diff --git a/web/prisma/seed.ts b/web/prisma/seed.ts
index 31c9f5ac32..66b8b9cb4d 100755
--- a/web/prisma/seed.ts
+++ b/web/prisma/seed.ts
@@ -3,6 +3,7 @@ import {
type Project,
type Prisma,
ObservationType,
+ ScoreSource,
} from "@prisma/client";
import {
hashSecretKey,
@@ -403,9 +404,10 @@ function createObjects(
? [
{
traceId: trace.id,
- name: "feedback",
+ name: "manual-score",
value: Math.floor(Math.random() * 3) - 1,
timestamp: traceTs,
+ source: ScoreSource.REVIEW,
},
]
: []),
@@ -416,6 +418,7 @@ function createObjects(
name: "sentiment",
value: Math.floor(Math.random() * 10) - 5,
timestamp: traceTs,
+ source: ScoreSource.API,
},
]
: []),
@@ -596,6 +599,7 @@ function createObjects(
value: Math.random() * 2 - 1,
observationId: generation.id,
traceId: trace.id,
+ source: ScoreSource.API,
});
if (Math.random() > 0.6)
scores.push({
@@ -603,6 +607,7 @@ function createObjects(
value: Math.random() * 2 - 1,
observationId: generation.id,
traceId: trace.id,
+ source: ScoreSource.API,
});
generationIds.push(generation.id);
diff --git a/web/src/__tests__/scores.servertest.ts b/web/src/__tests__/scores.servertest.ts
index d85f83ea8a..7d9bde5104 100644
--- a/web/src/__tests__/scores.servertest.ts
+++ b/web/src/__tests__/scores.servertest.ts
@@ -54,6 +54,7 @@ describe("/api/public/scores API Endpoint", () => {
expect(dbScore?.value).toBe(100.5);
expect(dbScore?.observationId).toBeNull();
expect(dbScore?.comment).toBe("comment");
+ expect(dbScore?.source).toBe("API");
});
it("should create score for a trace with int", async () => {
@@ -301,6 +302,7 @@ describe("/api/public/scores API Endpoint", () => {
comment: string;
traceId: string;
observationId: string;
+ source: string;
}>("GET", `/api/public/scores/${scoreId}`);
expect(getScore.status).toBe(200);
@@ -309,6 +311,7 @@ describe("/api/public/scores API Endpoint", () => {
name: "score-name",
value: 100.5,
comment: "comment",
+ source: "API",
traceId,
observationId: generationId,
});
diff --git a/web/src/features/manual-scoring/components/ManualScoreButton.tsx b/web/src/features/manual-scoring/components/ManualScoreButton.tsx
index 9d4c59bb63..e0109e2589 100644
--- a/web/src/features/manual-scoring/components/ManualScoreButton.tsx
+++ b/web/src/features/manual-scoring/components/ManualScoreButton.tsx
@@ -69,9 +69,15 @@ export function ManualScoreButton({
utils.sessions.invalidate(),
]);
};
- const mutCreateScore = api.scores.create.useMutation({ onSuccess });
- const mutUpdateScore = api.scores.update.useMutation({ onSuccess });
- const mutDeleteScore = api.scores.delete.useMutation({ onSuccess });
+ const mutCreateScore = api.scores.createReviewScore.useMutation({
+ onSuccess,
+ });
+ const mutUpdateScore = api.scores.updateReviewScore.useMutation({
+ onSuccess,
+ });
+ const mutDeleteScore = api.scores.deleteReviewScore.useMutation({
+ onSuccess,
+ });
const handleDelete = async () => {
if (score) {
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 27eab8562c..e06bbab36e 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -128,7 +128,7 @@ export const scoresRouter = createTRPCRouter({
},
}),
),
- create: protectedProcedure
+ createReviewScore: protectedProcedure
.input(
z.object({
traceId: z.string(),
@@ -176,6 +176,7 @@ export const scoresRouter = createTRPCRouter({
value: input.value,
name: input.name,
comment: input.comment,
+ source: "REVIEW",
},
});
await auditLog({
@@ -191,7 +192,7 @@ export const scoresRouter = createTRPCRouter({
});
return score;
}),
- update: protectedProcedure
+ updateReviewScore: protectedProcedure
.input(
z.object({
id: z.string(),
@@ -203,6 +204,7 @@ export const scoresRouter = createTRPCRouter({
const score = await ctx.prisma.score.findFirstOrThrow({
where: {
id: input.id,
+ source: "REVIEW",
trace: {
project: {
members: {
@@ -252,12 +254,13 @@ export const scoresRouter = createTRPCRouter({
},
});
}),
- delete: protectedProcedure
+ deleteReviewScore: protectedProcedure
.input(z.string())
.mutation(async ({ input, ctx }) => {
const score = await ctx.prisma.score.findFirstOrThrow({
where: {
id: input,
+ source: "REVIEW",
trace: {
project: {
members: {
diff --git a/web/src/server/api/services/EventProcessor.ts b/web/src/server/api/services/EventProcessor.ts
index dcbd0b8cc1..570812e253 100644
--- a/web/src/server/api/services/EventProcessor.ts
+++ b/web/src/server/api/services/EventProcessor.ts
@@ -544,6 +544,7 @@ export class ScoreProcessor implements EventProcessor {
timestamp: new Date(),
value: body.value,
name: body.name,
+ source: "API",
comment: body.comment,
...(body.observationId && {
observation: { connect: { id: body.observationId } },
@@ -554,6 +555,7 @@ export class ScoreProcessor implements EventProcessor {
value: body.value,
name: body.name,
comment: body.comment,
+ source: "API",
...(body.observationId && {
observation: { connect: { id: body.observationId } },
}),
diff --git a/worker/generated/types.ts b/worker/generated/types.ts
index 34b0193ff5..d05dcf086c 100644
--- a/worker/generated/types.ts
+++ b/worker/generated/types.ts
@@ -24,6 +24,11 @@ export const ObservationLevel = {
ERROR: "ERROR"
} as const;
export type ObservationLevel = (typeof ObservationLevel)[keyof typeof ObservationLevel];
+export const ScoreSource = {
+ API: "API",
+ REVIEW: "REVIEW"
+} as const;
+export type ScoreSource = (typeof ScoreSource)[keyof typeof ScoreSource];
export const PricingUnit = {
PER_1000_TOKENS: "PER_1000_TOKENS",
PER_1000_CHARS: "PER_1000_CHARS"
@@ -259,6 +264,7 @@ export type Score = {
timestamp: Generated<Timestamp>;
name: string;
value: number;
+ source: ScoreSource | null;
comment: string | null;
trace_id: string;
observation_id: string | null;