Skip to content

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

fix: fix order of matched models (#1175)

Max Deichmanndf802184a6da
diff --git a/prisma/migrations/20240215155710_update_observation_view_latency/migration.sql b/prisma/migrations/20240215234937_fix_observations_view/migration.sql
similarity index 90%
rename from prisma/migrations/20240215155710_update_observation_view_latency/migration.sql
rename to prisma/migrations/20240215234937_fix_observations_view/migration.sql
index 9389f7b19d..eaba8de862 100644
--- a/prisma/migrations/20240215155710_update_observation_view_latency/migration.sql
+++ b/prisma/migrations/20240215234937_fix_observations_view/migration.sql
@@ -1,4 +1,3 @@
--- This is an empty migration.
CREATE OR REPLACE VIEW "observations_view" AS
SELECT
o.*,
@@ -45,7 +44,7 @@ LEFT JOIN models m ON m.id = (
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
+ project_id ASC, -- in postgres, NULLs are sorted last when ordering ASC
+ start_date DESC NULLS LAST -- now, NULLs are sorted last when ordering DESC as well
LIMIT 1
)
diff --git a/src/__tests__/cost.servertest.ts b/src/__tests__/cost.servertest.ts
index 78e496c6d9..e9181988b4 100644
--- a/src/__tests__/cost.servertest.ts
+++ b/src/__tests__/cost.servertest.ts
@@ -239,8 +239,23 @@ describe("cost retrieval tests", () => {
);
});
});
+
it(`should prioritize latest models`, async () => {
await pruneDatabase();
+ await prisma.model.create({
+ data: {
+ id: "model-0",
+ modelName: "gpt-3.5-turbo",
+ inputPrice: "0.0000000",
+ outputPrice: "0.0000000",
+ totalPrice: "0.1",
+ matchPattern: "(.*)(gpt-)(35|3.5)(-turbo)?(.*)",
+ projectId: null,
+ startDate: null,
+ tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
+ unit: ModelUsageUnit.Tokens,
+ },
+ });
await prisma.model.create({
data: {
@@ -305,6 +320,70 @@ describe("cost retrieval tests", () => {
expect(view?.calculatedTotalCost?.toString()).toBe("0.0124");
});
+ it(`should prioritize own models`, async () => {
+ await pruneDatabase();
+ await prisma.model.create({
+ data: {
+ id: "model-0",
+ modelName: "gpt-3.5-turbo",
+ inputPrice: "0.0000000",
+ outputPrice: "0.0000000",
+ totalPrice: "0.1",
+ matchPattern: "(.*)(gpt-)(35|3.5)(-turbo)?(.*)",
+ projectId: null,
+ startDate: null,
+ tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
+ unit: ModelUsageUnit.Tokens,
+ },
+ });
+
+ await prisma.model.create({
+ data: {
+ id: "model-1",
+ modelName: "gpt-3.5-turbo",
+ inputPrice: "0.0000010",
+ outputPrice: "0.0000020",
+ totalPrice: "0.1",
+ matchPattern: "(.*)(gpt-)(35|3.5)(-turbo)?(.*)",
+ startDate: null,
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
+ unit: ModelUsageUnit.Tokens,
+ },
+ });
+
+ const dbTrace = await prisma.trace.create({
+ data: {
+ name: "trace-name",
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ },
+ });
+
+ await prisma.observation.create({
+ data: {
+ traceId: dbTrace.id,
+ type: "GENERATION",
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ model: "gpt-3.5-turbo",
+ internalModel: "gpt-3.5-turbo",
+ startTime: new Date("2024-01-01T00:00:00.000Z"),
+ unit: ModelUsageUnit.Tokens,
+ promptTokens: 200,
+ completionTokens: 3000,
+ totalTokens: undefined,
+ },
+ });
+
+ const view = await prisma.observationView.findFirst({
+ where: { traceId: dbTrace.id },
+ });
+
+ console.log(view);
+
+ // calculated cost fields
+ expect(view?.modelId).toBe("model-1");
+ });
+
it(`should prioritize old model if the latest model is not own one`, async () => {
await pruneDatabase();
@@ -371,6 +450,72 @@ describe("cost retrieval tests", () => {
expect(view?.calculatedTotalCost?.toString()).toBe("0.0124");
});
+ it(`should prioritize new model if the latest model is own one`, async () => {
+ await pruneDatabase();
+
+ await prisma.model.create({
+ data: {
+ id: "model-1",
+ modelName: "gpt-3.5-turbo",
+ inputPrice: "0.0010",
+ outputPrice: "0.0020",
+ totalPrice: "0.1",
+ matchPattern: "(.*)(gpt-)(35|3.5)(-turbo)?(.*)",
+ startDate: null,
+ tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
+ unit: ModelUsageUnit.Tokens,
+ },
+ });
+ await prisma.model.create({
+ data: {
+ id: "model-2",
+ modelName: "gpt-3.5-turbo",
+ inputPrice: "0.0000020",
+ outputPrice: "0.0000040",
+ totalPrice: undefined,
+ matchPattern: "(.*)(gpt-)(35|3.5)(-turbo)?(.*)",
+ startDate: new Date("2023-12-01"),
+ tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ unit: ModelUsageUnit.Tokens,
+ },
+ });
+
+ const dbTrace = await prisma.trace.create({
+ data: {
+ name: "trace-name",
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ },
+ });
+
+ await prisma.observation.create({
+ data: {
+ traceId: dbTrace.id,
+ type: "GENERATION",
+ project: { connect: { id: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a" } },
+ model: "gpt-3.5-turbo",
+ internalModel: "gpt-3.5-turbo",
+ startTime: new Date("2024-01-01T00:00:00.000Z"),
+ unit: ModelUsageUnit.Tokens,
+ promptTokens: 200,
+ completionTokens: 3000,
+ totalTokens: undefined,
+ },
+ });
+
+ const view = await prisma.observationView.findFirst({
+ where: { traceId: dbTrace.id },
+ });
+
+ console.log(view);
+
+ // calculated cost fields
+ expect(view?.modelId).toBe("model-2");
+ expect(view?.calculatedInputCost?.toString()).toBe("0.0004");
+ expect(view?.calculatedOutputCost?.toString()).toBe("0.012");
+ expect(view?.calculatedTotalCost?.toString()).toBe("0.0124");
+ });
+
it(`should prioritize user provided cost`, async () => {
await pruneDatabase();
diff --git a/src/__tests__/features/orderBy/hooks/useOrderByState.clienttest.tsx b/src/features/orderBy/hooks/useOrderByState.clienttest.tsx
similarity index 100%
rename from src/__tests__/features/orderBy/hooks/useOrderByState.clienttest.tsx
rename to src/features/orderBy/hooks/useOrderByState.clienttest.tsx
diff --git a/src/__tests__/features/orderBy/server/orderByToPrisma.clienttest.ts b/src/features/orderBy/server/orderByToPrisma.clienttest.ts
similarity index 100%
rename from src/__tests__/features/orderBy/server/orderByToPrisma.clienttest.ts
rename to src/features/orderBy/server/orderByToPrisma.clienttest.ts