Skip to content

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

filter by observation type (#317)

Maximilian Deichmann91adbdf8ffa8
diff --git a/src/__tests__/observations.servertest.ts b/src/__tests__/observations.servertest.ts
index 82f8164ef9..59c80d725a 100644
--- a/src/__tests__/observations.servertest.ts
+++ b/src/__tests__/observations.servertest.ts
@@ -9,7 +9,7 @@ describe("/api/public/generations API Endpoint", () => {
beforeEach(async () => await pruneDatabase());
afterEach(async () => await pruneDatabase());
- it("should create generation after trace", async () => {
+ it("should fetch all observations", async () => {
await pruneDatabase();
const traceId = uuidv4();
@@ -62,6 +62,79 @@ describe("/api/public/generations API Endpoint", () => {
expect(fetchedObservations.body.data[0]?.input).toEqual({ key: "input" });
expect(fetchedObservations.body.data[0]?.output).toEqual({ key: "output" });
});
+ it("should fetch all observations, filtered by generations", async () => {
+ await pruneDatabase();
+
+ const traceId = uuidv4();
+
+ await prisma.trace.create({
+ data: {
+ id: traceId,
+ name: "trace-name",
+ userId: "user-1",
+ projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",
+ metadata: { key: "value" },
+ release: "1.0.0",
+ version: "2.0.0",
+ },
+ });
+
+ await prisma.observation.create({
+ data: {
+ id: uuidv4(),
+ traceId: traceId,
+ name: "generation-name",
+ startTime: new Date("2021-01-01T00:00:00.000Z"),
+ endTime: new Date("2021-01-01T00:00:00.000Z"),
+ model: "model-name",
+ modelParameters: { key: "value" },
+ input: { key: "input" },
+ output: { key: "output" },
+ version: "2.0.0",
+ type: "GENERATION",
+ project: {
+ connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
+ },
+ },
+ });
+
+ await prisma.observation.create({
+ data: {
+ id: uuidv4(),
+ traceId: traceId,
+ name: "generation-name",
+ startTime: new Date("2021-01-01T00:00:00.000Z"),
+ endTime: new Date("2021-01-01T00:00:00.000Z"),
+ model: "model-name",
+ modelParameters: { key: "value" },
+ input: { key: "input" },
+ output: { key: "output" },
+ version: "2.0.0",
+ type: "SPAN",
+ project: {
+ connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" },
+ },
+ },
+ });
+
+ const fetchedObservations = await makeAPICall(
+ "GET",
+ "/api/public/observations?type=GENERATION",
+ undefined,
+ );
+
+ expect(fetchedObservations.status).toBe(200);
+
+ if (!isObservationList(fetchedObservations.body)) {
+ throw new Error("Expected body to be an array of observations");
+ }
+
+ expect(fetchedObservations.body.data.length).toBe(1);
+ expect(fetchedObservations.body.data[0]?.traceId).toBe(traceId);
+ expect(fetchedObservations.body.data[0]?.input).toEqual({ key: "input" });
+ expect(fetchedObservations.body.data[0]?.output).toEqual({ key: "output" });
+ expect(fetchedObservations.body.data[0]?.type).toEqual("GENERATION");
+ });
});
const isObservationList = (val: unknown): val is ObservationResponse => {
diff --git a/src/pages/api/public/observations/index.ts b/src/pages/api/public/observations/index.ts
index fbdfa51575..8b9bbea0c5 100644
--- a/src/pages/api/public/observations/index.ts
+++ b/src/pages/api/public/observations/index.ts
@@ -94,7 +94,7 @@ const getObservation = async (
: Prisma.empty;
const observationTypeCondition = query.type
- ? Prisma.sql`AND o."type" = ${query.type}::ObservationType`
+ ? Prisma.sql`AND o."type" = ${query.type}::"ObservationType"`
: Prisma.empty;
const [observations, count] = await Promise.all([