🪢 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
feat(api): add latency to generations endpoints (#1169)
Marc Klingen407d16909545
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.ymlindex 7849c7e1b7..31837faf82 100644--- a/fern/apis/server/definition/commons.yml+++ b/fern/apis/server/definition/commons.yml@@ -72,6 +72,7 @@ types:calculatedInputCost: optional<double>calculatedOutputCost: optional<double>calculatedTotalCost: optional<double>+ latency: optional<double>Usage:properties:diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.ymlindex ac92ce670e..50ee91af7b 100644--- a/generated/openapi-server/openapi.yml+++ b/generated/openapi-server/openapi.yml@@ -1134,6 +1134,10 @@ components:type: numberformat: doublenullable: true+ latency:+ type: number+ format: double+ nullable: trueallOf:- $ref: '#/components/schemas/Observation'Usage:diff --git a/prisma/migrations/20240215155710_update_observation_view_latency/migration.sql b/prisma/migrations/20240215155710_update_observation_view_latency/migration.sqlnew file mode 100644index 0000000000..9389f7b19d--- /dev/null+++ b/prisma/migrations/20240215155710_update_observation_view_latency/migration.sql@@ -0,0 +1,51 @@+-- This is an empty migration.+CREATE OR REPLACE VIEW "observations_view" AS+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 models m ON m.id = (+ SELECT+ id+ FROM+ models+ WHERE (project_id = o.project_id OR project_id IS NULL)+ AND model_name = o.internal_model+ AND (start_date < o.start_time OR start_date is NULL)+ AND o.unit::TEXT = unit+ ORDER BY+ project_id ASC, -- in postgres, NULLs are sorted first+ start_date DESC+ LIMIT 1+)diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 47583af882..89b071f08f 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -493,6 +493,7 @@ view ObservationView {calculatedInputCost Decimal? @map("calculated_input_cost")calculatedOutputCost Decimal? @map("calculated_output_cost")calculatedTotalCost Decimal? @map("calculated_total_cost")+ latency Decimal? @map("latency")@@map("observations_view")}diff --git a/src/pages/api/public/observations/index.ts b/src/pages/api/public/observations/index.tsindex 3d0aba63d2..0c9f78f09d 100644--- a/src/pages/api/public/observations/index.ts+++ b/src/pages/api/public/observations/index.ts@@ -138,8 +138,9 @@ const getObservation = async (o."total_price" as "totalPrice",o."calculated_input_cost" as "calculatedInputCost",o."calculated_output_cost" as "calculatedOutputCost",- o."calculated_total_cost" as "calculatedTotalCost"- FROM observations_view o LEFT JOIN traces ON o."trace_id" = traces."id"+ o."calculated_total_cost" as "calculatedTotalCost",+ o."latency"+ FROM observations_view o LEFT JOIN traces ON o."trace_id" = traces."id" AND traces."project_id" = o."project_id"WHERE o."project_id" = ${authenticatedProjectId}${nameCondition}${userIdCondition}diff --git a/src/server/api/routers/generations/db/getAllGenerationsSqlQuery.ts b/src/server/api/routers/generations/db/getAllGenerationsSqlQuery.tsindex d738fd38aa..95165a3283 100644--- a/src/server/api/routers/generations/db/getAllGenerationsSqlQuery.ts+++ b/src/server/api/routers/generations/db/getAllGenerationsSqlQuery.ts@@ -52,7 +52,7 @@ export function getAllGenerationsSqlQuery({): Prisma.empty;- // For exports: use a date cutoff filter to ignore ingested rows+ // For exports: use a date cutoff filter to ignore newly ingested rowsconst dateCutoffFilter =type === "export"? datetimeFilterToPrismaSql("start_time", "<", new Date())@@ -65,18 +65,7 @@ export function getAllGenerationsSqlQuery({: Prisma.empty;const rawSqlQuery = Prisma.sql`- WITH observations_with_latency AS (- SELECT- o.*,- 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_view o- WHERE o.type = 'GENERATION'- AND o.project_id = ${input.projectId}- ${datetimeFilter}- ${dateCutoffFilter}- ),- -- used for filtering- scores_avg AS (+ WITH scores_avg AS (SELECTtrace_id,observation_id,@@ -123,12 +112,17 @@ export function getAllGenerationsSqlQuery({o.total_price as "totalPrice",o.calculated_input_cost as "calculatedInputCost",o.calculated_output_cost as "calculatedOutputCost",- o.calculated_total_cost as "calculatedTotalCost"- FROM observations_with_latency o- JOIN traces t ON t.id = o.trace_id+ o.calculated_total_cost as "calculatedTotalCost",+ o."latency"+ FROM observations_view o+ JOIN traces t ON t.id = o.trace_id AND t.project_id = o.project_idLEFT JOIN scores_avg AS s_avg ON s_avg.trace_id = t.id and s_avg.observation_id = o.idWHERE- t.project_id = ${input.projectId}+ o.project_id = ${input.projectId}+ AND t.project_id = ${input.projectId}+ AND o.type = 'GENERATION'+ ${datetimeFilter}+ ${dateCutoffFilter}${searchCondition}${filterCondition}${orderByCondition}diff --git a/src/server/api/routers/generations/getAllQuery.ts b/src/server/api/routers/generations/getAllQuery.tsindex ef01e3296a..72236b99b8 100644--- a/src/server/api/routers/generations/getAllQuery.ts+++ b/src/server/api/routers/generations/getAllQuery.ts@@ -30,17 +30,7 @@ export const getAllQuery = protectedProjectProcedureArray<{ count: bigint }>>(Prisma.sql`- WITH observations_with_latency AS (- SELECT- o.*,- 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_view o- WHERE o.type = 'GENERATION'- AND o.project_id = ${input.projectId}- ${datetimeFilter}- ),- -- used for filtering- scores_avg AS (+ WITH scores_avg AS (SELECTtrace_id,observation_id,@@ -64,11 +54,14 @@ export const getAllQuery = protectedProjectProcedure)SELECTcount(*)- FROM observations_with_latency o- JOIN traces t ON t.id = o.trace_id+ FROM observations_view o+ JOIN traces t ON t.id = o.trace_id AND t.project_id = o.project_idLEFT JOIN scores_avg AS s_avg ON s_avg.trace_id = t.id and s_avg.observation_id = o.idWHEREt.project_id = ${input.projectId}+ AND o.type = 'GENERATION'+ AND o.project_id = ${input.projectId}+ ${datetimeFilter}${searchCondition}${filterCondition}`,