Skip to content

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

perf: fetch observations using timestamps (#4629)

Max Deichmann4cd1d167da29
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index dd1ece6265..055b2dfa2a 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -210,11 +210,13 @@ export const getObservationById = async (
id: string,
projectId: string,
fetchWithInputOutput: boolean = false,
+ startTime?: Date,
) => {
const records = await getObservationByIdInternal(
id,
projectId,
fetchWithInputOutput,
+ startTime,
);
const mapped = records.map(convertObservation);
@@ -311,6 +313,7 @@ const getObservationByIdInternal = async (
id: string,
projectId: string,
fetchWithInputOutput: boolean = false,
+ startTime?: Date,
) => {
const query = `
SELECT
@@ -345,11 +348,18 @@ const getObservationByIdInternal = async (
FROM observations
WHERE id = {id: String}
AND project_id = {projectId: String}
+ ${startTime ? `AND start_time = {startTime: DateTime64(3)}` : ""}
ORDER BY event_ts desc
LIMIT 1 by id, project_id`;
return await queryClickhouse<ObservationRecordReadType>({
query,
- params: { id, projectId },
+ params: {
+ id,
+ projectId,
+ ...(startTime
+ ? { startTime: convertDateToClickhouseDateTime(startTime) }
+ : {}),
+ },
});
};
diff --git a/web/src/components/table/use-cases/generations.tsx b/web/src/components/table/use-cases/generations.tsx
index 498aaea23e..c830f44cbe 100644
--- a/web/src/components/table/use-cases/generations.tsx
+++ b/web/src/components/table/use-cases/generations.tsx
@@ -590,6 +590,7 @@ export default function GenerationsTable({
observationId={observationId}
traceId={traceId}
projectId={projectId}
+ startTime={row.getValue("startTime")}
col="input"
singleLine={rowHeight === "s"}
/>
@@ -611,6 +612,7 @@ export default function GenerationsTable({
observationId={observationId}
traceId={traceId}
projectId={projectId}
+ startTime={row.getValue("startTime")}
col="output"
singleLine={rowHeight === "s"}
/>
@@ -635,6 +637,7 @@ export default function GenerationsTable({
observationId={observationId}
traceId={traceId}
projectId={projectId}
+ startTime={row.getValue("startTime")}
col="metadata"
singleLine={rowHeight === "s"}
/>
@@ -854,12 +857,14 @@ const GenerationsDynamicCell = ({
traceId,
observationId,
projectId,
+ startTime,
col,
singleLine = false,
}: {
traceId: string;
observationId: string;
projectId: string;
+ startTime?: Date;
col: "input" | "output" | "metadata";
singleLine: boolean;
}) => {
@@ -868,6 +873,7 @@ const GenerationsDynamicCell = ({
observationId,
traceId,
projectId,
+ startTime,
queryClickhouse: useClickhouse(),
},
{
diff --git a/web/src/components/trace/ObservationPreview.tsx b/web/src/components/trace/ObservationPreview.tsx
index 56990e5d65..5a737abea0 100644
--- a/web/src/components/trace/ObservationPreview.tsx
+++ b/web/src/components/trace/ObservationPreview.tsx
@@ -69,8 +69,13 @@ export const ObservationPreview = ({
const isAuthenticatedAndProjectMember =
useIsAuthenticatedAndProjectMember(projectId);
+ const currentObservation = observations.find(
+ (o) => o.id === currentObservationId,
+ );
+
const observationWithInputAndOutput = api.observations.byId.useQuery({
observationId: currentObservationId,
+ startTime: currentObservation?.startTime,
traceId: traceId,
projectId: projectId,
queryClickhouse: useClickhouse(),
diff --git a/web/src/server/api/routers/observations.ts b/web/src/server/api/routers/observations.ts
index e585c162bf..1662f5107b 100644
--- a/web/src/server/api/routers/observations.ts
+++ b/web/src/server/api/routers/observations.ts
@@ -14,6 +14,7 @@ export const observationsRouter = createTRPCRouter({
observationId: z.string(),
traceId: z.string(), // required for protectedGetTraceProcedure
projectId: z.string(), // required for protectedGetTraceProcedure
+ startTime: z.date().nullish(),
queryClickhouse: z.boolean().default(false),
}),
)
@@ -36,6 +37,7 @@ export const observationsRouter = createTRPCRouter({
input.observationId,
input.projectId,
true,
+ input.startTime ?? undefined,
);
if (!obs) {
throw new TRPCError({