Skip to content

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

perf: improve observations view and all traces (#1275)

Max Deichmann787eb90c54d1
diff --git a/web/prisma/migrations/20240228103642_observations_view_cte/migration.sql b/web/prisma/migrations/20240228103642_observations_view_cte/migration.sql
new file mode 100644
index 0000000000..9743a815e2
--- /dev/null
+++ b/web/prisma/migrations/20240228103642_observations_view_cte/migration.sql
@@ -0,0 +1,54 @@
+CREATE OR REPLACE VIEW "observations_view" AS
+WITH model_ranked AS (
+ SELECT
+ *,
+ ROW_NUMBER() OVER (
+ PARTITION BY project_id, model_name, unit
+ ORDER BY project_id ASC, start_date DESC NULLS LAST
+ ) AS rn
+ -- adds a new column to the models table ordering the rows by project_id, model_name, and unit
+ -- each query should take the first row within a partition
+ FROM
+ models
+)
+SELECT
+ o.*,
+ 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.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
+ o.input_cost
+ END AS "calculated_input_cost",
+ CASE
+ WHEN 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
+ o.output_cost
+ END AS "calculated_output_cost",
+ CASE
+ WHEN 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
+ 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"
+FROM
+ observations o
+LEFT JOIN model_ranked m ON
+ m.rn = 1 AND
+ (m.project_id = o.project_id OR m.project_id IS NULL) AND
+ m.model_name = o.internal_model AND
+ (m.start_date < o.start_time OR m.start_date IS NULL) AND
+ o.unit::TEXT = m.unit;
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 2ba3f9f987..f8672319f7 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -517,10 +517,9 @@ function createTracesQuery(
${select}
FROM
"traces" AS t
+ LEFT JOIN trace_latency AS tl ON tl.trace_id = t.id
LEFT JOIN usage AS u ON u.trace_id = t.id
- -- used for filtering
LEFT JOIN scores_avg AS s_avg ON s_avg.trace_id = t.id
- LEFT JOIN trace_latency AS tl ON tl.trace_id = t.id
WHERE
t."project_id" = ${projectId}
${searchCondition}
diff --git a/web/src/server/api/services/query-builder.ts b/web/src/server/api/services/query-builder.ts
index a2a249b117..7ee3cb3564 100644
--- a/web/src/server/api/services/query-builder.ts
+++ b/web/src/server/api/services/query-builder.ts
@@ -31,6 +31,7 @@ export const executeQuery = async (
) => {
const query = sqlInterface.parse(unsafeQuery);
const sql = enrichAndCreateQuery(projectId, query);
+
const response = await prisma.$queryRaw<InternalDatabaseRow[]>(sql);
const parsedResult = outputParser(response);