🪢 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
import { shouldSkipScoresDedup } from "../queries/clickhouse-sql/query-options";
import {
ScoreDataTypeType,
ScoreDomain,
ScoreSourceType,
} from "../../domain/scores";
import { PreferredClickhouseService } from "../clickhouse/client";
import { queryClickhouse } from "./clickhouse";
import { ScoreRecordReadType } from "./definitions";
import { convertClickhouseScoreToDomain } from "./scores_converters";
/**
* @internal
* Internal utility function for getting scores by ID.
* Do not use directly - use ScoresApiService or repository functions instead.
*/
export const _handleGetScoreById = async ({
projectId,
scoreId,
source,
scoreScope,
scoreDataTypes,
preferredClickhouseService,
apiVersion,
}: {
projectId: string;
scoreId: string;
source?: ScoreSourceType;
scoreScope: "traces_only" | "all";
scoreDataTypes?: readonly ScoreDataTypeType[];
preferredClickhouseService?: PreferredClickhouseService;
apiVersion?: "v1" | "v2";
}): Promise<ScoreDomain | undefined> => {
const query = `
SELECT *
FROM scores s
WHERE s.project_id = {projectId: String}
AND s.id = {scoreId: String}
${scoreDataTypes ? `AND s.data_type IN ({scoreDataTypes: Array(String)})` : ""}
${source ? `AND s.source = {source: String}` : ""}
${scoreScope === "traces_only" ? "AND s.session_id IS NULL AND s.dataset_run_id IS NULL" : ""}
ORDER BY s.event_ts DESC
${shouldSkipScoresDedup(projectId) ? "" : "LIMIT 1 BY s.id, s.project_id"}
LIMIT 1
`;
const rows = await queryClickhouse<ScoreRecordReadType>({
query,
params: {
projectId,
scoreId,
...(source !== undefined ? { source } : {}),
...(scoreDataTypes
? { scoreDataTypes: scoreDataTypes.map((d) => d.toString()) }
: {}),
},
tags: { projectId, ...(apiVersion ? { api_version: apiVersion } : {}) },
preferredClickhouseService,
});
return rows.map((row) => convertClickhouseScoreToDomain(row)).shift();
};
/**
* @internal
* Internal utility function for getting scores by ID.
* Do not use directly - use ScoresApiService or repository functions instead.
*/
export const _handleGetScoresByIds = async ({
projectId,
scoreId,
source,
scoreScope,
dataTypes,
preferredClickhouseService,
}: {
projectId: string;
scoreId: string[];
source?: ScoreSourceType;
scoreScope: "traces_only" | "all";
dataTypes?: readonly ScoreDataTypeType[];
preferredClickhouseService?: PreferredClickhouseService;
}): Promise<ScoreDomain[]> => {
const query = `
SELECT *
FROM scores s
WHERE s.project_id = {projectId: String}
AND s.id IN ({scoreId: Array(String)})
${source ? `AND s.source = {source: String}` : ""}
${scoreScope === "traces_only" ? "AND s.session_id IS NULL AND s.dataset_run_id IS NULL" : ""}
${dataTypes ? `AND s.data_type IN ({dataTypes: Array(String)})` : ""}
ORDER BY s.event_ts DESC
${shouldSkipScoresDedup(projectId) ? "" : "LIMIT 1 BY s.id, s.project_id"}
`;
const rows = await queryClickhouse<ScoreRecordReadType>({
query,
params: {
projectId,
scoreId,
...(dataTypes ? { dataTypes: dataTypes.map((d) => d.toString()) } : {}),
...(source !== undefined ? { source } : {}),
},
tags: { projectId },
preferredClickhouseService,
});
return rows.map((row) => convertClickhouseScoreToDomain(row));
};