Skip to content

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

fix: fix model paring from redis (#6157)

Max Deichmann3c02c54de215
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts
index 62c475a451..a8336d145b 100644
--- a/packages/shared/src/env.ts
+++ b/packages/shared/src/env.ts
@@ -32,8 +32,6 @@ const EnvSchema = z.object({
.optional(),
LANGFUSE_CACHE_PROMPT_ENABLED: z.enum(["true", "false"]).default("false"),
LANGFUSE_CACHE_PROMPT_TTL_SECONDS: z.coerce.number().default(60 * 60),
- LANGFUSE_CACHE_MODEL_MATCH_ENABLED: z.enum(["true", "false"]).default("true"),
- LANGFUSE_CACHE_MODEL_MATCH_TTL_SECONDS: z.coerce.number().default(30),
CLICKHOUSE_URL: z.string().url(),
CLICKHOUSE_CLUSTER_NAME: z.string().default("default"),
CLICKHOUSE_DB: z.string().default("default"),
diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.ts
index 2de0543793..5f1bd106a7 100644
--- a/packages/shared/src/server/index.ts
+++ b/packages/shared/src/server/index.ts
@@ -17,7 +17,6 @@ export * from "./clickhouse/schemaUtils";
export * from "./clickhouse/schema";
export * from "./repositories/definitions";
export * from "../server/ingestion/types";
-export * from "./ingestion/modelMatch";
export * from "./ingestion/processEventBatch";
export * from "../server/ingestion/types";
export * from "../server/ingestion/validateAndInflateScore";
diff --git a/worker/src/__tests__/modelMatch.test.ts b/worker/src/__tests__/modelMatch.test.ts
index 3dbf26e331..8a0f1a976e 100644
--- a/worker/src/__tests__/modelMatch.test.ts
+++ b/worker/src/__tests__/modelMatch.test.ts
@@ -1,12 +1,11 @@
import { expect, describe, it, beforeEach, afterEach } from "vitest";
import { prisma } from "@langfuse/shared/src/db";
+import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
import {
- createOrgProjectAndApiKey,
findModel,
findModelInPostgres,
getRedisModelKey,
- redis,
-} from "@langfuse/shared/src/server";
+} from "../services/modelMatch";
describe("modelMatch", () => {
describe("findModel", () => {
@@ -35,7 +34,25 @@ describe("modelMatch", () => {
model: "gpt-4",
});
- expect(result).toEqual(mockModel);
+ expect(result).not.toBeNull();
+ if (!result) {
+ throw new Error("Result is null");
+ }
+ expect(result.id).toEqual(mockModel.id);
+ expect(result.projectId).toEqual(mockModel.projectId);
+ expect(result.modelName).toEqual(mockModel.modelName);
+ expect(result.matchPattern).toEqual(mockModel.matchPattern);
+ expect(result.unit).toEqual(mockModel.unit);
+ expect(result.inputPrice?.toString()).toEqual(
+ mockModel.inputPrice?.toString(),
+ );
+ expect(result.outputPrice?.toString()).toEqual(
+ mockModel.outputPrice?.toString(),
+ );
+ expect(result.totalPrice?.toString()).toEqual(
+ mockModel.totalPrice?.toString(),
+ );
+
// Verify the model exists in Redis
const redisKey = getRedisModelKey({
projectId,
diff --git a/worker/src/env.ts b/worker/src/env.ts
index d9e9aa3a0e..3fbf1df449 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -137,6 +137,9 @@ const EnvSchema = z.object({
.enum(["true", "false"])
.default("false"),
+ LANGFUSE_CACHE_MODEL_MATCH_ENABLED: z.enum(["true", "false"]).default("true"),
+ LANGFUSE_CACHE_MODEL_MATCH_TTL_SECONDS: z.coerce.number().default(30),
+
// Flags to toggle queue consumers on or off.
QUEUE_CONSUMER_CLOUD_USAGE_METERING_QUEUE_IS_ENABLED: z
.enum(["true", "false"])
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 1565333009..22daa21765 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -1,7 +1,6 @@
import { Redis } from "ioredis";
import { v4 } from "uuid";
import { Prisma } from "@prisma/client";
-
import {
LangfuseNotFoundError,
Model,
@@ -38,7 +37,6 @@ import {
TraceUpsertQueue,
QueueJobs,
recordIncrement,
- findModel,
} from "@langfuse/shared/src/server";
import { tokenCount } from "../../features/tokenisation/usage";
@@ -50,6 +48,7 @@ import {
} from "./utils";
import { randomUUID } from "crypto";
import { env } from "../../env";
+import { findModel } from "../modelMatch";
type InsertRecord =
| TraceRecordInsertType
diff --git a/packages/shared/src/server/ingestion/modelMatch.ts b/worker/src/services/modelMatch.ts
similarity index 84%
rename from packages/shared/src/server/ingestion/modelMatch.ts
rename to worker/src/services/modelMatch.ts
index 91dea29fa1..90faacea50 100644
--- a/packages/shared/src/server/ingestion/modelMatch.ts
+++ b/worker/src/services/modelMatch.ts
@@ -1,10 +1,13 @@
-import { env } from "../../env";
-import { Model, Prisma } from "@prisma/client";
-import { prisma } from "../../db";
-import { instrumentAsync, recordIncrement } from "../instrumentation";
-import { logger } from "../logger";
-import { redis } from "../redis/redis";
-import Decimal from "decimal.js";
+import { Model, Prisma } from "@langfuse/shared";
+import {
+ instrumentAsync,
+ logger,
+ recordIncrement,
+} from "@langfuse/shared/src/server";
+import { env } from "../env";
+import { redis } from "@langfuse/shared/src/server";
+import { Decimal } from "decimal.js";
+import { prisma } from "@langfuse/shared/src/db";
export type ModelMatchProps = {
projectId: string;
@@ -179,14 +182,26 @@ const getModelMatchKeyPrefix = () => {
};
export const redisModelToPrismaModel = (redisModel: string): Model => {
- const parsed = JSON.parse(redisModel);
+ const parsed: Model = JSON.parse(redisModel);
return {
...parsed,
createdAt: new Date(parsed.createdAt),
updatedAt: new Date(parsed.updatedAt),
- inputPrice: new Decimal(parsed.inputPrice),
- outputPrice: new Decimal(parsed.outputPrice),
- totalPrice: new Decimal(parsed.totalPrice),
- startDate: parsed.startDate ? new Date(parsed.startDate) : null,
+ inputPrice:
+ parsed.inputPrice !== null && parsed.inputPrice !== undefined
+ ? new Decimal(parsed.inputPrice)
+ : null,
+ outputPrice:
+ parsed.outputPrice !== null && parsed.outputPrice !== undefined
+ ? new Decimal(parsed.outputPrice)
+ : null,
+ totalPrice:
+ parsed.totalPrice !== null && parsed.totalPrice !== undefined
+ ? new Decimal(parsed.totalPrice)
+ : null,
+ startDate:
+ parsed.startDate !== null && parsed.startDate !== undefined
+ ? new Date(parsed.startDate)
+ : null,
};
};