🪢 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
fix(ingestion): remove spread operator in cost calc (#2938)
Hassieb Pakzad857cf123a468
diff --git a/package.json b/package.jsonindex 4d4bc3b9dd..c33ed8de8d 100644--- a/package.json+++ b/package.json@@ -18,6 +18,7 @@"nuke": "bash ./scripts/nuke.sh","dx": "pnpm i && pnpm run infra:dev:up && pnpm --filter=shared run db:reset && pnpm --filter=shared run ch:reset && pnpm --filter=shared run db:seed:examples && pnpm run dev","dx-f": "pnpm i && pnpm run infra:dev:up && pnpm --filter=shared run db:reset -f && pnpm --filter=shared run ch:reset && pnpm --filter=shared run db:seed:examples && pnpm run dev",+ "dx:skip-infra": "pnpm i && pnpm --filter=shared run db:reset && pnpm --filter=shared run ch:reset && pnpm --filter=shared run db:seed:examples && pnpm run dev","build": "turbo run build","start": "turbo run start","dev": "turbo run dev",diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.tsindex d7da93ed8b..9b5c4744ba 100644--- a/worker/src/services/IngestionService/index.ts+++ b/worker/src/services/IngestionService/index.ts@@ -498,11 +498,7 @@ export class IngestionService {static calculateTokenCosts(model: Model | null | undefined,- userProvidedCosts: {- provided_input_cost?: number | null;- provided_output_cost?: number | null;- provided_total_cost?: number | null;- },+ observationRecord: ObservationRecordInsertType,tokenCounts: {input_usage_units?: number | null;output_usage_units?: number | null;@@ -513,20 +509,21 @@ export class IngestionService {output_cost: number | null | undefined;total_cost: number | null | undefined;} {+ const { provided_input_cost, provided_output_cost, provided_total_cost } =+ observationRecord;+// If user has provided any cost point, do not calculate anything elseif (- userProvidedCosts.provided_input_cost != null ||- userProvidedCosts.provided_output_cost != null ||- userProvidedCosts.provided_total_cost != null+ provided_input_cost != null ||+ provided_output_cost != null ||+ provided_total_cost != null) {return {- ...userProvidedCosts,- input_cost: userProvidedCosts.provided_input_cost,- output_cost: userProvidedCosts.provided_output_cost,+ input_cost: provided_input_cost,+ output_cost: provided_output_cost,total_cost:- userProvidedCosts.provided_total_cost ??- (userProvidedCosts.provided_input_cost ?? 0) +- (userProvidedCosts.provided_output_cost ?? 0),+ provided_total_cost ??+ (provided_input_cost ?? 0) + (provided_output_cost ?? 0),};}diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.tsindex b2cbd9efe6..ddd15c64b9 100644--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts@@ -1000,6 +1000,122 @@ describe("Ingestion end-to-end tests", () => {);});+ it("should correctly set tokens if usage provided as null", async () => {+ const generationId = randomUUID();+ const traceId = randomUUID();++ const timestamp = new Date().toISOString();+ const traceEventList: TraceEventType[] = [+ {+ id: randomUUID(),+ type: "trace-create",+ timestamp,+ body: {+ id: traceId,+ timestamp,+ name: "trace-name",+ userId: "user-1",+ },+ },+ ];++ const generationEventList1: ObservationEvent[] = [+ {+ id: randomUUID(),+ type: "observation-create",+ timestamp,+ body: {+ type: "GENERATION",+ id: generationId,+ traceId,+ name: "LiteLLM.run",+ // usage: null,+ },+ },+ ];++ await Promise.all([+ ingestionService.processTraceEventList({+ projectId,+ entityId: traceId,+ traceEventList,+ }),+ ingestionService.processObservationEventList({+ projectId,+ entityId: generationId,+ observationEventList: generationEventList1,+ }),+ ]);++ await clickhouseWriter.flushAll(true);+ console.log(+ "generation1",+ await getClickhouseRecord(TableName.Observations, generationId)+ );++ const generationEventList2: ObservationEvent[] = [+ {+ id: randomUUID(),+ type: "observation-update",+ timestamp,+ body: {+ traceId,+ type: "GENERATION",+ id: generationId,+ endTime: new Date().toISOString(),+ model: "azure/gpt35turbo0125",+ modelParameters: {+ model: "azure/gpt35turbo0125",+ max_tokens: 2000,+ temperature: 0,+ n: 1,+ stream: false,+ },+ usage: {+ input: 1285,+ output: 513,+ total: 1798,+ unit: "TOKENS" as any,+ inputCost: 0.0006425,+ outputCost: 0.0007695,+ totalCost: 0.001412,+ },+ },+ },+ ];++ await ingestionService.processObservationEventList({+ projectId,+ entityId: generationId,+ observationEventList: generationEventList2,+ });++ await clickhouseWriter.flushAll(true);++ const generation = await getClickhouseRecord(+ TableName.Observations,+ generationId+ );++ expect(generation.provided_input_usage_units).toEqual(1285);+ expect(generation.provided_output_usage_units).toEqual(513);+ expect(generation.provided_total_usage_units).toEqual(1798);++ expect(generation.input_usage_units).toEqual(1285);+ expect(generation.output_usage_units).toEqual(513);+ expect(generation.total_usage_units).toEqual(1798);++ expect(generation.provided_input_cost).toEqual(0.0006425);+ expect(generation.provided_output_cost).toEqual(0.0007695);+ expect(generation.provided_total_cost).toEqual(0.001412);++ expect(generation.provided_input_cost).toEqual(0.0006425);+ expect(generation.provided_output_cost).toEqual(0.0007695);+ expect(generation.provided_total_cost).toEqual(0.001412);++ expect(generation.unit).toEqual("TOKENS");+ });+it("should update all token counts if update does not contain model name", async () => {const traceId = randomUUID();const generationId = randomUUID();diff --git a/worker/src/services/IngestionService/tests/utils.unit.test.ts b/worker/src/services/IngestionService/tests/utils.unit.test.tsindex 6bc85f8296..a9b0660d68 100644--- a/worker/src/services/IngestionService/tests/utils.unit.test.ts+++ b/worker/src/services/IngestionService/tests/utils.unit.test.ts@@ -61,4 +61,13 @@ describe("overwriteObject", () => {const result = overwriteObject(objC, objD, []);expect(result.metadata).toEqual({});});++ it("should overwrite null keys", () => {+ const obj_1 = { key1: null, key2: "value2" };+ const obj_2 = { key1: "newValue1", key2: "newValue2 " };++ const result = overwriteObject(obj_1 as any, obj_2 as any, []);++ expect(result.key1).toBe("newValue1");+ });});