Skip to content

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

feat(api): add `promptName` and `promptVersion` to observations (#2750)

Marc Klingenb123cc5a4fef
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index 8868e89469..8cd4b7508d 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -87,6 +87,8 @@ types:
ObservationsView:
extends: Observation
properties:
+ promptName: optional<string>
+ promptVersion: optional<integer>
modelId: optional<string>
inputPrice: optional<double>
outputPrice: optional<double>
diff --git a/packages/shared/prisma/generated/types.ts b/packages/shared/prisma/generated/types.ts
index 37b2b1435e..93f26b0a6c 100644
--- a/packages/shared/prisma/generated/types.ts
+++ b/packages/shared/prisma/generated/types.ts
@@ -309,6 +309,8 @@ export type ObservationView = {
unit: string | null;
completion_start_time: Timestamp | null;
prompt_id: string | null;
+ prompt_name: string | null;
+ prompt_version: number | null;
model_id: string | null;
input_price: string | null;
output_price: string | null;
diff --git a/packages/shared/prisma/migrations/20240718011735_observation_view_add_created_at_updated_at copy/migration.sql b/packages/shared/prisma/migrations/20240718011735_observation_view_add_created_at_updated_at copy/migration.sql
new file mode 100644
index 0000000000..f285aa82af
--- /dev/null
+++ b/packages/shared/prisma/migrations/20240718011735_observation_view_add_created_at_updated_at copy/migration.sql
@@ -0,0 +1,100 @@
+DROP VIEW IF EXISTS "observations_view"; -- Drop view as column was added in 20240705154048_observation_view_add_created_at_updated_at and update view must have same columns
+CREATE VIEW "observations_view" AS -- Specify the columns that should be returned in the view, as calculated columns are added but exist in the observations table already
+SELECT
+ o.id,
+ o.name,
+ o.start_time,
+ o.end_time,
+ o.parent_observation_id,
+ o.type,
+ o.trace_id,
+ o.metadata,
+ o.model,
+ o."modelParameters",
+ o.input,
+ o.output,
+ o.level,
+ o.status_message,
+ o.completion_start_time,
+ o.completion_tokens,
+ o.prompt_tokens,
+ o.total_tokens,
+ o.version,
+ o.project_id,
+ o.created_at,
+ o.updated_at,
+ o.unit,
+ o.prompt_id,
+ p.name as prompt_name, -- added in this change
+ p.version as prompt_version, -- added in this change
+ o.input_cost,
+ o.output_cost,
+ o.total_cost,
+ o.internal_model,
+ m.id AS "model_id",
+ m.start_date AS "model_start_date",
+ m.input_price,
+ m.output_price,
+ m.total_price,
+ m.tokenizer_config AS "tokenizer_config",
+ CASE
+ WHEN o.calculated_input_cost IS NULL AND o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ o.prompt_tokens::decimal * m.input_price
+ ELSE
+ COALESCE(o.calculated_input_cost, o.input_cost)
+ END AS "calculated_input_cost",
+ CASE
+ WHEN o.calculated_output_cost IS NULL AND o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ o.completion_tokens::decimal * m.output_price
+ ELSE
+ COALESCE(o.calculated_output_cost, o.output_cost)
+ END AS "calculated_output_cost",
+ CASE
+ WHEN o.calculated_total_cost IS NULL AND o.input_cost IS NULL AND o.output_cost IS NULL AND o.total_cost IS NULL THEN
+ CASE
+ WHEN m.total_price IS NOT NULL AND o.total_tokens IS NOT NULL THEN
+ m.total_price * o.total_tokens
+ ELSE
+ o.prompt_tokens::decimal * m.input_price +
+ o.completion_tokens::decimal * m.output_price
+ END
+ ELSE
+ COALESCE(o.calculated_total_cost, o.total_cost)
+ END AS "calculated_total_cost",
+ CASE WHEN o.end_time IS NULL THEN NULL ELSE (EXTRACT(EPOCH FROM o."end_time") - EXTRACT(EPOCH FROM o."start_time"))::double precision END AS "latency",
+ CASE WHEN o.completion_start_time IS NOT NULL AND o.start_time IS NOT NULL THEN EXTRACT(EPOCH FROM (completion_start_time - start_time))::double precision ELSE NULL END as "time_to_first_token"
+
+FROM
+ observations o
+LEFT JOIN LATERAL (
+ SELECT
+ models.*
+ FROM
+ models
+ WHERE (models.project_id = o.project_id OR models.project_id IS NULL)
+ AND models.model_name = o.internal_model
+ AND (models.start_date < o.start_time OR models.start_date IS NULL)
+ AND o.unit::TEXT = models.unit
+ ORDER BY
+ models.project_id ASC, -- in postgres, NULLs are sorted last when ordering ASC
+ models.start_date DESC NULLS LAST -- now, NULLs are sorted last when ordering DESC as well
+ LIMIT 1
+) m ON TRUE
+LEFT JOIN LATERAL (
+ SELECT
+ prompts.*
+ FROM
+ prompts
+ WHERE prompts.id = o.prompt_id
+ AND prompts.project_id = o.project_id
+ LIMIT 1
+) p ON TRUE
+
+
+-- requirements:
+-- 1. The view should return all columns from the observations table
+-- 2. The view should match with only one model for each observation if:
+-- a. The model has the same project_id as the observation, otherwise the model without project_id.
+-- b. The model has the same model_name as the observation
+-- c. The model has a start_date that is less than the observation start_time, otherwise the model without start_date
+-- d. The model has the same unit as the observation
\ No newline at end of file
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index 89a72eaecc..8040e072a3 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -376,7 +376,10 @@ view ObservationView {
unit String?
completionStartTime DateTime? @map("completion_start_time")
- promptId String? @map("prompt_id")
+ // prompts
+ promptId String? @map("prompt_id")
+ promptName String? @map("prompt_name")
+ promptVersion Int? @map("prompt_version")
// model fields
modelId String? @map("model_id")
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index ccab007162..101cf47995 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -2181,6 +2181,12 @@ components:
title: ObservationsView
type: object
properties:
+ promptName:
+ type: string
+ nullable: true
+ promptVersion:
+ type: integer
+ nullable: true
modelId:
type: string
nullable: true
diff --git a/web/src/__tests__/observations.servertest.ts b/web/src/__tests__/observations.servertest.ts
index 39ce023ad9..9a28baf96a 100644
--- a/web/src/__tests__/observations.servertest.ts
+++ b/web/src/__tests__/observations.servertest.ts
@@ -110,6 +110,10 @@ describe("/api/public/observations API Endpoint", () => {
fetchedObservations.body.data[0]?.calculatedTotalCost,
).toBeGreaterThan(0);
expect(fetchedObservations.body.data[0]?.promptId).toBe(prompt.id);
+ expect(fetchedObservations.body.data[0]?.promptName).toBe(prompt.name);
+ expect(fetchedObservations.body.data[0]?.promptVersion).toBe(
+ prompt.version,
+ );
});
it("should fetch all observations, filtered by generations", async () => {
await pruneDatabase();
diff --git a/web/src/features/public-api/types/observations.ts b/web/src/features/public-api/types/observations.ts
index defcbe2370..e55f76353b 100644
--- a/web/src/features/public-api/types/observations.ts
+++ b/web/src/features/public-api/types/observations.ts
@@ -35,7 +35,11 @@ export const APIObservation = z
model: z.string().nullable(),
modelParameters: z.any(),
completionStartTime: z.coerce.date().nullable(),
+
+ // prompt
promptId: z.string().nullable(),
+ promptName: z.string().nullable(),
+ promptVersion: z.number().int().positive().nullable(),
// usage
usage: z.object({
diff --git a/web/src/pages/api/public/observations/index.ts b/web/src/pages/api/public/observations/index.ts
index a210f53bc5..3bba19a63b 100644
--- a/web/src/pages/api/public/observations/index.ts
+++ b/web/src/pages/api/public/observations/index.ts
@@ -81,6 +81,8 @@ export default withMiddlewares({
o."calculated_total_cost" as "calculatedTotalCost",
o."latency",
o."prompt_id" as "promptId",
+ o."prompt_name" as "promptName",
+ o."prompt_version" as "promptVersion",
o."created_at" as "createdAt",
o."updated_at" as "updatedAt",
o."time_to_first_token" as "timeToFirstToken"
@@ -115,8 +117,6 @@ export default withMiddlewares({
}
const totalItems = Number(countRes[0].count);
- console.log(observations);
-
return {
data: observations.map(transformDbToApiObservation),
meta: {