🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(db): refresh trace and observation view (#2550)
Marc Klingenf1c792e0c325
diff --git a/packages/shared/prisma/generated/types.ts b/packages/shared/prisma/generated/types.tsindex f233a14a40..90cbc16b09 100644--- a/packages/shared/prisma/generated/types.ts+++ b/packages/shared/prisma/generated/types.ts@@ -286,7 +286,7 @@ export type ObservationView = {trace_id: string | null;project_id: string;type: ObservationType;- start_time: Generated<Timestamp>;+ start_time: Timestamp;end_time: Timestamp | null;name: string | null;metadata: unknown | null;@@ -294,7 +294,8 @@ export type ObservationView = {level: Generated<ObservationLevel>;status_message: string | null;version: string | null;- created_at: Generated<Timestamp>;+ created_at: Timestamp;+ updated_at: Timestamp;model: string | null;modelParameters: unknown | null;input: unknown | null;@@ -438,6 +439,8 @@ export type TraceView = {input: unknown | null;output: unknown | null;session_id: string | null;+ created_at: Timestamp;+ updated_at: Timestamp;duration: number | null;};export type User = {diff --git a/packages/shared/prisma/migrations/20240705152639_traces_view_add_created_at_updated_at/migration.sql b/packages/shared/prisma/migrations/20240705152639_traces_view_add_created_at_updated_at/migration.sqlnew file mode 100644index 0000000000..dac05260aa--- /dev/null+++ b/packages/shared/prisma/migrations/20240705152639_traces_view_add_created_at_updated_at/migration.sql@@ -0,0 +1,20 @@+-- Drop and create to be able to change columns, otherwise new t.* cols cannot be added++DROP VIEW IF EXISTS traces_view;+CREATE VIEW traces_view AS+WITH observations_metrics AS (+ SELECT+ trace_id,+ project_id,+ EXTRACT(EPOCH FROM COALESCE(MAX(o.end_time), MAX(o.start_time))) - EXTRACT(EPOCH FROM MIN(o.start_time))::double precision AS duration+ FROM+ observations o+ GROUP BY+ project_id, trace_id+)+SELECT+ t.*,+ o.duration+FROM+ traces t+ LEFT JOIN observations_metrics o ON t.id = o.trace_id and t.project_id = o.project_id\ No newline at end of filediff --git a/packages/shared/prisma/migrations/20240705154048_observation_view_add_created_at_updated_at/migration.sql b/packages/shared/prisma/migrations/20240705154048_observation_view_add_created_at_updated_at/migration.sqlnew file mode 100644index 0000000000..0eb17490ec--- /dev/null+++ b/packages/shared/prisma/migrations/20240705154048_observation_view_add_created_at_updated_at/migration.sql@@ -0,0 +1,89 @@+DROP VIEW IF EXISTS "observations_view"; -- Drop view as column was added in 20240704103900_observations_view_read_from_calculated 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,+ 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+++-- 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 filediff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prismaindex 1ac7fe496c..d9a8451a1c 100644--- a/packages/shared/prisma/schema.prisma+++ b/packages/shared/prisma/schema.prisma@@ -272,6 +272,8 @@ view TraceView {input Json?output Json?sessionId String? @map("session_id")+ createdAt DateTime @map("created_at")+ updatedAt DateTime @map("updated_at")// calculated fieldsduration Float? @map("duration") // can be null if no observations in trace@@ -352,7 +354,7 @@ view ObservationView {traceId String? @map("trace_id")projectId String @map("project_id")type ObservationType- startTime DateTime @default(now()) @map("start_time")+ startTime DateTime @map("start_time")endTime DateTime? @map("end_time")name String?metadata Json?@@ -360,7 +362,8 @@ view ObservationView {level ObservationLevel @default(DEFAULT)statusMessage String? @map("status_message")version String?- createdAt DateTime @default(now()) @map("created_at")+ createdAt DateTime @map("created_at")+ updateAt DateTime @map("updated_at")// GENERATION ONLYmodel String?